From 4bc225f26b4bdcd4ed45d1055e3f561933b91ac5 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Tue, 25 Jul 2023 13:33:50 +0200 Subject: [PATCH] change output of "cscli metrics -o [json|raw]" from list of objects to map with table names (#2375) --- cmd/crowdsec-cli/metrics.go | 51 +++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/cmd/crowdsec-cli/metrics.go b/cmd/crowdsec-cli/metrics.go index 1ddf4ff66..1c506040f 100644 --- a/cmd/crowdsec-cli/metrics.go +++ b/cmd/crowdsec-cli/metrics.go @@ -14,7 +14,7 @@ import ( "github.com/prometheus/prom2json" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" "github.com/crowdsecurity/go-cs-lib/pkg/trace" ) @@ -244,26 +244,39 @@ func FormatPrometheusMetrics(out io.Writer, url string, formatType string) error decisionStatsTable(out, decisions_stats) alertStatsTable(out, alerts_stats) stashStatsTable(out, stash_stats) - } else if formatType == "json" { - for _, val := range []interface{}{acquis_stats, parsers_stats, buckets_stats, lapi_stats, lapi_bouncer_stats, lapi_machine_stats, lapi_decisions_stats, decisions_stats, alerts_stats, stash_stats} { - x, err := json.MarshalIndent(val, "", " ") - if err != nil { - return fmt.Errorf("failed to unmarshal metrics : %v", err) - } - out.Write(x) - } - return nil - - } else if formatType == "raw" { - for _, val := range []interface{}{acquis_stats, parsers_stats, buckets_stats, lapi_stats, lapi_bouncer_stats, lapi_machine_stats, lapi_decisions_stats, decisions_stats, alerts_stats, stash_stats} { - x, err := yaml.Marshal(val) - if err != nil { - return fmt.Errorf("failed to unmarshal metrics : %v", err) - } - out.Write(x) - } return nil } + + stats := make(map[string]any) + + stats["acquisition"] = acquis_stats + stats["buckets"] = buckets_stats + stats["parsers"] = parsers_stats + stats["lapi"] = lapi_stats + stats["lapi_machine"] = lapi_machine_stats + stats["lapi_bouncer"] = lapi_bouncer_stats + stats["lapi_decisions"] = lapi_decisions_stats + stats["decisions"] = decisions_stats + stats["alerts"] = alerts_stats + stats["stash"] = stash_stats + + switch formatType { + case "json": + x, err := json.MarshalIndent(stats, "", " ") + if err != nil { + return fmt.Errorf("failed to unmarshal metrics : %v", err) + } + out.Write(x) + case "raw": + x, err := yaml.Marshal(stats) + if err != nil { + return fmt.Errorf("failed to unmarshal metrics : %v", err) + } + out.Write(x) + default: + return fmt.Errorf("unknown format type %s", formatType) + } + return nil }