Skip to content

Adding new optional post_injection_hook to RequestsInstrumentor #3657

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 8 commits into
base: main
Choose a base branch
from

Conversation

eddawley
Copy link

@eddawley eddawley commented Jul 29, 2025

Description

The tracestate and baggage headers may contain sensitive information that is valuable to propagate to trusted systems but could pose concerns when sent to untrusted systems. Currently there is no way to support instrumenting these spans while ensuring these, or other, headers are conditionally propagated. This new option allows callers to define a callable to mutate the post-injection headers based on each request.

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

tox -e py312-test-instrumentation-requests

Does This PR Require a Core Repo Change?

  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

The `tracestate` and `baggage` headers may contain sensitive information that is valuable to propagate to trusted systems but could pose concerns when sent to untrusted systems. Currently there is no way to support instrumenting these spans while ensuring these, or other, headers are conditionally propagated. This new option allows callers to define a callable to mutate the post-injection headers based on each request.
@eddawley eddawley requested a review from a team as a code owner July 29, 2025 14:23
Copy link

linux-foundation-easycla bot commented Jul 29, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@@ -300,6 +329,9 @@ def get_or_create_headers():
headers = get_or_create_headers()
inject(headers)

if callable(post_injection_hook):
post_injection_hook(headers, request)
Copy link
Member

Choose a reason for hiding this comment

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

Would it work for your use case to -- instead of passing in a general-purpose post-injection hook -- pass in an allowlist and inject or not based on that? Seems like the allowlist use case would be common enough to justify baking in explicit support (even at the api/spec level).

Copy link
Author

@eddawley eddawley Jul 30, 2025

Choose a reason for hiding this comment

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

I considered that but I think users will differ in what and when to propagate these headers. For example, this user wants to avoid all headers for one host while only baggage for external domains.

I suspect it will be difficult to have a truly one-size fits all. I could even see a case where you base the decision on values in the baggage itself.

edit: also i noticed the request/response hooks started as separate single purpose options that were changed to hooks and I didn't want to repeat that

@robotadam
Copy link

If it helps, there have been a few discussions about this in opentelemetry-specification. I had originally requested something like this in open-telemetry/opentelemetry-specification#3799, but the consensus was to focus on baggage, which is in open-telemetry/opentelemetry-specification#3957. But since then there seemed to be some consensus around doing this in instrumentations, like this PR does: open-telemetry/opentelemetry-specification#1633 (comment)

FWIW, this style of hook (or the allowlist pmcollins suggested) would solve my original issue, so I hope some version of this moves forward.

@eddawley
Copy link
Author

If it helps, there have been a few discussions about this in opentelemetry-specification. I had originally requested something like this in open-telemetry/opentelemetry-specification#3799, but the consensus was to focus on baggage, which is in open-telemetry/opentelemetry-specification#3957. But since then there seemed to be some consensus around doing this in instrumentations, like this PR does: open-telemetry/opentelemetry-specification#1633 (comment)

FWIW, this style of hook (or the allowlist pmcollins suggested) would solve my original issue, so I hope some version of this moves forward.

Thank you so much for these links. I wondered how I could be the first person to consider the security implications of auto instrumentation sending baggage to 3rd parties. I could definitely see this being a broader need of the contrib package. This PR was really just a simple solution to get value as quickly as possible.

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