-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Implement strictTraceContinuation
#16313
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 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
33385ff
feat(core): Implement `strictTraceContinuation`
s1gr1d e2dd6a6
create utility function for getting org id
s1gr1d cd60e47
add `shouldContinueTrace` util function
s1gr1d 484aedf
shuffle around util functions
s1gr1d 7a60aa3
Merge branch 'develop' into sig/strictTraceContinuation
s1gr1d 452c353
fix imports
s1gr1d 08569aa
delete wrong imports
s1gr1d d4e5cc4
change wrong import
s1gr1d 780720e
fix test
s1gr1d 20b67c6
raise node size limit
s1gr1d d8ec911
Merge branch 'develop' into sig/strictTraceContinuation
s1gr1d 8e43a3f
always expect client, change some comments
s1gr1d 78023e3
extract client
s1gr1d e70507e
client check
s1gr1d 3b51968
reword and rename stuff to clarify it
s1gr1d 19c5afc
rename function, delete optional chaining
s1gr1d 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 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 | ||||
---|---|---|---|---|---|---|
@@ -1,3 +1,4 @@ | ||||||
import type { Client } from '../client'; | ||||||
import { DEBUG_BUILD } from '../debug-build'; | ||||||
import type { DsnComponents, DsnLike, DsnProtocol } from '../types-hoist/dsn'; | ||||||
import { consoleSandbox, debug } from './debug-logger'; | ||||||
|
@@ -129,6 +130,27 @@ export function extractOrgIdFromDsnHost(host: string): string | undefined { | |||||
return match?.[1]; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Returns the organization ID of the client. | ||||||
* | ||||||
* The organization ID is extracted from the DSN. If the client options include a `orgId`, this will always take precedence. | ||||||
*/ | ||||||
export function deriveOrgIdFromClient(client: Client): string | undefined { | ||||||
const options = client.getOptions(); | ||||||
|
||||||
const { host } = client.getDsn() || {}; | ||||||
|
||||||
let org_id: string | undefined; | ||||||
|
||||||
if (options?.orgId) { | ||||||
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.
Suggested change
I think this always exists now? |
||||||
org_id = String(options.orgId); | ||||||
} else if (host) { | ||||||
org_id = extractOrgIdFromDsnHost(host); | ||||||
} | ||||||
|
||||||
return org_id; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Creates a valid Sentry Dsn object, identifying a Sentry instance and project. | ||||||
* @returns a valid DsnComponents object or `undefined` if @param from is an invalid DSN source | ||||||
|
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 | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,10 @@ | ||||||
import type { Client } from '../client'; | ||||||
import type { DynamicSamplingContext } from '../types-hoist/envelope'; | ||||||
import type { PropagationContext } from '../types-hoist/tracing'; | ||||||
import type { TraceparentData } from '../types-hoist/transaction'; | ||||||
import { debug } from '../utils/debug-logger'; | ||||||
import { baggageHeaderToDynamicSamplingContext } from './baggage'; | ||||||
import { deriveOrgIdFromClient } from './dsn'; | ||||||
import { parseSampleRate } from './parseSampleRate'; | ||||||
import { generateSpanId, generateTraceId } from './propagationContext'; | ||||||
|
||||||
|
@@ -124,3 +127,38 @@ function getSampleRandFromTraceparentAndDsc( | |||||
return Math.random(); | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Determines whether a new trace should be continued based on the provided baggage org ID and the client's `strictTraceContinuation` option. | ||||||
* If the trace should not be continued, a new trace will be started. | ||||||
* | ||||||
* The result is dependent on the `strictTraceContinuation` option in the client. | ||||||
* See https://develop.sentry.dev/sdk/telemetry/traces/#stricttracecontinuation | ||||||
*/ | ||||||
export function shouldContinueTrace(client: Client, baggageOrgId?: string): boolean { | ||||||
const clientOrgId = deriveOrgIdFromClient(client); | ||||||
|
||||||
// Case: baggage orgID and Client orgID don't match - always start new trace | ||||||
if (baggageOrgId && clientOrgId && baggageOrgId !== clientOrgId) { | ||||||
debug.log( | ||||||
`Won't continue trace because org IDs don't match (incoming baggage: ${baggageOrgId}, SDK options: ${clientOrgId})`, | ||||||
); | ||||||
return false; | ||||||
} | ||||||
|
||||||
const strictTraceContinuation = client.getOptions()?.strictTraceContinuation || false; // default for `strictTraceContinuation` is `false` | ||||||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
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.
Suggested change
|
||||||
|
||||||
if (strictTraceContinuation) { | ||||||
// With strict continuation enabled, don't continue trace if: | ||||||
// - Baggage has orgID, but Client doesn't have one | ||||||
// - Client has orgID, but baggage doesn't have one | ||||||
if ((baggageOrgId && !clientOrgId) || (!baggageOrgId && clientOrgId)) { | ||||||
debug.log( | ||||||
`Starting a new trace because strict trace continuation is enabled but one org ID is missing (incoming baggage: ${baggageOrgId}, Sentry client: ${clientOrgId})`, | ||||||
); | ||||||
return false; | ||||||
} | ||||||
} | ||||||
|
||||||
return true; | ||||||
} |
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.
l: why did we change this? IMHO
derive
is not easier to understand/grasp asextract
...? 😅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.
with change, I mean re-word :D I would just go with
extractOrgIdFromClient
or something like this?Uh oh!
There was an error while loading. Please reload this page.
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.
It's a different function :D
I extracted the logic of getting the org ID.
extractOrgIdFromDsnHost
is still available: https://github.com/getsentry/sentry-javascript/pull/16313/files#diff-b6e0f71bb87e888ddb45cd78dcf2cb7efdf9c80b6d596c0224648b1f78bdcb29R148The one function is doing
extractOrIdFromDsnHost
, which is really just looking at thehost
string and extracting it from there. AndderiveOrgIdFromClient
is looking at different cues (either the org ID or the host on the client) to get the org ID.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.
yeah, right, that makes sense of course - what I mean is that I think
derive
is a non-ideal term/prefix for the function name :D we do not use it anywhere in the code, rather we always use e.g.extractXX
orgetXX
or something like this, so I'd rather use something along these lines for consistency - it is a different method of course :)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.
ah got you, that makes sense of course 👍