Skip to content
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

fix: Create spanRecorder whenever transactions are sampled #3255

Merged
merged 3 commits into from Feb 12, 2021

Conversation

rhcarvalho
Copy link
Contributor

There were cases in which the sample function would return early without
correctly setting up a spanRecorder, causing child spans to be lost.

Fixes #3254.

There were cases in which the sample function would return early without
correctly setting up a spanRecorder, causing child spans to be lost.

// nothing to do if there's no client or if tracing is disabled
if (!client || !hasTracingEnabled(options)) {
function sample<T extends Transaction>(transaction: T, options: Options, samplingContext: SamplingContext): T {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should probably be a method on Transaction, but didn't want to make that change now to keep this patch small.

I did however change the argument order to have transaction as the first argument, since this function operates (mutates!) the transaction, and removed the dependency on the hub as we only need to know about the client options.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that the options should go last though, but it's just a nitpick :P

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah agree, I'll leave that for later so we don't have to go change more lines on the call sites.

@@ -114,10 +117,6 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, samplingContext
return transaction;
}

// at this point we know we're keeping the transaction, whether because of an inherited decision or because it got
// lucky with the dice roll
transaction.initSpanRecorder(options._experiments?.maxSpans as number);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The culprit of #3254 was that in https://github.com/getsentry/sentry-javascript/pull/3255/files#diff-f5bf6e0cdf7709e5675fcdc3b4ff254dd68f3c9d1a399c8751e0fa1846fa85dbR54 we returned earlier and skipped initializing a span recorder.
Worked fine for forcing sampled: false, but clearly not the intended behavior for sampled: true.

Comment on lines 186 to 204
const transaction = new IdleTransaction(transactionContext, hub, idleTimeout, onScope);
return sample(hub, transaction, {
const client = hub.getClient();
const options = (client && client.getOptions()) || {};

let transaction = new IdleTransaction(transactionContext, hub, idleTimeout, onScope);
transaction = sample(transaction, options, {
parentSampled: transactionContext.parentSampled,
transactionContext,
...customSamplingContext,
});
if (transaction.sampled) {
transaction.initSpanRecorder(options._experiments?.maxSpans as number);
}
return transaction;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not cool that we have to repeat ourselves in _startTransaction and startIdleTransaction, but letting it be for now as we may soon move away from idle transactions anyway.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 12, 2021

size-limit report

Path Size
@sentry/browser - CDN Bundle (gzipped) 20.27 KB (+0.01% 🔺)
@sentry/browser - Webpack 21.18 KB (0%)
@sentry/react - Webpack 21.2 KB (0%)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 27.36 KB (+0.22% 🔺)

packages/tracing/src/hubextensions.ts Outdated Show resolved Hide resolved
packages/tracing/src/hubextensions.ts Outdated Show resolved Hide resolved

// nothing to do if there's no client or if tracing is disabled
if (!client || !hasTracingEnabled(options)) {
function sample<T extends Transaction>(transaction: T, options: Options, samplingContext: SamplingContext): T {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that the options should go last though, but it's just a nitpick :P

rhcarvalho and others added 2 commits February 12, 2021 13:51
Co-authored-by: Kamil Ogórek <kamil.ogorek@gmail.com>
@rhcarvalho rhcarvalho merged commit d5c8aa0 into master Feb 12, 2021
@rhcarvalho rhcarvalho deleted the fix/3254-missing-spans branch February 12, 2021 20:34
This was referenced Mar 7, 2021
This was referenced Mar 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Manually sampling transactions result in missing child spans
2 participants