Skip to content

Add opt-in telemetry auto-configuration#553

Merged
rbarbadillo merged 4 commits into
mainfrom
rbarbadillo/telemetry-auto-config
May 26, 2026
Merged

Add opt-in telemetry auto-configuration#553
rbarbadillo merged 4 commits into
mainfrom
rbarbadillo/telemetry-auto-config

Conversation

@rbarbadillo
Copy link
Copy Markdown
Contributor

@rbarbadillo rbarbadillo commented May 22, 2026

Summary

Adds opt-in OpenTelemetry telemetry auto-configuration for the Python SDK.

The SDK already emits spans via the OTel API. This PR adds a mistralai[telemetry] optional extra with the OTel SDK/exporter dependencies, plus helper/env-based configuration so users do not need to manually wire an OTLP exporter, endpoint, auth headers, span processor, and tracer provider just to get Mistral SDK traces working.

Telemetry remains off by default.

Usage

Explicit opt-in with an SDK-owned telemetry provider:

from mistralai import Mistral
from mistralai.extra.observability import configure_telemetry

client = Mistral(api_key="...")
configure_telemetry(client)

client.chat.complete(...)

Use the global OpenTelemetry provider:

from mistralai import Mistral
from mistralai.extra.observability import configure_telemetry

client = Mistral(api_key="...")
configure_telemetry(client, provider="global")

client.chat.complete(...)

Attach an already constructed provider:

from opentelemetry.sdk.trace import TracerProvider
from mistralai import Mistral
from mistralai.extra.observability import configure_telemetry

client = Mistral(api_key="...")
configure_telemetry(client, provider=TracerProvider())

client.chat.complete(...)

Or configure by environment:

MISTRAL_SDK_TELEMETRY=dedicated
MISTRAL_SDK_TELEMETRY=global
MISTRAL_SDK_TELEMETRY=false

For SDK-owned dedicated telemetry, the exporter endpoint can be overridden with:

MISTRAL_OTLP_TRACES_ENDPOINT=https://...

Longer term, the target generated API is:

client = Mistral(api_key="...", telemetry=True)

That constructor parameter is not included here because the constructor is Speakeasy-generated; this PR keeps the implementation in hand-owned code and leaves the path open for codegen support later.

Behavior

  • Adds mistralai[telemetry] with:
    • opentelemetry-sdk
    • opentelemetry-exporter-otlp-proto-http
  • Keeps telemetry disabled by default.
  • Enables SDK-owned telemetry with MISTRAL_SDK_TELEMETRY=dedicated or configure_telemetry(client).
  • Uses the global OpenTelemetry provider with MISTRAL_SDK_TELEMETRY=global or configure_telemetry(client, provider="global").
  • Attaches caller-owned providers with configure_telemetry(client, provider=<TracerProvider>).
  • Keeps set_tracer_provider(client, provider) as a compatibility wrapper over configure_telemetry(client, provider=provider).
  • Treats MISTRAL_SDK_TELEMETRY=false as an env opt-out.
  • Keeps internal telemetry=False handling for future SDK config/env resolution, but does not expose that on the public helper API.
  • Env-based dedicated auto-config skips if the application already has a real global OTel provider and logs a debug message.
  • Explicit configure_telemetry(client) can still attach an SDK-owned per-client provider even when a global provider exists.
  • SDK-owned dedicated telemetry sends to MISTRAL_OTLP_TRACES_ENDPOINT when set, otherwise to the default Mistral telemetry endpoint.
  • Standard OTel exporter env vars such as OTEL_EXPORTER_OTLP_ENDPOINT and OTEL_EXPORTER_OTLP_TRACES_ENDPOINT remain scoped to application/global OTel configuration; they do not opt into or steer the SDK-owned dedicated provider.
  • Raises a clear error pointing to mistralai[telemetry] if optional telemetry dependencies are missing.

@rbarbadillo rbarbadillo requested review from Yousria and jsurloppe May 22, 2026 19:02
@rbarbadillo rbarbadillo self-assigned this May 22, 2026
Comment thread uv.lock
Comment thread src/mistralai/extra/observability/telemetry.py Outdated
@rbarbadillo rbarbadillo force-pushed the rbarbadillo/telemetry-auto-config branch from aa7ab84 to bf80c79 Compare May 25, 2026 11:03
@rbarbadillo rbarbadillo marked this pull request as ready for review May 25, 2026 11:12
@rbarbadillo rbarbadillo requested a review from jsurloppe May 25, 2026 11:12
@rbarbadillo rbarbadillo force-pushed the rbarbadillo/telemetry-auto-config branch 4 times, most recently from ab1403d to a55a112 Compare May 25, 2026 21:50
@rbarbadillo rbarbadillo force-pushed the rbarbadillo/telemetry-auto-config branch from a55a112 to d7be63b Compare May 25, 2026 22:16
@rbarbadillo rbarbadillo requested review from Nelson-PROIA and removed request for Yousria May 25, 2026 22:25
@rbarbadillo rbarbadillo merged commit 784334a into main May 26, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants