Skip to content

Commit 5d76558

Browse files
committed
ref: Refactor internal usages of startTransaction hook
1 parent b1a6a43 commit 5d76558

File tree

5 files changed

+67
-51
lines changed

5 files changed

+67
-51
lines changed

packages/bun/test/integrations/bunserver.test.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ describe('Bun Serve Integration', () => {
2323
});
2424

2525
test('generates a transaction around a request', async () => {
26-
client.on('finishTransaction', transaction => {
27-
expect(spanToJSON(transaction).status).toBe('ok');
28-
expect(spanToJSON(transaction).data?.['http.response.status_code']).toEqual(200);
29-
expect(spanToJSON(transaction).op).toEqual('http.server');
30-
expect(spanToJSON(transaction).description).toEqual('GET /');
26+
client.on('spanEnd', span => {
27+
expect(spanToJSON(span).status).toBe('ok');
28+
expect(spanToJSON(span).data?.['http.response.status_code']).toEqual(200);
29+
expect(spanToJSON(span).op).toEqual('http.server');
30+
expect(spanToJSON(span).description).toEqual('GET /');
3131
});
3232

3333
const server = Bun.serve({
@@ -43,11 +43,11 @@ describe('Bun Serve Integration', () => {
4343
});
4444

4545
test('generates a post transaction', async () => {
46-
client.on('finishTransaction', transaction => {
47-
expect(spanToJSON(transaction).status).toBe('ok');
48-
expect(spanToJSON(transaction).data?.['http.response.status_code']).toEqual(200);
49-
expect(spanToJSON(transaction).op).toEqual('http.server');
50-
expect(spanToJSON(transaction).description).toEqual('POST /');
46+
client.on('spanEnd', span => {
47+
expect(spanToJSON(span).status).toBe('ok');
48+
expect(spanToJSON(span).data?.['http.response.status_code']).toEqual(200);
49+
expect(spanToJSON(span).op).toEqual('http.server');
50+
expect(spanToJSON(span).description).toEqual('POST /');
5151
});
5252

5353
const server = Bun.serve({
@@ -72,14 +72,13 @@ describe('Bun Serve Integration', () => {
7272
const SENTRY_TRACE_HEADER = `${TRACE_ID}-${PARENT_SPAN_ID}-${PARENT_SAMPLED}`;
7373
const SENTRY_BAGGAGE_HEADER = 'sentry-version=1.0,sentry-environment=production';
7474

75-
client.on('finishTransaction', transaction => {
76-
expect(transaction.spanContext().traceId).toBe(TRACE_ID);
77-
expect(transaction.parentSpanId).toBe(PARENT_SPAN_ID);
78-
expect(spanIsSampled(transaction)).toBe(true);
79-
// span.endTimestamp is already set in `finishTransaction` hook
80-
expect(transaction.isRecording()).toBe(false);
75+
client.on('spanEnd', span => {
76+
expect(span.spanContext().traceId).toBe(TRACE_ID);
77+
expect(spanToJSON(span).parent_span_id).toBe(PARENT_SPAN_ID);
78+
expect(spanIsSampled(span)).toBe(true);
79+
expect(span.isRecording()).toBe(false);
8180

82-
expect(getDynamicSamplingContextFromSpan(transaction)).toStrictEqual({
81+
expect(getDynamicSamplingContextFromSpan(span)).toStrictEqual({
8382
version: '1.0',
8483
environment: 'production',
8584
});
@@ -100,7 +99,7 @@ describe('Bun Serve Integration', () => {
10099
});
101100

102101
test('does not create transactions for OPTIONS or HEAD requests', async () => {
103-
client.on('finishTransaction', () => {
102+
client.on('spanEnd', () => {
104103
// This will never run, but we want to make sure it doesn't run.
105104
expect(false).toEqual(true);
106105
});

packages/core/test/lib/tracing/trace.test.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
withActiveSpan,
2424
} from '../../../src/tracing';
2525
import { SentryNonRecordingSpan } from '../../../src/tracing/sentryNonRecordingSpan';
26-
import { getActiveSpan, getSpanDescendants } from '../../../src/utils/spanUtils';
26+
import { getActiveSpan, getRootSpan, getSpanDescendants } from '../../../src/utils/spanUtils';
2727
import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';
2828

2929
beforeAll(() => {
@@ -84,8 +84,8 @@ describe('startSpan', () => {
8484

8585
it('creates a transaction', async () => {
8686
let _span: Span | undefined = undefined;
87-
client.on('finishTransaction', transaction => {
88-
_span = transaction;
87+
client.on('spanEnd', span => {
88+
_span = span;
8989
});
9090
try {
9191
await startSpan({ name: 'GET users/[id]' }, () => {
@@ -102,8 +102,8 @@ describe('startSpan', () => {
102102

103103
it('allows traceparent information to be overriden', async () => {
104104
let _span: Span | undefined = undefined;
105-
client.on('finishTransaction', transaction => {
106-
_span = transaction;
105+
client.on('spanEnd', span => {
106+
_span = span;
107107
});
108108
try {
109109
await startSpan(
@@ -129,8 +129,8 @@ describe('startSpan', () => {
129129

130130
it('allows for transaction to be mutated', async () => {
131131
let _span: Span | undefined = undefined;
132-
client.on('finishTransaction', transaction => {
133-
_span = transaction;
132+
client.on('spanEnd', span => {
133+
_span = span;
134134
});
135135
try {
136136
await startSpan({ name: 'GET users/[id]' }, span => {
@@ -146,8 +146,10 @@ describe('startSpan', () => {
146146

147147
it('creates a span with correct description', async () => {
148148
let _span: Span | undefined = undefined;
149-
client.on('finishTransaction', transaction => {
150-
_span = transaction;
149+
client.on('spanEnd', span => {
150+
if (span === getRootSpan(span)) {
151+
_span = span;
152+
}
151153
});
152154
try {
153155
await startSpan({ name: 'GET users/[id]', parentSampled: true }, () => {
@@ -170,8 +172,10 @@ describe('startSpan', () => {
170172

171173
it('allows for span to be mutated', async () => {
172174
let _span: Span | undefined = undefined;
173-
client.on('finishTransaction', transaction => {
174-
_span = transaction;
175+
client.on('spanEnd', span => {
176+
if (span === getRootSpan(span)) {
177+
_span = span;
178+
}
175179
});
176180
try {
177181
await startSpan({ name: 'GET users/[id]', parentSampled: true }, () => {
@@ -200,8 +204,8 @@ describe('startSpan', () => {
200204
{ origin: 'manual', attributes: { 'sentry.origin': 'auto.http.browser' } },
201205
])('correctly sets the span origin', async () => {
202206
let _span: Span | undefined = undefined;
203-
client.on('finishTransaction', transaction => {
204-
_span = transaction;
207+
client.on('spanEnd', span => {
208+
_span = span;
205209
});
206210
try {
207211
await startSpan({ name: 'GET users/[id]', origin: 'auto.http.browser' }, () => {

packages/replay/src/types/replay.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
ReplayRecordingData,
77
ReplayRecordingMode,
88
SentryWrappedXMLHttpRequest,
9-
Transaction,
9+
Span,
1010
XhrBreadcrumbHint,
1111
} from '@sentry/types';
1212

@@ -478,7 +478,7 @@ export interface ReplayContainer {
478478
session: Session | undefined;
479479
recordingMode: ReplayRecordingMode;
480480
timeouts: Timeouts;
481-
lastTransaction?: Transaction;
481+
lastActiveSpan?: Span;
482482
throttledAddEvent: (
483483
event: RecordingEvent,
484484
isCheckout?: boolean,

packages/replay/src/util/addGlobalListeners.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ export function addGlobalListeners(replay: ReplayContainer): void {
4444
}
4545
});
4646

47-
client.on('startTransaction', transaction => {
48-
replay.lastTransaction = transaction;
47+
client.on('spanStart', span => {
48+
replay.lastActiveSpan = span;
4949
});
5050

51-
// We may be missing the initial startTransaction due to timing issues,
51+
// We may be missing the initial spanStart due to timing issues,
5252
// so we capture it on finish again.
53-
client.on('finishTransaction', transaction => {
54-
replay.lastTransaction = transaction;
53+
client.on('spanEnd', span => {
54+
replay.lastActiveSpan = span;
5555
});
5656

5757
// We want to flush replay

packages/sveltekit/test/server/handle.test.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
33
addTracingExtensions,
4+
getRootSpan,
45
getSpanDescendants,
56
spanIsSampled,
67
spanToJSON,
@@ -124,8 +125,10 @@ describe('handleSentry', () => {
124125

125126
it("creates a transaction if there's no active span", async () => {
126127
let _span: Span | undefined = undefined;
127-
client.on('finishTransaction', (transaction: Transaction) => {
128-
_span = transaction;
128+
client.on('spanEnd', span => {
129+
if (span === getRootSpan(span)) {
130+
_span = span;
131+
}
129132
});
130133

131134
try {
@@ -150,9 +153,11 @@ describe('handleSentry', () => {
150153
it('creates a child span for nested server calls (i.e. if there is an active span)', async () => {
151154
let _span: Span | undefined = undefined;
152155
let txnCount = 0;
153-
client.on('finishTransaction', (transaction: Transaction) => {
154-
_span = transaction;
155-
++txnCount;
156+
client.on('spanEnd', span => {
157+
if (span === getRootSpan(span)) {
158+
_span = span;
159+
++txnCount;
160+
}
156161
});
157162

158163
try {
@@ -208,8 +213,10 @@ describe('handleSentry', () => {
208213
});
209214

210215
let _span: Span | undefined = undefined;
211-
client.on('finishTransaction', (transaction: Transaction) => {
212-
_span = transaction;
216+
client.on('spanEnd', span => {
217+
if (span === getRootSpan(span)) {
218+
_span = span;
219+
}
213220
});
214221

215222
try {
@@ -248,8 +255,10 @@ describe('handleSentry', () => {
248255
});
249256

250257
let _span: Span | undefined = undefined;
251-
client.on('finishTransaction', (transaction: Transaction) => {
252-
_span = transaction;
258+
client.on('spanEnd', span => {
259+
if (span === getRootSpan(span)) {
260+
_span = span;
261+
}
253262
});
254263

255264
try {
@@ -312,8 +321,10 @@ describe('handleSentry', () => {
312321

313322
it("doesn't create a transaction if there's no route", async () => {
314323
let _span: Span | undefined = undefined;
315-
client.on('finishTransaction', (transaction: Transaction) => {
316-
_span = transaction;
324+
client.on('spanEnd', span => {
325+
if (span === getRootSpan(span)) {
326+
_span = span;
327+
}
317328
});
318329

319330
try {
@@ -327,8 +338,10 @@ describe('handleSentry', () => {
327338

328339
it("Creates a transaction if there's no route but `handleUnknownRequests` is true", async () => {
329340
let _span: Span | undefined = undefined;
330-
client.on('finishTransaction', (transaction: Transaction) => {
331-
_span = transaction;
341+
client.on('spanEnd', span => {
342+
if (span === getRootSpan(span)) {
343+
_span = span;
344+
}
332345
});
333346

334347
try {

0 commit comments

Comments
 (0)