Skip to content

Distributed tracing for gateway endpoints#20864

Merged
TomeHirata merged 11 commits intomlflow:masterfrom
TomeHirata:gateway/usage/distributed-tracing
Feb 19, 2026
Merged

Distributed tracing for gateway endpoints#20864
TomeHirata merged 11 commits intomlflow:masterfrom
TomeHirata:gateway/usage/distributed-tracing

Conversation

@TomeHirata
Copy link
Collaborator

@TomeHirata TomeHirata commented Feb 16, 2026

Related Issues/PRs

n/a

What changes are proposed in this pull request?

When the gateway endpoint is called by a traced agent, we want to show gateway execution in the trace as well. To achieve both gateway endpoint observability and the distributed tracing between client and gateway, we will duplicate the gateway spans but without input/output payloads to minimize the additional storage utilization.

1. no distributed trace

## experiment linked to the gateway endpoint

- gateway invocation (request, response, token usage)

## experiment for the agent

-- no trace --

2. distributed trace

## experiment linked to the gateway endpoint

- gateway invocation (request, response, token usage)

## experiment for the agent

- agent trace
  - gateway invocation (link to gateway trace, token usage)

How is this PR tested?

  • Existing unit/integration tests
  • New unit/integration tests
  • Manual tests

Does this PR require documentation update?

  • No. You can skip the rest of this section.
  • Yes. I've updated:
    • Examples
    • API references
    • Instructions

Does this PR require updating the MLflow Skills repository?

  • No. You can skip the rest of this section.
  • Yes. Please link the corresponding PR or explain how you plan to update it.

Release Notes

Is this a user-facing change?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release notes for MLflow users.

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/tracking: Tracking Service, tracking client APIs, autologging
  • area/models: MLmodel format, model serialization/deserialization, flavors
  • area/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registry
  • area/scoring: MLflow Model server, model deployment tools, Spark UDFs
  • area/evaluation: MLflow model evaluation features, evaluation metrics, and evaluation workflows
  • area/gateway: MLflow AI Gateway client APIs, server, and third-party integrations
  • area/prompts: MLflow prompt engineering features, prompt templates, and prompt management
  • area/tracing: MLflow Tracing features, tracing APIs, and LLM tracing functionality
  • area/projects: MLproject format, project running backends
  • area/uiux: Front-end, user experience, plotting, JavaScript, JavaScript dev server
  • area/build: Build and test infrastructure for MLflow
  • area/docs: MLflow documentation pages

How should the PR be classified in the release notes? Choose one:

  • rn/none - No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" section
  • rn/breaking-change - The PR will be mentioned in the "Breaking Changes" section
  • rn/feature - A new user-facing feature worth mentioning in the release notes
  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes
  • rn/documentation - A user-facing documentation change worth mentioning in the release notes

Should this PR be included in the next patch release?

Yes should be selected for bug fixes, documentation updates, and other small changes. No should be selected for new features and larger changes. If you're unsure about the release classification of this PR, leave this unchecked to let the maintainers decide.

What is a minor/patch release?
  • Minor release: a release that increments the second part of the version number (e.g., 1.2.0 -> 1.3.0).
    Bug fixes, doc updates and new features usually go into minor releases.
  • Patch release: a release that increments the third part of the version number (e.g., 1.2.0 -> 1.2.1).
    Bug fixes and doc updates usually go into patch releases.
  • Yes (this PR will be cherry-picked and included in the next patch release)
  • No (this PR will be included in the next minor release)

Enhance the tracing utilities to create distributed spans for gateway calls when a traceparent header is present. This includes new functions to check for traceparent headers, retrieve token usage from spans, and create distributed spans linked to the gateway trace. Update the `maybe_traced_gateway_call` function to accept request headers and integrate tracing into the gateway API invocations. Additionally, add tests to validate the new tracing functionality and ensure proper behavior with and without traceparent headers.

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
Copilot AI review requested due to automatic review settings February 16, 2026 10:53
@github-actions github-actions bot added size/L Large PR (200-499 LoC) v3.10.0 rn/feature Mention under Features in Changelogs. labels Feb 16, 2026
@github-actions
Copy link
Contributor

🛠 DevTools 🛠

Install mlflow from this PR

# mlflow
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/20864/merge
# mlflow-skinny
pip install git+https://github.com/mlflow/mlflow.git@refs/pull/20864/merge#subdirectory=libs/skinny

For Databricks, use the following command:

%sh curl -LsSf https://raw.githubusercontent.com/mlflow/mlflow/HEAD/dev/install-skinny.sh | sh -s pull/20864/merge

