Skip to content

Add OpenTelemetry Config #101

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 3 commits into from
Mar 18, 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
10 changes: 6 additions & 4 deletions main/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@


class MainConfig(AppConfig):
"""
Main configuration
"""
"""Main app configuration."""

default_auto_field = "django.db.models.BigAutoField"
name = "main"

def ready(self):
"""Initialize the app"""
# Initialize features
from main import features

features.configure()

# Initialize OpenTelemetry
from main.telemetry import configure_opentelemetry
configure_opentelemetry()
7 changes: 7 additions & 0 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,4 +639,11 @@ def get_all_config_keys():
},
}

# OpenTelemetry configuration
OPENTELEMETRY_ENABLED = get_bool("OPENTELEMETRY_ENABLED", False) # noqa: FBT003
OPENTELEMETRY_SERVICE_NAME = get_string("OPENTELEMETRY_SERVICE_NAME", "learn-ai")
OPENTELEMETRY_ENDPOINT = get_string("OPENTELEMETRY_ENDPOINT", None)
OPENTELEMETRY_BATCH_SIZE = get_int("OPENTELEMETRY_BATCH_SIZE", 512)
OPENTELEMETRY_EXPORT_TIMEOUT_MS = get_int("OPENTELEMETRY_EXPORT_TIMEOUT_MS", 5000)
Comment on lines +643 to +647
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider prefixing all OpenTelemetry settings with OPENTELEMETRY_ for consistency and clarity. This makes it easier to identify related settings.

For example, OPENTELEMETRY_SERVICE_NAME is good, but consider renaming OPENTELEMETRY_BATCH_SIZE to OPENTELEMETRY_TRACES_BATCH_SIZE to differentiate it from metrics or logs batch sizes.

Suggested change
OPENTELEMETRY_ENABLED = get_bool("OPENTELEMETRY_ENABLED", False) # noqa: FBT003
OPENTELEMETRY_SERVICE_NAME = get_string("OPENTELEMETRY_SERVICE_NAME", "learn-ai")
OPENTELEMETRY_ENDPOINT = get_string("OPENTELEMETRY_ENDPOINT", None)
OPENTELEMETRY_BATCH_SIZE = get_int("OPENTELEMETRY_BATCH_SIZE", 512)
OPENTELEMETRY_EXPORT_TIMEOUT_MS = get_int("OPENTELEMETRY_EXPORT_TIMEOUT_MS", 5000)
OPENTELEMETRY_ENABLED = get_bool("OPENTELEMETRY_ENABLED", False) # noqa: FBT003
OPENTELEMETRY_SERVICE_NAME = get_string("OPENTELEMETRY_SERVICE_NAME", "learn-ai")
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)


SPECTACULAR_SETTINGS = open_spectacular_settings
77 changes: 77 additions & 0 deletions main/telemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""OpenTelemetry initialization and configuration for Learn AI."""

import logging
from typing import Optional
from urllib.parse import quote

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-ai"),
"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(f"Configuring OTLP exporter to endpoint: {otlp_endpoint}")

headers = {}

otlp_exporter = OTLPSpanExporter(
endpoint=otlp_endpoint,
headers=headers,
)

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),
)
)
Comment on lines +50 to +67
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It might be beneficial to add error handling or logging for cases where the OTLP exporter configuration fails. This could help in debugging issues related to telemetry data export.

    otlp_endpoint = getattr(settings, "OPENTELEMETRY_ENDPOINT", None)
    if otlp_endpoint:
        log.info(f"Configuring OTLP exporter to endpoint: {otlp_endpoint}")
        
        headers = {}

        try:
            otlp_exporter = OTLPSpanExporter(
                endpoint=otlp_endpoint,
                headers=headers,
            )
        
            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 as e:
            log.exception("Failed to configure OTLP exporter: %s", e)


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

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