Support metrics-v3 api in admin prometheus metrics (#4982)

Add a flag --api-version with possible values of v2 and v3, with the default
being v2. So when this flag is not passed, the command will work exactly
as before.

When using v3, a different set of metric types is supported. It
also supports two additional flags:

Bucket: the bucket for which the metrics are to be fetched. it is
applicable only for the metric types that are available at the bucket level,
which currently are 'api' and 'replication'
This commit is contained in:
Shireesh Anjal
2024-07-11 01:33:12 -07:00
committed by GitHub
parent 21d3ec0089
commit ea33d018ca
2 changed files with 229 additions and 41 deletions
+100
View File
@@ -0,0 +1,100 @@
// Copyright (c) 2015-2024 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/minio/cli"
"github.com/minio/minio-go/v7/pkg/set"
)
var (
metricsV3Flags = []cli.Flag{
cli.StringFlag{
Name: "bucket",
Usage: "bucket name to list metrics for. only applicable with api version v3 for metric type 'bucket'",
},
}
metricsV3SubSystems = set.CreateStringSet("api", "system", "debug", "cluster",
"ilm", "audit", "logger", "replication", "notification", "scanner")
bucketMetricsSubSystems = set.CreateStringSet("api", "replication")
)
const metricsV3EndPointRoot = "/minio/metrics/v3"
func printPrometheusMetricsV3(ctx *cli.Context, req prometheusMetricsReq) error {
subsys := req.subsystem
if subsys != "" && !metricsV3SubSystems.Contains(subsys) {
fatalIf(errInvalidArgument().Trace(),
"invalid metric type `"+subsys+"`. valid values are `"+
strings.Join(metricsV3SubSystems.ToSlice(), ", ")+"`")
}
bucket := ctx.String("bucket")
metricsURL := req.aliasURL + metricsV3EndPointRoot
params := url.Values{}
if len(bucket) > 0 {
bms := strings.Join(bucketMetricsSubSystems.ToSlice(), ", ")
if len(subsys) == 0 {
fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("metric type must be passed with --bucket. valid values are `"+bms+"`"))
}
if !bucketMetricsSubSystems.Contains(subsys) {
fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("--bucket is applicable only for metric types `"+bms+"`"))
}
metricsURL += "/bucket"
}
if len(subsys) > 0 {
metricsURL += "/" + subsys
}
if len(bucket) > 0 {
// bucket specific metrics endpoints have '/bucket' prefix and bucket name as suffix.
// e.g. bucket api metrics: /bucket/api/mybucket
metricsURL += "/" + bucket
}
qparams := params.Encode()
if len(qparams) > 0 {
metricsURL += "?" + qparams
}
resp, e := fetchMetrics(metricsURL, req.token)
if e != nil {
return e
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
printMsg(prometheusMetricsReader{Reader: resp.Body})
return nil
}
return errors.New(resp.Status)
}
+129 -41
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2022 MinIO, Inc.
// Copyright (c) 2015-2024 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
@@ -22,49 +22,105 @@ import (
"io"
"net/http"
"os"
"strings"
"time"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/madmin-go/v3"
"github.com/minio/mc/pkg/probe"
"github.com/minio/minio-go/v7/pkg/set"
)
var metricsFlags = append(metricsV3Flags,
cli.StringFlag{
Name: "api-version",
Usage: "version of metrics api to use. valid values are ['v2', 'v3']. defaults to 'v2' if not specified.",
Value: "v2",
})
var metricsV2SubSystems = set.CreateStringSet("node", "bucket", "cluster", "resource")
var adminPrometheusMetricsCmd = cli.Command{
Name: "metrics",
Usage: "print cluster wide prometheus metrics",
Usage: "print prometheus metrics",
OnUsageError: onUsageError,
Action: mainSupportMetrics,
Before: setGlobalsFromContext,
Flags: globalFlags,
Flags: append(globalFlags, metricsFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET [METRIC-TYPE]
METRIC-TYPE:
valid values are ['cluster', 'node', 'bucket', 'resource']. Defaults to 'cluster' if not specified.
valid values are
api-version v2 ['cluster', 'node', 'bucket', 'resource']. defaults to 'cluster' if not specified.
api-version v3 ["api", "system", "debug", "cluster", "ilm", "audit", "logger", "replication", "notification", "scanner"]. defaults to all if not specified.
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. List of metrics reported cluster wide.
EXAMPLES (v3):
1. API metrics
{{.Prompt}} {{.HelpName}} play api --api-version v3
2. API metrics for the bucket 'mybucket'
{{.Prompt}} {{.HelpName}} play api --bucket mybucket --api-version v3
3. System metrics
{{.Prompt}} {{.HelpName}} play system --api-version v3
4. Debug metrics
{{.Prompt}} {{.HelpName}} play debug --api-version v3
5. Cluster metrics
{{.Prompt}} {{.HelpName}} play cluster --api-version v3
6. ILM metrics
{{.Prompt}} {{.HelpName}} play ilm --api-version v3
7. Audit metrics
{{.Prompt}} {{.HelpName}} play audit --api-version v3
8. Logger metrics
{{.Prompt}} {{.HelpName}} play logger --api-version v3
9. Replication metrics
{{.Prompt}} {{.HelpName}} play replication --api-version v3
10. Replication metrics for the bucket 'mybucket'
{{.Prompt}} {{.HelpName}} play replication --bucket mybucket --api-version v3
11. Notification metrics
{{.Prompt}} {{.HelpName}} play notification --api-version v3
12. Scanner metrics
{{.Prompt}} {{.HelpName}} play scanner --api-version v3
EXAMPLES (v2):
1. Metrics reported cluster wide.
{{.Prompt}} {{.HelpName}} play
2. List of metrics reported at node level.
2. Metrics reported at node level.
{{.Prompt}} {{.HelpName}} play node
3. List of metrics reported at bucket level.
3. Metrics reported at bucket level.
{{.Prompt}} {{.HelpName}} play bucket
4. List of resource metrics.
4. Resource metrics.
{{.Prompt}} {{.HelpName}} play resource
`,
}
const metricsEndPointRoot = "/minio/v2/metrics/"
type prometheusMetricsReq struct {
aliasURL string
token string
subsystem string
}
// checkSupportMetricsSyntax - validate arguments passed by a user
func checkSupportMetricsSyntax(ctx *cli.Context) {
if len(ctx.Args()) == 0 || len(ctx.Args()) > 2 {
@@ -72,44 +128,39 @@ func checkSupportMetricsSyntax(ctx *cli.Context) {
}
}
func printPrometheusMetrics(ctx *cli.Context) error {
// Get the alias parameter from cli
args := ctx.Args()
alias := cleanAlias(args.Get(0))
if !isValidAlias(alias) {
fatalIf(errInvalidAlias(alias), "Invalid alias.")
}
hostConfig := mustGetHostConfig(alias)
if hostConfig == nil {
fatalIf(errInvalidAliasedURL(alias), "No such alias `"+alias+"` found.")
return nil
}
token, e := getPrometheusToken(hostConfig)
func fetchMetrics(metricsURL string, token string) (*http.Response, error) {
req, e := http.NewRequest(http.MethodGet, metricsURL, nil)
if e != nil {
return e
return nil, e
}
metricsSubSystem := args.Get(1)
switch metricsSubSystem {
case "node", "bucket", "cluster", "resource":
case "":
metricsSubSystem = "cluster"
default:
fatalIf(errInvalidArgument().Trace(), "invalid metric type '%v'", metricsSubSystem)
}
req, e := http.NewRequest(http.MethodGet, hostConfig.URL+metricsEndPointRoot+metricsSubSystem, nil)
if e != nil {
return e
}
if token != "" {
req.Header.Add("Authorization", "Bearer "+token)
}
client := httpClient(60 * time.Second)
resp, e := client.Do(req)
return client.Do(req)
}
func printPrometheusMetricsV2(ctx *cli.Context, req prometheusMetricsReq) error {
for _, flag := range metricsV3Flags {
flagName := flag.GetName()
if ctx.IsSet(flagName) {
fatalIf(errInvalidArgument().Trace(), "Flag `"+flagName+"` is not supported with v2 metrics")
}
}
subsys := req.subsystem
if subsys == "" {
subsys = "cluster"
}
if !metricsV2SubSystems.Contains(subsys) {
fatalIf(errInvalidArgument().Trace(),
"invalid metric type `"+subsys+"`. valid values are `"+
strings.Join(metricsV2SubSystems.ToSlice(), ", ")+"`")
}
resp, e := fetchMetrics(req.aliasURL+metricsEndPointRoot+subsys, req.token)
if e != nil {
return e
}
@@ -151,7 +202,44 @@ type prometheusMetricsReader struct {
func mainSupportMetrics(ctx *cli.Context) error {
checkSupportMetricsSyntax(ctx)
fatalIf(probe.NewError(printPrometheusMetrics(ctx)), "Unable to list prometheus metrics.")
// Get the alias parameter from cli
args := ctx.Args()
alias := cleanAlias(args.Get(0))
if !isValidAlias(alias) {
fatalIf(errInvalidAlias(alias), "Invalid alias.")
}
hostConfig := mustGetHostConfig(alias)
if hostConfig == nil {
fatalIf(errInvalidAliasedURL(alias), "No such alias `"+alias+"` found.")
return nil
}
token, e := getPrometheusToken(hostConfig)
if e != nil {
return e
}
metricsSubSystem := args.Get(1)
apiVer := ctx.String("api-version")
metricsReq := prometheusMetricsReq{
aliasURL: hostConfig.URL,
token: token,
subsystem: metricsSubSystem,
}
switch apiVer {
case "v2":
err := printPrometheusMetricsV2(ctx, metricsReq)
fatalIf(probe.NewError(err), "Unable to list prometheus metrics with api-version v2.")
case "v3":
err := printPrometheusMetricsV3(ctx, metricsReq)
fatalIf(probe.NewError(err), "Unable to list prometheus metrics with api-version v3.")
default:
fatalIf(errInvalidArgument().Trace(), "Invalid api version `"+apiVer+"`")
}
return nil
}