Skip to content

Add OpenTelemetry Config #2194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions main/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ def ready(self):
from main import features

features.configure()

# Initialize OpenTelemetry
from main.telemetry import configure_opentelemetry

configure_opentelemetry()
8 changes: 8 additions & 0 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,3 +833,11 @@ def get_all_config_keys():
}

CONTENT_FILE_SUMMARIZER_BATCH_SIZE = get_int("CONTENT_FILE_SUMMARIZER_BATCH_SIZE", 20)

# OpenTelemetry configuration
OPENTELEMETRY_ENABLED = get_bool("OPENTELEMETRY_ENABLED", False) # noqa: FBT003
OPENTELEMETRY_SERVICE_NAME = get_string("OPENTELEMETRY_SERVICE_NAME", "learn")
OPENTELEMETRY_INSECURE = get_bool("OPENTELEMETRY_INSECURE", default=True)
OPENTELEMETRY_ENDPOINT = get_string("OPENTELEMETRY_ENDPOINT", None)
OPENTELEMETRY_TRACES_BATCH_SIZE = get_int("OPENTELEMETRY_TRACES_BATCH_SIZE", 512)
OPENTELEMETRY_EXPORT_TIMEOUT_MS = get_int("OPENTELEMETRY_EXPORT_TIMEOUT_MS", 5000)
87 changes: 87 additions & 0 deletions main/telemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""OpenTelemetry initialization and configuration for Learn AI."""

import logging
from typing import Optional

from django.conf import settings
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.celery import CeleryInstrumentor
from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor
from opentelemetry.instrumentation.redis import RedisInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter

log = logging.getLogger(__name__)


def configure_opentelemetry() -> Optional[TracerProvider]:
"""
Configure OpenTelemetry with appropriate instrumentations and exporters.
Returns the tracer provider if configured, None otherwise.
"""
if not getattr(settings, "OPENTELEMETRY_ENABLED", False):
log.info("OpenTelemetry is disabled")
return None

log.info("Initializing OpenTelemetry")

# Create a resource with service info
resource = Resource.create(
{
"service.name": getattr(settings, "OPENTELEMETRY_SERVICE_NAME", "learn"),
"service.version": getattr(settings, "VERSION", "unknown"),
"deployment.environment": settings.ENVIRONMENT,
}
)

# Configure the tracer provider
tracer_provider = TracerProvider(resource=resource)
trace.set_tracer_provider(tracer_provider)

# Add console exporter for development/testing
if settings.DEBUG:
log.info("Adding console exporter for OpenTelemetry")
console_exporter = ConsoleSpanExporter()
tracer_provider.add_span_processor(BatchSpanProcessor(console_exporter))

# Add OTLP exporter if configured
otlp_endpoint = getattr(settings, "OPENTELEMETRY_ENDPOINT", None)
if otlp_endpoint:
log.info("Configuring OTLP exporter to endpoint: %s", otlp_endpoint)

headers = {}

try:
otlp_exporter = OTLPSpanExporter(
endpoint=otlp_endpoint,
headers=headers,
insecure=getattr(settings, "OPENTELEMETRY_INSECURE", True),
)

tracer_provider.add_span_processor(
BatchSpanProcessor(
otlp_exporter,
max_export_batch_size=getattr(
settings, "OPENTELEMETRY_BATCH_SIZE", 512
),
schedule_delay_millis=getattr(
settings, "OPENTELEMETRY_EXPORT_TIMEOUT_MS", 5000
),
)
)
except Exception:
log.exception("Failed to configure OTLP exporter")

# Initialize instrumentations
DjangoInstrumentor().instrument()
PsycopgInstrumentor().instrument()
RedisInstrumentor().instrument()
CeleryInstrumentor().instrument()
RequestsInstrumentor().instrument()

log.info("OpenTelemetry initialized successfully")
return tracer_provider
Loading
Loading