Skip to content

feat(core): Add inheritOrSampleWith helper to traceSampler #15277

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 4 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ const Sentry = require('@sentry/node');
Sentry.init({
dsn: 'https://[email protected]/1337',
transport: loggingTransport,
tracesSampler: ({ parentSampleRate }) => {
if (parentSampleRate) {
return parentSampleRate;
}

return 0.69;
tracesSampler: ({ inheritOrSampleWith }) => {
return inheritOrSampleWith(0.69);
},
});

Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/tracing/sampling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,21 @@ export function sampleSpan(
// work; prefer the hook if so
let sampleRate;
if (typeof options.tracesSampler === 'function') {
sampleRate = options.tracesSampler(samplingContext);
sampleRate = options.tracesSampler({
...samplingContext,
inheritOrSampleWith(fallbackSampleRate) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
inheritOrSampleWith(fallbackSampleRate) {
inheritOrSampleWith: (fallbackSampleRate) => {

is this maybe slightly cleaner regarding this context? 🤔

if (typeof samplingContext.parentSampleRate === 'number') {
return samplingContext.parentSampleRate;
}

// Fallback if parent sample rate is not on the incoming trace (e.g. if there is no baggage)
if (typeof samplingContext.parentSampled === 'boolean') {
return 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return 1;
return Number(samplingContext.parentSampled);

!! otherwise this would propagate 1 for false :D

}

return fallbackSampleRate;
},
});
localSampleRateWasApplied = true;
} else if (samplingContext.parentSampled !== undefined) {
sampleRate = samplingContext.parentSampled;
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/types-hoist/samplingcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ export interface SamplingContext extends CustomSamplingContext {

/** Initial attributes that have been passed to the span being sampled. */
attributes?: SpanAttributes;

/**
* Returns a sample rate value that matches the sampling decision from the incoming trace, or falls back to the provided `fallbackSampleRate`.
*/
inheritOrSampleWith: (fallbackSampleRate: number) => number;
}
Loading