fix(otel): lazy-init metrics and fix Grafana dashboards
Run Tests on Branches / Detect Changes (push) Successful in 14s
Run Tests on Branches / Backend Tests (push) Successful in 4m3s
Run Tests on Branches / Frontend Tests (push) Has been skipped
Run Tests on Branches / Frontend Mobile Tests (push) Has been skipped
Run Tests on Branches / Parapharmacy API Tests (push) Has been skipped
Run Tests on Branches / PIP Platform Tests (push) Has been skipped

- Fix OTel custom metrics not reaching Prometheus: ESM static import
  hoisting caused metrics.getMeter() to run before sdk.start(), getting
  NoopMeterProvider. Changed to lazy-init pattern so instruments are
  created on first use after the SDK configures the real MeterProvider.

- Fix Grafana dashboards job labels: OTel adds service.namespace prefix
  (job=farmafinder/farmafinder-backend), updated queries to use regex
  match (job=~".*farmafinder-backend").

- Fix histogram metric names: OTel appends unit suffix (_milliseconds)
  to histogram names (e.g. http_request_duration_ms_milliseconds_bucket).

Verified: app_heartbeat_total, http_requests_total, db_query_duration_ms,
redis_cmd_duration_ms, and all other custom metrics now flow correctly
through OTLP -> Alloy -> Prometheus remote_write.
This commit is contained in:
Antoni Nuñez Romeu
2026-07-21 13:18:18 +02:00
parent 159e14bed8
commit f32ff701e1
3 changed files with 87 additions and 48 deletions
+21 -21
View File
@@ -12,7 +12,7 @@
"gridPos": { "h": 6, "w": 8, "x": 0, "y": 0 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "sum(rate(http_requests_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "req/s" }
{ "expr": "sum(rate(http_requests_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "req/s" }
]
},
{
@@ -20,7 +20,7 @@
"gridPos": { "h": 6, "w": 8, "x": 8, "y": 0 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "sum by (status_class) (rate(http_requests_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "{{status_class}}" }
{ "expr": "sum by (status_class) (rate(http_requests_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "{{status_class}}" }
]
},
{
@@ -28,7 +28,7 @@
"gridPos": { "h": 6, "w": 8, "x": 16, "y": 0 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "sum(rate(http_requests_total{status_class=\"5xx\",job=\"farmafinder-backend\"}[5m])) / sum(rate(http_requests_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "5xx ratio" }
{ "expr": "sum(rate(http_requests_total{status_class=\"5xx\",job=~\".*farmafinder-backend\"}[5m])) / sum(rate(http_requests_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "5xx ratio" }
],
"fieldConfig": { "defaults": { "unit": "percentunit", "thresholds": { "steps": [ { "color": "green", "value": 0 }, { "color": "yellow", "value": 0.01 }, { "color": "red", "value": 0.05 } ] } } }
},
@@ -37,7 +37,7 @@
"gridPos": { "h": 6, "w": 12, "x": 0, "y": 6 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "histogram_quantile(0.95, sum by (le, route) (rate(http_request_duration_ms_bucket{job=\"farmafinder-backend\"}[5m])))", "legendFormat": "{{route}} p95" }
{ "expr": "histogram_quantile(0.95, sum by (le, route) (rate(http_request_duration_ms_milliseconds_bucket{job=~\".*farmafinder-backend\"}[5m])))", "legendFormat": "{{route}} p95" }
],
"fieldConfig": { "defaults": { "unit": "ms" } }
},
@@ -46,8 +46,8 @@
"gridPos": { "h": 6, "w": 12, "x": 12, "y": 6 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "sum(rate(medicine_searches_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "searches/s" },
{ "expr": "sum(rate(cima_requests_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "cima req/s" }
{ "expr": "sum(rate(medicine_searches_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "searches/s" },
{ "expr": "sum(rate(cima_requests_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "cima req/s" }
]
},
{
@@ -55,7 +55,7 @@
"gridPos": { "h": 6, "w": 8, "x": 0, "y": 12 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "sum(rate(cache_hits_total{job=\"farmafinder-backend\"}[5m])) / (sum(rate(cache_hits_total{job=\"farmafinder-backend\"}[5m])) + sum(rate(cache_misses_total{job=\"farmafinder-backend\"}[5m])))", "legendFormat": "hit ratio" }
{ "expr": "sum(rate(cache_hits_total{job=~\".*farmafinder-backend\"}[5m])) / (sum(rate(cache_hits_total{job=~\".*farmafinder-backend\"}[5m])) + sum(rate(cache_misses_total{job=~\".*farmafinder-backend\"}[5m])))", "legendFormat": "hit ratio" }
],
"fieldConfig": { "defaults": { "unit": "percentunit" } }
},
@@ -64,7 +64,7 @@
"gridPos": { "h": 6, "w": 8, "x": 8, "y": 12 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "sum(rate(cima_requests_total{status=\"error\",job=\"farmafinder-backend\"}[5m]))", "legendFormat": "errors/s" }
{ "expr": "sum(rate(cima_requests_total{status=\"error\",job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "errors/s" }
]
},
{
@@ -72,8 +72,8 @@
"gridPos": { "h": 6, "w": 8, "x": 16, "y": 12 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "sum(rate(login_success_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "success/s" },
{ "expr": "sum(rate(login_failure_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "failure/s" }
{ "expr": "sum(rate(login_success_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "success/s" },
{ "expr": "sum(rate(login_failure_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "failure/s" }
]
},
{
@@ -81,7 +81,7 @@
"gridPos": { "h": 6, "w": 12, "x": 0, "y": 18 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "sum by (route) (rate(rate_limit_rejected_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "{{route}}" }
{ "expr": "sum by (route) (rate(rate_limit_rejected_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "{{route}}" }
]
},
{
@@ -89,8 +89,8 @@
"gridPos": { "h": 6, "w": 12, "x": 12, "y": 18 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "sum by (channel) (rate(push_sent_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "sent {{channel}}" },
{ "expr": "sum by (channel) (rate(push_failed_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "failed {{channel}}" }
{ "expr": "sum by (channel) (rate(push_sent_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "sent {{channel}}" },
{ "expr": "sum by (channel) (rate(push_failed_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "failed {{channel}}" }
]
},
{
@@ -98,8 +98,8 @@
"gridPos": { "h": 6, "w": 12, "x": 0, "y": 24 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "histogram_quantile(0.95, sum by (le, engine) (rate(db_query_duration_ms_bucket{job=\"farmafinder-backend\"}[5m])))", "legendFormat": "p95 {{engine}}" },
{ "expr": "sum by (engine) (rate(db_errors_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "errors {{engine}}" }
{ "expr": "histogram_quantile(0.95, sum by (le, engine) (rate(db_query_duration_ms_milliseconds_bucket{job=~\".*farmafinder-backend\"}[5m])))", "legendFormat": "p95 {{engine}}" },
{ "expr": "sum by (engine) (rate(db_errors_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "errors {{engine}}" }
],
"fieldConfig": { "defaults": { "unit": "ms" } }
},
@@ -108,8 +108,8 @@
"gridPos": { "h": 6, "w": 12, "x": 12, "y": 24 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "histogram_quantile(0.95, sum by (le) (rate(redis_cmd_duration_ms_bucket{job=\"farmafinder-backend\"}[5m])))", "legendFormat": "p95 redis" },
{ "expr": "sum(rate(redis_errors_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "redis errors/s" }
{ "expr": "histogram_quantile(0.95, sum by (le) (rate(redis_cmd_duration_ms_milliseconds_bucket{job=~\".*farmafinder-backend\"}[5m])))", "legendFormat": "p95 redis" },
{ "expr": "sum(rate(redis_errors_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "redis errors/s" }
],
"fieldConfig": { "defaults": { "unit": "ms" } }
},
@@ -118,8 +118,8 @@
"gridPos": { "h": 6, "w": 12, "x": 0, "y": 30 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "sum by (op) (rate(pharmacy_write_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "pharmacy {{op}}" },
{ "expr": "sum by (op) (rate(pharmacy_medicine_link_total{job=\"farmafinder-backend\"}[5m]))", "legendFormat": "link {{op}}" }
{ "expr": "sum by (op) (rate(pharmacy_write_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "pharmacy {{op}}" },
{ "expr": "sum by (op) (rate(pharmacy_medicine_link_total{job=~\".*farmafinder-backend\"}[5m]))", "legendFormat": "link {{op}}" }
]
},
{
@@ -127,7 +127,7 @@
"gridPos": { "h": 6, "w": 12, "x": 12, "y": 30 },
"datasource": { "type": "prometheus", "uid": "PBFA97CFB590B2093" },
"targets": [
{ "expr": "max(app_heartbeat_total{job=\"farmafinder-backend\"})", "legendFormat": "heartbeat total" }
{ "expr": "max(app_heartbeat_total{job=~\".*farmafinder-backend\"})", "legendFormat": "heartbeat total" }
]
}
],
@@ -140,6 +140,6 @@
"timezone": "browser",
"title": "FarmaFinder — Backend",
"uid": "farmafinder-backend",
"version": 1,
"version": 2,
"weekStart": ""
}