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

import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { JaegerExporter } from '@opentelemetry/exporter-jaeger';
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus';
import { resourceFromAttributes } from '@opentelemetry/resources';
import { MeterProvider } from '@opentelemetry/sdk-metrics';
import { NodeTracerProvider, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node';
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';

let tracerProvider: NodeTracerProvider | undefined;
let meterProvider: MeterProvider | undefined;

/** Wires OpenTelemetry tracing (Jaeger-compatible export) and metrics
 * (Prometheus-compatible export) — REQ-2.3.2: request counts/durations, MCP
 * operation counts/durations, and auth attempt/failure counts are recorded
 * by the modules that own those events (auth-manager, tool-executor). */
export function initTracing(): void {
  const resource = resourceFromAttributes({ [ATTR_SERVICE_NAME]: '{{ project_name }}' });

  tracerProvider = new NodeTracerProvider({
    resource,
    spanProcessors: [new SimpleSpanProcessor(new JaegerExporter())],
  });
  tracerProvider.register();

  registerInstrumentations({ instrumentations: [new HttpInstrumentation()], tracerProvider });

  meterProvider = new MeterProvider({
    resource,
    readers: [new PrometheusExporter({ port: Number(process.env['{{ tool_prefix_env }}_METRICS_PORT'] ?? 9464) })],
  });
}

export async function shutdownTracing(): Promise<void> {
  await tracerProvider?.shutdown();
  await meterProvider?.shutdown();
}
