Skip to content

Commit 233d35e

Browse files
committed
feat(core): Deprecate StartSpanOptions.origin in favour of passing attribute
1 parent 6868256 commit 233d35e

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

packages/core/src/tracing/span.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ export class Span implements SpanInterface {
134134
// eslint-disable-next-line deprecation/deprecation
135135
this.instrumenter = spanContext.instrumenter || 'sentry';
136136

137-
this.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanContext.origin || 'manual');
137+
this.setAttribute(
138+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
139+
spanContext.origin || this._attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] || 'manual',
140+
);
138141

139142
// eslint-disable-next-line deprecation/deprecation
140143
this._name = spanContext.name || spanContext.description;

packages/core/src/tracing/trace.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { handleCallbackErrors } from '../utils/handleCallbackErrors';
2020
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
2121
import { spanTimeInputToSeconds, spanToJSON } from '../utils/spanUtils';
2222

23-
interface StartSpanOptions extends TransactionContext {
23+
interface StartSpanOptions extends Omit<TransactionContext, 'origin'> {
2424
/** A manually specified start time for the created `Span` object. */
2525
startTime?: SpanTimeInput;
2626

@@ -33,7 +33,11 @@ interface StartSpanOptions extends TransactionContext {
3333
/** An op for the span. This is a categorization for spans. */
3434
op?: string;
3535

36-
/** The origin of the span - if it comes from auto instrumenation or manual instrumentation. */
36+
/**
37+
* The origin of the span - if it comes from auto instrumentation or manual instrumentation.
38+
*
39+
* @deprecated Set `attributes['sentry.origin']` instead.
40+
*/
3741
origin?: SpanOrigin;
3842

3943
/** Attributes for the span. */
@@ -387,5 +391,17 @@ function normalizeContext(context: StartSpanOptions): TransactionContext {
387391
return ctx;
388392
}
389393

394+
// TODO (v8): Remove this normalization once we remove top-level context.origin
395+
// eslint-disable-next-line deprecation/deprecation
396+
if (context.origin) {
397+
context.attributes = {
398+
// eslint-disable-next-line deprecation/deprecation
399+
'sentry.origin': context.origin,
400+
...context.attributes, // potentially set attributes value takes precedence
401+
};
402+
// eslint-disable-next-line deprecation/deprecation
403+
delete context.origin;
404+
}
405+
390406
return context;
391407
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,40 @@ describe('startSpan', () => {
229229
expect(ref.spanRecorder.spans).toHaveLength(2);
230230
expect(spanToJSON(ref.spanRecorder.spans[1]).op).toEqual('db.query');
231231
});
232+
233+
it.each([
234+
{ origin: 'auto.http.browser' },
235+
{ attributes: { 'sentry.origin': 'auto.http.browser' } },
236+
// attribute should take precedence over top level origin
237+
{ origin: 'manual', attributes: { 'sentry.origin': 'auto.http.browser' } },
238+
])('correctly sets the span origin', async () => {
239+
let ref: any = undefined;
240+
client.on('finishTransaction', transaction => {
241+
ref = transaction;
242+
});
243+
try {
244+
await startSpan({ name: 'GET users/[id]', origin: 'auto.http.browser' }, () => {
245+
return callback();
246+
});
247+
} catch (e) {
248+
//
249+
}
250+
251+
const jsonSpan = spanToJSON(ref);
252+
expect(jsonSpan).toEqual({
253+
data: {
254+
'sentry.origin': 'auto.http.browser',
255+
'sentry.sample_rate': 0,
256+
},
257+
origin: 'auto.http.browser',
258+
description: 'GET users/[id]',
259+
span_id: expect.any(String),
260+
start_timestamp: expect.any(Number),
261+
status: isError ? 'internal_error' : undefined,
262+
timestamp: expect.any(Number),
263+
trace_id: expect.any(String),
264+
});
265+
});
232266
});
233267

234268
it('creates & finishes span', async () => {

packages/types/src/span.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ export type SpanAttributeValue =
2323
| Array<null | undefined | number>
2424
| Array<null | undefined | boolean>;
2525

26-
export type SpanAttributes = Record<string, SpanAttributeValue | undefined>;
26+
export type SpanAttributes = {
27+
'sentry.origin': string;
28+
'sentry.op': string;
29+
'sentry.source': string;
30+
'sentry.sample_rate': number;
31+
} & Record<string, SpanAttributeValue | undefined>;
2732

2833
/** This type is aligned with the OpenTelemetry TimeInput type. */
2934
export type SpanTimeInput = HrTime | number | Date;

0 commit comments

Comments
 (0)