Varying labels in Prometheus

3/12/2017

I annotate my Kubernetes objects with things like version and whom to contact when there are failures. How would I relay this information to Prometheus, knowing that these annotation values will frequently change? I can't capture this information in Prometheus labels, as they serve as the primary key for a target (e.g. if the version changes, it's a new target altogether, which I don't want). Thanks!

-- SS781
kubernetes
prometheus

1 Answer

3/13/2017

I just wrote a blog post about this exact topic! https://www.weave.works/aggregating-pod-resource-cpu-memory-usage-arbitrary-labels-prometheus/

The trick is Kubelet/cAdvisor doesn't expose them directly, so I run a little exporter which does, and join this with the pod name in PromQL. The exporter is: https://github.com/tomwilkie/kube-api-exporter

You can do a join in Prometheus like this:

sum by (namespace, name) (
      sum(rate(container_cpu_usage_seconds_total{image!=""}[5m])) by (pod_name, namespace)
    * on (pod_name) group_left(name)
      k8s_pod_labels{job="monitoring/kube-api-exporter"}
   )

Here I'm using a label called "name", but it could be any label.

We use the same trick to get metrics (such as error rate) by version, which we then use to drive our continuous deployment system. kube-api-exporter exports a bunch of useful meta-information about Kubernetes objects to Prometheus.

Hope this helps!

-- tom.wilkie
Source: StackOverflow