Skip to content

Commit dd240f7

Browse files
committed
feat(core): Deprecate Span.op in favor of op attribute
1 parent 0bcf0fb commit dd240f7

File tree

12 files changed

+62
-13
lines changed

12 files changed

+62
-13
lines changed

packages/core/src/semanticAttributes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ export const SEMANTIC_ATTRIBUTE_SENTRY_SOURCE = 'sentry.source';
99
* Use this attribute to represent the sample rate used for a span.
1010
*/
1111
export const SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE = 'sentry.sample_rate';
12+
13+
/**
14+
* Use this attribute to represent the operation of a span.
15+
*/
16+
export const SEMANTIC_ATTRIBUTE_SENTRY_OP = 'sentry.op';

packages/core/src/tracing/idletransaction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,14 @@ export class IdleTransaction extends Transaction {
148148
this._finished = true;
149149
this.activities = {};
150150

151+
// eslint-disable-next-line deprecation/deprecation
151152
if (this.op === 'ui.action.click') {
152153
this.setAttribute(FINISH_REASON_TAG, this._finishReason);
153154
}
154155

155156
if (this.spanRecorder) {
156157
DEBUG_BUILD &&
158+
// eslint-disable-next-line deprecation/deprecation
157159
logger.log('[Tracing] finishing IdleTransaction', new Date(endTimestampInS * 1000).toISOString(), this.op);
158160

159161
for (const callback of this._beforeFinishCallbacks) {

packages/core/src/tracing/sampling.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export function sampleTransaction<T extends Transaction>(
9595
}
9696

9797
DEBUG_BUILD &&
98+
// eslint-disable-next-line deprecation/deprecation
9899
logger.log(`[Tracing] starting ${transaction.op} transaction - ${spanToJSON(transaction).description}`);
99100
return transaction;
100101
}

packages/core/src/tracing/span.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
import { dropUndefinedKeys, logger, timestampInSeconds, uuid4 } from '@sentry/utils';
1717

1818
import { DEBUG_BUILD } from '../debug-build';
19+
import { SEMANTIC_ATTRIBUTE_SENTRY_OP } from '../semanticAttributes';
1920
import { getRootSpan } from '../utils/getRootSpan';
2021
import {
2122
TRACE_FLAG_NONE,
@@ -71,11 +72,6 @@ export class Span implements SpanInterface {
7172
*/
7273
public status?: SpanStatusType | string;
7374

74-
/**
75-
* @inheritDoc
76-
*/
77-
public op?: string;
78-
7975
/**
8076
* Tags for the span.
8177
* @deprecated Use `getSpanAttributes(span)` instead.
@@ -158,7 +154,7 @@ export class Span implements SpanInterface {
158154
this._sampled = spanContext.sampled;
159155
}
160156
if (spanContext.op) {
161-
this.op = spanContext.op;
157+
this.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, spanContext.op);
162158
}
163159
if (spanContext.status) {
164160
this.status = spanContext.status;
@@ -299,6 +295,25 @@ export class Span implements SpanInterface {
299295
this._endTime = endTime;
300296
}
301297

298+
/**
299+
* Operation of the span
300+
*
301+
* @deprecated Use `spanToJSON().op` to read the op instead.
302+
*/
303+
public get op(): string | undefined {
304+
return this._attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] as string | undefined;
305+
}
306+
307+
/**
308+
* Operation of the span
309+
*
310+
* @deprecated Use `startSpan()` functions to set or `span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'op')
311+
* to update the span instead.
312+
*/
313+
public set op(op: string | undefined) {
314+
this.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, op);
315+
}
316+
302317
/* eslint-enable @typescript-eslint/member-ordering */
303318

304319
/** @inheritdoc */
@@ -491,6 +506,7 @@ export class Span implements SpanInterface {
491506
data: this._getData(),
492507
description: this._name,
493508
endTimestamp: this._endTime,
509+
// eslint-disable-next-line deprecation/deprecation
494510
op: this.op,
495511
parentSpanId: this.parentSpanId,
496512
sampled: this._sampled,
@@ -514,6 +530,7 @@ export class Span implements SpanInterface {
514530
// eslint-disable-next-line deprecation/deprecation
515531
this._name = spanContext.name || spanContext.description;
516532
this._endTime = spanContext.endTimestamp;
533+
// eslint-disable-next-line deprecation/deprecation
517534
this.op = spanContext.op;
518535
this.parentSpanId = spanContext.parentSpanId;
519536
this._sampled = spanContext.sampled;
@@ -548,7 +565,7 @@ export class Span implements SpanInterface {
548565
return dropUndefinedKeys({
549566
data: this._getData(),
550567
description: this._name,
551-
op: this.op,
568+
op: this._attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] as string | undefined,
552569
parent_span_id: this.parentSpanId,
553570
span_id: this._spanId,
554571
start_timestamp: this._startTime,

packages/core/src/tracing/transaction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
338338
transaction.measurements = this._measurements;
339339
}
340340

341+
// eslint-disable-next-line deprecation/deprecation
341342
DEBUG_BUILD && logger.log(`[Tracing] Finishing ${this.op} transaction: ${this._name}.`);
342343

343344
return transaction;

packages/core/test/lib/utils/spanUtils.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ describe('spanToJSON', () => {
8787
origin: 'auto',
8888
start_timestamp: 123,
8989
timestamp: 456,
90+
data: {
91+
'sentry.op': 'test op',
92+
},
9093
});
9194
});
9295

packages/node/test/integrations/http.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe('tracing', () => {
7979
// our span is at index 1 because the transaction itself is at index 0
8080
expect(spanToJSON(spans[1]).description).toEqual('GET http://dogs.are.great/');
8181
expect(spans[1].op).toEqual('http.client');
82+
expect(spanToJSON(spans[1]).op).toEqual('http.client');
8283
});
8384

8485
it("doesn't create a span for outgoing sentry requests", () => {
@@ -280,7 +281,9 @@ describe('tracing', () => {
280281

281282
// our span is at index 1 because the transaction itself is at index 0
282283
expect(spanToJSON(spans[1]).description).toEqual('GET http://dogs.are.great/spaniel');
284+
// eslint-disable-next-line deprecation/deprecation
283285
expect(spans[1].op).toEqual('http.client');
286+
expect(spanToJSON(spans[1]).op).toEqual('http.client');
284287

285288
const spanAttributes = spanToJSON(spans[1]).data || {};
286289

@@ -304,7 +307,9 @@ describe('tracing', () => {
304307

305308
// our span is at index 1 because the transaction itself is at index 0
306309
expect(spanToJSON(spans[1]).description).toEqual('GET http://dogs.are.great/spaniel');
310+
// eslint-disable-next-line deprecation/deprecation
307311
expect(spans[1].op).toEqual('http.client');
312+
expect(spanToJSON(spans[1]).op).toEqual('http.client');
308313
expect(spanAttributes['http.method']).toEqual('GET');
309314
expect(spanAttributes.url).toEqual('http://dogs.are.great/spaniel');
310315
expect(spanAttributes['http.query']).toEqual('tail=wag&cute=true');
@@ -385,6 +390,7 @@ describe('tracing', () => {
385390
const request = http.get(url);
386391

387392
// There should be no http spans
393+
// eslint-disable-next-line deprecation/deprecation
388394
const httpSpans = spans.filter(span => span.op?.startsWith('http'));
389395
expect(httpSpans.length).toBe(0);
390396

@@ -490,6 +496,7 @@ describe('tracing', () => {
490496
const request = http.get(url);
491497

492498
// There should be no http spans
499+
// eslint-disable-next-line deprecation/deprecation
493500
const httpSpans = spans.filter(span => span.op?.startsWith('http'));
494501
expect(httpSpans.length).toBe(0);
495502

packages/opentelemetry-node/src/spanprocessor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SpanKind, context, trace } from '@opentelemetry/api';
33
import { suppressTracing } from '@opentelemetry/core';
44
import type { Span as OtelSpan, SpanProcessor as OtelSpanProcessor } from '@opentelemetry/sdk-trace-base';
55
import {
6+
SEMANTIC_ATTRIBUTE_SENTRY_OP,
67
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
78
Transaction,
89
addEventProcessor,
@@ -220,7 +221,7 @@ function updateTransactionWithOtelData(transaction: Transaction, otelSpan: OtelS
220221

221222
transaction.setStatus(mapOtelStatus(otelSpan));
222223

223-
transaction.op = op;
224+
transaction.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, op);
224225
transaction.updateName(description);
225226
transaction.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
226227
}

packages/opentelemetry-node/test/spanprocessor.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,10 @@ describe('SentrySpanProcessor', () => {
770770
parentOtelSpan.setAttribute(SemanticAttributes.FAAS_TRIGGER, 'test faas trigger');
771771
parentOtelSpan.end();
772772

773+
// eslint-disable-next-line deprecation/deprecation
773774
expect(transaction.op).toBe('test faas trigger');
775+
expect(spanToJSON(transaction).op).toBe('test faas trigger');
776+
774777
expect(spanToJSON(transaction).description).toBe('test operation');
775778
});
776779
});

packages/tracing-internal/src/browser/backgroundtab.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { IdleTransaction, SpanStatusType } from '@sentry/core';
22
import { getActiveTransaction } from '@sentry/core';
33
import { logger } from '@sentry/utils';
44

5+
import { spanToJSON } from '@sentry/core';
56
import { DEBUG_BUILD } from '../common/debug-build';
67
import { WINDOW } from './types';
78

@@ -19,7 +20,9 @@ export function registerBackgroundTabDetection(): void {
1920

2021
DEBUG_BUILD &&
2122
logger.log(
22-
`[Tracing] Transaction: ${statusType} -> since tab moved to the background, op: ${activeTransaction.op}`,
23+
`[Tracing] Transaction: ${statusType} -> since tab moved to the background, op: ${
24+
spanToJSON(activeTransaction).op
25+
}`,
2326
);
2427
// We should not set status if it is already set, this prevent important statuses like
2528
// error or data loss from being overwritten on transaction.

packages/tracing-internal/src/browser/metrics/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable max-lines */
22
import type { IdleTransaction, Transaction } from '@sentry/core';
3-
import { spanToJSON } from '@sentry/core';
43
import { getActiveTransaction, setMeasurement } from '@sentry/core';
54
import type { Measurements, SpanContext } from '@sentry/types';
65
import { browserPerformanceTimeOrigin, getComponentName, htmlTreeAsString, logger } from '@sentry/utils';
76

7+
import { spanToJSON } from '@sentry/core';
88
import { DEBUG_BUILD } from '../../common/debug-build';
99
import {
1010
addClsInstrumentationHandler,
@@ -186,7 +186,7 @@ export function addPerformanceEntries(transaction: Transaction): void {
186186
let responseStartTimestamp: number | undefined;
187187
let requestStartTimestamp: number | undefined;
188188

189-
const transactionStartTime = spanToJSON(transaction).start_timestamp;
189+
const { op, start_timestamp: transactionStartTime } = spanToJSON(transaction);
190190

191191
// eslint-disable-next-line @typescript-eslint/no-explicit-any
192192
performanceEntries.slice(_performanceCursor).forEach((entry: Record<string, any>) => {
@@ -239,7 +239,7 @@ export function addPerformanceEntries(transaction: Transaction): void {
239239
_trackNavigator(transaction);
240240

241241
// Measurements are only available for pageload transactions
242-
if (transaction.op === 'pageload') {
242+
if (op === 'pageload') {
243243
// Generate TTFB (Time to First Byte), which measured as the time between the beginning of the transaction and the
244244
// start of the response in milliseconds
245245
if (typeof responseStartTimestamp === 'number' && transactionStartTime) {

packages/tracing/test/span.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ describe('Span', () => {
484484
startTimestamp: expect.any(Number),
485485
tags: {},
486486
traceId: expect.any(String),
487+
data: {
488+
'sentry.op': 'op',
489+
},
487490
});
488491
});
489492

@@ -535,6 +538,9 @@ describe('Span', () => {
535538
tags: {
536539
tag1: 'bye',
537540
},
541+
data: {
542+
...span.toContext().data,
543+
},
538544
};
539545

540546
if (newContext.data) newContext.data.data1 = 'bar';
@@ -548,7 +554,7 @@ describe('Span', () => {
548554
expect(span.op).toBe('new-op');
549555
expect(span.sampled).toBe(true);
550556
expect(span.tags).toStrictEqual({ tag1: 'bye' });
551-
expect(span.data).toStrictEqual({ data0: 'foo', data1: 'bar' });
557+
expect(span.data).toStrictEqual({ data0: 'foo', data1: 'bar', 'sentry.op': 'op' });
552558
});
553559
});
554560

0 commit comments

Comments
 (0)