Problematic API: `Scope.setTransactionName()`
We've identified this API, or rather its implementation as problematic.
Scope.setTransactionName() will populate the transaction field of any event that comes after that call on that scope (and any scopes that were cloned from that scope).
The transaction field is used for associating an error with a transaction (not an actual transaction event in Sentry but rather a route or error origin) when dealing with an error event, and it contains the name of a transaction when dealing with a transaction event.
While this sounds nice and all, overwriting the name of transactions like this is a bit problematic:
- It's a bit opaque that this is the behaviour of
Scope.setTransactionName() - Often times we already have a high-quality transaction name through auto-instrumentation (
source: route) and we don't want to have it overridden by the user. While we should provide users to overwrite high quality transaction names if they really want to, they probably shouldn't do it withScope.setTransactionName(). It's very likely that this function just causes side-effects that the user didn't intend to introduce. - If
transactionis overriden byScope.setTransactionName()we're not updating thesourceof the transaction tocustom. We needsourceto be correct for Dynamic Sampling.
In my opinion, the problem with this API is that it does completely different things for errors and transaction. While the changing of the transaction field is quite inconsequential for error events, it completely messes up transactions when used wrong.
I'd approach it like the following:
- Keep the behaviour for error events.
- Don't update the name for transaction events.
I'd approach it like the following:
- Keep the behaviour for error events.
- Don't update the name for transaction events.
I +100 this. Seems like we can't get rid of this weird API entirely so let's at least make sure that the field isn't applied to the transaction event anymore.
IMHO this is part of a larger problem we have, which is that we have a number of generations of behavior with respect to the transaction field (both on the scope and in events), with the result that the end result is hard to reason about, inconsistent between error types, packages, etc, and prone to problems such as the one in this issue. (Probably that should be written up as a separate issue, though. UPDATE: I did end up writing it up: https://github.com/getsentry/sentry-javascript/issues/5718) As for @lforst's suggestion...
- Don't update the name for transaction events.
That can be done just by changing https://github.com/getsentry/sentry-javascript/blob/efd32f0f616bf23eac9373a997376aac57a3f00f/packages/hub/src/scope.ts#L468
to https://github.com/getsentry/sentry-javascript/blob/99100cec548de7752c36e4d8bd6581b8079c98cf/packages/hub/src/scope.ts#L468
(This works because transaction events always have a transaction value from the very start: https://github.com/getsentry/sentry-javascript/blob/efd32f0f616bf23eac9373a997376aac57a3f00f/packages/tracing/src/transaction.ts#L142-L161)
Making this change would bring the behavior in line with what happens in addRequestDataToEvent here: https://github.com/getsentry/sentry-javascript/blob/efd32f0f616bf23eac9373a997376aac57a3f00f/packages/utils/src/requestdata.ts#L393-L397
We will look at data to determine if this problem is as big as we think.