Skip to content

Commit 936d598

Browse files
authored
Adding OTEL metric shipping to mitxonline (#2649)
1 parent 880b79a commit 936d598

File tree

6 files changed

+824
-76
lines changed

6 files changed

+824
-76
lines changed

app.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,30 @@
549549
"description": "The number of hours until an access token for the Open edX API expires",
550550
"required": false
551551
},
552+
"OPENTELEMETRY_ENABLED": {
553+
"description": "Enable collection and shipment of opentelemetry data",
554+
"required": false
555+
},
556+
"OPENTELEMETRY_ENDPOINT": {
557+
"description": "Endpoint for opentelemetry",
558+
"required": false
559+
},
560+
"OPENTELEMETRY_EXPORT_TIMEOUT_MS": {
561+
"description": "Timeout for opentelemetry export",
562+
"required": false
563+
},
564+
"OPENTELEMETRY_INSECURE": {
565+
"description": "Use insecure connection to opentelemetry",
566+
"required": false
567+
},
568+
"OPENTELEMETRY_SERVICE_NAME": {
569+
"description": "The name of the service to report to opentelemetry",
570+
"required": false
571+
},
572+
"OPENTELEMETRY_TRACES_BATCH_SIZE": {
573+
"description": "Batch size for traces",
574+
"required": false
575+
},
552576
"OPEN_EXCHANGE_RATES_APP_ID": {
553577
"description": "open exchange app id for fetching currency exchange rate",
554578
"required": false

main/apps.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ def ready(self):
1616

1717
envs.validate()
1818
configure()
19+
20+
from main.telemetry import configure_opentelemetry
21+
22+
configure_opentelemetry()

main/settings.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,3 +1351,35 @@
13511351
"localhost",
13521352
"mitxonline.odl.local",
13531353
]
1354+
1355+
1356+
OPENTELEMETRY_ENABLED = get_bool(
1357+
name="OPENTELEMETRY_ENABLED",
1358+
default=False,
1359+
description="Enable collection and shipment of opentelemetry data",
1360+
)
1361+
OPENTELEMETRY_SERVICE_NAME = get_string(
1362+
name="OPENTELEMETRY_SERVICE_NAME",
1363+
default="mitxonline",
1364+
description="The name of the service to report to opentelemetry",
1365+
)
1366+
OPENTELEMETRY_INSECURE = get_bool(
1367+
name="OPENTELEMETRY_INSECURE",
1368+
default=True,
1369+
description="Use insecure connection to opentelemetry",
1370+
)
1371+
OPENTELEMETRY_ENDPOINT = get_string(
1372+
name="OPENTELEMETRY_ENDPOINT",
1373+
default=None,
1374+
description="Endpoint for opentelemetry",
1375+
)
1376+
OPENTELEMETRY_TRACES_BATCH_SIZE = get_int(
1377+
name="OPENTELEMETRY_TRACES_BATCH_SIZE",
1378+
default=512,
1379+
description="Batch size for traces",
1380+
)
1381+
OPENTELEMETRY_EXPORT_TIMEOUT_MS = get_int(
1382+
name="OPENTELEMETRY_EXPORT_TIMEOUT_MS",
1383+
default=5000,
1384+
description="Timeout for opentelemetry export",
1385+
)

main/telemetry.py

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

0 commit comments

Comments
 (0)