Skip to content

test(node): Add tests for current DSC transaction name updating behavior #13961

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 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
tracesSampleRate: 1.0,
transport: loggingTransport,
});

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Sentry.startSpan(
{ name: 'initial-name', attributes: { [Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url' } },
async span => {
Sentry.captureMessage('message-1');

span.updateName('updated-name-1');
span.setAttribute(Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');

Sentry.captureMessage('message-2');

span.updateName('updated-name-2');
span.setAttribute(Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'custom');

Sentry.captureMessage('message-3');

span.end();
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
tracesSampleRate: 1.0,
transport: loggingTransport,
});

import * as http from 'http';

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Sentry.startSpan(
{ name: 'initial-name', attributes: { [Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url' } },
async span => {
await makeHttpRequest(`${process.env.SERVER_URL}/api/v0`);

span.updateName('updated-name-1');
span.setAttribute(Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');

await makeHttpRequest(`${process.env.SERVER_URL}/api/v1`);

span.updateName('updated-name-2');
span.setAttribute(Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'custom');
await makeHttpRequest(`${process.env.SERVER_URL}/api/v2`);

span.end();
},
);

function makeHttpRequest(url: string): Promise<void> {
return new Promise<void>(resolve => {
http
.request(url, httpRes => {
httpRes.on('data', () => {
// we don't care about data
});
httpRes.on('end', () => {
resolve();
});
})
.end();
});
}
Copy link
Member Author

@Lms24 Lms24 Oct 11, 2024

Choose a reason for hiding this comment

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

I fought multiple hours with our test runner to get these tests working. Very frustrating.

Turns out, you can't assert on headers in the test server as well as on events in the same test. It will appear as if it's working but it won't fail if an assertion actually fails :/
moreover, there#s also super weird inter operation between expect and ecpectHeaders when you want to check on an event and its headers. After trying to make everything work in one test, I eventually gave up and made one for baggage and one for the trace envelope header.

I think part of the problem is that there is some race condition with the test server and the test runners both calling done which completely fucks up the assertion count.

Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { createRunner } from '../../../utils/runner';
import { createTestServer } from '../../../utils/server';

test('adds current transaction name to baggage when the txn name is high-quality', done => {
expect.assertions(5);

let traceId: string | undefined;

createTestServer(done)
.get('/api/v0', headers => {
const baggageItems = getBaggageHeaderItems(headers);
traceId = baggageItems.find(item => item.startsWith('sentry-trace_id='))?.split('=')[1] as string;

expect(traceId).toMatch(/^[0-9a-f]{32}$/);

expect(baggageItems).toEqual([
'sentry-environment=production',
'sentry-public_key=public',
'sentry-release=1.0',
'sentry-sample_rate=1',
'sentry-sampled=true',
`sentry-trace_id=${traceId}`,
]);
})
.get('/api/v1', headers => {
expect(getBaggageHeaderItems(headers)).toEqual([
'sentry-environment=production',
'sentry-public_key=public',
'sentry-release=1.0',
'sentry-sample_rate=1',
'sentry-sampled=true',
`sentry-trace_id=${traceId}`,
'sentry-transaction=updated-name-1',
]);
})
.get('/api/v2', headers => {
expect(getBaggageHeaderItems(headers)).toEqual([
'sentry-environment=production',
'sentry-public_key=public',
'sentry-release=1.0',
'sentry-sample_rate=1',
'sentry-sampled=true',
`sentry-trace_id=${traceId}`,
'sentry-transaction=updated-name-2',
]);
})
.start()
.then(([SERVER_URL, closeTestServer]) => {
createRunner(__dirname, 'scenario-headers.ts')
.withEnv({ SERVER_URL })
.expect({
transaction: {},
})
.start(closeTestServer);
});
});

test('adds current transaction name to trace envelope header when the txn name is high-quality', done => {
expect.assertions(4);

createRunner(__dirname, 'scenario-events.ts')
.expectHeader({
event: {
trace: {
environment: 'production',
public_key: 'public',
release: '1.0',
sample_rate: '1',
sampled: 'true',
trace_id: expect.any(String),
},
},
})
.expectHeader({
event: {
trace: {
environment: 'production',
public_key: 'public',
release: '1.0',
sample_rate: '1',
sampled: 'true',
trace_id: expect.any(String),
transaction: 'updated-name-1',
},
},
})
.expectHeader({
event: {
trace: {
environment: 'production',
public_key: 'public',
release: '1.0',
sample_rate: '1',
sampled: 'true',
trace_id: expect.any(String),
transaction: 'updated-name-2',
},
},
})
.expectHeader({
transaction: {
trace: {
environment: 'production',
public_key: 'public',
release: '1.0',
sample_rate: '1',
sampled: 'true',
trace_id: expect.any(String),
transaction: 'updated-name-2',
},
},
})
.start(done);
});

function getBaggageHeaderItems(headers: Record<string, string | string[] | undefined>) {
const baggage = headers['baggage'] as string;
const baggageItems = baggage
.split(',')
.map(b => b.trim())
.sort();
return baggageItems;
}
Loading