Skip to content

Add opt-in telemetry auto-configuration#553

Draft
rbarbadillo wants to merge 1 commit into
mainfrom
rbarbadillo/telemetry-auto-config
Draft

Add opt-in telemetry auto-configuration#553
rbarbadillo wants to merge 1 commit 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 can be done with:

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

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

client.chat.complete(...)

or with:

MISTRAL_SDK_TELEMETRY=true

Longer term, the target 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 telemetry with MISTRAL_SDK_TELEMETRY=true or configure_telemetry(client, telemetry=True).
  • Treats MISTRAL_SDK_TELEMETRY=false and telemetry=False as opt-out.
  • Creates an isolated provider attached to the client tracing hook instead of replacing the global OTel provider.
  • Env-based auto-config skips if the application already has a real global OTel provider.
  • Explicit configure_telemetry(client, telemetry=True) can still attach a per-client provider even when a global provider exists.
  • Respects OTEL_EXPORTER_OTLP_ENDPOINT / OTEL_EXPORTER_OTLP_TRACES_ENDPOINT as exporter destination config only when telemetry is explicitly enabled.
  • Raises a clear error pointing to mistralai[telemetry] if optional telemetry dependencies are missing.

Checks / QA

1. Default: telemetry remains off

from mistralai import Mistral

client = Mistral(api_key="...")
client.chat.complete(...)

Expected: no SDK telemetry provider is auto-configured unless the app already configured OTel globally.

2. One-flag Mistral telemetry

MISTRAL_SDK_TELEMETRY=true
from mistralai import Mistral

client = Mistral(api_key="...")
client.chat.complete(...)

Expected: the SDK attaches a per-client telemetry provider and sends traces to Mistral's telemetry endpoint.

3. Explicit helper opt-in

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

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

client.chat.complete(...)

Expected: the SDK attaches a per-client telemetry provider for this client.

4. Existing global OTel provider is respected for env auto-config

MISTRAL_SDK_TELEMETRY=true
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from mistralai import Mistral

trace.set_tracer_provider(TracerProvider())

client = Mistral(api_key="...")
client.chat.complete(...)

Expected: env-based SDK telemetry auto-config skips because the app already owns the global provider. Mistral SDK spans use the global provider.

5. Explicit helper can still configure one client

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

trace.set_tracer_provider(TracerProvider())

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

client.chat.complete(...)

Expected: the global provider remains unchanged, but this Mistral client uses its own per-client telemetry provider.

6. Standard OTel endpoint env vars do not enable telemetry alone

OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
from mistralai import Mistral

client = Mistral(api_key="...")
client.chat.complete(...)

Expected: the SDK does not auto-configure telemetry just because an OTel endpoint env var exists.

7. Standard OTel endpoint env vars are respected after opt-in

MISTRAL_SDK_TELEMETRY=true
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4318/v1/traces
from mistralai import Mistral

client = Mistral(api_key="...")
client.chat.complete(...)

Expected: the SDK creates a per-client telemetry provider, but the OTLP exporter reads the standard OTel env config and sends traces to the configured collector instead of the Mistral telemetry endpoint.

@rbarbadillo rbarbadillo requested review from Yousria and jsurloppe May 22, 2026 19:02
@rbarbadillo rbarbadillo self-assigned this May 22, 2026
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.

1 participant