from opentelemetry import trace
from opentelemetry.sdk.resources import (
    SERVICE_NAME,
    Resource,
)
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    SpanExporter,
    SimpleSpanProcessor,
)
from ._SpanExporterSpy import SpanExporterSpy


def configure_open_telemetry(
    exporter: SpanExporter,
):
    processor = SimpleSpanProcessor(span_exporter=exporter)

    provider: TracerProvider = trace.get_tracer_provider()
    if isinstance(provider, TracerProvider):
        provider.add_span_processor(processor)
        return

    provider = TracerProvider(
        resource=Resource(
            attributes={
                SERVICE_NAME: "test"
            }
        )
    )
    provider.add_span_processor(processor)
    trace.set_tracer_provider(provider)
