-
-
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
+84
−48
Merged
Changes from 1 commit
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
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
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 | ||
|---|---|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |||
| 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 @@ | |||
| // 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 @@ | |||
| } | ||||
|
|
||||
| /** | ||||
| * 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 | ||||
|
||||
| * @hidden |
very logaf-L
JPeer264 marked this conversation as resolved.
Show resolved
Hide resolved
JPeer264 marked this conversation as resolved.
Show resolved
Hide resolved
Check failure on line 255 in packages/core/src/fetch.ts
GitHub Actions / Lint
typescript-eslint(no-unsafe-member-access)
Unsafe member access [1] on an `any` value.
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.
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.
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.