Skip to content

Commit 99016ee

Browse files
Lms24lizokm
andauthored
ref(js): Rewrite outdated "Transaction Name" documentation (#12091)
--------- Co-authored-by: Liza Mock <[email protected]>
1 parent 3286279 commit 99016ee

File tree

3 files changed

+108
-19
lines changed
  • docs/platforms/javascript/common/enriching-events/transaction-name
  • platform-includes

3 files changed

+108
-19
lines changed
Lines changed: 107 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,128 @@
11
---
22
title: Transaction Name
3-
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."
3+
description: "Learn how to set or override transaction names to improve issue and trace-grouping."
44
supported:
55
- javascript
66
notSupported:
77
- javascript.cordova
88
- javascript.nextjs
99
---
1010

11-
The current transaction name is used to group transactions in our
12-
[Performance](/product/performance/) product, as well as annotate error events
13-
with their point of failure.
11+
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.
1412

15-
The transaction name can reference the current web app route, or the current
16-
task being executed. For example:
13+
This means that there are in fact two transaction names:
14+
15+
1. The _error transaction name_ which is applied to error events
16+
2. The _root span transaction name_ which is the name of the root span in your applications span tree.
17+
18+
Both are used to annotate and group error and trace events respectively and are usually set by the SDK or an integration automatically.
19+
20+
<PlatformCategorySection supported={['browser']}>
21+
22+
To automatically set transaction names, add `browserTracingIntegration` when initializing the SDK.
23+
24+
</PlatformCategorySection>
25+
26+
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.**
27+
28+
## What's a "Good" Transaction Name?
29+
30+
Your transaction name can reference the current web app or server route, mobile screen or activity, or the current task being executed.
31+
For example:
1732

1833
- `GET /api/{version}/users/`
1934
- `UserListView`
2035
- `myapp.tasks.renew_all_subscriptions`
2136

22-
Ideally, the transaction name does not contain variable values such as user
23-
IDs but has rather low cardinality while still uniquely identifying a piece of
24-
code you care about.
37+
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.
38+
For example, instead of naming a transaction`'GET /api/users/123/details'`, name it `'GET /api/users/:id/details'`.
39+
This will group all errors and traces for requests to the same route.
40+
41+
### When to Set the Transaction Name
42+
43+
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.
44+
45+
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/).
46+
47+
For example, in a server app, you can set the transaction name in middleware before handling a request.
48+
In a web app, you can set it when navigating to a new route, like in an SPA router.
49+
50+
## Setting the Error Transaction Name
51+
52+
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.
53+
54+
### Setting the Name on the Scope
55+
56+
Set the transaction name on the current scope when the operation you want to group starts:
57+
58+
```javascript
59+
Sentry.getCurrentScope().setTransactionName("UserListView");
60+
```
61+
62+
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.
63+
64+
### Setting the Name on the Event
65+
66+
You can use `beforeSend` to set the transaction name on the event:
67+
68+
```javascript {3}
69+
Sentry.init({
70+
beforeSend(event) {
71+
event.transaction = "UserListView";
72+
return event;
73+
},
74+
});
75+
```
76+
77+
## Setting the Root Span Name
78+
79+
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):
80+
81+
<PlatformCategorySection supported={['browser']}>
82+
83+
### At Root Span Start
84+
85+
Sentry's `browserTracingIntegration` automatically sets the transaction name based on the current route or URL for all `pageload` and `navigation` transactions.
86+
To override the transaction name, use the <PlatformLink to="/tracing/instrumentation/automatic-instrumentation/#beforestartspan">`beforeStartSpan` callback</PlatformLink> of the integration.
87+
88+
</PlatformCategorySection>
89+
90+
### While the Root Span Is Active
91+
92+
_Available starting with: v8.47.0_
93+
94+
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:
95+
96+
```javascript
97+
const activeSpan = Sentry.getActiveSpan();
98+
const rootSpan = activeSpan && Sentry.getRootSpan(activeSpan);
99+
100+
Sentry.updateSpanName(rootSpan, "UserListView");
101+
```
102+
103+
Learn more about <PlatformLink to="/tracing/instrumentation/custom-instrumentation/#updating-the-span-name">updating the span name</PlatformLink>.
104+
105+
### After the Root Span Has Finished
106+
107+
If you can't determine the root span name before the span finishes, you'll be able to change it retroactively in `beforeSendTransaction`:
25108

26-
A lot of our framework integrations already set a transaction name, though you can set one yourself.
109+
```javascript
110+
Sentry.init({
111+
beforeSendTransaction(event) {
112+
event.transaction = "UserListView";
113+
return event;
114+
},
115+
});
116+
```
27117

28-
You'll first need to import the SDK, as usual:
118+
<Note>
119+
Only do this if you can't set the root span name earlier using other options.
120+
</Note>
29121

30-
<PlatformContent includePath="enriching-events/import" />
122+
## Further Information
31123

32-
To override the name of the currently running transaction:
124+
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.
33125

34-
<PlatformContent includePath="enriching-events/set-transaction-name" />
126+
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).
35127

36-
Please refer to [the tracing documentation](../../tracing/) for how to start and stop transactions.
128+
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.

platform-includes/enriching-events/set-transaction-name/javascript.mdx

Lines changed: 0 additions & 3 deletions
This file was deleted.

platform-includes/performance/beforeNavigate-example/javascript.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
One common use case is parameterizing transaction names. For both `pageload` and `navigation` transactions, the `BrowserTracing` integration uses the browser's `window.location` value to generate a transaction name. Using `beforeStartSpan` lets you modify the transaction name to make it more generic, so that, for example, transactions named `GET /users/12312012` and `GET /users/11212012` can both be renamed to `GET /users/:userid`. That way they'll be grouped together.
1+
One common use case is parameterizing transaction names. For both `pageload` and `navigation` transactions, the `browserTracingIntegration` uses the browser's `window.location` value to generate a transaction name. Using `beforeStartSpan` lets you modify the transaction name to make it more generic, so that for example, transactions named `GET /users/12312012` and `GET /users/11212012` can both be renamed to `GET /users/:userid`. That way they'll be grouped together.
22

33
```javascript
44
Sentry.init({

0 commit comments

Comments
 (0)