Skip to content

Commit

Permalink
added status as a label to the metric
Browse files Browse the repository at this point in the history
  • Loading branch information
ekrucio authored and sstarcher committed Aug 17, 2022
1 parent ae366c1 commit a893453
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
3 changes: 3 additions & 0 deletions helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
1 change: 1 addition & 0 deletions helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespacesIgnore: ""
infoMetric: true
timestampMetric: true
latestChartVersion: true
statusInMetric: false
intervalDuration: 0

serviceMonitor:
Expand Down
46 changes: 40 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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 := ""
Expand All @@ -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))
}
Expand Down

0 comments on commit a893453

Please sign in to comment.