Skip to content

Commit 6259d23

Browse files
authored
fix: Dont require transaction to be on the scope to be delivered correctly (#2353)
* fix: Dont require transaction to be on the scope to be delivered correctly * test: Added tests for contexts.trace
1 parent bbd81b6 commit 6259d23

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

5-
Coming soon...
5+
- [apm] fix: Always attach `contexts.trace` to finished transaction (#2353)
66

77
- [integrations] fix: Make RewriteFrame integration process all exceptions (#2362)
88

packages/apm/src/span.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ export class Span implements SpanInterface, SpanContext {
287287
}
288288

289289
return this._hub.captureEvent({
290+
contexts: {
291+
trace: this.getTraceContext(),
292+
},
290293
spans: finishedSpans,
291294
start_timestamp: this.startTimestamp,
292295
tags: this.tags,

packages/apm/test/span.test.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,31 +164,48 @@ describe('Span', () => {
164164
expect(span.timestamp).toBeGreaterThan(1);
165165
});
166166

167-
test('finish a scope span without transaction', () => {
167+
test('finish a span without transaction', () => {
168168
const spy = jest.spyOn(hub as any, 'captureEvent');
169169
const span = new Span({}, hub);
170170
span.finish();
171171
expect(spy).not.toHaveBeenCalled();
172172
});
173173

174-
test('finish a scope span with transaction', () => {
174+
test('finish a span with transaction', () => {
175175
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
176176
const span = new Span({ transaction: 'test', sampled: false }, hub);
177177
span.initFinishedSpans();
178178
span.finish();
179-
expect(spy).toHaveBeenCalled();
180179
expect(spy.mock.calls[0][0].spans).toHaveLength(0);
180+
expect(spy.mock.calls[0][0].contexts.trace).toEqual(span.getTraceContext());
181181
});
182182

183-
test('finish a scope span with transaction + child span', () => {
183+
test('finish a span with transaction + child span', () => {
184184
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
185185
const parentSpan = new Span({ transaction: 'test', sampled: false }, hub);
186186
parentSpan.initFinishedSpans();
187187
const childSpan = parentSpan.child();
188188
childSpan.finish();
189189
parentSpan.finish();
190-
expect(spy).toHaveBeenCalled();
191190
expect(spy.mock.calls[0][0].spans).toHaveLength(1);
191+
expect(spy.mock.calls[0][0].contexts.trace).toEqual(parentSpan.getTraceContext());
192+
});
193+
194+
test('finish a span with another one on the scope shouldnt override contexts.trace', () => {
195+
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
196+
197+
const spanOne = new Span({ transaction: 'testOne', sampled: false }, hub);
198+
spanOne.initFinishedSpans();
199+
const childSpanOne = spanOne.child();
200+
childSpanOne.finish();
201+
hub.configureScope((scope) => { scope.setSpan(spanOne) })
202+
203+
const spanTwo = new Span({ transaction: 'testTwo', sampled: false }, hub);
204+
spanTwo.initFinishedSpans();
205+
spanTwo.finish();
206+
207+
expect(spy.mock.calls[0][0].spans).toHaveLength(0);
208+
expect(spy.mock.calls[0][0].contexts.trace).toEqual(spanTwo.getTraceContext());
192209
});
193210
});
194211

packages/hub/src/scope.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,6 @@ export class Scope implements ScopeInterface {
327327
if (this._transaction) {
328328
event.transaction = this._transaction;
329329
}
330-
if (this._span) {
331-
event.contexts = event.contexts || {};
332-
event.contexts.trace = this._span.getTraceContext();
333-
}
334330

335331
this._applyFingerprint(event);
336332

0 commit comments

Comments
 (0)