…distributed-tracing

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds distributed tracing support for MLflow Gateway endpoints so incoming requests carrying W3C traceparent context can be linked back to an upstream “agent” trace while preserving a separate gateway trace.

Changes:

  • Extend maybe_traced_gateway_call to optionally create a distributed span using incoming request headers.
  • Plumb HTTP request headers through gateway API handlers into the tracing wrapper.
  • Add a new span attribute key for linking the distributed span to the gateway trace, plus new unit/integration tests.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
mlflow/gateway/tracing_utils.py Adds distributed tracing helpers and creates a distributed span when traceparent is present.
mlflow/server/gateway_api.py Captures request headers and passes them into maybe_traced_gateway_call across gateway endpoints.
mlflow/tracing/constant.py Introduces SpanAttributeKey.LINKED_GATEWAY_TRACE_ID for cross-trace linkage.
tests/gateway/test_tracing_utils.py Adds tests for traceparent detection and distributed tracing behavior (streaming + non-streaming).
Comments suppressed due to low confidence (1)

mlflow/gateway/tracing_utils.py:25

  • _has_traceparent only checks for exact keys "traceparent" and "Traceparent". HTTP header keys are case-insensitive and some frameworks may produce other casings (e.g., "TRACEPARENT"), which would silently disable distributed tracing. Consider normalizing keys (e.g., casefold/lower) before checking.
    if not span:
        return

@github-actions
Copy link
Contributor

github-actions bot commented Feb 16, 2026

Documentation preview for f7002d6 is available at:

Changed Pages (2)

More info
  • Ignore this comment if this PR does not change the documentation.
  • The preview is updated when a new commit is pushed to this PR.
  • This comment was created by this workflow run.
  • The documentation was built by this workflow run.

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
…eval

- Introduced a new dataclass `_ProviderSpanInfo` to encapsulate span name and attributes.
- Updated `_get_provider_span_attributes` to `_get_provider_span_info`, returning a list of `_ProviderSpanInfo` instead of a dictionary.
- Modified the `maybe_create_distributed_span` function to create individual spans for each provider span, improving traceability in distributed tracing scenarios.
- Updated tests to reflect changes in the span information retrieval and ensure correct functionality.

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
@TomeHirata TomeHirata changed the title [draft] Distributed tracing for gateway endpoints Distributed tracing for gateway endpoints Feb 17, 2026
TomeHirata and others added 2 commits February 17, 2026 18:06
Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
- Extract only W3C trace context headers (traceparent, tracestate) before
  passing to distributed tracing, avoiding propagation of sensitive headers
- Make gateway_experiment_id test fixture resilient to duplicate experiment
  names by reusing existing experiments

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>

try:
with set_tracing_context_from_http_request_headers(request_headers):
with mlflow.start_span(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm actually not sure if there's any clearer way to copy spans with some modification to a distributed span.

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
return {
"endpoint_id": endpoint_config.endpoint_id,
"endpoint_name": endpoint_config.endpoint_name,
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we log the server URL as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The actual server hostname?

Copy link
Collaborator

@B-Step62 B-Step62 left a comment

Choose a reason for hiding this comment

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

Looks good once #20864 (comment) is addressed

Copy link
Collaborator

@B-Step62 B-Step62 Feb 19, 2026

Choose a reason for hiding this comment

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

[Follow-up] I think it is useful to have a guide page about how to use Gateway with different llm/agent libraries, like OpenAI client, LangChain/Graph, LiteLLM, etc, including the distributed tracing setup.

TomeHirata and others added 2 commits February 19, 2026 15:06
- Add ASCII diagram documenting distributed trace shape
- Log server URL (from Host header) in gateway span attributes
- Rename provider → model in span naming to align with endpoint IA
- Propagate span status to distributed model spans
- Add timing assertions in tests

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
…distributed-tracing

Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
@TomeHirata TomeHirata enabled auto-merge February 19, 2026 06:26
@TomeHirata TomeHirata disabled auto-merge February 19, 2026 07:05
@TomeHirata TomeHirata merged commit fea5bbc into mlflow:master Feb 19, 2026
56 of 58 checks passed
@github-actions github-actions bot added size/XL Extra-large PR (500+ LoC) and removed size/L Large PR (200-499 LoC) labels Feb 19, 2026
daniellok-db pushed a commit to daniellok-db/mlflow that referenced this pull request Feb 20, 2026
Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
daniellok-db pushed a commit that referenced this pull request Feb 20, 2026
Signed-off-by: Tomu Hirata <tomu.hirata@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rn/feature Mention under Features in Changelogs. size/XL Extra-large PR (500+ LoC) v3.10.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants