Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(core): Export a reusable function to add tracing headers#20076
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fb1e8a2
feat(core): Export a reusable function to add tracing headers
JPeer264 1622527
fixup! feat(core): Export a reusable function to add tracing headers
JPeer264 4e235e3
fixup! feat(core): Export a reusable function to add tracing headers
JPeer264 3d15dcf
feat: Loosen types and header check
JPeer264 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -19,8 +19,9 @@ import { | ||
| } from './utils/url'; | ||
| type PolymorphicRequestHeaders = | ||
| | Record<string, string | undefined> | ||
| | Array<[string, string]> | ||
| | Record<string, unknown> | ||
| | Array<[string, unknown]> | ||
| | Iterable<Iterable<unknown>> | ||
| // the below is not precisely the Header type used in Request, but it'll pass duck-typing | ||
| | { | ||
| append: (key: string, value: string) => void; | ||
| @@ -124,7 +125,7 @@ export function instrumentFetchRequest( | ||
| // Examples: users re-using same options object for multiple fetch calls, frozen objects | ||
| const options: { [key: string]: unknown } = { ...(handlerData.args[1] || {}) }; | ||
| const headers = _addTracingHeadersToFetchRequest( | ||
| const headers = _INTERNAL_getTracingHeadersForFetchRequest( | ||
| request, | ||
| options, | ||
| // If performance is disabled (TWP) or there's no active root span (pageload/navigation/interaction), | ||
| @@ -176,17 +177,21 @@ export function _callOnRequestSpanEnd( | ||
| } | ||
| /** | ||
| * Adds sentry-trace and baggage headers to the various forms of fetch headers. | ||
| * exported only for testing purposes | ||
| * Builds merged fetch headers that include `sentry-trace` and `baggage` (and optionally `traceparent`) | ||
| * for the given request and init, without mutating the original request or options. | ||
| * Returns `undefined` when there is no `sentry-trace` value to attach. | ||
| * | ||
| * When we determine if we should add a baggage header, there are 3 cases: | ||
| * 1. No previous baggage header -> add baggage | ||
| * 2. Previous baggage header has no sentry baggage values -> add our baggage | ||
| * 3. Previous baggage header has sentry baggage values -> do nothing (might have been added manually by users) | ||
| * @internal Exported for cross-package instrumentation (for example Cloudflare Workers fetcher bindings) | ||
| * and unit tests | ||
| * | ||
| * Baggage handling: | ||
| * 1. No previous baggage header → include Sentry baggage | ||
| * 2. Previous baggage has no Sentry entries → merge Sentry baggage in | ||
| * 3. Previous baggage already has Sentry entries → leave as-is (may be user-defined) | ||
| */ | ||
| // eslint-disable-next-line complexity -- yup it's this complicated :( | ||
| export function _addTracingHeadersToFetchRequest( | ||
| request: string | Request, | ||
| export function _INTERNAL_getTracingHeadersForFetchRequest( | ||
| request: string | URL | Request, | ||
JPeer264 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| fetchOptionsObj: { | ||
| headers?: | ||
| | { | ||
| @@ -234,19 +239,20 @@ export function _addTracingHeadersToFetchRequest( | ||
| } | ||
| return newHeaders; | ||
| } else if (Array.isArray(originalHeaders)) { | ||
| } else if (isHeadersInitTupleArray(originalHeaders)) { | ||
| const newHeaders = [...originalHeaders]; | ||
| if (!originalHeaders.find(header => header[0] === 'sentry-trace')) { | ||
| if (!newHeaders.find(header => header[0] === 'sentry-trace')) { | ||
| newHeaders.push(['sentry-trace', sentryTrace]); | ||
| } | ||
| if (propagateTraceparent && traceparent && !originalHeaders.find(header => header[0] === 'traceparent')) { | ||
| if (propagateTraceparent && traceparent && !newHeaders.find(header => header[0] === 'traceparent')) { | ||
| newHeaders.push(['traceparent', traceparent]); | ||
| } | ||
| const prevBaggageHeaderWithSentryValues = originalHeaders.find( | ||
| header => header[0] === 'baggage' && baggageHeaderHasSentryBaggageValues(header[1]), | ||
| header => | ||
| header[0] === 'baggage' && typeof header[1] === 'string' && baggageHeaderHasSentryBaggageValues(header[1]), | ||
| ); | ||
| if (baggage && !prevBaggageHeaderWithSentryValues) { | ||
| @@ -255,7 +261,7 @@ export function _addTracingHeadersToFetchRequest( | ||
| newHeaders.push(['baggage', baggage]); | ||
| } | ||
| return newHeaders as PolymorphicRequestHeaders; | ||
| return newHeaders; | ||
| } else { | ||
| const existingSentryTraceHeader = 'sentry-trace' in originalHeaders ? originalHeaders['sentry-trace'] : undefined; | ||
| const existingTraceparentHeader = 'traceparent' in originalHeaders ? originalHeaders.traceparent : undefined; | ||
| @@ -313,14 +319,29 @@ function endSpan(span: Span, handlerData: HandlerDataFetch): void { | ||
| span.end(); | ||
| } | ||
| function baggageHeaderHasSentryBaggageValues(baggageHeader: string): boolean { | ||
| function baggageHeaderHasSentryBaggageValues(baggageHeader: unknown): boolean { | ||
| if (typeof baggageHeader !== 'string') { | ||
| return false; | ||
| } | ||
| return baggageHeader.split(',').some(baggageEntry => baggageEntry.trim().startsWith(SENTRY_BAGGAGE_KEY_PREFIX)); | ||
| } | ||
| function isHeaders(headers: unknown): headers is Headers { | ||
| return typeof Headers !== 'undefined' && isInstanceOf(headers, Headers); | ||
| } | ||
| /** `HeadersInit` array form: each entry is a [name, value] pair of strings. */ | ||
| function isHeadersInitTupleArray(headers: unknown): headers is [string, unknown][] { | ||
| if (!Array.isArray(headers)) { | ||
| return false; | ||
| } | ||
| return headers.every( | ||
| (item): item is [string, unknown] => Array.isArray(item) && item.length === 2 && typeof item[0] === 'string', | ||
| ); | ||
| } | ||
| function getSpanStartOptions( | ||
| url: string, | ||
| method: string, | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The function
_INTERNAL_getTracingHeadersForFetchRequestdoesn't handleIterableheader types likeMap, causing user-provided headers to be silently discarded when passed.Severity: HIGH
Suggested Fix
Add a new branch to
_INTERNAL_getTracingHeadersForFetchRequestto handleIterable<Iterable<unknown>>types. This branch should convert the iterable into aHeadersobject or a plain object before merging it with the Sentry tracing headers. For example,new Headers(originalHeaders)can be used to correctly process the iterable.Prompt for AI Agent