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): Introduce Sentry.startActiveSpan and Sentry.startSpan #8803

Merged
merged 2 commits into from
Aug 15, 2023

Conversation

AbhiPrasad
Copy link
Member

@AbhiPrasad AbhiPrasad commented Aug 11, 2023

ref #8724

This PR introduces two new methods to the JavaScript SDK's public API. Sentry.startActiveSpan and Sentry.startSpan, detailed in the RFC about the new performance API.

Sentry.startActiveSpan automatically wraps a callback with a span and makes that span the active span for the execution context of the callback. The callback can be async or sync, and can return arbitrary values.

Sentry.startSpan just creates a span, but does not wrap it in a callback. It needs to be explicitly set on the scope just like Sentry.startTransaction.

const result = Sentry.startActiveSpan({ name: importantThing }, async () => {
  await someWork();
  return Sentry.startActiveSpan({ op: 'db', name: 'query-stuff' }, () => expensiveQuery());
});
// result === expensiveQuery() 

Under the hood these methods will create a transaction or span depending on if there is already an active span on the scope.

@AbhiPrasad AbhiPrasad requested review from a team, kamilogorek and stephanie-anderson and removed request for a team August 11, 2023 18:51
@AbhiPrasad AbhiPrasad self-assigned this Aug 11, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Aug 11, 2023

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 75.12 KB (+0.36% 🔺)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.14 KB (+0.09% 🔺)
@sentry/browser - Webpack (gzipped) 21.82 KB (+0.03% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 69.73 KB (+0.27% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 28.18 KB (+0.1% 🔺)
@sentry/browser - ES6 CDN Bundle (gzipped) 20.18 KB (+0.05% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 219.89 KB (+0.38% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 84.78 KB (+0.13% 🔺)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 59.86 KB (+0.04% 🔺)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 31.04 KB (+0.09% 🔺)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 65.3 KB (+0.39% 🔺)
@sentry/react - Webpack (gzipped) 21.85 KB (+0.03% 🔺)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 92.94 KB (+0.31% 🔺)
@sentry/nextjs Client - Webpack (gzipped) 50.66 KB (+0.11% 🔺)


const parentSpan = scope.getSpan();

function getActiveSpan(): Span | undefined {
Copy link
Member

Choose a reason for hiding this comment

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

l: The naming is a bit confusing IMHO, as it appears as if this would just get the currently active span, but actually we create a new active span? What about e.g. createNewSpan or createChildSpan or something like this?

Copy link
Member Author

Choose a reason for hiding this comment

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

great point - renamed

@@ -14,7 +14,7 @@ const enum Type {
let hub: Hub;
let client: TestClient;

describe('trace', () => {
describe('startActiveSpan', () => {
Copy link
Member

Choose a reason for hiding this comment

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

l: Should we keep the trace tests around? 🤔 not sure, maybe it's fine to remove them as well 🤷

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 replaced the trace tests with startActiveSpan. I'll be removing trace afterwards considering it was an internal API.

@AbhiPrasad AbhiPrasad enabled auto-merge (squash) August 15, 2023 15:20
@AbhiPrasad AbhiPrasad merged commit 5c085ef into develop Aug 15, 2023
78 checks passed
@AbhiPrasad AbhiPrasad deleted the abhi-start-active-span branch August 15, 2023 15:47
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.

None yet

2 participants