Skip to content

Commit cf75ffe

Browse files
authored
feat(core): Update span performance API names (#8971)
As per the new changes in RFC 101 in getsentry/rfcs#113, update the span performance API names. - `startActiveSpan` -> `startSpan` - `startSpan` -> `startInactiveSpan` https://github.com/getsentry/rfcs/blob/main/text/0101-revamping-the-sdk-performance-api.md `startActiveSpan` is deprecated, while `startInactiveSpan` is being introduced. The breaking change is that `startSpan` is being changed, but considering that basically no-one is using the `startSpan` API, we should be fine to break here for correctness reasons. Better break now than to have everyone refactor their code in v8.
1 parent eafe791 commit cf75ffe

File tree

9 files changed

+60
-40
lines changed

9 files changed

+60
-40
lines changed

packages/browser/src/exports.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export {
3737
makeMain,
3838
Scope,
3939
startTransaction,
40+
getActiveSpan,
41+
startSpan,
42+
startInactiveSpan,
4043
SDK_VERSION,
4144
setContext,
4245
setExtra,

packages/core/src/tracing/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export { extractTraceparentData, getActiveTransaction } from './utils';
66
// eslint-disable-next-line deprecation/deprecation
77
export { SpanStatus } from './spanstatus';
88
export type { SpanStatusType } from './span';
9-
export { trace, getActiveSpan, startActiveSpan, startSpan } from './trace';
9+
// eslint-disable-next-line deprecation/deprecation
10+
export { trace, getActiveSpan, startSpan, startInactiveSpan, startActiveSpan } from './trace';
1011
export { getDynamicSamplingContextFromClient } from './dynamicSamplingContext';
1112
export { setMeasurement } from './measurement';

packages/core/src/tracing/trace.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ export function trace<T>(
3434

3535
const parentSpan = scope.getSpan();
3636

37-
function startActiveSpan(): Span | undefined {
37+
function createChildSpanOrTransaction(): Span | undefined {
3838
if (!hasTracingEnabled()) {
3939
return undefined;
4040
}
4141
return parentSpan ? parentSpan.startChild(ctx) : hub.startTransaction(ctx);
4242
}
4343

44-
const activeSpan = startActiveSpan();
44+
const activeSpan = createChildSpanOrTransaction();
4545
scope.setSpan(activeSpan);
4646

4747
function finishAndSetSpan(): void {
@@ -82,13 +82,13 @@ export function trace<T>(
8282
* The created span is the active span and will be used as parent by other spans created inside the function
8383
* and can be accessed via `Sentry.getSpan()`, as long as the function is executed while the scope is active.
8484
*
85-
* If you want to create a span that is not set as active, use {@link startSpan}.
85+
* If you want to create a span that is not set as active, use {@link startInactiveSpan}.
8686
*
8787
* Note that if you have not enabled tracing extensions via `addTracingExtensions`
8888
* or you didn't set `tracesSampleRate`, this function will not generate spans
8989
* and the `span` returned from the callback will be undefined.
9090
*/
91-
export function startActiveSpan<T>(context: TransactionContext, callback: (span: Span | undefined) => T): T {
91+
export function startSpan<T>(context: TransactionContext, callback: (span: Span | undefined) => T): T {
9292
const ctx = { ...context };
9393
// If a name is set and a description is not, set the description to the name.
9494
if (ctx.name !== undefined && ctx.description === undefined) {
@@ -100,14 +100,14 @@ export function startActiveSpan<T>(context: TransactionContext, callback: (span:
100100

101101
const parentSpan = scope.getSpan();
102102

103-
function startActiveSpan(): Span | undefined {
103+
function createChildSpanOrTransaction(): Span | undefined {
104104
if (!hasTracingEnabled()) {
105105
return undefined;
106106
}
107107
return parentSpan ? parentSpan.startChild(ctx) : hub.startTransaction(ctx);
108108
}
109109

110-
const activeSpan = startActiveSpan();
110+
const activeSpan = createChildSpanOrTransaction();
111111
scope.setSpan(activeSpan);
112112

113113
function finishAndSetSpan(): void {
@@ -141,17 +141,22 @@ export function startActiveSpan<T>(context: TransactionContext, callback: (span:
141141
return maybePromiseResult;
142142
}
143143

144+
/**
145+
* @deprecated Use {@link startSpan} instead.
146+
*/
147+
export const startActiveSpan = startSpan;
148+
144149
/**
145150
* Creates a span. This span is not set as active, so will not get automatic instrumentation spans
146151
* as children or be able to be accessed via `Sentry.getSpan()`.
147152
*
148-
* If you want to create a span that is set as active, use {@link startActiveSpan}.
153+
* If you want to create a span that is set as active, use {@link startSpan}.
149154
*
150155
* Note that if you have not enabled tracing extensions via `addTracingExtensions`
151156
* or you didn't set `tracesSampleRate` or `tracesSampler`, this function will not generate spans
152157
* and the `span` returned from the callback will be undefined.
153158
*/
154-
export function startSpan(context: TransactionContext): Span | undefined {
159+
export function startInactiveSpan(context: TransactionContext): Span | undefined {
155160
if (!hasTracingEnabled()) {
156161
return undefined;
157162
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addTracingExtensions, Hub, makeMain } from '../../../src';
2-
import { startActiveSpan } from '../../../src/tracing';
2+
import { startSpan } from '../../../src/tracing';
33
import { getDefaultTestClientOptions, TestClient } from '../../mocks/client';
44

55
beforeAll(() => {
@@ -14,7 +14,7 @@ const enum Type {
1414
let hub: Hub;
1515
let client: TestClient;
1616

17-
describe('startActiveSpan', () => {
17+
describe('startSpan', () => {
1818
beforeEach(() => {
1919
const options = getDefaultTestClientOptions({ tracesSampleRate: 0.0 });
2020
client = new TestClient(options);
@@ -38,7 +38,7 @@ describe('startActiveSpan', () => {
3838
])('with %s callback and error %s', (_type, isError, callback, expected) => {
3939
it('should return the same value as the callback', async () => {
4040
try {
41-
const result = await startActiveSpan({ name: 'GET users/[id]' }, () => {
41+
const result = await startSpan({ name: 'GET users/[id]' }, () => {
4242
return callback();
4343
});
4444
expect(result).toEqual(expected);
@@ -53,7 +53,7 @@ describe('startActiveSpan', () => {
5353
// if tracingExtensions are not enabled
5454
jest.spyOn(hub, 'startTransaction').mockReturnValue(undefined);
5555
try {
56-
const result = await startActiveSpan({ name: 'GET users/[id]' }, () => {
56+
const result = await startSpan({ name: 'GET users/[id]' }, () => {
5757
return callback();
5858
});
5959
expect(result).toEqual(expected);
@@ -68,7 +68,7 @@ describe('startActiveSpan', () => {
6868
ref = transaction;
6969
});
7070
try {
71-
await startActiveSpan({ name: 'GET users/[id]' }, () => {
71+
await startSpan({ name: 'GET users/[id]' }, () => {
7272
return callback();
7373
});
7474
} catch (e) {
@@ -86,7 +86,7 @@ describe('startActiveSpan', () => {
8686
ref = transaction;
8787
});
8888
try {
89-
await startActiveSpan(
89+
await startSpan(
9090
{
9191
name: 'GET users/[id]',
9292
parentSampled: true,
@@ -113,7 +113,7 @@ describe('startActiveSpan', () => {
113113
ref = transaction;
114114
});
115115
try {
116-
await startActiveSpan({ name: 'GET users/[id]' }, span => {
116+
await startSpan({ name: 'GET users/[id]' }, span => {
117117
if (span) {
118118
span.op = 'http.server';
119119
}
@@ -132,8 +132,8 @@ describe('startActiveSpan', () => {
132132
ref = transaction;
133133
});
134134
try {
135-
await startActiveSpan({ name: 'GET users/[id]', parentSampled: true }, () => {
136-
return startActiveSpan({ name: 'SELECT * from users' }, () => {
135+
await startSpan({ name: 'GET users/[id]', parentSampled: true }, () => {
136+
return startSpan({ name: 'SELECT * from users' }, () => {
137137
return callback();
138138
});
139139
});
@@ -153,8 +153,8 @@ describe('startActiveSpan', () => {
153153
ref = transaction;
154154
});
155155
try {
156-
await startActiveSpan({ name: 'GET users/[id]', parentSampled: true }, () => {
157-
return startActiveSpan({ name: 'SELECT * from users' }, childSpan => {
156+
await startSpan({ name: 'GET users/[id]', parentSampled: true }, () => {
157+
return startSpan({ name: 'SELECT * from users' }, childSpan => {
158158
if (childSpan) {
159159
childSpan.op = 'db.query';
160160
}

packages/node-experimental/src/sdk/trace.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import type { NodeExperimentalClient } from './client';
1212
* The created span is the active span and will be used as parent by other spans created inside the function
1313
* and can be accessed via `Sentry.getSpan()`, as long as the function is executed while the scope is active.
1414
*
15-
* If you want to create a span that is not set as active, use {@link startSpan}.
15+
* If you want to create a span that is not set as active, use {@link startInactiveSpan}.
1616
*
1717
* Note that if you have not enabled tracing extensions via `addTracingExtensions`
1818
* or you didn't set `tracesSampleRate`, this function will not generate spans
1919
* and the `span` returned from the callback will be undefined.
2020
*/
21-
export function startActiveSpan<T>(context: TransactionContext, callback: (span: Span | undefined) => T): T {
21+
export function startSpan<T>(context: TransactionContext, callback: (span: Span | undefined) => T): T {
2222
const tracer = getTracer();
2323
if (!tracer) {
2424
return callback(undefined);
@@ -66,17 +66,22 @@ export function startActiveSpan<T>(context: TransactionContext, callback: (span:
6666
});
6767
}
6868

69+
/**
70+
* @deprecated Use {@link startSpan} instead.
71+
*/
72+
export const startActiveSpan = startSpan;
73+
6974
/**
7075
* Creates a span. This span is not set as active, so will not get automatic instrumentation spans
7176
* as children or be able to be accessed via `Sentry.getSpan()`.
7277
*
73-
* If you want to create a span that is set as active, use {@link startActiveSpan}.
78+
* If you want to create a span that is set as active, use {@link startSpan}.
7479
*
7580
* Note that if you have not enabled tracing extensions via `addTracingExtensions`
7681
* or you didn't set `tracesSampleRate` or `tracesSampler`, this function will not generate spans
7782
* and the `span` returned from the callback will be undefined.
7883
*/
79-
export function startSpan(context: TransactionContext): Span | undefined {
84+
export function startInactiveSpan(context: TransactionContext): Span | undefined {
8085
const tracer = getTracer();
8186
if (!tracer) {
8287
return undefined;

packages/node-experimental/test/sdk/trace.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ describe('trace', () => {
88
mockSdkInit({ enableTracing: true });
99
});
1010

11-
describe('startActiveSpan', () => {
11+
describe('startSpan', () => {
1212
it('works with a sync callback', () => {
1313
const spans: Span[] = [];
1414

1515
expect(Sentry.getActiveSpan()).toEqual(undefined);
1616

17-
Sentry.startActiveSpan({ name: 'outer' }, outerSpan => {
17+
Sentry.startSpan({ name: 'outer' }, outerSpan => {
1818
expect(outerSpan).toBeDefined();
1919
spans.push(outerSpan!);
2020

2121
expect(outerSpan?.name).toEqual('outer');
2222
expect(outerSpan).toBeInstanceOf(Transaction);
2323
expect(Sentry.getActiveSpan()).toEqual(outerSpan);
2424

25-
Sentry.startActiveSpan({ name: 'inner' }, innerSpan => {
25+
Sentry.startSpan({ name: 'inner' }, innerSpan => {
2626
expect(innerSpan).toBeDefined();
2727
spans.push(innerSpan!);
2828

@@ -49,7 +49,7 @@ describe('trace', () => {
4949

5050
expect(Sentry.getActiveSpan()).toEqual(undefined);
5151

52-
await Sentry.startActiveSpan({ name: 'outer' }, async outerSpan => {
52+
await Sentry.startSpan({ name: 'outer' }, async outerSpan => {
5353
expect(outerSpan).toBeDefined();
5454
spans.push(outerSpan!);
5555

@@ -59,7 +59,7 @@ describe('trace', () => {
5959
expect(outerSpan).toBeInstanceOf(Transaction);
6060
expect(Sentry.getActiveSpan()).toEqual(outerSpan);
6161

62-
await Sentry.startActiveSpan({ name: 'inner' }, async innerSpan => {
62+
await Sentry.startSpan({ name: 'inner' }, async innerSpan => {
6363
expect(innerSpan).toBeDefined();
6464
spans.push(innerSpan!);
6565

@@ -89,15 +89,15 @@ describe('trace', () => {
8989

9090
expect(Sentry.getActiveSpan()).toEqual(undefined);
9191

92-
Sentry.startActiveSpan({ name: 'outer' }, outerSpan => {
92+
Sentry.startSpan({ name: 'outer' }, outerSpan => {
9393
expect(outerSpan).toBeDefined();
9494
spans1.push(outerSpan!);
9595

9696
expect(outerSpan?.name).toEqual('outer');
9797
expect(outerSpan).toBeInstanceOf(Transaction);
9898
expect(Sentry.getActiveSpan()).toEqual(outerSpan);
9999

100-
Sentry.startActiveSpan({ name: 'inner' }, innerSpan => {
100+
Sentry.startSpan({ name: 'inner' }, innerSpan => {
101101
expect(innerSpan).toBeDefined();
102102
spans1.push(innerSpan!);
103103

@@ -108,15 +108,15 @@ describe('trace', () => {
108108
});
109109
});
110110

111-
Sentry.startActiveSpan({ name: 'outer2' }, outerSpan => {
111+
Sentry.startSpan({ name: 'outer2' }, outerSpan => {
112112
expect(outerSpan).toBeDefined();
113113
spans2.push(outerSpan!);
114114

115115
expect(outerSpan?.name).toEqual('outer2');
116116
expect(outerSpan).toBeInstanceOf(Transaction);
117117
expect(Sentry.getActiveSpan()).toEqual(outerSpan);
118118

119-
Sentry.startActiveSpan({ name: 'inner2' }, innerSpan => {
119+
Sentry.startSpan({ name: 'inner2' }, innerSpan => {
120120
expect(innerSpan).toBeDefined();
121121
spans2.push(innerSpan!);
122122

@@ -133,9 +133,9 @@ describe('trace', () => {
133133
});
134134
});
135135

136-
describe('startSpan', () => {
136+
describe('startInactiveSpan', () => {
137137
it('works at the root', () => {
138-
const span = Sentry.startSpan({ name: 'test' });
138+
const span = Sentry.startInactiveSpan({ name: 'test' });
139139

140140
expect(span).toBeDefined();
141141
expect(span).toBeInstanceOf(Transaction);
@@ -150,11 +150,11 @@ describe('trace', () => {
150150
});
151151

152152
it('works as a child span', () => {
153-
Sentry.startActiveSpan({ name: 'outer' }, outerSpan => {
153+
Sentry.startSpan({ name: 'outer' }, outerSpan => {
154154
expect(outerSpan).toBeDefined();
155155
expect(Sentry.getActiveSpan()).toEqual(outerSpan);
156156

157-
const innerSpan = Sentry.startSpan({ name: 'test' });
157+
const innerSpan = Sentry.startInactiveSpan({ name: 'test' });
158158

159159
expect(innerSpan).toBeDefined();
160160
expect(innerSpan).toBeInstanceOf(Span);

packages/node/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ export {
5656
captureCheckIn,
5757
setMeasurement,
5858
getActiveSpan,
59-
startActiveSpan,
6059
startSpan,
60+
// eslint-disable-next-line deprecation/deprecation
61+
startActiveSpan,
62+
startInactiveSpan,
6163
} from '@sentry/core';
6264
export type { SpanStatusType } from '@sentry/core';
6365
export { autoDiscoverNodePerformanceMonitoringIntegrations } from './tracing';

packages/serverless/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export {
5151
Integrations,
5252
setMeasurement,
5353
getActiveSpan,
54-
startActiveSpan,
5554
startSpan,
55+
// eslint-disable-next-line deprecation/deprecation
56+
startActiveSpan,
57+
startInactiveSpan,
5658
} from '@sentry/node';

packages/sveltekit/src/server/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ export {
4646
Handlers,
4747
setMeasurement,
4848
getActiveSpan,
49-
startActiveSpan,
5049
startSpan,
50+
// eslint-disable-next-line deprecation/deprecation
51+
startActiveSpan,
52+
startInactiveSpan,
5153
} from '@sentry/node';
5254

5355
// We can still leave this for the carrier init and type exports

0 commit comments

Comments
 (0)