Skip to content

add request ID injection to context to enable tracking requests across services #6895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

erlan-z
Copy link
Contributor

@erlan-z erlan-z commented Jul 20, 2025

What this PR does:
This PR adds support for injecting a request ID into the request context to enable tracking requests across services.

  • If the configured request_id header is present and non-empty, its value is used as the request ID.
  • Otherwise, a new UUID is generated.
  • The request ID is stored in the context, accessible via requestutil.RequestIdFromContext(r.Context()).

This ID is propagated through the service call chain and enables future tracking of downstream operations tied to a single originating request.

Note: While there is no active usage of the request ID yet, the immediate goal is to use it for tracing storage-related resource usage, especially for identifying heavy or costly queries originating from a single logical request.

Which issue(s) this PR fixes:
Fixes #

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

Logs from testsing split queries got same request id

  • query-frontend logs:
ts=2025-07-21T22:30:37.95781Z caller=handler.go:314 level=error org_id=fake msg="query request received in QFE handler" requestId=request-id-sharded
  • querier logs:
ts=2025-07-21T22:30:37.958862Z caller=scheduler_processor.go:170 level=error org_id=fake msg="query request received in Querier" requestId=request-id-sharded
ts=2025-07-21T22:30:37.958876Z caller=scheduler_processor.go:170 level=error org_id=fake msg="query request received in Querier" requestId=request-id-sharded
ts=2025-07-21T22:30:37.958901Z caller=scheduler_processor.go:170 level=error org_id=fake msg="query request received in Querier" requestId=request-id-sharded
  • ingester logs:
ts=2025-07-21T22:30:37.967425Z caller=ingester.go:2208 level=error msg="query request received in Ingester" requestId=request-id-sharded
ts=2025-07-21T22:30:37.967434Z caller=ingester.go:2208 level=error msg="query request received in Ingester" requestId=request-id-sharded
ts=2025-07-21T22:30:37.96775Z caller=ingester.go:2208 level=error msg="query request received in Ingester" requestId=request-id-sharded
  • ruler logs:
ts=2025-07-22T00:51:39.887737Z caller=compat.go:191 level=error msg="query request id from ruler" requestId=d0437ed4-a924-48c6-bbee-eb9152bbbf5b
  • ingester logs:
ts=2025-07-22T00:51:39.889274Z caller=ingester.go:2208 level=error msg="query request received in Ingester" requestId=d0437ed4-a924-48c6-bbee-eb9152bbbf5b

@dosubot dosubot bot added the type/feature label Jul 20, 2025
@erlan-z erlan-z force-pushed the add-request-id branch 2 times, most recently from 583fa70 to d57c37c Compare July 21, 2025 06:21
…s downstream services

Signed-off-by: Erlan Zholdubai uulu <[email protected]>
Copy link
Contributor

@justinjung04 justinjung04 left a comment

Choose a reason for hiding this comment

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

Nice!

require.Equal(t, providedID, requestID, "Request ID from header should be used")
}

func TestExistingRequestIdIsPreserved(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the goal here? Do you imagine a downstream component changing the header?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No specific case—just a precaution to avoid changing the context if the value already exists. This shouldn't normally happen, but in case it does, it's safer not to override it.

@friedrichg friedrichg changed the title add request ID injection to context to enable tracking reqeusts acros… add request ID injection to context to enable tracking requests across services Jul 21, 2025
Copy link
Contributor

@justinjung04 justinjung04 left a comment

Choose a reason for hiding this comment

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

Thx for the logs from testing!

Copy link
Contributor

@danielblando danielblando left a comment

Choose a reason for hiding this comment

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

The code lgtm, but it feels we are rewriting code from the log headers. The logs header is specific for logging, but one of the key parts was transferring ctx between services which seems to be duplicating here. I think we can reuse at least that logic

What if, we have a new config for the requestId header, but use the same middleware but adding the new header as targetHeader?
we can also refactor that util_log context function to a generic util.
would that also work? seems we would avoid some of this new changes

@@ -56,6 +56,7 @@
* [ENHANCEMENT] Distributor: Add native histograms max sample size bytes limit validation. #6834
* [ENHANCEMENT] Querier: Support caching parquet labels file in parquet queryable. #6835
* [ENHANCEMENT] Querier: Support query limits in parquet queryable. #6870
* [ENHANCEMENT] API: add request ID injection to context to enable tracking reqeusts across downstream services. #6895
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: s/reqeusts/requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants