Source code for aidputils.agents.toolkit.service_metrics_util
"""Shared service-level metrics helpers for AIDP toolkit runtime instrumentation."""
from opentelemetry import metrics
[docs]
class ServiceMetricUtility:
"""Factory and recorder for service-scoped OpenTelemetry metrics.
Unlike :class:`aidputils.agents.toolkit.metrics_util.MetricUtility`, this class uses a
fixed meter scope so all runtime-service metrics are published under a consistent
namespace.
"""
AIDP_SERVICE_METER_SCOPE = "aidp_agent_runtime_service_meter"
def __init__(self):
"""Create the shared service meter used by toolkit runtime components."""
self.service_meter = metrics.get_meter(ServiceMetricUtility.AIDP_SERVICE_METER_SCOPE)
[docs]
def create_counter(self, name: str, description: str = ""):
"""Create a counter under the service meter scope."""
return self.service_meter.create_counter(name, description=description)
[docs]
def increment_counter(self, counter, value: int = 1, attributes: dict | None = None) -> None:
"""Record an increment against a counter instrument."""
if attributes:
counter.add(value, attributes=attributes)
else:
counter.add(value)
[docs]
def create_histogram(self, name: str, description: str = ""):
"""Create a histogram instrument under the service meter scope."""
return self.service_meter.create_histogram(name, description=description)
[docs]
def record_histogram(self, histogram, value, attributes: dict | None = None) -> None:
"""Record a histogram sample with optional dimensions."""
if attributes:
histogram.record(value, attributes=attributes)
else:
histogram.record(value)
_aidp_util_service_metrics = ServiceMetricUtility()
[docs]
def get_service_metrics() -> ServiceMetricUtility:
"""Return the process-wide shared :class:`ServiceMetricUtility` instance."""
return _aidp_util_service_metrics