Skip to content

Commit

Permalink
make sure all finish reasons are defined
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Feb 28, 2022
1 parent 770ce5d commit e0f9ca0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/tracing/src/browser/browsertracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface BrowserTracingOptions extends RequestInstrumentationOptions {
idleTimeout: number;

/**
* The max transaction duration for a transaction. If a transaction duration hits the `finalTimeout` value, it
* The max duration for a transaction. If a transaction duration hits the `finalTimeout` value, it
* will be finished.
*
* Time is in ms.
Expand Down
3 changes: 2 additions & 1 deletion packages/tracing/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Store finish reasons in tuple to save on bundle size
// Readonly type should enforce that this is not mutated.
export const FINISH_REASON_TAG = 'finishReason';
export const FINISH_REASON_TAG = 'finishReason' as const;

export const IDLE_TRANSACTION_FINISH_REASONS = [
'heartbeatFailed',
'idleTimeout',
'documentHidden',
'finalTimeout',
'externalFinish',
] as const;
26 changes: 18 additions & 8 deletions packages/tracing/src/idletransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ export class IdleTransaction extends Transaction {
*/
private _idleTimeoutID: ReturnType<typeof global.setTimeout> | undefined;

/**
*
* Defaults to `externalFinish`, which means transaction.finish() was called outside of the idle transaction (for example,
* by a navigation transaction ending the previous pageload/navigation in some routing instrumentation).
* @default 'externalFinish'
*/
private _finishReason: typeof IDLE_TRANSACTION_FINISH_REASONS[number] = IDLE_TRANSACTION_FINISH_REASONS[4];

public constructor(
transactionContext: TransactionContext,
private readonly _idleHub?: Hub,
Expand Down Expand Up @@ -114,7 +122,7 @@ export class IdleTransaction extends Transaction {
this._startIdleTimeout();
global.setTimeout(() => {
if (!this._finished) {
this.setTag(FINISH_REASON_TAG, IDLE_TRANSACTION_FINISH_REASONS[3]);
this._finishReason = IDLE_TRANSACTION_FINISH_REASONS[3]; /* 'finalTimeout' */
this.finish();
}
}, this._finalTimeout);
Expand All @@ -125,6 +133,8 @@ export class IdleTransaction extends Transaction {
this._finished = true;
this.activities = {};

this.setTag(FINISH_REASON_TAG, this._finishReason);

if (this.spanRecorder) {
logger.log('[Tracing] finishing IdleTransaction', new Date(endTimestamp * 1000).toISOString(), this.op);

Expand Down Expand Up @@ -207,7 +217,7 @@ export class IdleTransaction extends Transaction {
}

/**
* Creates an idletimeout
* Cancels the existing idletimeout, if there is one
*/
private _cancelIdleTimeout(): void {
if (this._idleTimeoutID) {
Expand All @@ -219,12 +229,12 @@ export class IdleTransaction extends Transaction {
/**
* Creates an idletimeout
*/
private _startIdleTimeout(end?: Parameters<IdleTransaction['finish']>[0]): void {
private _startIdleTimeout(endTimestamp?: Parameters<IdleTransaction['finish']>[0]): void {
this._cancelIdleTimeout();
this._idleTimeoutID = global.setTimeout(() => {
if (!this._finished && Object.keys(this.activities).length === 0) {
this.setTag(FINISH_REASON_TAG, IDLE_TRANSACTION_FINISH_REASONS[1]);
this.finish(end);
this._finishReason = IDLE_TRANSACTION_FINISH_REASONS[1]; /* 'idleTimeout' */
this.finish(endTimestamp);
}
}, this._idleTimeout);
}
Expand Down Expand Up @@ -256,8 +266,8 @@ export class IdleTransaction extends Transaction {
const timeout = this._idleTimeout;
// We need to add the timeout here to have the real endtimestamp of the transaction
// Remember timestampWithMs is in seconds, timeout is in ms
const end = timestampWithMs() + timeout / 1000;
this._startIdleTimeout(end);
const endTimestamp = timestampWithMs() + timeout / 1000;
this._startIdleTimeout(endTimestamp);
}
}

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

0 comments on commit e0f9ca0

Please sign in to comment.