Skip to content

fix(apm): Set the transaction name for JavaScript transactions before they are flushed #18822

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 6 commits into from
May 20, 2020

Conversation

dashed
Copy link
Member

@dashed dashed commented May 14, 2020

For JavaScript transactions, translate the transaction name if it exists and doesn't start with /
using the app's react-router routes; the expected transaction name would be the URL, which is set by the JS SDK.

If the transaction name doesn't exist, use the window.location.pathname as the fallback.

The transaction name is translated based on the app react-router routes using the react-router utility functions.

This should deal with the race condition between setting the transaction name during the React render reconciliation and when transactions are flushed.

TODO

  • update ui.route tag

HazAT
HazAT previously requested changes May 14, 2020
Copy link
Member

@HazAT HazAT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the code from app/utils/apm.tsx that sets the transaction?

location: createLocation(prevTransactionName),
},
(error, _redirectLocation, renderProps) => {
if (error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know the cases when this might fail?
Because in those cases we would still get the full url.
I just wanted to highlight this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we may not know.

We need to at least handle this otherwise the transaction name will be overridden by a worst choice of a transaction name; in this case, a blank string.

@benvinegar
Copy link
Contributor

How do we document this in our SDKs, framework docs, etc. so that other people can easily follow this example? cc @HazAT

@billyvg
Copy link
Member

billyvg commented May 14, 2020

@HazAT I've noticed that we do not call the set transaction name on DidMount - only on DidUpdate, wonder if this would solve some of the problems we're having.

@dashed and I were talking about removing utils/apm but there could be cases where window.location changes before the transaction event is sent, in which case the route would not be accurate.

@dashed
Copy link
Member Author

dashed commented May 14, 2020

Going to put this PR on pause and try #18836 for a week or so.

@lobsterkatie
Copy link
Member

@benvinegar Revisiting the docs explaining how to APM in Python/JS is my next project. We currently have an example of instrumenting a react component which I'd like to flesh out and clean up a bit. Will definitely be looking for feedback on that section once I have a draft, since I'm still fairly new to react.

event.transaction = transactionName;

if (event.tags) {
event.tags['ui.route'] = transactionName;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we tag this event to identify how often this happens?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@billyvg do you mean analytics?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dashed Yeah, might be nice to have it as a tag in the event

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@billyvg is this something you had in mind? 58bb0bf

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dashed Yeah, you may be able to simplify to prev !== transactionName if you don't need the prev transaction name (since you already have transaction name).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@billyvg I'm unsure what you meant. prevTransactionName !== transactionName should be guaranteed to be true in this code path.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, then the previous tag should be enough, and if you wanted to know the number of transactions that required normalization you can query for something like !transaction.rename.before:''?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think has:transaction.rename.source might be the search condition. Since this gets set knowing that normalization is required. And regardless of whether or not the normalization process was successful.

@@ -70,10 +72,15 @@ const tracesSampleRate = config ? config.apmSampling : 0;
const hasReplays =
window.__SENTRY__USER && window.__SENTRY__USER.isStaff && !!process.env.DISABLE_RR_WEB;

const appRoutes = Router.createRoutes(routes());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, I cannot move this to src/sentry/static/sentry/app/utils/apm.tsx

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you move it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The app doesn't load.

@dashed dashed requested a review from billyvg May 20, 2020 00:35
return event;
}

set(event, ['tags', 'transaction.rename.source'], 'existing transaction name');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@billyvg nah I don't think so. I think, semantically, tags that begin transaction.rename should only apply to events that have undergone a transaction rename process.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, we shouldn't be renaming transactions that begin with /. Since these transactions are what we'd expect. They've already been renamed at the React app level from within the lifecycle method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, the word "existing" kind of threw me off (I was thinking existing mean it has a correct transaction name).

Copy link
Member

@billyvg billyvg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

dashed added 6 commits May 20, 2020 06:16
… they are flushed

Set the transaction name for JavaScript transactions based on latest window.location object just before the transaction is flushed. The window.location object is translated based on the app react-router routes using the react-router utility functions.
@dashed dashed force-pushed the apm-fix-transaction-name branch from 58bb0bf to 5249c9b Compare May 20, 2020 10:16
@dashed dashed dismissed HazAT’s stale review May 20, 2020 19:35

addressed

@dashed dashed merged commit 4065210 into master May 20, 2020
@dashed dashed deleted the apm-fix-transaction-name branch May 20, 2020 19:37
@github-actions github-actions bot locked and limited conversation to collaborators Dec 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants