Skip to content
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

Add OpenTelemetry Config #101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add OpenTelemetry Config #101

wants to merge 1 commit into from

Conversation

shaidar
Copy link

@shaidar shaidar commented Mar 14, 2025

What are the relevant tickets?

Part of https://github.com/mitodl/hq/issues/6893

Description (What does it do?)

Adds necessary packages and configs to the app to send OpenTelemetry data to running container.

How can this be tested?

Ran it locally and got the data shipping to Grafana

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @shaidar, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request adds OpenTelemetry configuration to the Learn AI application. It includes the necessary packages and configurations to send OpenTelemetry data to a running container. The changes involve modifying main/apps.py to initialize OpenTelemetry, adding OpenTelemetry-related settings to main/settings.py, creating a new file main/telemetry.py to handle the OpenTelemetry configuration, and adding OpenTelemetry dependencies to pyproject.toml.

Highlights

  • OpenTelemetry Integration: Adds OpenTelemetry to the application to enable tracing and monitoring.
  • Configuration: Introduces new settings for configuring OpenTelemetry, such as enabling it, setting the service name, endpoint, batch size, and export timeout.
  • Instrumentation: Instruments Django, Psycopg, Redis, Celery, and Requests to automatically collect tracing data.

Changelog

  • main/apps.py
    • Modified the ready method to initialize OpenTelemetry by calling configure_opentelemetry from main.telemetry.
  • main/settings.py
    • Added new settings for OpenTelemetry, including OPENTELEMETRY_ENABLED, OPENTELEMETRY_SERVICE_NAME, OPENTELEMETRY_ENDPOINT, OPENTELEMETRY_BATCH_SIZE, and OPENTELEMETRY_EXPORT_TIMEOUT_MS.
  • main/telemetry.py
    • Created a new file to configure OpenTelemetry, including setting up the tracer provider, adding console and OTLP exporters, and instrumenting Django, Psycopg, Redis, Celery, and Requests.
  • pyproject.toml
    • Added dependencies for OpenTelemetry, including opentelemetry-api, opentelemetry-sdk, opentelemetry-instrumentation-*, and opentelemetry-exporter-otlp.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Did you know?

OpenTelemetry is a Cloud Native Computing Foundation (CNCF) project, joining other graduated projects like Kubernetes and Prometheus.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces OpenTelemetry configuration to the application, including necessary packages and configurations to send telemetry data to a running container. The changes look good overall, but there are a few areas that could be improved for clarity and maintainability.

Summary of Findings

  • Settings Naming Convention: Consider using a more consistent naming convention for OpenTelemetry settings in settings.py to improve readability and maintainability. For example, prefixing all settings with OPENTELEMETRY_.
  • Error Handling in Telemetry Configuration: While the code checks if OpenTelemetry is enabled, it might be beneficial to add more robust error handling or logging for cases where the OTLP exporter configuration fails.
  • Missing Instrumentation: The code initializes several instrumentations, but it might be useful to consider adding more instrumentations based on the application's dependencies (e.g., database client, message queue).

Merge Readiness

The pull request is almost ready for merging. Addressing the naming convention and adding more robust error handling would improve the code quality. I am unable to directly approve the pull request, and other reviewers should review and approve this code before merging.

Comment on lines +643 to +647
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)

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)

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

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant