-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Add getTraceData
function
#13134
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
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9296b88
feat(node): Add `getTracingMetaTags` function
Lms24 a400285
actually add files in node lol
Lms24 91472d4
add missing exports, cleanup
Lms24 63283d0
s/getTracingMetaTags/getTracingMetaTagValues
Lms24 63926bf
rework: export `getTraceData` from core
Lms24 324b747
adjust test
Lms24 3106bf6
Apply suggestions from code review
Lms24 2a3f134
improve invalid sentry-trace handling
Lms24 a44cb30
simplify
Lms24 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
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import type { Client, Scope, Span } from '@sentry/types'; | ||
import { | ||
TRACEPARENT_REGEXP, | ||
dynamicSamplingContextToSentryBaggageHeader, | ||
generateSentryTraceHeader, | ||
logger, | ||
} from '@sentry/utils'; | ||
import { getClient, getCurrentScope } from '../currentScopes'; | ||
import { getDynamicSamplingContextFromClient, getDynamicSamplingContextFromSpan } from '../tracing'; | ||
import { getActiveSpan, getRootSpan, spanToTraceHeader } from './spanUtils'; | ||
|
||
/** | ||
* Extracts trace propagation data from the current span or from the client's scope (via transaction or propagation | ||
* context) and serializes it to `sentry-trace` and `baggage` values to strings. These values can be used to propagate | ||
* a trace via our tracing Http headers or Html `<meta>` tags. | ||
* | ||
* This function also applies some validation to the generated sentry-trace and baggage values to ensure that | ||
* only valid strings are returned. | ||
* | ||
* @param span a span to take the trace data from. By default, the currently active span is used. | ||
* @param scope the scope to take trace data from By default, the active current scope is used. | ||
* @param client the SDK's client to take trace data from. By default, the current client is used. | ||
* | ||
* @returns an object with the tracing data values. The object keys are the name of the tracing key to be used as header | ||
* or meta tag name. | ||
*/ | ||
export function getTraceData( | ||
span?: Span, | ||
scope?: Scope, | ||
client?: Client, | ||
): { 'sentry-trace': string; baggage?: string } { | ||
const clientToUse = client || getClient(); | ||
const scopeToUse = scope || getCurrentScope(); | ||
const spanToUse = span || getActiveSpan(); | ||
|
||
const { dsc, sampled, traceId } = scopeToUse.getPropagationContext(); | ||
const rootSpan = spanToUse && getRootSpan(spanToUse); | ||
|
||
const sentryTrace = spanToUse ? spanToTraceHeader(spanToUse) : generateSentryTraceHeader(traceId, undefined, sampled); | ||
|
||
const dynamicSamplingContext = rootSpan | ||
? getDynamicSamplingContextFromSpan(rootSpan) | ||
: dsc | ||
? dsc | ||
: clientToUse | ||
? getDynamicSamplingContextFromClient(traceId, clientToUse) | ||
: undefined; | ||
|
||
const baggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext); | ||
|
||
const isValidSentryTraceHeader = TRACEPARENT_REGEXP.test(sentryTrace); | ||
if (!isValidSentryTraceHeader) { | ||
logger.warn('Invalid sentry-trace data. Returning empty "sentry-trace" value'); | ||
} | ||
|
||
const validBaggage = isValidBaggageString(baggage); | ||
if (!validBaggage) { | ||
logger.warn('Invalid baggage data. Not returning "baggage" value'); | ||
} | ||
|
||
return { | ||
'sentry-trace': isValidSentryTraceHeader ? sentryTrace : '', | ||
Lms24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
...(validBaggage && { baggage }), | ||
}; | ||
} | ||
|
||
/** | ||
* Tests string against baggage spec as defined in: | ||
* | ||
* - W3C Baggage grammar: https://www.w3.org/TR/baggage/#definition | ||
* - RFC7230 token definition: https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.6 | ||
* | ||
* exported for testing | ||
*/ | ||
export function isValidBaggageString(baggage?: string): boolean { | ||
if (!baggage || !baggage.length) { | ||
return false; | ||
} | ||
const keyRegex = "[-!#$%&'*+.^_`|~A-Za-z0-9]+"; | ||
const valueRegex = '[!#-+-./0-9:<=>?@A-Z\\[\\]a-z{-}]+'; | ||
const spaces = '\\s*'; | ||
// eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor -- RegExp for readability, no user input | ||
const baggageRegex = new RegExp( | ||
`^${keyRegex}${spaces}=${spaces}${valueRegex}(${spaces},${spaces}${keyRegex}${spaces}=${spaces}${valueRegex})*$`, | ||
); | ||
return baggageRegex.test(baggage); | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.