Skip to content

Commit

Permalink
change port to 9157, create metrics only on /metric scrape, add appVe…
Browse files Browse the repository at this point in the history
…rsion label
  • Loading branch information
sstarcher committed Apr 13, 2019
1 parent 9fb608f commit aadddfd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 10 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/http"
"strconv"
"time"

"k8s.io/helm/pkg/helm"

Expand All @@ -26,6 +25,7 @@ var (
"chart",
"release",
"version",
"appVersion",
"updated",
"namespace",
})
Expand All @@ -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))
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}

0 comments on commit aadddfd

Please sign in to comment.