newduino
newduino

Reputation: 187

Prometheus Library Python - Dynamic instance name

I'm using the Python Prometheus Library to instrument my application. Let's assume I have the following code:

from prometheus_client import start_http_server, Counter

data = {}
data['locations'] = 'berlin'

for i in data['location']
  metric = Counter("location_service_" + i + "_http_requests_count", "This metric tracks the requests from location: " + i)

metric.inc(1)

I'm struggling with the "metric" how can I make this dynamic based on the "location" value?

Upvotes: 1

Views: 1420

Answers (1)

anemyte
anemyte

Reputation: 20226

It's better to use labels instead of changing the metric name. Using labels will make it easier to create dashboards/alerts. Here's how:

from prometheus_client import start_http_server, Counter

label_names = ['location']
c = Counter("metric_name", "metric_descr", labelnames=label_names)
c.labels(location="berlin").inc()

Then you can query the value of this metric like this:

metric_name{location="berlin"}

More on querying here and I also encourage you to read best practices on naming.

Upvotes: 2

Related Questions