|
| 1 | +"""OpenTelemetry initialization and configuration for Learn AI.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Optional |
| 5 | + |
| 6 | +from django.conf import settings |
| 7 | +from opentelemetry import trace |
| 8 | +from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter |
| 9 | +from opentelemetry.instrumentation.celery import CeleryInstrumentor |
| 10 | +from opentelemetry.instrumentation.django import DjangoInstrumentor |
| 11 | +from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor |
| 12 | +from opentelemetry.instrumentation.redis import RedisInstrumentor |
| 13 | +from opentelemetry.instrumentation.requests import RequestsInstrumentor |
| 14 | +from opentelemetry.sdk.resources import Resource |
| 15 | +from opentelemetry.sdk.trace import TracerProvider |
| 16 | +from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter |
| 17 | + |
| 18 | +log = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +def configure_opentelemetry() -> Optional[TracerProvider]: |
| 22 | + """ |
| 23 | + Configure OpenTelemetry with appropriate instrumentations and exporters. |
| 24 | + Returns the tracer provider if configured, None otherwise. |
| 25 | + """ |
| 26 | + if not getattr(settings, "OPENTELEMETRY_ENABLED", False): |
| 27 | + log.info("OpenTelemetry is disabled") |
| 28 | + return None |
| 29 | + |
| 30 | + log.info("Initializing OpenTelemetry") |
| 31 | + |
| 32 | + # Create a resource with service info |
| 33 | + resource = Resource.create( |
| 34 | + { |
| 35 | + "service.name": getattr(settings, "OPENTELEMETRY_SERVICE_NAME", "learn"), |
| 36 | + "service.version": getattr(settings, "VERSION", "unknown"), |
| 37 | + "deployment.environment": settings.ENVIRONMENT, |
| 38 | + } |
| 39 | + ) |
| 40 | + |
| 41 | + # Configure the tracer provider |
| 42 | + tracer_provider = TracerProvider(resource=resource) |
| 43 | + trace.set_tracer_provider(tracer_provider) |
| 44 | + |
| 45 | + # Add console exporter for development/testing |
| 46 | + if settings.DEBUG: |
| 47 | + log.info("Adding console exporter for OpenTelemetry") |
| 48 | + console_exporter = ConsoleSpanExporter() |
| 49 | + tracer_provider.add_span_processor(BatchSpanProcessor(console_exporter)) |
| 50 | + |
| 51 | + # Add OTLP exporter if configured |
| 52 | + otlp_endpoint = getattr(settings, "OPENTELEMETRY_ENDPOINT", None) |
| 53 | + if otlp_endpoint: |
| 54 | + log.info("Configuring OTLP exporter to endpoint: %s", otlp_endpoint) |
| 55 | + |
| 56 | + headers = {} |
| 57 | + |
| 58 | + try: |
| 59 | + otlp_exporter = OTLPSpanExporter( |
| 60 | + endpoint=otlp_endpoint, |
| 61 | + headers=headers, |
| 62 | + insecure=getattr(settings, "OPENTELEMETRY_INSECURE", True), |
| 63 | + ) |
| 64 | + |
| 65 | + tracer_provider.add_span_processor( |
| 66 | + BatchSpanProcessor( |
| 67 | + otlp_exporter, |
| 68 | + max_export_batch_size=getattr( |
| 69 | + settings, "OPENTELEMETRY_BATCH_SIZE", 512 |
| 70 | + ), |
| 71 | + schedule_delay_millis=getattr( |
| 72 | + settings, "OPENTELEMETRY_EXPORT_TIMEOUT_MS", 5000 |
| 73 | + ), |
| 74 | + ) |
| 75 | + ) |
| 76 | + except Exception: |
| 77 | + log.exception("Failed to configure OTLP exporter") |
| 78 | + |
| 79 | + # Initialize instrumentations |
| 80 | + DjangoInstrumentor().instrument() |
| 81 | + PsycopgInstrumentor().instrument() |
| 82 | + RedisInstrumentor().instrument() |
| 83 | + CeleryInstrumentor().instrument() |
| 84 | + RequestsInstrumentor().instrument() |
| 85 | + |
| 86 | + log.info("OpenTelemetry initialized successfully") |
| 87 | + return tracer_provider |
0 commit comments