From aadddfdaf7c605aba6d8b1cb0e74039a7eeb711e Mon Sep 17 00:00:00 2001 From: Shane Starcher Date: Sat, 13 Apr 2019 08:04:04 -0700 Subject: [PATCH] change port to 9157, create metrics only on /metric scrape, add appVersion label --- README.md | 8 ++++---- main.go | 21 ++++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index cd639c6..d6985f4 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,14 @@ Exports helm release, chart, and version staistics in the prometheus format. * If using Grafana you can use this Dashboard to have a list of what's running https://grafana.com/dashboards/9367 # Metrics -* http://host:9100/metrics +* http://host:9571/metrics # Format ``` -helm_chart_info{chart="ark",release="ark",version="1.2.1",namespace="test"} 1 -helm_chart_info{chart="cluster-autoscaler",release="cluster-autoscaler",version="0.7.0",namespace="other"} 4 -helm_chart_info{chart="dex",release="dex",version="0.1.0",namespace="test"} 1 +helm_chart_info{chart="ark",release="ark",version="1.2.1",appVersion="1.2.3",updated="1553201431",namespace="test"} 1 +helm_chart_info{chart="cluster-autoscaler",release="cluster-autoscaler",version="0.7.0",appVersion="",updated="1553201431",namespace="other"} 4 +helm_chart_info{chart="dex",release="dex",version="0.1.0",appVersion="1.2.3",updated="1553201431",namespace="test"} 1 ``` The metric value is the helm status code. These status codes indexes do not map up directly to helm. This is so I can make the bad cases negative values. diff --git a/main.go b/main.go index 151f502..4e43561 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" "strconv" - "time" "k8s.io/helm/pkg/helm" @@ -26,6 +25,7 @@ var ( "chart", "release", "version", + "appVersion", "updated", "namespace", }) @@ -46,8 +46,11 @@ var ( release.Status_PENDING_UPGRADE, release.Status_PENDING_ROLLBACK, } + + prometheusHandler = promhttp.Handler() ) +// NewClient is the connection to tiller func NewClient() *helm.Client { fmt.Printf("attempting to connect to %s\n", inClusterTiller) client := helm.NewClient(helm.Host(inClusterTiller)) @@ -91,7 +94,7 @@ func filterList(rels []*release.Release) []*release.Release { return uniq } -func helmStats() { +func helmStats(w http.ResponseWriter, r *http.Request) { items, err := client.ListReleases(helm.ReleaseListStatuses(statusCodes)) if err == nil { stats.Reset() @@ -100,26 +103,22 @@ func helmStats() { status := item.GetInfo().GetStatus().GetCode() releaseName := item.GetName() version := item.GetChart().GetMetadata().GetVersion() + appVersion := item.GetChart().GetMetadata().GetAppVersion() updated := strconv.FormatInt(item.GetInfo().GetLastDeployed().Seconds, 10) namespace := item.GetNamespace() if status == release.Status_FAILED { status = -1 } - stats.WithLabelValues(chart, releaseName, version, updated, namespace).Set(float64(status)) + stats.WithLabelValues(chart, releaseName, version, appVersion, updated, namespace).Set(float64(status)) } } + prometheusHandler.ServeHTTP(w, r) } func main() { flagenv.Parse() flag.Parse() - go func() { - for { - helmStats() - time.Sleep(30 * time.Second) - } - }() - http.Handle("/metrics", promhttp.Handler()) - http.ListenAndServe(":9100", nil) + http.HandleFunc("/metrics", helmStats) + http.ListenAndServe(":9571", nil) }