Лаба 6: OpenTelemetry + Grafana наблюдаемость (оценка 4)

Переиспользован стек lab5. API/worker шлют OTLP (traces/metrics/logs) в
otel-collector -> Prometheus/Loki/Tempo; Grafana с provisioning датасорсов,
дашборда и alert rules в Git. W3C trace context переносится через поле
traceparent в задании очереди (API producer span -> worker consumer span
в одном trace, подтверждено прямым запросом к Tempo). Три контролируемых
инцидента прогнаны и задокументированы (worker down -> queue lag + alert,
DB down отличим от медленного handler по error-span, telemetry backend down
не роняет API/Collector). В процессе найдены и исправлены 4 реальных бага
(middleware после старта приложения, detached span context, замерзающая
метрика очереди при падении её единственного источника, неатомарный
XACK+XDEL) — см. REPORT.md и evidence/.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-10-30 01:59:33 +03:00
co-authored by Claude Sonnet 5
parent 470d9d6f1e
commit 6c50a63743
40 changed files with 3032 additions and 17 deletions
+91
View File
@@ -0,0 +1,91 @@
{
"title": "Messenger overview",
"uid": "messenger-overview",
"timezone": "utc",
"schemaVersion": 39,
"version": 1,
"time": {"from": "now-1h", "to": "now"},
"refresh": "10s",
"panels": [
{
"id": 1, "title": "HTTP requests/s by status class", "type": "timeseries",
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 0},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "sum(rate(http_requests_total{route!~\"/health.*\"}[1m])) by (status_class)", "legendFormat": "{{status_class}}"}],
"fieldConfig": {"defaults": {"unit": "reqps"}, "overrides": []}
},
{
"id": 2, "title": "HTTP error ratio (5xx / all business requests)", "type": "stat",
"gridPos": {"h": 8, "w": 6, "x": 12, "y": 0},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "sum(rate(http_requests_total{status_class=\"5xx\", route!~\"/health.*\"}[5m])) / sum(rate(http_requests_total{route!~\"/health.*\"}[5m]))"}],
"fieldConfig": {"defaults": {"unit": "percentunit", "decimals": 2}, "overrides": []}
},
{
"id": 3, "title": "HTTP duration p50/p95/p99", "type": "timeseries",
"gridPos": {"h": 8, "w": 6, "x": 18, "y": 0},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [
{"expr": "histogram_quantile(0.50, sum(rate(http_request_duration_seconds_bucket{route!~\"/health.*\"}[5m])) by (le))", "legendFormat": "p50"},
{"expr": "histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket{route!~\"/health.*\"}[5m])) by (le))", "legendFormat": "p95"},
{"expr": "histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket{route!~\"/health.*\"}[5m])) by (le))", "legendFormat": "p99"}
],
"fieldConfig": {"defaults": {"unit": "s"}, "overrides": []}
},
{
"id": 4, "title": "Jobs processed/s by outcome", "type": "timeseries",
"gridPos": {"h": 8, "w": 8, "x": 0, "y": 8},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "sum(rate(jobs_processed_total[1m])) by (outcome)", "legendFormat": "{{outcome}}"}],
"fieldConfig": {"defaults": {"unit": "ops"}, "overrides": []}
},
{
"id": 5, "title": "Job processing duration p95", "type": "timeseries",
"gridPos": {"h": 8, "w": 8, "x": 8, "y": 8},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "histogram_quantile(0.95, sum(rate(job_processing_duration_seconds_bucket[5m])) by (le))", "legendFormat": "p95 processing"}],
"fieldConfig": {"defaults": {"unit": "s"}, "overrides": []}
},
{
"id": 6, "title": "Time to ready (upload -> ready) p95", "type": "timeseries",
"gridPos": {"h": 8, "w": 8, "x": 16, "y": 8},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "histogram_quantile(0.95, sum(rate(time_to_ready_seconds_bucket[5m])) by (le))", "legendFormat": "p95 time-to-ready"}],
"fieldConfig": {"defaults": {"unit": "s"}, "overrides": []}
},
{
"id": 7, "title": "Queue depth (pending jobs)", "type": "timeseries",
"gridPos": {"h": 8, "w": 8, "x": 0, "y": 16},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "queue_backlog{metric=\"depth\"}", "legendFormat": "pending"}],
"fieldConfig": {"defaults": {"unit": "short"}, "overrides": []}
},
{
"id": 8, "title": "Oldest pending job age", "type": "timeseries",
"gridPos": {"h": 8, "w": 8, "x": 8, "y": 16},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "queue_backlog{metric=\"oldest_age_seconds\"}", "legendFormat": "oldest age"}],
"fieldConfig": {"defaults": {"unit": "s"}, "overrides": []}
},
{
"id": 9, "title": "DB pool used/idle", "type": "timeseries",
"gridPos": {"h": 8, "w": 8, "x": 16, "y": 16},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "db_pool_connections", "legendFormat": "{{service_instance_id}} {{state}}"}],
"fieldConfig": {"defaults": {"unit": "short"}, "overrides": []}
},
{
"id": 10, "title": "Worker busy/capacity", "type": "timeseries",
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 24},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "worker_concurrency_slots", "legendFormat": "{{state}}"}],
"fieldConfig": {"defaults": {"unit": "short"}, "overrides": []}
},
{
"id": 11, "title": "Recent API logs", "type": "logs",
"gridPos": {"h": 8, "w": 12, "x": 12, "y": 24},
"datasource": {"type": "loki", "uid": "Loki"},
"targets": [{"expr": "{service_name=\"messenger-api\"}"}]
}
]
}
@@ -0,0 +1,68 @@
apiVersion: 1
groups:
- orgId: 1
name: messenger-slo
folder: Messenger
interval: 30s
rules:
- uid: messenger-high-error-rate
title: High HTTP error rate
condition: C
for: 2m
data:
- refId: A
relativeTimeRange: {from: 300, to: 0}
datasourceUid: Prometheus
model:
expr: sum(rate(http_requests_total{status_class="5xx", route!~"/health.*"}[5m])) / sum(rate(http_requests_total{route!~"/health.*"}[5m]))
instant: true
refId: A
- refId: C
datasourceUid: "__expr__"
model:
type: threshold
expression: A
conditions:
- evaluator: {type: gt, params: [0.05]}
refId: C
labels:
severity: critical
annotations:
summary: "5xx ratio on business requests above 5% for 2 minutes"
description: >
SLI: 5xx / all business requests (excluding /health*), 5m window.
SLO: error ratio < 5%. Runbook: check `db_pool_connections` (DB
saturation?) and recent error-level entries in {service_name="messenger-api"}
in Loki; correlate via trace_id with Tempo to find the failing dependency.
- uid: messenger-growing-queue-lag
title: Growing attachment queue lag
condition: C
for: 2m
data:
- refId: A
relativeTimeRange: {from: 60, to: 0}
datasourceUid: Prometheus
model:
expr: max(queue_backlog{metric="oldest_age_seconds"})
instant: true
refId: A
- refId: C
datasourceUid: "__expr__"
model:
type: threshold
expression: A
conditions:
- evaluator: {type: gt, params: [120]}
refId: C
labels:
severity: warning
annotations:
summary: "Oldest pending attachment job older than 2 minutes"
description: >
SLI: oldest_age_seconds from queue_backlog. SLO: < 120s under
normal load. Runbook: check `worker_concurrency_slots` (busy==capacity
sustained => worker saturated) and whether the worker container is
running (`docker compose ps worker`); check messenger-worker logs
in Loki for repeated processing_failed entries (S3/DB dependency down).
@@ -0,0 +1,11 @@
apiVersion: 1
providers:
- name: messenger
orgId: 1
folder: Messenger
type: file
disableDeletion: false
updateIntervalSeconds: 30
options:
path: /etc/grafana/dashboards
@@ -0,0 +1,29 @@
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
uid: Prometheus
access: proxy
url: http://prometheus:9090
isDefault: true
jsonData:
timeInterval: 5s
- name: Loki
type: loki
uid: Loki
access: proxy
url: http://loki:3100
jsonData:
derivedFields:
- name: TraceID
matcherRegex: "trace_id\":\"(\\w+)\""
url: "$${__value.raw}"
datasourceUid: tempo
- name: Tempo
type: tempo
access: proxy
url: http://tempo:3200
uid: tempo