fix(observability): preserve event names and escape metric labels

This commit is contained in:
2026-09-19 21:30:21 +03:30
parent 0869282949
commit 178741590e
2 changed files with 8 additions and 3 deletions

View File

@@ -25,7 +25,8 @@ class StructuredJsonFormatter(logging.Formatter):
"level": record.levelname, "level": record.levelname,
"service": getattr(record, "service", self.service_name), "service": getattr(record, "service", self.service_name),
"raw_id": getattr(record, "raw_id", None), "raw_id": getattr(record, "raw_id", None),
"event": record.getMessage(), "event": getattr(record, "event", record.getMessage()),
"message": record.getMessage(),
} }
# Include exception info if present # Include exception info if present

View File

@@ -3,6 +3,10 @@ from collections import defaultdict
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
def escape_label(value: str) -> str:
return value.replace(chr(92), chr(92)*2).replace('"', chr(92)+'"').replace(chr(10), chr(92)+'n')
class SimpleHistogram: class SimpleHistogram:
""" """
Lightweight, thread-safe Prometheus-compatible histogram. Lightweight, thread-safe Prometheus-compatible histogram.
@@ -136,13 +140,13 @@ class WorkerMetrics:
lines.append("# TYPE cdr_processed_total counter") lines.append("# TYPE cdr_processed_total counter")
with self._lock: with self._lock:
for (svc, rat), count in self._processed_total.items(): for (svc, rat), count in self._processed_total.items():
lines.append(f'cdr_processed_total{{service_type="{svc}",radio_access_type="{rat}"}} {count}') lines.append(f'cdr_processed_total{{service_type="{escape_label(svc)}",radio_access_type="{escape_label(rat)}"}} {count}')
# 2. cdr_dlq_total # 2. cdr_dlq_total
lines.append("# HELP cdr_dlq_total Total CDRs routed to DLQ by reason") lines.append("# HELP cdr_dlq_total Total CDRs routed to DLQ by reason")
lines.append("# TYPE cdr_dlq_total counter") lines.append("# TYPE cdr_dlq_total counter")
for reason, count in self._dlq_total.items(): for reason, count in self._dlq_total.items():
lines.append(f'cdr_dlq_total{{reason="{reason}"}} {count}') lines.append(f'cdr_dlq_total{{reason="{escape_label(reason)}"}} {count}')
# 3. Gauges # 3. Gauges
lines.append("# HELP cdr_in_flight_buffer_size Current in-memory buffer size") lines.append("# HELP cdr_in_flight_buffer_size Current in-memory buffer size")