Skip to content

Commit 7588379

Browse files
authored
feat: Add common namespace for metrics (#40)
1 parent d0ea561 commit 7588379

File tree

9 files changed

+31
-29
lines changed

9 files changed

+31
-29
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,21 @@ The following default metrics are exposed:
6363
##### Common metrics
6464

6565
- `flagsmith_build_info`: Has the labels `version` and `ci_commit_sha`.
66-
- `http_server_request_duration_seconds`: Histogram labeled with `method`, `route`, and `response_status`.
67-
- `http_server_requests_total`: Counter labeled with `method`, `route`, and `response_status`.
68-
- `task_processor_enqueued_tasks_total`: Counter labeled with `task_identifier`.
66+
- `flagsmith_http_server_request_duration_seconds`: Histogram labeled with `method`, `route`, and `response_status`.
67+
- `flagsmith_http_server_requests_total`: Counter labeled with `method`, `route`, and `response_status`.
68+
- `flagsmith_task_processor_enqueued_tasks_total`: Counter labeled with `task_identifier`.
6969

7070
##### Task Processor metrics
7171

72-
- `task_processor_finished_tasks_total`: Counter labeled with `task_identifier` and `result` (`"success"`, `"failure"`).
73-
- `task_processor_task_duration_seconds`: Histogram labeled with `task_identifier` and `result` (`"success"`, `"failure"`).
72+
- `flagsmith_task_processor_finished_tasks_total`: Counter labeled with `task_identifier` and `result` (`"success"`, `"failure"`).
73+
- `flagsmith_task_processor_task_duration_seconds`: Histogram labeled with `task_identifier` and `result` (`"success"`, `"failure"`).
7474

7575
##### Guidelines
7676

7777
Try to come up with meaningful metrics to cover your feature with when developing it. Refer to [Prometheus best practices][1] when naming your metric and labels.
7878

79+
As a reasonable default, Flagsmith metrics are expected to be namespaced with the `"flagsmith_"` prefix.
80+
7981
Define your metrics in a `metrics.py` module of your Django application — see [example][2]. Contrary to Prometheus Python client examples and documentation, please name a metric variable exactly as your metric name.
8082

8183
It's generally a good idea to allow users to define histogram buckets of their own. Flagsmith accepts a `PROMETHEUS_HISTOGRAM_BUCKETS` setting so users can customise their buckets. To honour the setting, use the `common.prometheus.Histogram` class when defining your histograms. When using `prometheus_client.Histogram` directly, please expose a dedicated setting like so:
@@ -84,8 +86,8 @@ It's generally a good idea to allow users to define histogram buckets of their o
8486
import prometheus_client
8587
from django.conf import settings
8688

87-
distance_from_earth_au = prometheus.Histogram(
88-
"distance_from_earth_au",
89+
flagsmith_distance_from_earth_au = prometheus.Histogram(
90+
"flagsmith_distance_from_earth_au",
8991
"Distance from Earth in astronomical units",
9092
buckets=settings.DISTANCE_FROM_EARTH_AU_HISTOGRAM_BUCKETS,
9193
)

src/common/gunicorn/logging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ def access(
6060
"method": environ.get("REQUEST_METHOD") or "",
6161
"response_status": resp.status_code,
6262
}
63-
metrics.http_server_request_duration_seconds.labels(**labels).observe(
63+
metrics.flagsmith_http_server_request_duration_seconds.labels(**labels).observe(
6464
duration_seconds
6565
)
66-
metrics.http_server_requests_total.labels(**labels).inc()
66+
metrics.flagsmith_http_server_requests_total.labels(**labels).inc()
6767

6868

6969
class GunicornJsonCapableLogger(PrometheusGunicornLogger):

src/common/gunicorn/metrics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
from common.prometheus import Histogram
44

5-
http_server_requests_total = prometheus_client.Counter(
6-
"http_server_requests_total",
5+
flagsmith_http_server_requests_total = prometheus_client.Counter(
6+
"flagsmith_http_server_requests_total",
77
"Total number of HTTP requests",
88
["route", "method", "response_status"],
99
)
10-
http_server_request_duration_seconds = Histogram(
11-
"http_server_request_duration_seconds",
10+
flagsmith_http_server_request_duration_seconds = Histogram(
11+
"flagsmith_http_server_request_duration_seconds",
1212
"HTTP request duration in seconds",
1313
["route", "method", "response_status"],
1414
)

src/task_processor/decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def delay(
8888
self.run_in_thread(args=args, kwargs=kwargs)
8989
else:
9090
logger.debug("Creating task for function '%s'...", task_identifier)
91-
metrics.task_processor_enqueued_tasks_total.labels(
91+
metrics.flagsmith_task_processor_enqueued_tasks_total.labels(
9292
task_identifier=task_identifier
9393
).inc()
9494
try:

src/task_processor/metrics.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33

44
from common.prometheus import Histogram
55

6-
task_processor_enqueued_tasks_total = prometheus_client.Counter(
7-
"task_processor_enqueued_tasks_total",
6+
flagsmith_task_processor_enqueued_tasks_total = prometheus_client.Counter(
7+
"flagsmith_task_processor_enqueued_tasks_total",
88
"Total number of enqueued tasks",
99
["task_identifier"],
1010
)
1111

1212
if settings.TASK_PROCESSOR_MODE:
13-
task_processor_finished_tasks_total = prometheus_client.Counter(
14-
"task_processor_finished_tasks_total",
13+
flagsmith_task_processor_finished_tasks_total = prometheus_client.Counter(
14+
"flagsmith_task_processor_finished_tasks_total",
1515
"Total number of finished tasks",
1616
["task_identifier", "result"],
1717
)
18-
task_processor_task_duration_seconds = Histogram(
19-
"task_processor_task_duration_seconds",
18+
flagsmith_task_processor_task_duration_seconds = Histogram(
19+
"flagsmith_task_processor_task_duration_seconds",
2020
"Task processor task duration in seconds",
2121
["task_identifier", "result"],
2222
)

src/task_processor/processor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def _run_task(
109109
)
110110

111111
ctx = ExitStack()
112-
timer = metrics.task_processor_task_duration_seconds.time()
112+
timer = metrics.flagsmith_task_processor_task_duration_seconds.time()
113113
ctx.enter_context(timer)
114114

115115
task_identifier = task.task_identifier
@@ -158,7 +158,7 @@ def _run_task(
158158
) # type: ignore[no-untyped-call]
159159
ctx.close()
160160

161-
metrics.task_processor_finished_tasks_total.labels(
161+
metrics.flagsmith_task_processor_finished_tasks_total.labels(
162162
task_identifier=task_identifier,
163163
result=result_label_value,
164164
).inc()

tests/unit/common/gunicorn/test_logging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ def test_gunicorn_prometheus_gunicorn_logger__expected_metrics(
9999

100100
# Then
101101
assert_metric(
102-
name="http_server_requests_total",
102+
name="flagsmith_http_server_requests_total",
103103
value=1.0,
104104
labels={"method": "GET", "route": "^health", "response_status": "200"},
105105
)
106106
assert_metric(
107-
name="http_server_request_duration_seconds_sum",
107+
name="flagsmith_http_server_request_duration_seconds_sum",
108108
value=0.101,
109109
labels={"method": "GET", "route": "^health", "response_status": "200"},
110110
)

tests/unit/task_processor/test_unit_task_processor_decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def my_function(*args: typing.Any, **kwargs: typing.Any) -> None:
220220

221221
# Then
222222
assert_metric(
223-
name="task_processor_enqueued_tasks_total",
223+
name="flagsmith_task_processor_enqueued_tasks_total",
224224
value=1.0,
225225
labels={"task_identifier": "test_unit_task_processor_decorators.my_function"},
226226
)

tests/unit/task_processor/test_unit_task_processor_processor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,31 +603,31 @@ def test_run_tasks__expected_metrics(
603603

604604
# Then
605605
assert_metric(
606-
name="task_processor_finished_tasks_total",
606+
name="flagsmith_task_processor_finished_tasks_total",
607607
value=1.0,
608608
labels={
609609
"task_identifier": dummy_task_identifier,
610610
"result": "success",
611611
},
612612
)
613613
assert_metric(
614-
name="task_processor_finished_tasks_total",
614+
name="flagsmith_task_processor_finished_tasks_total",
615615
value=1.0,
616616
labels={
617617
"task_identifier": raise_exception_task_identifier,
618618
"result": "failure",
619619
},
620620
)
621621
assert_metric(
622-
name="task_processor_task_duration_seconds",
622+
name="flagsmith_task_processor_task_duration_seconds",
623623
value=mocker.ANY,
624624
labels={
625625
"task_identifier": dummy_task_identifier,
626626
"result": "success",
627627
},
628628
)
629629
assert_metric(
630-
name="task_processor_task_duration_seconds",
630+
name="flagsmith_task_processor_task_duration_seconds",
631631
value=mocker.ANY,
632632
labels={
633633
"task_identifier": raise_exception_task_identifier,

0 commit comments

Comments
 (0)