-
-
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ import { | |
| type PolymorphicRequestHeaders = | ||
| | Record<string, string | undefined> | ||
| | Array<[string, string]> | ||
| | Iterable<Iterable<string>> | ||
| // 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 = getTracingHeadersForFetchRequest( | ||
| request, | ||
| options, | ||
| // If performance is disabled (TWP) or there's no active root span (pageload/navigation/interaction), | ||
|
|
@@ -176,17 +177,22 @@ 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 | ||
| * @hidden | ||
| * | ||
| * 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 getTracingHeadersForFetchRequest( | ||
| request: string | URL | Request, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. feat PR lacks integration or E2E testLow Severity This Additional Locations (1)Triggered by project rule: PR Review Guidelines for Cursor Bot Reviewed by Cursor Bugbot for commit 1622527. Configure here. |
||
| fetchOptionsObj: { | ||
| headers?: | ||
| | { | ||
|
|
@@ -234,7 +240,7 @@ 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')) { | ||
|
|
@@ -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; | ||
|
|
@@ -321,6 +327,18 @@ 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, string][] { | ||
| if (!Array.isArray(headers)) { | ||
| return false; | ||
| } | ||
|
|
||
| return headers.every( | ||
| (item): item is [string, string] => | ||
| Array.isArray(item) && item.length === 2 && typeof item[0] === 'string' && typeof item[1] === 'string', | ||
| ); | ||
| } | ||
|
|
||
| function getSpanStartOptions( | ||
| url: string, | ||
| method: string, | ||
|
|
||


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.
Accepted
Iterabletype lacks runtime handling in functionLow Severity
The new
Iterable<Iterable<string>>variant was added toPolymorphicRequestHeaders, which is used as the type forfetchOptionsObj.headers. However, the function body only handlesHeadersinstances (viaisHeaders), arrays (viaArray.isArray), and plain objects (the else branch). A non-array, non-Headersiterable (e.g. aMap) would silently fall into the else branch, whereinoperator checks and object spread would not correctly process the iterable's entries, producing malformed headers. Since this function is now exported for cross-package use, the type promises broader acceptance than the implementation supports.Additional Locations (1)
packages/core/src/fetch.ts#L242-L265There 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.
This type is needed for Cloudflare headers. I've added a test to also check for that case.