Skip to content

Commit e0f9ca0

Browse files
committed
make sure all finish reasons are defined
1 parent 770ce5d commit e0f9ca0

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

packages/tracing/src/browser/browsertracing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
2929
idleTimeout: number;
3030

3131
/**
32-
* The max transaction duration for a transaction. If a transaction duration hits the `finalTimeout` value, it
32+
* The max duration for a transaction. If a transaction duration hits the `finalTimeout` value, it
3333
* will be finished.
3434
*
3535
* Time is in ms.

packages/tracing/src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Store finish reasons in tuple to save on bundle size
22
// Readonly type should enforce that this is not mutated.
3-
export const FINISH_REASON_TAG = 'finishReason';
3+
export const FINISH_REASON_TAG = 'finishReason' as const;
44

55
export const IDLE_TRANSACTION_FINISH_REASONS = [
66
'heartbeatFailed',
77
'idleTimeout',
88
'documentHidden',
99
'finalTimeout',
10+
'externalFinish',
1011
] as const;

packages/tracing/src/idletransaction.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ export class IdleTransaction extends Transaction {
7777
*/
7878
private _idleTimeoutID: ReturnType<typeof global.setTimeout> | undefined;
7979

80+
/**
81+
*
82+
* Defaults to `externalFinish`, which means transaction.finish() was called outside of the idle transaction (for example,
83+
* by a navigation transaction ending the previous pageload/navigation in some routing instrumentation).
84+
* @default 'externalFinish'
85+
*/
86+
private _finishReason: typeof IDLE_TRANSACTION_FINISH_REASONS[number] = IDLE_TRANSACTION_FINISH_REASONS[4];
87+
8088
public constructor(
8189
transactionContext: TransactionContext,
8290
private readonly _idleHub?: Hub,
@@ -114,7 +122,7 @@ export class IdleTransaction extends Transaction {
114122
this._startIdleTimeout();
115123
global.setTimeout(() => {
116124
if (!this._finished) {
117-
this.setTag(FINISH_REASON_TAG, IDLE_TRANSACTION_FINISH_REASONS[3]);
125+
this._finishReason = IDLE_TRANSACTION_FINISH_REASONS[3]; /* 'finalTimeout' */
118126
this.finish();
119127
}
120128
}, this._finalTimeout);
@@ -125,6 +133,8 @@ export class IdleTransaction extends Transaction {
125133
this._finished = true;
126134
this.activities = {};
127135

136+
this.setTag(FINISH_REASON_TAG, this._finishReason);
137+
128138
if (this.spanRecorder) {
129139
logger.log('[Tracing] finishing IdleTransaction', new Date(endTimestamp * 1000).toISOString(), this.op);
130140

@@ -207,7 +217,7 @@ export class IdleTransaction extends Transaction {
207217
}
208218

209219
/**
210-
* Creates an idletimeout
220+
* Cancels the existing idletimeout, if there is one
211221
*/
212222
private _cancelIdleTimeout(): void {
213223
if (this._idleTimeoutID) {
@@ -219,12 +229,12 @@ export class IdleTransaction extends Transaction {
219229
/**
220230
* Creates an idletimeout
221231
*/
222-
private _startIdleTimeout(end?: Parameters<IdleTransaction['finish']>[0]): void {
232+
private _startIdleTimeout(endTimestamp?: Parameters<IdleTransaction['finish']>[0]): void {
223233
this._cancelIdleTimeout();
224234
this._idleTimeoutID = global.setTimeout(() => {
225235
if (!this._finished && Object.keys(this.activities).length === 0) {
226-
this.setTag(FINISH_REASON_TAG, IDLE_TRANSACTION_FINISH_REASONS[1]);
227-
this.finish(end);
236+
this._finishReason = IDLE_TRANSACTION_FINISH_REASONS[1]; /* 'idleTimeout' */
237+
this.finish(endTimestamp);
228238
}
229239
}, this._idleTimeout);
230240
}
@@ -256,8 +266,8 @@ export class IdleTransaction extends Transaction {
256266
const timeout = this._idleTimeout;
257267
// We need to add the timeout here to have the real endtimestamp of the transaction
258268
// Remember timestampWithMs is in seconds, timeout is in ms
259-
const end = timestampWithMs() + timeout / 1000;
260-
this._startIdleTimeout(end);
269+
const endTimestamp = timestampWithMs() + timeout / 1000;
270+
this._startIdleTimeout(endTimestamp);
261271
}
262272
}
263273

@@ -284,7 +294,7 @@ export class IdleTransaction extends Transaction {
284294
if (this._heartbeatCounter >= 3) {
285295
logger.log(`[Tracing] Transaction finished because of no change for 3 heart beats`);
286296
this.setStatus('deadline_exceeded');
287-
this.setTag(FINISH_REASON_TAG, IDLE_TRANSACTION_FINISH_REASONS[0]);
297+
this._finishReason = IDLE_TRANSACTION_FINISH_REASONS[0]; /* 'heartbeatFailed' */
288298
this.finish();
289299
} else {
290300
this._pingHeartbeat();

0 commit comments

Comments
 (0)