Skip to content

Initial OpenTelemetry integration #182

@khvn26

Description

@khvn26

We are bringing OTel support to Flagsmith backend! In this issue, we're tracking the bare minimum required to start tracking log events for logs emitted from the Django veiew context, and attach the incoming W3C baggage to them.

1. Instrumentation setup

In ensure_cli_env:

  • Setup
  • Setup tracer and logger providers.
  • Setup configurable OTLP log and trace exporters.
  • Setup TraceContextTextMapPropagator and W3CBaggagePropagator.
  • Invoke otel's DjangoInstrumentor.

Having completed this, we should start seeing traces and spans for Django views invoked via HTTP, enriched by W3C baggage.

2. Structured logging integration

Add a Structlog processor that routes events to OTel's LoggerProvider.

  • Normalise event names by dot-prepending the logger name, and applying inflection.underscore. Use the result as OTLP 'EventName'.
  • Convert dunders to dots in Structlog attribute names, e.g. organisation__id -> organisation.id.
  • Make sure span/trace baggage is propagated for the Amplitude exporter. amplitude.device_id and amplitude.session_id are expected.

For a log emitted like this:

structlog.get_logger("code_references").info("scan-created", code_references__count=3, feature__count=2)

We should see an OTLP event like this:

Resource:
  service.name = "flagsmith-api"

LogRecord:
  EventName = "code_references.scan_created"
  SeverityNumber = Info
  Body = "scan.created"
  Attributes:
    code_references.count = 3
    feature.count = 2

Refer to https://github.com/Flagsmith/flagsmith/blob/527f72c55b8326b4c857d3f3f5c99cdf25f6065e/api/observability/otel.py and https://github.com/Flagsmith/flagsmith/blob/f4be7bbb8aba92c10b0cc4e93500c56a68550b7b/api/tests/unit/observability/test_unit_otel.py for implementation details.

3. Release, test and integrate in Core API

Acceptance criteria: Core API is configurable to send traces and logs over OTLP.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions