-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 renamingOPENTELEMETRY_BATCH_SIZE
toOPENTELEMETRY_TRACES_BATCH_SIZE
to differentiate it from metrics or logs batch sizes.