trustgraph/trustgraph-base/trustgraph/base/audit_publisher.py
cybermaggedon f7026efeda
refactor: reduce Prometheus metrics cardinality and clarify label names (#1059)
With many workspaces and flows, the prometheus metrics expand
greatly. Many significant metrics (e.g. latency) make more sense
measured globally. This change introduces a consolidation.

Histograms (request_latency, text_completion_duration, chunk_size, etc.)
are now global with only processor-level labels, eliminating per-workspace
time series multiplication. Counters and enums retain workspace and flow
labels for per-workspace debugging visibility.

Renamed metric labels for clarity: name -> consumer/producer/subscriber,
and added explicit workspace label alongside flow. Fixed bug where
request_response_spec labelled response subscriber with request_name
instead of response_name. Removed unused Histogram imports from model
and service files.
2026-07-23 14:51:47 +01:00

41 lines
1.2 KiB
Python

import json
import logging
from datetime import datetime, timezone
from uuid import uuid4
from . producer import Producer
from . metrics import ProducerMetrics
from trustgraph.schema import AuditEvent, audit_events_queue
logger = logging.getLogger(__name__)
class AuditPublisher:
def __init__(self, backend, component_name, processor_id=None):
self.component_name = component_name
self.producer = Producer(
backend=backend,
topic=audit_events_queue,
schema=AuditEvent,
metrics=ProducerMetrics(
processor=processor_id or component_name,
producer="audit-events",
),
)
async def emit(self, event_type, payload):
event = AuditEvent(
schema_version=1,
event_id=str(uuid4()),
event_type=event_type,
timestamp=datetime.now(timezone.utc).isoformat(),
producer=self.component_name,
payload_json=json.dumps(payload),
)
try:
await self.producer.send(event)
except Exception as e:
logger.warning(f"Failed to emit audit event: {e}")