-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
ref(js): Rewrite outdated "Transaction Name" documentation #12091
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5e1b85f
ref(js): Rewrite outdated "Transaction Name" documentation
Lms24 baa9940
Update docs/platforms/javascript/common/enriching-events/transaction-…
Lms24 69acbaf
Apply suggestions from code review
Lms24 4818caf
Update docs/platforms/javascript/common/enriching-events/transaction-…
Lms24 34512e2
further suggestions
Lms24 4a56229
further suggestion
Lms24 a55d1f8
Update docs/platforms/javascript/common/enriching-events/transaction-…
Lms24 fc3fe31
Update docs/platforms/javascript/common/enriching-events/transaction-…
Lms24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
122 changes: 107 additions & 15 deletions
122
docs/platforms/javascript/common/enriching-events/transaction-name/index.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,128 @@ | ||
--- | ||
title: Transaction Name | ||
description: "Learn how to set or override the transaction name to capture the user and gain critical pieces of information that construct a unique identity in Sentry." | ||
description: "Learn how to set or override transaction names to improve issue and trace-grouping." | ||
supported: | ||
- javascript | ||
notSupported: | ||
- javascript.cordova | ||
- javascript.nextjs | ||
--- | ||
|
||
The current transaction name is used to group transactions in our | ||
[Performance](/product/performance/) product, as well as annotate error events | ||
with their point of failure. | ||
The Sentry SDK uses an _error transaction name_ to mark where something went wrong in an error event. If you use <PlatformLink to="/tracing/">Tracing</PlatformLink>, Sentry uses the name of your root span as the _root span transaction name_, which will group traces around that name. | ||
|
||
The transaction name can reference the current web app route, or the current | ||
task being executed. For example: | ||
This means that there are in fact two transaction names: | ||
|
||
1. The _error transaction name_ which is applied to error events | ||
2. The _root span transaction name_ which is the name of the root span in your applications span tree. | ||
|
||
Both are used to annotate and group error and trace events respectively and are usually set by the SDK or an integration automatically. | ||
|
||
<PlatformCategorySection supported={['browser']}> | ||
|
||
To automatically set transaction names, add `browserTracingIntegration` when initializing the SDK. | ||
|
||
</PlatformCategorySection> | ||
|
||
Because automatically-determined names are a good grouping mechanism, unless you have a special use-case, **you won't need to set or override transaction names manually.** | ||
|
||
## What's a "Good" Transaction Name? | ||
|
||
Your transaction name can reference the current web app or server route, mobile screen or activity, or the current task being executed. | ||
For example: | ||
|
||
- `GET /api/{version}/users/` | ||
- `UserListView` | ||
- `myapp.tasks.renew_all_subscriptions` | ||
|
||
Ideally, the transaction name does not contain variable values such as user | ||
IDs but has rather low cardinality while still uniquely identifying a piece of | ||
code you care about. | ||
A good transaction name shouldn't have too many variables (such as user IDs), but should still be unique enough to help you easily identify the piece of code you care about. | ||
For example, instead of naming a transaction`'GET /api/users/123/details'`, name it `'GET /api/users/:id/details'`. | ||
This will group all errors and traces for requests to the same route. | ||
|
||
### When to Set the Transaction Name | ||
|
||
It's usually best to let the SDK set the transaction name automatically. However, if it can't or the name doesn't suit your needs, you can set it manually. | ||
|
||
We recommend setting it _as early as possible_ in your application code, ideally when the operation you want to track starts. This ensures that errors thrown early in the application lifecycle are annotated correctly and keeps traces consistent if you're using [distributed tracing](/concepts/key-terms/tracing/distributed-tracing/). | ||
|
||
For example, in a server app, you can set the transaction name in middleware before handling a request. | ||
In a web app, you can set it when navigating to a new route, like in an SPA router. | ||
|
||
## Setting the Error Transaction Name | ||
|
||
There are several ways to set the transaction name for error events sent to Sentry. This name will show up as the location of the issue next to the issue title. | ||
|
||
### Setting the Name on the Scope | ||
|
||
Set the transaction name on the current scope when the operation you want to group starts: | ||
|
||
```javascript | ||
Sentry.getCurrentScope().setTransactionName("UserListView"); | ||
``` | ||
|
||
The transaction name on the scope only applies to error events, not the active root span. Similarly, if an error happens during an active span, the span's name won’t be applied to the error's transaction name. | ||
|
||
### Setting the Name on the Event | ||
|
||
You can use `beforeSend` to set the transaction name on the event: | ||
|
||
```javascript {3} | ||
Sentry.init({ | ||
beforeSend(event) { | ||
event.transaction = "UserListView"; | ||
return event; | ||
}, | ||
}); | ||
``` | ||
|
||
## Setting the Root Span Name | ||
|
||
There are multiple options for overriding the root span name (this name shows up in the Sentry UI, for example when searching for traces or when in performance insights): | ||
|
||
<PlatformCategorySection supported={['browser']}> | ||
|
||
### At Root Span Start | ||
|
||
Sentry's `browserTracingIntegration` automatically sets the transaction name based on the current route or URL for all `pageload` and `navigation` transactions. | ||
To override the transaction name, use the <PlatformLink to="/tracing/instrumentation/automatic-instrumentation/#beforestartspan">`beforeStartSpan` callback</PlatformLink> of the integration. | ||
|
||
</PlatformCategorySection> | ||
|
||
### While the Root Span Is Active | ||
|
||
_Available starting with: v8.47.0_ | ||
|
||
Sometimes, (for example if you have a router that only emits the route change after it occurred) you might not be able to set the final root span name at span start. In this case, you can update the root span name while the span is active: | ||
|
||
```javascript | ||
const activeSpan = Sentry.getActiveSpan(); | ||
const rootSpan = activeSpan && Sentry.getRootSpan(activeSpan); | ||
|
||
Sentry.updateSpanName(rootSpan, "UserListView"); | ||
``` | ||
|
||
Learn more about <PlatformLink to="/tracing/instrumentation/custom-instrumentation/#updating-the-span-name">updating the span name</PlatformLink>. | ||
|
||
### After the Root Span Has Finished | ||
|
||
If you can't determine the root span name before the span finishes, you'll be able to change it retroactively in `beforeSendTransaction`: | ||
|
||
A lot of our framework integrations already set a transaction name, though you can set one yourself. | ||
```javascript | ||
Sentry.init({ | ||
beforeSendTransaction(event) { | ||
event.transaction = "UserListView"; | ||
return event; | ||
}, | ||
}); | ||
``` | ||
|
||
You'll first need to import the SDK, as usual: | ||
<Note> | ||
Only do this if you can't set the root span name earlier using other options. | ||
</Note> | ||
|
||
<PlatformContent includePath="enriching-events/import" /> | ||
## Further Information | ||
|
||
To override the name of the currently running transaction: | ||
You might wonder why there are two separate transaction names and why they're set independently. This is mainly due to Sentry's history and evolution. The error transaction name existed before Sentry offered tracing, and it was used solely to group error events. When tracing was introduced, the root span in a span tree was also called a "transaction," with its own name. | ||
|
||
<PlatformContent includePath="enriching-events/set-transaction-name" /> | ||
In later iterations, Sentry shifted focus in the UI from "transactions" to "spans" and "traces," but the older concepts remain in parts of the UI. The SDKs adapted their APIs, moving away from transaction-based designs. For example, the `setTransactionName` API on the `Scope` refers to error transaction names, not spans. Similarly, a span name doesn’t automatically apply to error events, keeping the two concepts [separate](https://github.com/getsentry/sentry-javascript/issues/10846). | ||
|
||
Please refer to [the tracing documentation](../../tracing/) for how to start and stop transactions. | ||
As Sentry continues to evolve, the ambiguity between these terms is expected to decrease as the product moves away from associating "transactions" with tracing and spans. |
3 changes: 0 additions & 3 deletions
3
platform-includes/enriching-events/set-transaction-name/javascript.mdx
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
platform-includes/performance/beforeNavigate-example/javascript.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.