Skip to content

Commit e403bd5

Browse files
committed
feat(tracing): Add span origin to trace context
1 parent 9a94e23 commit e403bd5

File tree

9 files changed

+32
-86
lines changed

9 files changed

+32
-86
lines changed

packages/core/src/tracing/span.ts

+1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ export class Span implements SpanInterface {
332332
status: this.status,
333333
tags: Object.keys(this.tags).length > 0 ? this.tags : undefined,
334334
trace_id: this.traceId,
335+
origin: this.origin,
335336
});
336337
}
337338

packages/node-experimental/test/integration/scope.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ describe('Integration | Scope', () => {
9292
span_id: spanId,
9393
status: 'ok',
9494
trace_id: traceId,
95+
origin: 'manual',
9596
},
9697
}),
9798
spans: [],

packages/node-experimental/test/integration/transactions.test.ts

+12-48
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,18 @@ describe('Integration | Transactions', () => {
3737
metadata: { requestPath: 'test-path' },
3838
},
3939
span => {
40-
if (!span) {
41-
return;
42-
}
43-
4440
Sentry.addBreadcrumb({ message: 'test breadcrumb 2', timestamp: 123456 });
4541

4642
span.setAttributes({
4743
'test.outer': 'test value',
4844
});
4945

5046
const subSpan = Sentry.startInactiveSpan({ name: 'inner span 1' });
51-
subSpan?.end();
47+
subSpan.end();
5248

5349
Sentry.setTag('test.tag', 'test value');
5450

5551
Sentry.startSpan({ name: 'inner span 2' }, innerSpan => {
56-
if (!innerSpan) {
57-
return;
58-
}
59-
6052
Sentry.addBreadcrumb({ message: 'test breadcrumb 3', timestamp: 123456 });
6153

6254
innerSpan.setAttributes({
@@ -97,6 +89,7 @@ describe('Integration | Transactions', () => {
9789
span_id: expect.any(String),
9890
status: 'ok',
9991
trace_id: expect.any(String),
92+
origin: 'auto.test',
10093
},
10194
},
10295
environment: 'production',
@@ -189,26 +182,18 @@ describe('Integration | Transactions', () => {
189182
Sentry.addBreadcrumb({ message: 'test breadcrumb 1', timestamp: 123456 });
190183

191184
Sentry.startSpan({ op: 'test op', name: 'test name', source: 'task', origin: 'auto.test' }, span => {
192-
if (!span) {
193-
return;
194-
}
195-
196185
Sentry.addBreadcrumb({ message: 'test breadcrumb 2', timestamp: 123456 });
197186

198187
span.setAttributes({
199188
'test.outer': 'test value',
200189
});
201190

202191
const subSpan = Sentry.startInactiveSpan({ name: 'inner span 1' });
203-
subSpan?.end();
192+
subSpan.end();
204193

205194
Sentry.setTag('test.tag', 'test value');
206195

207196
Sentry.startSpan({ name: 'inner span 2' }, innerSpan => {
208-
if (!innerSpan) {
209-
return;
210-
}
211-
212197
Sentry.addBreadcrumb({ message: 'test breadcrumb 3', timestamp: 123456 });
213198

214199
innerSpan.setAttributes({
@@ -218,26 +203,18 @@ describe('Integration | Transactions', () => {
218203
});
219204

220205
Sentry.startSpan({ op: 'test op b', name: 'test name b' }, span => {
221-
if (!span) {
222-
return;
223-
}
224-
225206
Sentry.addBreadcrumb({ message: 'test breadcrumb 2b', timestamp: 123456 });
226207

227208
span.setAttributes({
228209
'test.outer': 'test value b',
229210
});
230211

231212
const subSpan = Sentry.startInactiveSpan({ name: 'inner span 1b' });
232-
subSpan?.end();
213+
subSpan.end();
233214

234215
Sentry.setTag('test.tag', 'test value b');
235216

236217
Sentry.startSpan({ name: 'inner span 2b' }, innerSpan => {
237-
if (!innerSpan) {
238-
return;
239-
}
240-
241218
Sentry.addBreadcrumb({ message: 'test breadcrumb 3b', timestamp: 123456 });
242219

243220
innerSpan.setAttributes({
@@ -268,6 +245,7 @@ describe('Integration | Transactions', () => {
268245
span_id: expect.any(String),
269246
status: 'ok',
270247
trace_id: expect.any(String),
248+
origin: 'auto.test',
271249
},
272250
}),
273251
spans: [
@@ -309,6 +287,7 @@ describe('Integration | Transactions', () => {
309287
span_id: expect.any(String),
310288
status: 'ok',
311289
trace_id: expect.any(String),
290+
origin: 'manual',
312291
},
313292
}),
314293
spans: [
@@ -362,19 +341,11 @@ describe('Integration | Transactions', () => {
362341
context.with(
363342
trace.setSpanContext(setPropagationContextOnContext(context.active(), propagationContext), spanContext),
364343
() => {
365-
Sentry.startSpan({ op: 'test op', name: 'test name', source: 'task', origin: 'auto.test' }, span => {
366-
if (!span) {
367-
return;
368-
}
369-
344+
Sentry.startSpan({ op: 'test op', name: 'test name', source: 'task', origin: 'auto.test' }, () => {
370345
const subSpan = Sentry.startInactiveSpan({ name: 'inner span 1' });
371-
subSpan?.end();
346+
subSpan.end();
372347

373-
Sentry.startSpan({ name: 'inner span 2' }, innerSpan => {
374-
if (!innerSpan) {
375-
return;
376-
}
377-
});
348+
Sentry.startSpan({ name: 'inner span 2' }, () => {});
378349
});
379350
},
380351
);
@@ -395,6 +366,7 @@ describe('Integration | Transactions', () => {
395366
parent_span_id: parentSpanId,
396367
status: 'ok',
397368
trace_id: traceId,
369+
origin: 'auto.test',
398370
},
399371
}),
400372
// spans are circular (they have a reference to the transaction), which leads to jest choking on this
@@ -481,20 +453,12 @@ describe('Integration | Transactions', () => {
481453
let innerSpan1Id: string | undefined;
482454
let innerSpan2Id: string | undefined;
483455

484-
void Sentry.startSpan({ name: 'test name' }, async span => {
485-
if (!span) {
486-
return;
487-
}
488-
456+
void Sentry.startSpan({ name: 'test name' }, async () => {
489457
const subSpan = Sentry.startInactiveSpan({ name: 'inner span 1' });
490458
innerSpan1Id = subSpan?.spanContext().spanId;
491-
subSpan?.end();
459+
subSpan.end();
492460

493461
Sentry.startSpan({ name: 'inner span 2' }, innerSpan => {
494-
if (!innerSpan) {
495-
return;
496-
}
497-
498462
innerSpan2Id = innerSpan.spanContext().spanId;
499463
});
500464

packages/opentelemetry/test/custom/transaction.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('NodeExperimentalTransaction', () => {
2828
trace: {
2929
span_id: expect.any(String),
3030
trace_id: expect.any(String),
31+
origin: 'manual',
3132
},
3233
},
3334
spans: [],
@@ -106,6 +107,7 @@ describe('NodeExperimentalTransaction', () => {
106107
trace: {
107108
span_id: expect.any(String),
108109
trace_id: expect.any(String),
110+
origin: 'manual',
109111
},
110112
},
111113
spans: [],

packages/opentelemetry/test/integration/scope.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ describe('Integration | Scope', () => {
9898
span_id: spanId,
9999
status: 'ok',
100100
trace_id: traceId,
101+
origin: 'manual',
101102
},
102103
}),
103104

packages/opentelemetry/test/integration/transactions.test.ts

+10-38
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,18 @@ describe('Integration | Transactions', () => {
3737
metadata: { requestPath: 'test-path' },
3838
},
3939
span => {
40-
if (!span) {
41-
return;
42-
}
43-
4440
addBreadcrumb({ message: 'test breadcrumb 2', timestamp: 123456 });
4541

4642
span.setAttributes({
4743
'test.outer': 'test value',
4844
});
4945

5046
const subSpan = startInactiveSpan({ name: 'inner span 1' });
51-
subSpan?.end();
47+
subSpan.end();
5248

5349
setTag('test.tag', 'test value');
5450

5551
startSpan({ name: 'inner span 2' }, innerSpan => {
56-
if (!innerSpan) {
57-
return;
58-
}
59-
6052
addBreadcrumb({ message: 'test breadcrumb 3', timestamp: 123456 });
6153

6254
innerSpan.setAttributes({
@@ -96,6 +88,7 @@ describe('Integration | Transactions', () => {
9688
span_id: expect.any(String),
9789
status: 'ok',
9890
trace_id: expect.any(String),
91+
origin: 'auto.test',
9992
},
10093
},
10194
environment: 'production',
@@ -186,26 +179,18 @@ describe('Integration | Transactions', () => {
186179
addBreadcrumb({ message: 'test breadcrumb 1', timestamp: 123456 });
187180

188181
startSpan({ op: 'test op', name: 'test name', source: 'task', origin: 'auto.test' }, span => {
189-
if (!span) {
190-
return;
191-
}
192-
193182
addBreadcrumb({ message: 'test breadcrumb 2', timestamp: 123456 });
194183

195184
span.setAttributes({
196185
'test.outer': 'test value',
197186
});
198187

199188
const subSpan = startInactiveSpan({ name: 'inner span 1' });
200-
subSpan?.end();
189+
subSpan.end();
201190

202191
setTag('test.tag', 'test value');
203192

204193
startSpan({ name: 'inner span 2' }, innerSpan => {
205-
if (!innerSpan) {
206-
return;
207-
}
208-
209194
addBreadcrumb({ message: 'test breadcrumb 3', timestamp: 123456 });
210195

211196
innerSpan.setAttributes({
@@ -215,26 +200,18 @@ describe('Integration | Transactions', () => {
215200
});
216201

217202
startSpan({ op: 'test op b', name: 'test name b' }, span => {
218-
if (!span) {
219-
return;
220-
}
221-
222203
addBreadcrumb({ message: 'test breadcrumb 2b', timestamp: 123456 });
223204

224205
span.setAttributes({
225206
'test.outer': 'test value b',
226207
});
227208

228209
const subSpan = startInactiveSpan({ name: 'inner span 1b' });
229-
subSpan?.end();
210+
subSpan.end();
230211

231212
setTag('test.tag', 'test value b');
232213

233214
startSpan({ name: 'inner span 2b' }, innerSpan => {
234-
if (!innerSpan) {
235-
return;
236-
}
237-
238215
addBreadcrumb({ message: 'test breadcrumb 3b', timestamp: 123456 });
239216

240217
innerSpan.setAttributes({
@@ -265,6 +242,7 @@ describe('Integration | Transactions', () => {
265242
span_id: expect.any(String),
266243
status: 'ok',
267244
trace_id: expect.any(String),
245+
origin: 'auto.test',
268246
},
269247
}),
270248
spans: [
@@ -306,6 +284,7 @@ describe('Integration | Transactions', () => {
306284
span_id: expect.any(String),
307285
status: 'ok',
308286
trace_id: expect.any(String),
287+
origin: 'manual',
309288
},
310289
}),
311290
spans: [
@@ -359,19 +338,11 @@ describe('Integration | Transactions', () => {
359338
context.with(
360339
trace.setSpanContext(setPropagationContextOnContext(context.active(), propagationContext), spanContext),
361340
() => {
362-
startSpan({ op: 'test op', name: 'test name', source: 'task', origin: 'auto.test' }, span => {
363-
if (!span) {
364-
return;
365-
}
366-
341+
startSpan({ op: 'test op', name: 'test name', source: 'task', origin: 'auto.test' }, () => {
367342
const subSpan = startInactiveSpan({ name: 'inner span 1' });
368-
subSpan?.end();
343+
subSpan.end();
369344

370-
startSpan({ name: 'inner span 2' }, innerSpan => {
371-
if (!innerSpan) {
372-
return;
373-
}
374-
});
345+
startSpan({ name: 'inner span 2' }, () => {});
375346
});
376347
},
377348
);
@@ -392,6 +363,7 @@ describe('Integration | Transactions', () => {
392363
parent_span_id: parentSpanId,
393364
status: 'ok',
394365
trace_id: traceId,
366+
origin: 'auto.test',
395367
},
396368
}),
397369
// spans are circular (they have a reference to the transaction), which leads to jest choking on this

packages/tracing/test/span.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ describe('Span', () => {
327327
expect(context).toStrictEqual({
328328
span_id: 'd',
329329
trace_id: 'c',
330+
origin: 'manual',
330331
});
331332
});
332333
});

packages/tracing/test/transaction.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ describe('`Transaction` class', () => {
153153
trace: {
154154
span_id: transaction.spanId,
155155
trace_id: transaction.traceId,
156+
origin: 'manual',
156157
},
157158
},
158159
}),
@@ -179,6 +180,7 @@ describe('`Transaction` class', () => {
179180
trace: {
180181
span_id: transaction.spanId,
181182
trace_id: transaction.traceId,
183+
origin: 'manual',
182184
},
183185
},
184186
}),

packages/types/src/context.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Primitive } from './misc';
2+
import { SpanOrigin } from './span';
23

34
export type Context = Record<string, unknown>;
45

@@ -101,6 +102,7 @@ export interface TraceContext extends Record<string, unknown> {
101102
status?: string;
102103
tags?: { [key: string]: Primitive };
103104
trace_id: string;
105+
origin?: SpanOrigin;
104106
}
105107

106108
export interface CloudResourceContext extends Record<string, unknown> {

0 commit comments

Comments
 (0)