Skip to content

fix(tracing): ignore the xhr/fetch response if its request is not being tracked #4428

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 5 commits into from
Jan 24, 2022

Conversation

datbth
Copy link
Contributor

@datbth datbth commented Jan 20, 2022

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).

A possible fix for #4427

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

Thank you so much for the PR! This is def something we should have accounted for. Test is great as well.

Will merge after we've addressed the comment.

@@ -216,7 +216,9 @@ export function xhrCallback(
const xhr = handlerData.xhr.__sentry_xhr__;

// check first if the request has finished and is tracked by an existing span which should now end
if (handlerData.endTimestamp && handlerData.xhr.__sentry_xhr_span_id__) {
if (handlerData.endTimestamp) {
Copy link
Member

Choose a reason for hiding this comment

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

we probably want this logic to be like:

if (handlerData.endTimestamp) {
  const spanId = handlerData.xhr.__sentry_xhr_span_id__;
  if (spanId) {
    const span = spans[spanId];
    if (span) {
      span.setHttpStatus(xhr.status_code);
      span.finish();

      // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
      delete spans[spanId];
    }
  }
  return;
}

This is also more bundle size efficient

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hi, thanks for looking into this.

I have refactored into using the spanId variable as suggested.
But I'm using eager-return style because it modifies less codes and I also believe multiple nesting levels is less readable. Not sure if it's significant to the bundle size?

Copy link
Member

Choose a reason for hiding this comment

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

Well it's two return keywords rather than one (both of which can't be minified), but yeah the impact is probably minimal. We can leave it like this!

@datbth datbth changed the title fix(tracing): ignore the xhr response if its request is not being tracked fix(tracing): ignore the xhr/fetch response if its request is not being tracked Jan 21, 2022
@datbth datbth requested a review from AbhiPrasad January 21, 2022 02:58
@@ -418,6 +419,7 @@ describe('Hub', () => {

const request = new XMLHttpRequest();
request.open('GET', '/chase-partners');
request.send();
Copy link
Member

Choose a reason for hiding this comment

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

This errors because we are now actually making the request, to a resource that doesn't exist.

As a quick thing to unblock we can just request http://example.com - but we should probably use SinonFakeServer like we do to test the xhr transport in

let server: SinonFakeServer;
let transport: Transports.BaseTransport;
describe('XHRTransport', () => {
beforeEach(() => {
server = fakeServer.create();
server.respondImmediately = true;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh somehow the tests did not fail on my machine. They do fail when I disable the Internet. I'm working on this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could not get the tests to work properly with fake servers. But since these tests do not really care about the responses, I have fixed them by waiting for the requests to complete regardless of the result

by waiting for requests properly
@datbth datbth force-pushed the tracing/fix_xhr_tracing branch from 19a538d to ccecb71 Compare January 23, 2022 04:04
@AbhiPrasad AbhiPrasad enabled auto-merge (squash) January 24, 2022 14:27
@AbhiPrasad AbhiPrasad merged commit c224f9c into getsentry:master Jan 24, 2022
@AbhiPrasad
Copy link
Member

@datbth both your PRs were merged, they'll be out with the next release. Really appreciate the help!

If you're interested in continuing to help us out, we are hiring - and flexible to different locations! Email in GH profile if you want more details :)

@datbth datbth deleted the tracing/fix_xhr_tracing branch January 25, 2022 06:39
@datbth
Copy link
Contributor Author

datbth commented Jan 25, 2022

Thanks, @AbhiPrasad. I'm happy to have worked with you guys.
For now I'll probably just be making these adhoc contributions. Thank you for the info!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants