Skip to content

feat(metrics): attach trace exemplars to the inference latency histogram - #541

Merged
jtechapps merged 4 commits into
llm-d:mainfrom
sohom-cs:feat/inference-latency-exemplar
Nov 28, 2026
Merged

jtechapps merged 4 commits into
llm-d:mainfrom
sohom-cs:feat/inference-latency-exemplar

Conversation

@sohom-cs

@sohom-cs sohom-cs commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds a trace exemplar to llm_d_async_async_inference_latency_time_millis, the async processor's row of item 2 in llm-d/llm-d-router#3584. When the request's trace is sampled, each observation carries its trace_id and the span_id of its process-request span. The helper follows llm-d-router's ObserveWithTraceExemplar, cut down to this histogram, with the same label names.

The classic text format has no exemplars, so the metrics server now enables OpenMetrics through controller-runtime's Options.HandlerOpts, as the EPP's newMetricsServerOptions does. Scrapers that don't ask for OpenMetrics get the classic format, and --metrics-endpoint-auth works as before. No series is renamed, since every counter already ends in _total. On Prometheus 2.x, an OpenMetrics scrape stores whole-number bucket bounds as le="10.0", so a query on le="0" of llm_d_async_async_deadline_proximity_millis needs le="0.0"; docs/observability.md says so.

The chart's dashboard gets an inference latency panel with exemplars on its p99 query. llm-d's copy of the dashboard gets the same toggle in llm-d/llm-d#3140.

Why is this change needed?

So a slow point on the inference latency panel opens the trace behind it, as the EPP's latency histograms have since llm-d/llm-d-router#2774.

How was this tested?

  • Unit tests added/updated

  • Integration/e2e tests added/updated

  • Manual testing performed

  • pkg/metrics: a recording span gives both IDs, a propagated sampled one only trace_id, an unsampled one or none no exemplar (the cases of observability: add exemplars to tokenizer render latency llm-d-router#4069).

  • pkg/asyncworker: a request through Worker leaves an exemplar naming its process-request span. It fails on main.

  • pkg/server: the metrics server built from startMetricsServer's options serves the exemplar to an OpenMetrics scrape and the classic format to a classic one; with auth on, a scrape without a token gets 401.

  • go test -race on the three packages, make test, make test-integration and golangci-lint on the changed code pass.

  • Not run: Grafana against a Prometheus that stores exemplars.

The tests are larger than the code because each step is checked on its own: the helper, the worker's call and the scrape.

Checklist

  • Commits are signed off (git commit -s) per DCO
  • Code follows project contributing guidelines
  • Tests pass locally (make test)
  • Linters pass (golangci-lint on the changed packages)
  • Documentation updated (if applicable)

Related Issues

Part of llm-d/llm-d-router#3584

Release note

`llm_d_async_async_inference_latency_time_millis` carries the request's trace as an exemplar (`trace_id`, plus `span_id` of the `process-request` span) when the trace is sampled, and the metrics endpoint serves OpenMetrics to scrapers that ask for it, so a point on a latency panel links to its trace. Scrapers that don't ask get the classic format. The chart's dashboard gets an inference latency panel that shows the exemplars. On Prometheus 2.x, OpenMetrics scrapes store whole-number bucket bounds as `le="10.0"` rather than `le="10"`, so a query that matches `le="0"` on `llm_d_async_async_deadline_proximity_millis` needs `le="0.0"`.

llm_d_async_async_inference_latency_time_millis now carries the
request's trace as an exemplar when the trace is sampled: trace_id,
plus span_id of the process-request span.

Exemplars only reach a scrape over OpenMetrics, and controller-runtime
v0.22 builds the /metrics handler without it and has no option to
change that. The metrics server's FilterProvider now serves /metrics
from a promhttp handler with EnableOpenMetrics, with the auth filter
still in front when --metrics-endpoint-auth is on. Scrapers that don't
ask for OpenMetrics get the classic format.

Part of llm-d/llm-d-router#3584.

Signed-off-by: Sohom Chakraborty <16609933+sohom-cs@users.noreply.github.com>
Signed-off-by: Sohom Chakraborty <16609933+sohom-cs@users.noreply.github.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T07:38:02.011561Z cbc20f9 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

shimib pushed a commit that referenced this pull request Nov 19, 2026
)

* deps(go): bump controller-runtime to v0.25.3 and k8s.io to v0.37.0

