Skip to content

Commit

Permalink
Adapt usage of unique parameter to groups
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Oct 11, 2024
1 parent d30ac63 commit 80f2244
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
17 changes: 14 additions & 3 deletions kinto/plugins/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,22 @@ def timer(self, key):
def count(self, key, count=1, unique=None):
global _METRICS

# Turn `unique` into a group and a value:
# eg. `method.basicauth.mat` -> `method_basicauth="mat"`
label_value = None
if unique:
if "." not in unique:
unique = f"group.{unique}"
label_name, label_value = unique.rsplit(".", 1)
label_names = (_fix_metric_name(label_name),)
else:
label_names = tuple()

if key not in _METRICS:
_METRICS[key] = prometheus_module.Counter(
_fix_metric_name(key),
f"Counter of {key}",
labelnames=(_fix_metric_name(unique),) if unique else (),
labelnames=label_names,
registry=get_registry(),
)

Expand All @@ -105,8 +116,8 @@ def count(self, key, count=1, unique=None):
)

m = _METRICS[key]
if unique is not None:
m = m.labels(_fix_metric_name(unique))
if label_value is not None:
m = m.labels(label_value)

m.inc(count)

Expand Down
19 changes: 12 additions & 7 deletions tests/plugins/test_prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,18 @@ def test_count_by_key_value(self):
self.assertIn("bigstep_total 2.0", resp.text)

def test_count_by_key_grouped(self):
self.app.app.registry.metrics.count("status", unique="http_500")
self.app.app.registry.metrics.count("status", unique="http_200")
self.app.app.registry.metrics.count("http", unique="status.500")
self.app.app.registry.metrics.count("http", unique="status.200")

resp = self.app.get("/__metrics__")
self.assertIn('status_total{http_500="http_500"} 1.0', resp.text)
# TODO: grouping using "unique" param looks wrong
self.assertIn('status_total{http_500="http_200"} 1.0', resp.text)
self.assertIn('http_total{status="500"} 1.0', resp.text)
self.assertIn('http_total{status="200"} 1.0', resp.text)

def test_count_with_generic_group(self):
self.app.app.registry.metrics.count("mushrooms", unique="boletus")

resp = self.app.get("/__metrics__")
self.assertIn('mushrooms_total{group="boletus"} 1.0', resp.text)

def test_metrics_cant_be_mixed(self):
self.app.app.registry.metrics.count("counter")
Expand All @@ -108,7 +113,7 @@ def test_metrics_cant_be_mixed(self):
self.app.app.registry.metrics.count("timer")

def test_metrics_names_and_labels_are_transformed(self):
self.app.app.registry.metrics.count("http.home.status", unique="get.200")
self.app.app.registry.metrics.count("http.home.status", unique="code.get.200")

resp = self.app.get("/__metrics__")
self.assertIn('http_home_status_total{get_200="get_200"} 1.0', resp.text)
self.assertIn('http_home_status_total{code_get="200"} 1.0', resp.text)

0 comments on commit 80f2244

Please sign in to comment.