You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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."
4
4
supported:
5
5
- javascript
6
6
notSupported:
7
7
- javascript.cordova
8
8
- javascript.nextjs
9
9
---
10
10
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 <PlatformLinkto="/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.
14
13
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
+
<PlatformCategorySectionsupported={['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:
17
35
18
36
-`GET /api/{version}/users/`
19
37
-`UserListView`
@@ -22,15 +40,104 @@ task being executed. For example:
22
40
Ideally, the transaction name does not contain variable values such as user
23
41
IDs but has rather low cardinality while still uniquely identifying a piece of
24
42
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:
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
+
returnevent;
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
+
<PlatformCategorySectionsupported={['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 <PlatformLinkto="/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:
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.
31
134
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.
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.
35
142
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.
Copy file name to clipboardExpand all lines: platform-includes/performance/beforeNavigate-example/javascript.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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.
0 commit comments