mirror of
https://git.linux-kernel.at/oliver/ivatar.git
synced 2025-11-20 15:08:02 +00:00
Fix OpenTelemetry Prometheus metrics server startup
- Add _start_prometheus_server method to start HTTP server - Register PrometheusMetricReader collector with prometheus_client REGISTRY - Parse endpoint to extract host and port for HTTP server - This fixes the issue where metrics endpoint was not accessible
This commit is contained in:
@@ -129,6 +129,9 @@ class OpenTelemetryConfig:
|
||||
)
|
||||
metrics.set_meter_provider(meter_provider)
|
||||
|
||||
# Start Prometheus HTTP server for metrics endpoint
|
||||
self._start_prometheus_server(prometheus_reader, prometheus_endpoint)
|
||||
|
||||
logger.info(
|
||||
f"OpenTelemetry metrics configured with Prometheus endpoint: {prometheus_endpoint}"
|
||||
)
|
||||
@@ -137,6 +140,33 @@ class OpenTelemetryConfig:
|
||||
logger.error(f"Failed to setup OpenTelemetry metrics: {e}")
|
||||
self.enabled = False
|
||||
|
||||
def _start_prometheus_server(
|
||||
self, prometheus_reader: PrometheusMetricReader, endpoint: str
|
||||
) -> None:
|
||||
"""Start Prometheus HTTP server for metrics endpoint."""
|
||||
try:
|
||||
from prometheus_client import start_http_server, REGISTRY
|
||||
|
||||
# Parse endpoint to get host and port
|
||||
if ":" in endpoint:
|
||||
host, port = endpoint.split(":", 1)
|
||||
port = int(port)
|
||||
else:
|
||||
host = "0.0.0.0"
|
||||
port = int(endpoint)
|
||||
|
||||
# Register the PrometheusMetricReader collector with prometheus_client
|
||||
REGISTRY.register(prometheus_reader._collector)
|
||||
|
||||
# Start HTTP server
|
||||
start_http_server(port, addr=host)
|
||||
|
||||
logger.info(f"Prometheus metrics server started on {host}:{port}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to start Prometheus metrics server: {e}")
|
||||
self.enabled = False
|
||||
|
||||
def setup_instrumentation(self) -> None:
|
||||
"""Set up OpenTelemetry instrumentation for various libraries."""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user