Support metrics-v3 api in admin prometheus generate (#4985)

Add a flag --api-version with possible values of v2 and v3, with default
being v2.

So when this flag is not passed, the command will work exactly as it did
before.

When using v3, there is a different set of metric types supported. It
also supports a new flag:

bucket: the bucket for which the metrics are to be fetched. it is
applicable only for the metric types that are available at bucket level,
which currently are 'api' and 'replication'
This commit is contained in:
Shireesh Anjal
2024-07-11 11:00:59 -07:00
committed by GitHub
parent ea33d018ca
commit ddfd44cb35
3 changed files with 126 additions and 98 deletions
+86 -71
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
//
@@ -31,22 +31,15 @@ import (
)
const (
defaultJobName = "minio-job"
nodeJobName = "minio-job-node"
bucketJobName = "minio-job-bucket"
defaultMetricsPath = "/minio/v2/metrics/cluster"
nodeMetricsPath = "/minio/v2/metrics/node"
bucketMetricsPath = "/minio/v2/metrics/bucket"
resourceJobName = "minio-job-resource"
resourceMetricsPath = "/minio/v2/metrics/resource"
defaultJobName = "minio-job"
metricsV2BasePath = "/minio/v2/metrics"
)
var prometheusFlags = []cli.Flag{
var prometheusFlags = append(metricsFlags,
cli.BoolFlag{
Name: "public",
Usage: "disable bearer token generation for scrape_configs",
},
}
})
var adminPrometheusGenerateCmd = cli.Command{
Name: "generate",
@@ -63,14 +56,56 @@ USAGE:
{{.HelpName}} TARGET [METRIC-TYPE]
METRIC-TYPE:
valid values are ['cluster', 'node', 'bucket']. 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:
EXAMPLES (v3):
1. Generate a default prometheus config.
{{.Prompt}} {{.HelpName}} myminio
{{.Prompt}} {{.HelpName}} play --api-version v3
2. Generate prometheus config for api metrics.
{{.Prompt}} {{.HelpName}} play api --api-version v3
3. Generate prometheus config for api metrics of bucket 'mybucket'.
{{.Prompt}} {{.HelpName}} play api --bucket mybucket --api-version v3
4. Generate prometheus config for system metrics.
{{.Prompt}} {{.HelpName}} play system --api-version v3
5. Generate prometheus config for debug metrics.
{{.Prompt}} {{.HelpName}} play debug --api-version v3
6. Generate prometheus config for cluster metrics.
{{.Prompt}} {{.HelpName}} play cluster --api-version v3
7. Generate prometheus config for ilm metrics.
{{.Prompt}} {{.HelpName}} play ilm --api-version v3
8. Generate prometheus config for audit metrics.
{{.Prompt}} {{.HelpName}} play audit --api-version v3
9. Generate prometheus config for logger metrics.
{{.Prompt}} {{.HelpName}} play logger --api-version v3
10. Generate prometheus config for replication metrics.
{{.Prompt}} {{.HelpName}} play replication --api-version v3
11. Generate prometheus config for replication metrics of bucket 'mybucket'.
{{.Prompt}} {{.HelpName}} play replication --bucket mybucket --api-version v3
12. Generate prometheus config for notification metrics.
{{.Prompt}} {{.HelpName}} play notification --api-version v3
13. Generate prometheus config for scanner metrics.
{{.Prompt}} {{.HelpName}} play scanner --api-version v3
EXAMPLES (v2):
1. Generate a default prometheus config.
{{.Prompt}} {{.HelpName}} play
2. Generate prometheus config for node metrics.
{{.Prompt}} {{.HelpName}} play node
@@ -80,6 +115,9 @@ EXAMPLES:
4. Generate prometheus config for resource metrics.
{{.Prompt}} {{.HelpName}} play resource
5. Generate prometheus config for cluster metrics.
{{.Prompt}} {{.HelpName}} play cluster
`,
}
@@ -164,66 +202,43 @@ func generatePrometheusConfig(ctx *cli.Context) error {
}
metricsSubSystem := args.Get(1)
var config PrometheusConfig
switch metricsSubSystem {
case "node":
config = PrometheusConfig{
ScrapeConfigs: []ScrapeConfig{
{
JobName: nodeJobName,
MetricsPath: nodeMetricsPath,
StaticConfigs: []StatConfig{
{
Targets: []string{""},
},
},
},
},
apiVer := ctx.String("api-version")
jobName := defaultJobName
metricsPath := ""
switch apiVer {
case "v2":
if metricsSubSystem == "" {
metricsSubSystem = "cluster"
}
case "bucket":
config = PrometheusConfig{
ScrapeConfigs: []ScrapeConfig{
{
JobName: bucketJobName,
MetricsPath: bucketMetricsPath,
StaticConfigs: []StatConfig{
{
Targets: []string{""},
},
},
},
},
validateV2Args(ctx, metricsSubSystem)
if metricsSubSystem != "cluster" {
jobName = defaultJobName + "-" + metricsSubSystem
}
case "resource":
config = PrometheusConfig{
ScrapeConfigs: []ScrapeConfig{
{
JobName: resourceJobName,
MetricsPath: resourceMetricsPath,
StaticConfigs: []StatConfig{
{
Targets: []string{""},
},
},
},
},
}
case "", "cluster":
config = PrometheusConfig{
ScrapeConfigs: []ScrapeConfig{
{
JobName: defaultJobName,
MetricsPath: defaultMetricsPath,
StaticConfigs: []StatConfig{
{
Targets: []string{""},
},
},
},
},
metricsPath = metricsV2BasePath + "/" + metricsSubSystem
case "v3":
bucket := ctx.String("bucket")
validateV3Args(metricsSubSystem, bucket)
metricsPath = getMetricsV3Path(metricsSubSystem, bucket)
if metricsSubSystem != "" {
jobName = defaultJobName + "-" + metricsSubSystem
}
default:
fatalIf(errInvalidArgument().Trace(), "invalid metric type '%v'", metricsSubSystem)
fatalIf(errInvalidArgument().Trace(), "Invalid api version `"+apiVer+"`")
}
config := PrometheusConfig{
ScrapeConfigs: []ScrapeConfig{
{
JobName: jobName,
MetricsPath: metricsPath,
StaticConfigs: []StatConfig{
{
Targets: []string{""},
},
},
},
},
}
if !ctx.Bool("public") {
+31 -21
View File
@@ -44,18 +44,38 @@ var (
const metricsV3EndPointRoot = "/minio/metrics/v3"
func printPrometheusMetricsV3(ctx *cli.Context, req prometheusMetricsReq) error {
subsys := req.subsystem
func getMetricsV3Path(subsys string, bucket string) string {
params := url.Values{}
metricsPath := metricsV3EndPointRoot
if len(bucket) > 0 {
metricsPath += "/bucket"
}
if len(subsys) > 0 {
metricsPath += "/" + 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
metricsPath += "/" + bucket
}
qparams := params.Encode()
if len(qparams) > 0 {
metricsPath += "?" + qparams
}
return metricsPath
}
func validateV3Args(subsys string, bucket string) {
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 {
@@ -65,24 +85,14 @@ func printPrometheusMetricsV3(ctx *cli.Context, req prometheusMetricsReq) error
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
}
func printPrometheusMetricsV3(ctx *cli.Context, req prometheusMetricsReq) error {
bucket := ctx.String("bucket")
validateV3Args(req.subsystem, bucket)
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
}
metricsURL := req.aliasURL + getMetricsV3Path(req.subsystem, bucket)
resp, e := fetchMetrics(metricsURL, req.token)
if e != nil {
+9 -6
View File
@@ -141,7 +141,7 @@ func fetchMetrics(metricsURL string, token string) (*http.Response, error) {
return client.Do(req)
}
func printPrometheusMetricsV2(ctx *cli.Context, req prometheusMetricsReq) error {
func validateV2Args(ctx *cli.Context, subsys string) {
for _, flag := range metricsV3Flags {
flagName := flag.GetName()
if ctx.IsSet(flagName) {
@@ -149,16 +149,19 @@ func printPrometheusMetricsV2(ctx *cli.Context, req prometheusMetricsReq) error
}
}
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(), ", ")+"`")
}
}
func printPrometheusMetricsV2(ctx *cli.Context, req prometheusMetricsReq) error {
subsys := req.subsystem
if subsys == "" {
subsys = "cluster"
}
validateV2Args(ctx, subsys)
resp, e := fetchMetrics(req.aliasURL+metricsEndPointRoot+subsys, req.token)
if e != nil {