From 104cebf4e9f4ccd6a27c682a02756ec51002bbb9 Mon Sep 17 00:00:00 2001 From: Dani Baeyens Date: Sun, 21 Feb 2021 14:33:07 +0100 Subject: [PATCH] ODPX-47 Adds a leanIX value as prometheus label via CLI flags --- cmd/root.go | 7 +++++++ internal/report/builder.go | 4 ++-- internal/report/prometheus.go | 14 +++++++++----- pkg/config/flags.go | 4 +++- pkg/popeye.go | 1 + 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 7603b883..f38a9ea1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -263,6 +263,13 @@ func initFlags() { false, "Force zero exit status when report errors are present", ) + + rootCmd.Flags().StringVar( + flags.AddLeanIXAsLabel, + "add-lean-ix-as-label", + "", + "Used for prometheus output. If present, adds a string as a prometheus lean-ix label value", + ) } func checkFlags() error { diff --git a/internal/report/builder.go b/internal/report/builder.go index d7137ea8..8b919a05 100644 --- a/internal/report/builder.go +++ b/internal/report/builder.go @@ -189,12 +189,12 @@ func (b *Builder) ToHTML() (string, error) { } // ToPrometheus returns prometheus pusher. -func (b *Builder) ToPrometheus(address *string, namespace string) *push.Pusher { +func (b *Builder) ToPrometheus(address *string, namespace string, leanIX string) *push.Pusher { b.finalize() if namespace == "" { namespace = "all" } - return prometheusMarshal(b, address, b.clusterName, namespace) + return prometheusMarshal(b, address, b.clusterName, namespace, leanIX) } // ToScore dumps sanitizer to only the score value. diff --git a/internal/report/prometheus.go b/internal/report/prometheus.go index c258a6eb..121f713c 100644 --- a/internal/report/prometheus.go +++ b/internal/report/prometheus.go @@ -20,6 +20,7 @@ var ( "cluster", "namespace", "grade", + "leanix", }) sanitizers = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: namespace, @@ -31,6 +32,7 @@ var ( "namespace", "resource", "level", + "leanix", }) sanitizersScore = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: namespace, @@ -41,6 +43,7 @@ var ( "cluster", "namespace", "resource", + "leanix", }) errs = prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: namespace, @@ -50,21 +53,22 @@ var ( []string{ "cluster", "namespace", + "leanix", }) ) -func prometheusMarshal(b *Builder, address *string, cluster, namespace string) *push.Pusher { +func prometheusMarshal(b *Builder, address *string, cluster, namespace string, leanIX string) *push.Pusher { pusher := newPusher(address, namespace) - score.WithLabelValues(cluster, namespace, b.Report.Grade).Set(float64(b.Report.Score)) - errs.WithLabelValues(cluster, namespace).Set(float64(len(b.Report.Errors))) + score.WithLabelValues(cluster, namespace, b.Report.Grade, leanIX).Set(float64(b.Report.Score)) + errs.WithLabelValues(cluster, namespace, leanIX).Set(float64(len(b.Report.Errors))) for _, section := range b.Report.Sections { for i, v := range section.Tally.counts { sanitizers.WithLabelValues(cluster, namespace, section.Title, - strings.ToLower(indexToTally(i))).Set(float64(v)) + strings.ToLower(indexToTally(i)), leanIX).Set(float64(v)) } - sanitizersScore.WithLabelValues(cluster, namespace, section.Title).Set(float64(section.Tally.score)) + sanitizersScore.WithLabelValues(cluster, namespace, section.Title, leanIX).Set(float64(section.Tally.score)) } return pusher } diff --git a/pkg/config/flags.go b/pkg/config/flags.go index 2029be1b..22758f3a 100644 --- a/pkg/config/flags.go +++ b/pkg/config/flags.go @@ -19,6 +19,7 @@ type Flags struct { Spinach *string Sections *[]string PushGatewayAddress *string + AddLeanIXAsLabel *string InClusterName *string StandAlone bool ActiveNamespace *string @@ -41,7 +42,8 @@ func NewFlags() *Flags { Sections: &[]string{}, ConfigFlags: genericclioptions.NewConfigFlags(false), PushGatewayAddress: strPtr(""), - ForceExitZero: boolPtr(false), + ForceExitZero: boolPtr(false), + AddLeanIXAsLabel: strPtr(""), } } diff --git a/pkg/popeye.go b/pkg/popeye.go index a08aa268..a277b5c2 100644 --- a/pkg/popeye.go +++ b/pkg/popeye.go @@ -373,6 +373,7 @@ func (p *Popeye) dumpPrometheus() error { pusher := p.builder.ToPrometheus( p.flags.PushGatewayAddress, p.factory.Client().ActiveNamespace(), + *p.flags.AddLeanIXAsLabel, ) return pusher.Add() }