"""Tracing module with OTLP export support.

Mirrors t_obs.traces interface for easy migration.
"""

from typing import Optional

from opentelemetry import trace

from thoughtful.supervisor.telemetry.config import get_service_name
from thoughtful.supervisor.telemetry.traces.spans import (
    close_root_span,
    create_root_span,
)
from thoughtful.supervisor.telemetry.traces.step_tracer import StepTracer


def get_tracer(name: Optional[str] = None) -> trace.Tracer:
    """Get a namespaced tracer for creating spans.

    Args:
        name: Optional namespace suffix. Results in "{service}.{name}" tracer.

    Returns:
        Tracer instance for creating and managing spans.

    Example:
        from thoughtful.supervisor.telemetry.traces import get_tracer

        tracer = get_tracer()
        with tracer.start_as_current_span("my_operation"):
            do_work()
    """
    service_name = get_service_name()
    tracer_name = service_name if name is None else f"{service_name}.{name}"
    return trace.get_tracer(tracer_name)


__all__ = [
    "get_tracer",
    "create_root_span",
    "close_root_span",
    "StepTracer",
]
