from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.resources import (
    SERVICE_NAME,
    Resource,
)

from ._OpenTelemetryProcessorCallback import OpenTelemetryProcessorCallback


def configure_open_telemetry(
    service_name: str,
    otlp_endpoint: str = "localhost:4317",
    batch_delay_millis: int = 5000,
):
    resource = Resource(
        attributes={
            SERVICE_NAME: service_name
        }
    )

    provider = TracerProvider(resource=resource)
    otlp_exporter = OTLPSpanExporter(
        endpoint=otlp_endpoint,
        insecure=True
    )
    processor = BatchSpanProcessor(
        span_exporter=otlp_exporter,
        schedule_delay_millis=batch_delay_millis,
    )
    provider.add_span_processor(processor)
    trace.set_tracer_provider(provider)