controller-runtime v0.25.2 added metricsserver.Options.HandlerOpts, which
lets the metrics server enable OpenMetrics (needed for trace exemplars,
see #541) without replacing the /metrics handler. v0.25 requires the
k8s.io v0.37 libraries, which dependabot does not propose because it
skips Kubernetes minor versions.

No code changes were needed. ENVTEST_K8S_VERSION follows k8s.io/api, so
envtest now runs against Kubernetes 1.37.

Signed-off-by: Jacob Murry <jacobmurry@google.com>

* deps(go): take k8s.io v0.37.2

Two patch releases are out, and llm-d-router is on v0.37.2, so this keeps
one k8s.io line across the two repos.

Signed-off-by: Jacob Murry <jacobmurry@google.com>

---------

Signed-off-by: Jacob Murry <jacobmurry@google.com>
@jtechapps

Copy link
Copy Markdown
Collaborator

@sohom-cs On your offer to switch to HandlerOpts: yes, please. #567 has merged, so main is on controller-runtime v0.25.3 and k8s.io v0.37.2. Could you rebase onto main and enable OpenMetrics through metricsserver.Options.HandlerOpts instead of the FilterProvider wrapper? That's the same thing the EPP does in llm-d-router's newMetricsServerOptions:

opts := metricsserver.Options{
	BindAddress: fmt.Sprintf(":%d", serverCfg.MetricsPort),
	HandlerOpts: []func(*promhttp.HandlerOpts){
		func(o *promhttp.HandlerOpts) { o.EnableOpenMetrics = true },
	},
}
if serverCfg.MetricsEndpointAuth {
	opts.FilterProvider = filters.WithAuthenticationAndAuthorization
}

That should let pkg/server/openmetrics.go go away, along with TestOpenMetricsFilterLeavesOtherPathsAlone. TestMetricsServerServesTraceExemplars is still worth keeping against the new options. TestOpenMetricsFilterKeepsAuth can stay as a check that auth still applies, or be dropped since auth goes back to how it worked before. The docs and release note don't need to change, since what scrapers see is the same.

Comment thread docs/observability.md Outdated
Comment on lines +139 to +141
`_total`. On Prometheus 2.x, an OpenMetrics scrape stores whole-number bucket
bounds as `le="10.0"` rather than `le="10"` (Prometheus 3 stores `10.0` either
way), which only matters to queries that match `le` exactly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This caveat is right, but the repo already documents one exact le match that it breaks: the deadline proximity histogram says le="0" holds items past their deadline. After this change, Prometheus 2.x stores that bucket as le="0.0", so an alert on le="0" silently returns nothing. Please name it here, and ideally fix the deadline proximity row in the metrics table too.

Suggest:

which only matters to queries that match `le` exactly, such as
`le="0"` on `llm_d_async_async_deadline_proximity_millis`, which becomes `le="0.0"`.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 1915573: the caveat now names le="0" on llm_d_async_async_deadline_proximity_millis, which becomes le="0.0", in your wording. The deadline proximity row says le="0.0" too, or le="0" when Prometheus 2.x scrapes the classic text format, and the release note warns about that query.

Comment thread docs/observability.md Outdated
Comment on lines +132 to +134
[OpenTelemetry Tracing](#opentelemetry-tracing)). Grafana turns it into a link
from a point on a latency panel to that trace when the Prometheus data source has
an exemplar link to the traces backend.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The dashboard this chart ships (Async Processor, under the chart's dashboards) has no inference latency panel, and none of its queries turn on exemplars. So someone who installs the chart has no "point on a latency panel" to click, which is the motivation the PR gives. Could you add an inference latency quantile panel to that dashboard with exemplars enabled on its query? If you'd rather keep it out of scope, please say here that the bundled dashboard doesn't show it yet.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added in 1915573: the chart's dashboard has an Inference Latency (p50 / p95 / p99) panel with exemplars on the p99 query only, since the three quantiles read the same buckets and enabling all three would draw every exemplar three times. The llm-d dashboard already has that panel without exemplars; the one-line PR that turns them on for its p99 query is open on llm-d/llm-d and links back here.

@jtechapps

Copy link
Copy Markdown
Collaborator

@sohom-cs when will you be able to address the latest feedback on this PR?

Brings in controller-runtime v0.25.3, whose metrics server options take
HandlerOpts, for the review change that follows. Two conflicts:
- pkg/metrics/metrics.go imports: this branch added context, main added
  slices; both are kept.
- pkg/server/runner.go: main gave initInferenceClient a WorkerConfig
  parameter next to this branch's metricsServerOptions; both are kept.

Signed-off-by: Sohom Chakraborty <16609933+sohom-cs@users.noreply.github.com>
Review on llm-d#541: with controller-runtime v0.25.3 on main, the metrics
server options turn on EnableOpenMetrics for the /metrics handler that
controller-runtime builds, as the EPP's newMetricsServerOptions does.
That replaces the FilterProvider wrapper in openmetrics.go and its test
for other paths. With --metrics-endpoint-auth, FilterProvider is
filters.WithAuthenticationAndAuthorization again; the auth test checks
that wiring and is renamed TestMetricsServerKeepsAuth.

Also from the review:
- The chart's dashboard gets an inference latency panel with exemplars
  on its p99 query. The three quantiles read the same buckets, so only
  one of them shows exemplars.
- docs/observability.md names le="0" on the deadline proximity
  histogram as a query that OpenMetrics scrapes on Prometheus 2.x turn
  into le="0.0", and the histogram's row in the metrics table and the
  release note say so.

Signed-off-by: Sohom Chakraborty <16609933+sohom-cs@users.noreply.github.com>
@sohom-cs

Copy link
Copy Markdown
Contributor Author

Done in 1915573, on top of a merge of main: metricsServerOptions now enables OpenMetrics through HandlerOpts as the EPP does, and openmetrics.go and TestOpenMetricsFilterLeavesOtherPathsAlone are gone. I kept the auth test as TestMetricsServerKeepsAuth, since metricsServerOptions sets FilterProvider and nothing else checks that a scrape without a token gets a 401. The dashboard panel and the le caveat are answered inline.

@gyliu513

Copy link
Copy Markdown
Member

lgtm, thanks @sohom-cs !

@jtechapps
jtechapps merged commit 9c4bf9d into llm-d:main Nov 28, 2026
9 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