github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/metrics-router.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"net/http"
    22  	"strings"
    23  
    24  	"github.com/minio/mux"
    25  	"github.com/minio/pkg/v2/env"
    26  )
    27  
    28  const (
    29  	prometheusMetricsPathLegacy     = "/prometheus/metrics"
    30  	prometheusMetricsV2ClusterPath  = "/v2/metrics/cluster"
    31  	prometheusMetricsV2BucketPath   = "/v2/metrics/bucket"
    32  	prometheusMetricsV2NodePath     = "/v2/metrics/node"
    33  	prometheusMetricsV2ResourcePath = "/v2/metrics/resource"
    34  
    35  	// Metrics v3 endpoints
    36  	metricsV3Path = "/metrics/v3"
    37  )
    38  
    39  // Standard env prometheus auth type
    40  const (
    41  	EnvPrometheusAuthType = "MINIO_PROMETHEUS_AUTH_TYPE"
    42  )
    43  
    44  type prometheusAuthType string
    45  
    46  const (
    47  	prometheusJWT    prometheusAuthType = "jwt"
    48  	prometheusPublic prometheusAuthType = "public"
    49  )
    50  
    51  // registerMetricsRouter - add handler functions for metrics.
    52  func registerMetricsRouter(router *mux.Router) {
    53  	// metrics router
    54  	metricsRouter := router.NewRoute().PathPrefix(minioReservedBucketPath).Subrouter()
    55  	authType := prometheusAuthType(strings.ToLower(env.Get(EnvPrometheusAuthType, string(prometheusJWT))))
    56  
    57  	auth := AuthMiddleware
    58  	if authType == prometheusPublic {
    59  		auth = NoAuthMiddleware
    60  	}
    61  	metricsRouter.Handle(prometheusMetricsPathLegacy, auth(metricsHandler()))
    62  	metricsRouter.Handle(prometheusMetricsV2ClusterPath, auth(metricsServerHandler()))
    63  	metricsRouter.Handle(prometheusMetricsV2BucketPath, auth(metricsBucketHandler()))
    64  	metricsRouter.Handle(prometheusMetricsV2NodePath, auth(metricsNodeHandler()))
    65  	metricsRouter.Handle(prometheusMetricsV2ResourcePath, auth(metricsResourceHandler()))
    66  
    67  	// Metrics v3!
    68  	metricsV3Server := newMetricsV3Server(authType)
    69  
    70  	// Register metrics v3 handler. It also accepts an optional query
    71  	// parameter `?list` - see handler for details.
    72  	metricsRouter.Methods(http.MethodGet).Path(metricsV3Path + "{pathComps:.*}").Handler(metricsV3Server)
    73  }