Skip to content

feat(js): Document new performance APIs #7707

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 5 commits into from
Aug 31, 2023
Merged

Conversation

AbhiPrasad
Copy link
Member

resolves getsentry/sentry-javascript#8724

Motivation

Documents the new performance APIs under the custom instrumentation section.

In the future we'll want to revamp both the automatic and custom instrumentation sections, but for now this is a good starting point.

The APIs are detailed in the RFC about the new performance API, and were introduced in getsentry/sentry-javascript#8803

Details

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, Lms24 and ale-cota and removed request for a team August 29, 2023 16:19
@vercel
Copy link

vercel bot commented Aug 29, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sentry-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 31, 2023 7:37pm

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.

LGTM generally, just had some minor comments.

Side note/no action required as too late: I'm only now realizing that startActiveSpan to me conveys that we only start a span but don't finish it after running the callback. Super sorry for not bringing this up earlier! But it's also not the end of the world.

});

const result = await Sentry.startActiveSpan(
{ name: "Important Function" },
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 shop op as well here or do we consider this not necessary for custom instrumentation?

Copy link
Member Author

Choose a reason for hiding this comment

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

op is optional so I didn't include it. We need to go back and expand documentation on it, but that can happen later on.


<PlatformContent includePath="performance/add-independent-span" />

## Start Transaction
Copy link
Member

Choose a reason for hiding this comment

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

WDYT about

Suggested change
## Start Transaction
## [Deprecated] Start Transaction

or something similar that discourages folks from usingstartTransaction?

We probably need to make this JS-specific though so maybe throwing in a note/alert might also work

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 don't necessarily want to deprecate this method for now, but once we're more comfortable with the API we can come back and adjust.

@@ -0,0 +1,5 @@
<Note>

The span APIs require SDK version `7.65.0` or higher. If you are using an older version of the SDK, you can use the [explicit transaction APIs](#start-transaction) for custom instrumentation.
Copy link
Member

Choose a reason for hiding this comment

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

l: Is there a reason why this note is different to the Node version? I'd prefer the Node version as it explicitly names the APIs.

Copy link
Member Author

Choose a reason for hiding this comment

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

nope, I just forgot lol

good call!

@AbhiPrasad
Copy link
Member Author

I'm only now realizing that startActiveSpan to me conveys that we only start a span but don't finish it after running the callback

Yeah I thought about this too, but the UX of just not having to think about span finish meant that it kinda just got included with the name. We can alias the name to something else in a future version if users get confused, we have room to move here.

{ name: "Important Function" },
(span) => {
// Can access the span to add data or set specific status
span?.setData("foo", "bar");
Copy link
Member

Choose a reason for hiding this comment

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

m: Should we mention/explain when/why span is undefined? May not be entirely clear from the outside why this may be unset and why I need to guard here.

Copy link
Member Author

Choose a reason for hiding this comment

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

yup, ill add a note

@AbhiPrasad AbhiPrasad enabled auto-merge (squash) August 31, 2023 19:29
@AbhiPrasad AbhiPrasad merged commit dba65dc into master Aug 31, 2023
@AbhiPrasad AbhiPrasad deleted the abhi-js-new-span-apis branch August 31, 2023 19:41
@github-actions github-actions bot locked and limited conversation to collaborators Sep 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JS SDK Simplified Performance API
3 participants