Skip to content

Commit cc7beed

Browse files
committed
ref(span): deprecate span status enum
1 parent 37a1c24 commit cc7beed

15 files changed

+114
-85
lines changed

packages/node/test/handlers.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as sentryCore from '@sentry/core';
22
import { Hub } from '@sentry/hub';
33
import * as sentryHub from '@sentry/hub';
4-
import { SpanStatus, Transaction } from '@sentry/tracing';
4+
import { Transaction } from '@sentry/tracing';
55
import { RequestSessionStatus, Runtime } from '@sentry/types';
66
import * as http from 'http';
77
import * as net from 'net';
@@ -403,7 +403,7 @@ describe('tracingHandler', () => {
403403

404404
setImmediate(() => {
405405
expect(finishTransaction).toHaveBeenCalled();
406-
expect(transaction.status).toBe(SpanStatus.Ok);
406+
expect(transaction.status).toBe('ok');
407407
expect(transaction.tags).toEqual(expect.objectContaining({ 'http.status_code': '200' }));
408408
done();
409409
});

packages/tracing/src/browser/backgroundtab.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getGlobalObject, logger } from '@sentry/utils';
22

33
import { FINISH_REASON_TAG, IDLE_TRANSACTION_FINISH_REASONS } from '../constants';
44
import { IdleTransaction } from '../idletransaction';
5-
import { SpanStatus } from '../spanstatus';
5+
import { SpanStatusType } from '../spanstatus';
66
import { getActiveTransaction } from '../utils';
77

88
const global = getGlobalObject<Window>();
@@ -16,13 +16,15 @@ export function registerBackgroundTabDetection(): void {
1616
global.document.addEventListener('visibilitychange', () => {
1717
const activeTransaction = getActiveTransaction() as IdleTransaction;
1818
if (global.document.hidden && activeTransaction) {
19+
const statusType: SpanStatusType = 'cancelled';
20+
1921
logger.log(
20-
`[Tracing] Transaction: ${SpanStatus.Cancelled} -> since tab moved to the background, op: ${activeTransaction.op}`,
22+
`[Tracing] Transaction: ${statusType} -> since tab moved to the background, op: ${activeTransaction.op}`,
2123
);
2224
// We should not set status if it is already set, this prevent important statuses like
2325
// error or data loss from being overwritten on transaction.
2426
if (!activeTransaction.status) {
25-
activeTransaction.setStatus(SpanStatus.Cancelled);
27+
activeTransaction.setStatus(statusType);
2628
}
2729
activeTransaction.setTag('visibilitychange', 'document.hidden');
2830
activeTransaction.setTag(FINISH_REASON_TAG, IDLE_TRANSACTION_FINISH_REASONS[2]);

packages/tracing/src/browser/browsertracing.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { getGlobalObject, logger } from '@sentry/utils';
44

55
import { startIdleTransaction } from '../hubextensions';
66
import { DEFAULT_IDLE_TIMEOUT, IdleTransaction } from '../idletransaction';
7-
import { SpanStatus } from '../spanstatus';
87
import { extractTraceparentData, secToMs } from '../utils';
98
import { registerBackgroundTabDetection } from './backgroundtab';
109
import { MetricsInstrumentation } from './metrics';
@@ -269,7 +268,7 @@ function adjustTransactionDuration(maxDuration: number, transaction: IdleTransac
269268
const diff = endTimestamp - transaction.startTimestamp;
270269
const isOutdatedTransaction = endTimestamp && (diff > maxDuration || diff < 0);
271270
if (isOutdatedTransaction) {
272-
transaction.setStatus(SpanStatus.DeadlineExceeded);
271+
transaction.setStatus('deadline_exceeded');
273272
transaction.setTag('maxTransactionDurationExceeded', 'true');
274273
}
275274
}

packages/tracing/src/browser/request.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { addInstrumentationHandler, isInstanceOf, isMatchingPattern } from '@sentry/utils';
22

33
import { Span } from '../span';
4-
import { SpanStatus } from '../spanstatus';
54
import { getActiveTransaction, hasTracingEnabled } from '../utils';
65

76
export const DEFAULT_TRACING_ORIGINS = ['localhost', /^\//];
@@ -156,7 +155,7 @@ export function fetchCallback(
156155
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
157156
span.setHttpStatus(handlerData.response.status);
158157
} else if (handlerData.error) {
159-
span.setStatus(SpanStatus.InternalError);
158+
span.setStatus('internal_error');
160159
}
161160
span.finish();
162161

packages/tracing/src/errors.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { addInstrumentationHandler, logger } from '@sentry/utils';
22

3-
import { SpanStatus } from './spanstatus';
3+
import { SpanStatusType } from './spanstatus';
44
import { getActiveTransaction } from './utils';
55

66
/**
@@ -23,7 +23,8 @@ export function registerErrorInstrumentation(): void {
2323
function errorCallback(): void {
2424
const activeTransaction = getActiveTransaction();
2525
if (activeTransaction) {
26-
logger.log(`[Tracing] Transaction: ${SpanStatus.InternalError} -> Global error occured`);
27-
activeTransaction.setStatus(SpanStatus.InternalError);
26+
const status: SpanStatusType = 'internal_error';
27+
logger.log(`[Tracing] Transaction: ${status} -> Global error occured`);
28+
activeTransaction.setStatus(status);
2829
}
2930
}

packages/tracing/src/idletransaction.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { logger, timestampWithMs } from '@sentry/utils';
44

55
import { FINISH_REASON_TAG, IDLE_TRANSACTION_FINISH_REASONS } from './constants';
66
import { Span, SpanRecorder } from './span';
7-
import { SpanStatus } from './spanstatus';
87
import { Transaction } from './transaction';
98

109
export const DEFAULT_IDLE_TIMEOUT = 1000;
@@ -125,7 +124,7 @@ export class IdleTransaction extends Transaction {
125124
// We cancel all pending spans with status "cancelled" to indicate the idle transaction was finished early
126125
if (!span.endTimestamp) {
127126
span.endTimestamp = endTimestamp;
128-
span.setStatus(SpanStatus.Cancelled);
127+
span.setStatus('cancelled');
129128
logger.log('[Tracing] cancelling span since transaction ended early', JSON.stringify(span, undefined, 2));
130129
}
131130

@@ -253,7 +252,7 @@ export class IdleTransaction extends Transaction {
253252

254253
if (this._heartbeatCounter >= 3) {
255254
logger.log(`[Tracing] Transaction finished because of no change for 3 heart beats`);
256-
this.setStatus(SpanStatus.DeadlineExceeded);
255+
this.setStatus('deadline_exceeded');
257256
this.setTag(FINISH_REASON_TAG, IDLE_TRANSACTION_FINISH_REASONS[0]);
258257
this.finish();
259258
} else {

packages/tracing/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export {
2929
RequestInstrumentationOptions,
3030
defaultRequestInstrumentationOptions,
3131
} from './browser';
32-
export { SpanStatus } from './spanstatus';
32+
export { SpanStatus, spanStatusfromHttpCode } from './spanstatus';
3333
export { IdleTransaction } from './idletransaction';
3434
export { startIdleTransaction } from './hubextensions';
3535

packages/tracing/src/span.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
33
import { dropUndefinedKeys, timestampWithMs, uuid4 } from '@sentry/utils';
44

5-
import { SpanStatus } from './spanstatus';
5+
import { spanStatusfromHttpCode, SpanStatusType } from './spanstatus';
66

77
/**
88
* Keeps track of finished spans for a given transaction
@@ -56,7 +56,7 @@ export class Span implements SpanInterface {
5656
/**
5757
* Internal keeper of the status
5858
*/
59-
public status?: SpanStatus | string;
59+
public status?: SpanStatusType;
6060

6161
/**
6262
* @inheritDoc
@@ -204,7 +204,7 @@ export class Span implements SpanInterface {
204204
/**
205205
* @inheritDoc
206206
*/
207-
public setStatus(value: SpanStatus): this {
207+
public setStatus(value: SpanStatusType): this {
208208
this.status = value;
209209
return this;
210210
}
@@ -214,8 +214,8 @@ export class Span implements SpanInterface {
214214
*/
215215
public setHttpStatus(httpStatus: number): this {
216216
this.setTag('http.status_code', String(httpStatus));
217-
const spanStatus = SpanStatus.fromHttpCode(httpStatus);
218-
if (spanStatus !== SpanStatus.UnknownError) {
217+
const spanStatus = spanStatusfromHttpCode(httpStatus);
218+
if (spanStatus !== 'unknown_error') {
219219
this.setStatus(spanStatus);
220220
}
221221
return this;
@@ -225,7 +225,7 @@ export class Span implements SpanInterface {
225225
* @inheritDoc
226226
*/
227227
public isSuccess(): boolean {
228-
return this.status === SpanStatus.Ok;
228+
return this.status === 'ok';
229229
}
230230

231231
/**

packages/tracing/src/spanstatus.ts

+75-42
Original file line numberDiff line numberDiff line change
@@ -37,51 +37,84 @@ export enum SpanStatus {
3737
DataLoss = 'data_loss',
3838
}
3939

40-
// eslint-disable-next-line @typescript-eslint/no-namespace, import/export
41-
export namespace SpanStatus {
42-
/**
43-
* Converts a HTTP status code into a {@link SpanStatus}.
44-
*
45-
* @param httpStatus The HTTP response status code.
46-
* @returns The span status or {@link SpanStatus.UnknownError}.
47-
*/
48-
export function fromHttpCode(httpStatus: number): SpanStatus {
49-
if (httpStatus < 400 && httpStatus >= 100) {
50-
return SpanStatus.Ok;
51-
}
40+
export type SpanStatusType =
41+
/** The operation completed successfully. */
42+
| 'ok'
43+
/** Deadline expired before operation could complete. */
44+
| 'deadline_exceeded'
45+
/** 401 Unauthorized (actually does mean unauthenticated according to RFC 7235) */
46+
| 'unauthenticated'
47+
/** 403 Forbidden */
48+
| 'permission_denied'
49+
/** 404 Not Found. Some requested entity (file or directory) was not found. */
50+
| 'not_found'
51+
/** 429 Too Many Requests */
52+
| 'resource_exhausted'
53+
/** Client specified an invalid argument. 4xx. */
54+
| 'invalid_argument'
55+
/** 501 Not Implemented */
56+
| 'unimplemented'
57+
/** 503 Service Unavailable */
58+
| 'unavailable'
59+
/** Other/generic 5xx. */
60+
| 'internal_error'
61+
/** Unknown. Any non-standard HTTP status code. */
62+
| 'unknown_error'
63+
/** The operation was cancelled (typically by the user). */
64+
| 'cancelled'
65+
/** Already exists (409) */
66+
| 'already_exists'
67+
/** Operation was rejected because the system is not in a state required for the operation's */
68+
| 'failed_precondition'
69+
/** The operation was aborted, typically due to a concurrency issue. */
70+
| 'aborted'
71+
/** Operation was attempted past the valid range. */
72+
| 'out_of_range'
73+
/** Unrecoverable data loss or corruption */
74+
| 'data_loss';
5275

53-
if (httpStatus >= 400 && httpStatus < 500) {
54-
switch (httpStatus) {
55-
case 401:
56-
return SpanStatus.Unauthenticated;
57-
case 403:
58-
return SpanStatus.PermissionDenied;
59-
case 404:
60-
return SpanStatus.NotFound;
61-
case 409:
62-
return SpanStatus.AlreadyExists;
63-
case 413:
64-
return SpanStatus.FailedPrecondition;
65-
case 429:
66-
return SpanStatus.ResourceExhausted;
67-
default:
68-
return SpanStatus.InvalidArgument;
69-
}
70-
}
76+
/**
77+
* Converts a HTTP status code into a {@link SpanStatusType}.
78+
*
79+
* @param httpStatus The HTTP response status code.
80+
* @returns The span status or unknown_error.
81+
*/
82+
export function spanStatusfromHttpCode(httpStatus: number): SpanStatusType {
83+
if (httpStatus < 400 && httpStatus >= 100) {
84+
return 'ok';
85+
}
7186

72-
if (httpStatus >= 500 && httpStatus < 600) {
73-
switch (httpStatus) {
74-
case 501:
75-
return SpanStatus.Unimplemented;
76-
case 503:
77-
return SpanStatus.Unavailable;
78-
case 504:
79-
return SpanStatus.DeadlineExceeded;
80-
default:
81-
return SpanStatus.InternalError;
82-
}
87+
if (httpStatus >= 400 && httpStatus < 500) {
88+
switch (httpStatus) {
89+
case 401:
90+
return 'unauthenticated';
91+
case 403:
92+
return 'permission_denied';
93+
case 404:
94+
return 'not_found';
95+
case 409:
96+
return 'already_exists';
97+
case 413:
98+
return 'failed_precondition';
99+
case 429:
100+
return 'resource_exhausted';
101+
default:
102+
return 'invalid_argument';
83103
}
104+
}
84105

85-
return SpanStatus.UnknownError;
106+
if (httpStatus >= 500 && httpStatus < 600) {
107+
switch (httpStatus) {
108+
case 501:
109+
return 'unimplemented';
110+
case 503:
111+
return 'unavailable';
112+
case 504:
113+
return 'deadline_exceeded';
114+
default:
115+
return 'internal_error';
116+
}
86117
}
118+
119+
return 'unknown_error';
87120
}

packages/tracing/test/browser/backgroundtab.test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { BrowserClient } from '@sentry/browser';
22
import { Hub, makeMain } from '@sentry/hub';
33
import { JSDOM } from 'jsdom';
44

5-
import { SpanStatus } from '../../src';
65
import { registerBackgroundTabDetection } from '../../src/browser/backgroundtab';
76

87
describe('registerBackgroundTabDetection', () => {
@@ -49,7 +48,7 @@ describe('registerBackgroundTabDetection', () => {
4948
global.document.hidden = true;
5049
events.visibilitychange();
5150

52-
expect(transaction.status).toBe(SpanStatus.Cancelled);
51+
expect(transaction.status).toBe('cancelled');
5352
expect(transaction.tags.visibilitychange).toBe('document.hidden');
5453
expect(transaction.endTimestamp).toBeDefined();
5554
});

packages/tracing/test/browser/browsertracing.test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Hub, makeMain } from '@sentry/hub';
33
import { getGlobalObject } from '@sentry/utils';
44
import { JSDOM } from 'jsdom';
55

6-
import { SpanStatus } from '../../src';
76
import {
87
BrowserTracing,
98
BrowserTracingOptions,
@@ -273,7 +272,7 @@ describe('BrowserTracing', () => {
273272
const transaction = getActiveTransaction(hub) as IdleTransaction;
274273
transaction.finish(transaction.startTimestamp + secToMs(DEFAULT_MAX_TRANSACTION_DURATION_SECONDS) + 1);
275274

276-
expect(transaction.status).toBe(SpanStatus.DeadlineExceeded);
275+
expect(transaction.status).toBe('deadline_exceeded');
277276
expect(transaction.tags.maxTransactionDurationExceeded).toBeDefined();
278277
});
279278

packages/tracing/test/browser/request.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { BrowserClient } from '@sentry/browser';
22
import { Hub, makeMain } from '@sentry/hub';
33
import * as utils from '@sentry/utils';
44

5-
import { Span, SpanStatus, Transaction } from '../../src';
5+
import { Span, Transaction, spanStatusfromHttpCode } from '../../src';
66
import { fetchCallback, FetchData, instrumentOutgoingRequests, xhrCallback, XHRData } from '../../src/browser/request';
77
import { addExtensionMethods } from '../../src/hubextensions';
88
import * as tracingUtils from '../../src/utils';
@@ -176,7 +176,7 @@ describe('callbacks', () => {
176176
// triggered by response coming back
177177
fetchCallback(postRequestFetchHandlerData, alwaysCreateSpan, spans);
178178

179-
expect(newSpan!.status).toBe(SpanStatus.fromHttpCode(404));
179+
expect(newSpan!.status).toBe(spanStatusfromHttpCode(404));
180180
});
181181

182182
it('adds sentry-trace header to fetch requests', () => {
@@ -267,7 +267,7 @@ describe('callbacks', () => {
267267
// triggered by response coming back
268268
xhrCallback(postRequestXHRHandlerData, alwaysCreateSpan, spans);
269269

270-
expect(newSpan!.status).toBe(SpanStatus.fromHttpCode(404));
270+
expect(newSpan!.status).toBe(spanStatusfromHttpCode(404));
271271
});
272272
});
273273
});

packages/tracing/test/errors.test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { BrowserClient } from '@sentry/browser';
22
import { Hub, makeMain } from '@sentry/hub';
33

4-
import { SpanStatus } from '../src';
54
import { registerErrorInstrumentation } from '../src/errors';
65
import { _addTracingExtensions } from '../src/hubextensions';
76

@@ -71,7 +70,7 @@ describe('registerErrorHandlers()', () => {
7170
hub.configureScope(scope => scope.setSpan(transaction));
7271

7372
mockErrorCallback();
74-
expect(transaction.status).toBe(SpanStatus.InternalError);
73+
expect(transaction.status).toBe('internal_error');
7574

7675
transaction.finish();
7776
});
@@ -82,7 +81,7 @@ describe('registerErrorHandlers()', () => {
8281
hub.configureScope(scope => scope.setSpan(transaction));
8382

8483
mockUnhandledRejectionCallback();
85-
expect(transaction.status).toBe(SpanStatus.InternalError);
84+
expect(transaction.status).toBe('internal_error');
8685
transaction.finish();
8786
});
8887
});

0 commit comments

Comments
 (0)