Skip to content

Commit 55031de

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

File tree

20 files changed

+138
-101
lines changed

20 files changed

+138
-101
lines changed

packages/browser/test/unit/mocks/simpletransport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SyncPromise, statusFromHttpCode } from '@sentry/utils';
1+
import { statusFromHttpCode,SyncPromise } from '@sentry/utils';
22

33
import { Event, Response } from '../../../src';
44
import { BaseTransport } from '../../../src/transports';

packages/node/test/handlers.test.ts

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { SpanStatusType } from '@sentry/types';
12
import { getGlobalObject, logger } from '@sentry/utils';
23

34
import { FINISH_REASON_TAG, IDLE_TRANSACTION_FINISH_REASONS } from '../constants';
45
import { IdleTransaction } from '../idletransaction';
5-
import { SpanStatus } from '../spanstatus';
66
import { getActiveTransaction } from '../utils';
77

88
const global = getGlobalObject<Window>();
@@ -16,13 +16,14 @@ export function registerBackgroundTabDetection(): void {
1616
global.document.addEventListener('visibilitychange', () => {
1717
const activeTransaction = getActiveTransaction() as IdleTransaction;
1818
if (global.document.hidden && activeTransaction) {
19+
const status: SpanStatusType = 'cancelled';
1920
logger.log(
20-
`[Tracing] Transaction: ${SpanStatus.Cancelled} -> since tab moved to the background, op: ${activeTransaction.op}`,
21+
`[Tracing] Transaction: ${status} -> since tab moved to the background, op: ${activeTransaction.op}`,
2122
);
2223
// We should not set status if it is already set, this prevent important statuses like
2324
// error or data loss from being overwritten on transaction.
2425
if (!activeTransaction.status) {
25-
activeTransaction.setStatus(SpanStatus.Cancelled);
26+
activeTransaction.setStatus(status);
2627
}
2728
activeTransaction.setTag('visibilitychange', 'document.hidden');
2829
activeTransaction.setTag(FINISH_REASON_TAG, IDLE_TRANSACTION_FINISH_REASONS[2]);

packages/tracing/src/browser/browsertracing.ts

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { SpanStatusType } from '@sentry/types';
12
import { addInstrumentationHandler, logger } from '@sentry/utils';
23

3-
import { SpanStatus } 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 errorType: SpanStatusType = 'internal_error';
27+
logger.log(`[Tracing] Transaction: ${errorType} -> Global error occured`);
28+
activeTransaction.setStatus(errorType);
2829
}
2930
}

packages/tracing/src/idletransaction.ts

Lines changed: 2 additions & 3 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export {
2929
RequestInstrumentationOptions,
3030
defaultRequestInstrumentationOptions,
3131
} from './browser';
32+
/* eslint-disable-next-line deprecation/deprecation */
3233
export { SpanStatus } from './spanstatus';
3334
export { IdleTransaction } from './idletransaction';
3435
export { startIdleTransaction } from './hubextensions';

packages/tracing/src/span.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* eslint-disable max-lines */
2-
import { Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
3-
import { dropUndefinedKeys, timestampWithMs, uuid4 } from '@sentry/utils';
4-
5-
import { SpanStatus } from './spanstatus';
2+
import { Primitive, Span as SpanInterface, SpanContext, SpanStatusType, Transaction } from '@sentry/types';
3+
import { dropUndefinedKeys, spanStatusfromHttpCode, timestampWithMs, uuid4 } from '@sentry/utils';
64

75
/**
86
* Keeps track of finished spans for a given transaction
@@ -56,7 +54,7 @@ export class Span implements SpanInterface {
5654
/**
5755
* Internal keeper of the status
5856
*/
59-
public status?: SpanStatus | string;
57+
public status?: SpanStatusType;
6058

6159
/**
6260
* @inheritDoc
@@ -204,7 +202,7 @@ export class Span implements SpanInterface {
204202
/**
205203
* @inheritDoc
206204
*/
207-
public setStatus(value: SpanStatus): this {
205+
public setStatus(value: SpanStatusType): this {
208206
this.status = value;
209207
return this;
210208
}
@@ -214,8 +212,8 @@ export class Span implements SpanInterface {
214212
*/
215213
public setHttpStatus(httpStatus: number): this {
216214
this.setTag('http.status_code', String(httpStatus));
217-
const spanStatus = SpanStatus.fromHttpCode(httpStatus);
218-
if (spanStatus !== SpanStatus.UnknownError) {
215+
const spanStatus = spanStatusfromHttpCode(httpStatus);
216+
if (spanStatus !== 'unknown_error') {
219217
this.setStatus(spanStatus);
220218
}
221219
return this;
@@ -225,7 +223,7 @@ export class Span implements SpanInterface {
225223
* @inheritDoc
226224
*/
227225
public isSuccess(): boolean {
228-
return this.status === SpanStatus.Ok;
226+
return this.status === 'ok';
229227
}
230228

231229
/**

packages/tracing/src/spanstatus.ts

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/** The status of an Span. */
2-
// eslint-disable-next-line import/export
1+
/** JSDoc
2+
* @deprecated Use string literals - if you require type casting, cast to SpanStatusType type
3+
*/
34
export enum SpanStatus {
45
/** The operation completed successfully. */
56
Ok = 'ok',
@@ -36,52 +37,3 @@ export enum SpanStatus {
3637
/** Unrecoverable data loss or corruption */
3738
DataLoss = 'data_loss',
3839
}
39-
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-
}
52-
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-
}
71-
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-
}
83-
}
84-
85-
return SpanStatus.UnknownError;
86-
}
87-
}

0 commit comments

Comments
 (0)