From cb08bc85a9dd1e971493e7aec84fe294caec6293 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 31 Jan 2024 10:28:14 +0300 Subject: [PATCH] scripts: Allow to export metrics in markdorn Signed-off-by: Evgenii Stratonikov --- scripts/export-metrics/main.go | 40 ++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/scripts/export-metrics/main.go b/scripts/export-metrics/main.go index 63571e96d..61773eb0d 100644 --- a/scripts/export-metrics/main.go +++ b/scripts/export-metrics/main.go @@ -1,18 +1,22 @@ package main import ( + "bytes" "encoding/json" "flag" "fmt" "os" + "sort" + "strings" local_metrics "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/metrics" "git.frostfs.info/TrueCloudLab/frostfs-observability/metrics" ) var ( - node = flag.String("node", "", "File to export storage node metrics to.") - ir = flag.String("ir", "", "File to export innerring node metrics to.") + node = flag.String("node", "", "File to export storage node metrics to.") + ir = flag.String("ir", "", "File to export innerring node metrics to.") + format = flag.String("format", "json", "Format to export in. Supported formats: json (default), md.") ) func main() { @@ -37,12 +41,34 @@ func main() { os.Exit(1) } - ds := metrics.DescribeAll() + var data []byte + var err error - data, err := json.Marshal(ds) - if err != nil { - fmt.Fprintf(os.Stderr, "Could not parse marshal: %v\n", err) - os.Exit(1) + ds := metrics.DescribeAll() + switch *format { + case "md": + sort.Slice(ds, func(i, j int) bool { + return ds[i].Name < ds[j].Name + }) + b := bytes.NewBuffer(nil) + b.WriteString("# FrostFS Node prometheus metrics\n") + b.WriteString("Name | Description | Constant labels | Variable labels\n") + b.WriteString("-- | -- | -- | --\n") + for i := range ds { + constLabels := make([]string, 0, len(ds[i].ConstantLabels)) + for key, value := range ds[i].ConstantLabels { + constLabels = append(constLabels, fmt.Sprintf("%s=%s", key, value)) + } + varLabels := strings.Join(ds[i].VariableLabels, "; ") + fmt.Fprintf(b, "%s | %s | %s | %s\n", ds[i].Name, ds[i].Help, strings.Join(constLabels, "; "), varLabels) + } + data = b.Bytes() + default: + data, err = json.Marshal(ds) + if err != nil { + fmt.Fprintf(os.Stderr, "Could not parse marshal: %v\n", err) + os.Exit(1) + } } if err := os.WriteFile(filename, data, 0o644); err != nil {