Skip to content

Commit

Permalink
Pull request derailed#6: ODPX-47 Adds a leanIX value as prometheus la…
Browse files Browse the repository at this point in the history
…bel via CLI flags

Merge in ODP/popeye from feature/odpx-47-adds-lean-ix-as-prometheus-label to master

* commit '104cebf4e9f4ccd6a27c682a02756ec51002bbb9':
  ODPX-47 Adds a leanIX value as prometheus label via CLI flags
  • Loading branch information
Baeyens, Daniel committed Apr 7, 2021
2 parents 0cd8164 + 104cebf commit 2298762
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/report/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 9 additions & 5 deletions internal/report/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
"cluster",
"namespace",
"grade",
"leanix",
})
sanitizers = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Expand All @@ -31,6 +32,7 @@ var (
"namespace",
"resource",
"level",
"leanix",
})
sanitizersScore = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Expand All @@ -41,6 +43,7 @@ var (
"cluster",
"namespace",
"resource",
"leanix",
})
errs = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Expand All @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Flags struct {
Spinach *string
Sections *[]string
PushGatewayAddress *string
AddLeanIXAsLabel *string
InClusterName *string
StandAlone bool
ActiveNamespace *string
Expand All @@ -41,7 +42,8 @@ func NewFlags() *Flags {
Sections: &[]string{},
ConfigFlags: genericclioptions.NewConfigFlags(false),
PushGatewayAddress: strPtr(""),
ForceExitZero: boolPtr(false),
ForceExitZero: boolPtr(false),
AddLeanIXAsLabel: strPtr(""),
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/popeye.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 2298762

Please sign in to comment.