Skip to content
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

test(node): Add tests for Undici #7628

Merged
merged 12 commits into from
Mar 29, 2023
Merged

test(node): Add tests for Undici #7628

merged 12 commits into from
Mar 29, 2023

Conversation

AbhiPrasad
Copy link
Member

ref #7624

Tests for #7582!

Ended up pulling in the undici dep to make testing easier.

Tests are not as clean as I wished to be, but I'm optimizing for speed here - we can clean this up afterwards.

@@ -89,7 +93,7 @@ export class Undici implements Integration {
const url = new URL(request.path, request.origin);
const stringUrl = url.toString();

if (isSentryRequest(stringUrl)) {
if (isSentryRequest(stringUrl) || request.__sentry__ !== undefined) {
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 kept running into this, but I'm not sure why it's happening, the spec doesn't allow for it.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm this looks strange. As if the same request went twice through this channel? Anyway, I think it's fine to bail out in this case

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

This looks good, thanks for adding the tests!

Comment on lines +36 to +39
// Please note that you cannot use `console.log` to debug the callbacks registered to the `diagnostics_channel` API.
// To debug, you can use `writeFileSync` to write to a file:
// https://nodejs.org/api/async_hooks.html#printing-in-asynchook-callbacks

Copy link
Member

Choose a reason for hiding this comment

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

Sounds painful :/

@@ -89,7 +93,7 @@ export class Undici implements Integration {
const url = new URL(request.path, request.origin);
const stringUrl = url.toString();

if (isSentryRequest(stringUrl)) {
if (isSentryRequest(stringUrl) || request.__sentry__ !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

Hmm this looks strange. As if the same request went twice through this channel? Anyway, I think it's fine to bail out in this case

Comment on lines 124 to 154
it('does not create a span for sentry requests', async () => {
const transaction = hub.startTransaction({ name: 'test-transaction' }) as Transaction;
hub.getScope().setSpan(transaction);

try {
await fetch(`${SENTRY_DSN}/sub/route`, {
method: 'POST',
});
} catch (e) {
// ignore
}

expect(transaction.spanRecorder?.spans.length).toBe(1);
});

it('does not create a span for sentry requests', async () => {
const transaction = hub.startTransaction({ name: 'test-transaction' }) as Transaction;
hub.getScope().setSpan(transaction);

expect(transaction.spanRecorder?.spans.length).toBe(1);

try {
await fetch(`${SENTRY_DSN}/sub/route`, {
method: 'POST',
});
} catch (e) {
// ignore
}

expect(transaction.spanRecorder?.spans.length).toBe(1);
});
Copy link
Member

Choose a reason for hiding this comment

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

l: duplicate?

Suggested change
it('does not create a span for sentry requests', async () => {
const transaction = hub.startTransaction({ name: 'test-transaction' }) as Transaction;
hub.getScope().setSpan(transaction);
try {
await fetch(`${SENTRY_DSN}/sub/route`, {
method: 'POST',
});
} catch (e) {
// ignore
}
expect(transaction.spanRecorder?.spans.length).toBe(1);
});
it('does not create a span for sentry requests', async () => {
const transaction = hub.startTransaction({ name: 'test-transaction' }) as Transaction;
hub.getScope().setSpan(transaction);
expect(transaction.spanRecorder?.spans.length).toBe(1);
try {
await fetch(`${SENTRY_DSN}/sub/route`, {
method: 'POST',
});
} catch (e) {
// ignore
}
expect(transaction.spanRecorder?.spans.length).toBe(1);
});
it('does not create a span for sentry requests', async () => {
const transaction = hub.startTransaction({ name: 'test-transaction' }) as Transaction;
hub.getScope().setSpan(transaction);
try {
await fetch(`${SENTRY_DSN}/sub/route`, {
method: 'POST',
});
} catch (e) {
// ignore
}
expect(transaction.spanRecorder?.spans.length).toBe(1);
});

Comment on lines +129 to +131
await fetch(`${SENTRY_DSN}/sub/route`, {
method: 'POST',
});
Copy link
Member

Choose a reason for hiding this comment

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

l (and more a question than a suggestion):
This makes me wonder about our isSentryRequest check. As I mentioned yesterday, we have 5 slightly different checks for identifying a sentry request all over the code base. I'll sooner or later clean this up (after #7626) but should this check take the Http method into account? Some checks do (example) while others (like the one you're using here) don't. I'd argue it's probably not necessary to check for the method but wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

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

All sentry requests use POST atm, so I don't see anything wrong with this for now, but the more future-proof and correct solution is to not take into account the http method (in case we do ever introduce GET methods - think something like dynamic config).

Going to keep this as is for the test, we can come back and refactor all instances of isSentryRequest later.

Comment on lines +166 to +178
it('does create a span if `shouldCreateSpanForRequest` is defined', async () => {
const transaction = hub.startTransaction({ name: 'test-transaction' }) as Transaction;
hub.getScope().setSpan(transaction);

const client = new NodeClient({ ...DEFAULT_OPTIONS, shouldCreateSpanForRequest: url => url.includes('yes') });
hub.bindClient(client);

await fetch('http://localhost:18099/no', { method: 'POST' });

expect(transaction.spanRecorder?.spans.length).toBe(1);

await fetch('http://localhost:18099/yes', { method: 'POST' });

expect(transaction.spanRecorder?.spans.length).toBe(2);
});

it('attaches the sentry trace and baggage headers', async () => {
const transaction = hub.startTransaction({ name: 'test-transaction' }) as Transaction;
hub.getScope().setSpan(transaction);

await fetch('http://localhost:18099', { method: 'POST' });

expect(transaction.spanRecorder?.spans.length).toBe(2);
const span = transaction.spanRecorder?.spans[1];

expect(requestHeaders['sentry-trace']).toEqual(span?.toTraceparent());
expect(requestHeaders['baggage']).toEqual(
`sentry-environment=production,sentry-transaction=test-transaction,sentry-public_key=0,sentry-trace_id=${transaction.traceId},sentry-sample_rate=1`,
);
});
Copy link
Member

Choose a reason for hiding this comment

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

l: Let's add a test to cover the case where shouldCreateSpanForRequest returns false which should lead to the tracing headers not being attached.

@github-actions
Copy link
Contributor

size-limit report 📦

Path Size
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 20.62 KB (+0.06% 🔺)
@sentry/browser - ES5 CDN Bundle (minified) 64.4 KB (+0.02% 🔺)
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 19.15 KB (-0.03% 🔽)
@sentry/browser - ES6 CDN Bundle (minified) 56.78 KB (+0.02% 🔺)
@sentry/browser - Webpack (gzipped + minified) 21.53 KB (-0.39% 🔽)
@sentry/browser - Webpack (minified) 72 KB (-0.04% 🔽)
@sentry/react - Webpack (gzipped + minified) 21.55 KB (-0.39% 🔽)
@sentry/nextjs Client - Webpack (gzipped + minified) 51.98 KB (-0.01% 🔽)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 28.16 KB (+0.06% 🔺)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 26.36 KB (+0.06% 🔺)
@sentry/replay ES6 CDN Bundle (gzipped + minified) 44.74 KB (+0.61% 🔺)
@sentry/replay - Webpack (gzipped + minified) 38.86 KB (+0.83% 🔺)
@sentry/browser + @sentry/tracing + @sentry/replay - ES6 CDN Bundle (gzipped + minified) 63.41 KB (+0.46% 🔺)
@sentry/browser + @sentry/replay - ES6 CDN Bundle (gzipped + minified) 56.49 KB (+0.49% 🔺)

@AbhiPrasad AbhiPrasad merged commit 32675e8 into develop Mar 29, 2023
@AbhiPrasad AbhiPrasad deleted the abhi-undici-test branch March 29, 2023 09:29
@AbhiPrasad AbhiPrasad self-assigned this Mar 29, 2023
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