"""{{ display_name }} MCP server — generated by mcpify. Do not hand-edit.

OpenTelemetry wiring (REQ-2.3.2): traces exported over OTLP (ingested by
any Jaeger-compatible collector) and metrics exported for Prometheus
scraping — request counts/durations, MCP operation counts/durations, and
auth attempt/failure counts are recorded through the meter this module
builds.
"""

from __future__ import annotations

from opentelemetry import metrics, trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.exporter.prometheus import PrometheusMetricReader
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor


def build_tracer_provider(service_name: str) -> TracerProvider:
    resource = Resource.create({SERVICE_NAME: service_name})
    provider = TracerProvider(resource=resource)
    provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
    trace.set_tracer_provider(provider)
    return provider


def build_meter_provider(service_name: str) -> MeterProvider:
    """Starts a Prometheus scrape endpoint (default `:9464/metrics`) via
    `PrometheusMetricReader` — no separate exporter process to run.
    """
    resource = Resource.create({SERVICE_NAME: service_name})
    reader = PrometheusMetricReader()
    provider = MeterProvider(resource=resource, metric_readers=[reader])
    metrics.set_meter_provider(provider)
    return provider


def shutdown_tracing(provider: TracerProvider) -> None:
    provider.shutdown()
