From a893453ee58a4d8161ae835e3d1f88b7f5b2fc9f Mon Sep 17 00:00:00 2001 From: krastan Date: Wed, 17 Aug 2022 15:52:49 +0300 Subject: [PATCH] added status as a label to the metric --- helm/templates/deployment.yaml | 3 +++ helm/values.yaml | 1 + main.go | 46 +++++++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/helm/templates/deployment.yaml b/helm/templates/deployment.yaml index c836843..028ee9f 100644 --- a/helm/templates/deployment.yaml +++ b/helm/templates/deployment.yaml @@ -63,6 +63,9 @@ spec: {{- if not .Values.latestChartVersion }} - "-latest-chart-version=false" {{- end }} + {{- if .Values.statusInMetric }} + - "-status-in-metric=true" + {{- end }} {{- with .Values.intervalDuration }} - "-interval-duration={{ . }}" {{- end }} diff --git a/helm/values.yaml b/helm/values.yaml index 4ee6f5c..5998da1 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -5,6 +5,7 @@ namespacesIgnore: "" infoMetric: true timestampMetric: true latestChartVersion: true +statusInMetric: false intervalDuration: 0 serviceMonitor: diff --git a/main.go b/main.go index feb4d8e..a9f94b4 100644 --- a/main.go +++ b/main.go @@ -60,6 +60,8 @@ var ( fetchLatest = flag.Bool("latest-chart-version", true, "Attempt to fetch the latest chart version from registries. Defaults to true") + statusInMetric = flag.Bool("status-in-metric", false, "Adds the status to the metric as a label. Defaults to false") + verbose = flag.Bool("verbose", false, "Enables debug logging. Defaults to false") statusCodeMap = map[string]float64{ @@ -73,16 +75,24 @@ var ( "pending-upgrade": 7, "pending-rollback": 8, } + statusCodeMapToStr = map[float64]string{ + 0: "unknown", + 1: "deployed", + 2: "uninstalled", + 3: "superseded", + -1: "failed", + 5: "uninstalling", + 6: "pending-install", + 7: "pending-upgrade", + 8: "pending-rollback", + } prometheusHandler = promhttp.Handler() ) func configureMetrics() (info *prometheus.GaugeVec, timestamp *prometheus.GaugeVec, outdated *prometheus.GaugeVec) { if *infoMetric == true { - info = prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Name: "helm_chart_info", - Help: "Information on helm releases", - }, []string{ + infoLabels := []string{ "chart", "release", "version", @@ -91,7 +101,15 @@ func configureMetrics() (info *prometheus.GaugeVec, timestamp *prometheus.GaugeV "updated", "namespace", "latestVersion", - "description"}) + "description"} + if *statusInMetric { + infoLabels = append(infoLabels, "status") + } + + info = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Name: "helm_chart_info", + Help: "Information on helm releases", + }, infoLabels) } if *timestampMetric == true { @@ -151,6 +169,7 @@ func runStats(config config.Config, info *prometheus.GaugeVec, timestamp *promet updated := item.Info.LastDeployed.Unix() * 1000 namespace := item.Namespace status := statusCodeMap[item.Info.Status.String()] + statusStr := statusCodeMapToStr[status] revision := item.Version description := item.Info.Description latestVersion := "" @@ -176,8 +195,23 @@ func runStats(config config.Config, info *prometheus.GaugeVec, timestamp *promet } if info != nil { - info.WithLabelValues(chart, releaseName, version, appVersion, strconv.FormatInt(int64(revision), 10), strconv.FormatInt(updated, 10), namespace, latestVersion, description).Set(status) + labelValues := []string{ + chart, + releaseName, + version, + appVersion, + strconv.FormatInt(int64(revision), 10), + strconv.FormatInt(updated, 10), + namespace, + latestVersion, + description, + } + if *statusInMetric { + labelValues = append(labelValues, statusStr) + } + info.WithLabelValues(labelValues...).Set(status) } + if timestamp != nil { timestamp.WithLabelValues(chart, releaseName, version, appVersion, strconv.FormatInt(updated, 10), namespace, latestVersion).Set(float64(updated)) }