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

feat(core): Add continueTrace method #9164

Merged
merged 1 commit into from Oct 4, 2023
Merged

feat(core): Add continueTrace method #9164

merged 1 commit into from Oct 4, 2023

Conversation

mydea
Copy link
Member

@mydea mydea commented Oct 3, 2023

This adds a new continueTrace method which can be used to continue a trace from headers.

This method has the following signature:

function continueTrace<V>(
  {
    sentryTrace,
    baggage,
  }: {
    sentryTrace: Parameters<typeof tracingContextFromHeaders>[0];
    baggage: Parameters<typeof tracingContextFromHeaders>[1];
  },
  callback: (transactionContext: Partial<TransactionContext>) => V,
): V

In practice, this can be used like this:

const transaction = continueTrace({ sentryTrace, baggage }, transactionContext => {
  return startTransaction({
    ...transactionContext,
    name: 'my transaction',
    op: 'my op',
    metadata: {
      ...transactionContext.metadata,
      source: 'route',
    }
  });
});

Closes #9011

@mydea mydea requested review from Lms24 and AbhiPrasad October 3, 2023 10:05
@mydea mydea self-assigned this Oct 3, 2023
@mydea mydea requested a review from lforst October 3, 2023 10:05
@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2023

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 84.24 KB (0%)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.41 KB (0%)
@sentry/browser - Webpack (gzipped) 22 KB (0%)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 78.77 KB (+0.11% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 28.59 KB (+0.26% 🔺)
@sentry/browser - ES6 CDN Bundle (gzipped) 21 KB (+2.03% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 254.38 KB (+0.1% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 86.66 KB (+0.28% 🔺)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 62.35 KB (+1.84% 🔺)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 31.45 KB (+0.24% 🔺)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 84.27 KB (0%)
@sentry/react - Webpack (gzipped) 22.05 KB (0%)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 102.23 KB (-0.02% 🔽)
@sentry/nextjs Client - Webpack (gzipped) 50.99 KB (-0.03% 🔽)

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

This is really elegant, it means we can use it with startSpan also.

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

Do we also export this in the browser SDKs? I believe we should, for meta tags.
(If there's a reason not to, disregard my other comment).

Comment on lines 206 to 214
/**
* Continue a trace from a sentryTrace & baggage header.
* This method takes a sentryTrace, baggage, and an optional request,
* and will continue tracing from the context provided.
* It takes a callback that receives a transactionContext that may be used for `startTransaction` or `startSpan`.
*/
export function continueTrace<V>(
Copy link
Member

Choose a reason for hiding this comment

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

l: I'd add meta tags here for the backend->frontend use case.

Suggested change
/**
* Continue a trace from a sentryTrace & baggage header.
* This method takes a sentryTrace, baggage, and an optional request,
* and will continue tracing from the context provided.
* It takes a callback that receives a transactionContext that may be used for `startTransaction` or `startSpan`.
*/
export function continueTrace<V>(
/**
* Continue a trace from a `sentry-trace` and `baggage` values.
* These values can be obtained from incoming request headers or, in the browser, from `<meta name="sentry-trace">` and `<meta name="baggage">` HTML tags.
*
* This function takes a `sentry-trace`, `baggage`, and optionally, a `request` object.
* It will continue tracing from the context provided.
*
* It takes a callback that receives a transactionContext that may be used for `startTransaction` or `startSpan`.
*/
export function continueTrace<V>(

Copy link
Member Author

Choose a reason for hiding this comment

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

I updated the comment to reflect this 👍

@mydea
Copy link
Member Author

mydea commented Oct 3, 2023

Do we also export this in the browser SDKs? I believe we should, for meta tags.
(If there's a reason not to, disregard my other comment).

Oops, missed that - added it to browser exports as well!

@lforst
Copy link
Member

lforst commented Oct 4, 2023

I am a bit in favor of removing the request option. It seems a bit off encapsulation wise. While they are often related, continuing a trace should have nothing to do with a request.

@mydea
Copy link
Member Author

mydea commented Oct 4, 2023

I am a bit in favor of removing the request option. It seems a bit off encapsulation wise. While they are often related, continuing a trace should have nothing to do with a request.

I'll remove the request then. I have no strong feelings and was also not sure. Maybe we can also streamline around just always setting scope.setSDKProcessingMetadata({ request: req }); everywhere and stop also setting this directly on the event, as actually this is redundant and not really needed.

@mydea mydea merged commit dee4f69 into develop Oct 4, 2023
82 checks passed
@mydea mydea deleted the fn/continue-trace branch October 4, 2023 10:14
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.

Add continueTrace function
4 participants