Skip to content

Commit 008c454

Browse files
committed
ref(js): Rewrite outdated "Transaction Name" documentation
1 parent e5d3f66 commit 008c454

File tree

3 files changed

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

3 files changed

+120
-16
lines changed
Lines changed: 119 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
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 the transaction name to improve issue and span 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 sets a "transaction name" which is used to annotate error events with their point of failure.
12+
Furthermore, if you use <PlatformLink to="/tracing/">Tracing</PlatformLink>, Sentry refers to the root spans of your application as a "transaction" and groups traces based on the root span's "transaction" name.
1413

15-
The transaction name can reference the current web app route, or the current
16-
task being executed. For example:
14+
This means that there are in fact two transaction names:
15+
16+
1. The _error transaction name_ which is applied to error events
17+
2. The _root span transaction name_ which is the name of the root span in your applications span tree.
18+
19+
Both are used to annotate and group error and trace events respectively and are usually set by the SDK or an integration automatically.
20+
21+
<PlatformCategorySection supported={['browser']}>
22+
23+
You need to add `browserTracingIntegration` when initializing the SDK to automatically set a transaction name.
24+
25+
</PlatformCategorySection>
26+
27+
Generally, **you don't need to set or override the transaction name manually.**
28+
In most cases, the automatically determined name is a good grouping mechanism.
29+
However, if this does not work for your use case, you can set both transaction names independently.
30+
31+
## What is a "Good" Transaction Name?
32+
33+
The transaction name can reference the current web app or server route, mobile screen or activity, or the current task being executed.
34+
For example:
1735

1836
- `GET /api/{version}/users/`
1937
- `UserListView`
@@ -22,15 +40,104 @@ task being executed. For example:
2240
Ideally, the transaction name does not contain variable values such as user
2341
IDs but has rather low cardinality while still uniquely identifying a piece of
2442
code you care about.
43+
For example, instead of `'GET /api/users/123/details'`, set `'GET /api/users/:id/details'`.
44+
This ensures that errors or traces for all requests made to this route are grouped together.
45+
46+
### When to set the Transaction Name
47+
48+
We generally recommend to let the SDK set the transaction name automatically.
49+
However, in some cases, the SDK might not be able to set a name or the set name doesn't work for your use case.
50+
51+
If you need to set the transaction name manually, we recommend setting it _as early as possible_ in your application code, specifically when the operation you want to describe with the transaction name starts.
52+
This ensures that errors thrown early in the life cycle of your application are correctly annotated.
53+
Furthermore, it ensures consistency across a trace if you use [distributed tracing](/concepts/key-terms/tracing/distributed-tracing/).
54+
55+
For example, in a server application, you might set the transaction name in a middleware that runs before the request is processed.
56+
In a web application, you might set the transaction name when you enter a new route (e.g. in an SPA Router).
57+
58+
## Setting the Error Transaction Name
59+
60+
There are several ways to set the transaction name for error events sent to Sentry.
61+
62+
### Setting the Name on the Scope
63+
64+
Set the transaction name on the current scope when the operation you want to group starts:
65+
66+
```javascript
67+
Sentry.getCurrentScope().setTransactionName("UserListView");
68+
```
69+
70+
Note that the transaction name on the scope is only applied to error events. It does not influence the name of a potentially active root span.
71+
Likewise, if a span is active while an error is thrown, its name will not be applied to the error event's transaction name.
72+
73+
### Setting the Name on the Event
74+
75+
You can use `beforeSend` to set the transaction name on the event:
76+
77+
```javascript {3}
78+
Sentry.init({
79+
beforeSend(event) {
80+
event.transaction = "UserListView";
81+
return event;
82+
},
83+
});
84+
```
85+
86+
## Setting the Root Span Name
87+
88+
To override the name of the root span name (i.e. the trace name or transaction name in the Sentry UI), you have multiple options:
89+
90+
<PlatformCategorySection supported={['browser']}>
91+
92+
### At Root Span Start
93+
94+
Sentry's `browserTracingIntegration` automatically sets the transaction name based on the current route or URL for all `pageload` and `navigation` transactions.
95+
If you want to override the transaction name, you can do so in the <PlatformLink to="/tracing/instrumentation/automatic-instrumentation/#beforestartspan">`beforeStartSpan` callback</PlatformLink> of the integration.
96+
97+
</PlatformCategorySection>
98+
99+
### While the root span is active
100+
101+
_Available since: v8.44.0_
102+
103+
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 but a little bit later. In this case, you can update the root span name while the span is active:
104+
105+
```javascript
106+
const activeSpan = Sentry.getActiveSpan();
107+
const rootSpan = activeSpan && Sentry.getRootSpan(activeSpan);
108+
109+
Sentry.updateSpanName(rootSpan, "UserListView");
110+
```
111+
112+
Learn more about <PlatformLink to="/tracing/instrumentation/custom-instrumentation/#updating-the-span-name">updating the span name</PlatformLink>.
113+
114+
### After the root span finished
115+
116+
If you cannot determine the root span name before the span finishes, you can retroactively change it in `beforeSendTransaction`:
117+
118+
```javascript
119+
Sentry.init({
120+
beforeSendTransaction(event) {
121+
event.transaction = "UserListView";
122+
return event;
123+
},
124+
});
125+
```
25126

26-
A lot of our framework integrations already set a transaction name, though you can set one yourself.
127+
We recommend to only do this, if you cannot use any of the other options to set the root span name earlier.
27128

28-
You'll first need to import the SDK, as usual:
129+
## Further Information
29130

30-
<PlatformContent includePath="enriching-events/import" />
131+
You might be wondering why these two types of transaction names exist and why they are set independently.
132+
The reason is mostly historic evolvement of the Sentry product and the SDKs.
133+
The error transaction name existed long before Sentry provided tracing capabilities and where it was only applied to error events to better group issues.
31134

32-
To override the name of the currently running transaction:
135+
In the first iteration of Sentry's tracing or performance monitoring product, we decided to call the root span of a span tree within an application a "Transaction", which also had a name.
136+
In the second, now ongoing, tracing iteration, Sentry's new UI features no longer mention "Transactions" for root spans but rather just "spans" and "traces".
137+
However, the two concepts (error transaction name and root span name) are still ambiguous and used so throughout various older parts of the Sentry UI.
33138

34-
<PlatformContent includePath="enriching-events/set-transaction-name" />
139+
With this product evolvement in mind, the SDKs also had to adapt their APIs around tracing, specifically moving away from transaction-based APIs in version 8.
140+
Consequently, the SDK exposes the `setTransactionName` API on the `Scope` which—although the name might suggest differently from historic context—has nothing to do with spans.
141+
Likewise, the span name is not automatically applied to error events either, meaning both concepts are completely [decoupled](https://github.com/getsentry/sentry-javascript/issues/10846) from each other.
35142

36-
Please refer to [the tracing documentation](../../tracing/) for how to start and stop transactions.
143+
We believe that the ambiguity of these APIs will decrease over time as the Sentry product further moves away from associating the "Transaction" term 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)