Skip to content

Commit 27f9d0a

Browse files
committed
multiple before finishes
1 parent 68ee001 commit 27f9d0a

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

packages/tracing/src/browser/browsertracing.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ export class BrowserTracing implements Integration {
123123

124124
private _getCurrentHub?: () => Hub;
125125

126+
// navigationTransactionInvoker() -> Uses history API NavigationTransaction[]
127+
126128
public constructor(_options?: Partial<BrowserTracingOptions>) {
127129
this.options = {
128130
...this.options,
@@ -156,6 +158,7 @@ export class BrowserTracing implements Integration {
156158

157159
let startingUrl: string | undefined = global.location.href;
158160

161+
// Could this be the one that changes?
159162
addInstrumentationHandler({
160163
callback: ({ to, from }: { to: string; from?: string }) => {
161164
/**

packages/tracing/src/idletransaction.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export class IdleTransactionSpanRecorder extends SpanRecorder {
4545
}
4646
}
4747

48+
export type BeforeFinishCallback = (transactionSpan: IdleTransaction) => void;
49+
4850
/**
4951
* An IdleTransaction is a transaction that automatically finishes. It does this by tracking child spans as activities.
5052
* You can have multiple IdleTransactions active, but if the `onScope` option is specified, the idle transaction will
@@ -66,7 +68,7 @@ export class IdleTransaction extends Transaction {
6668
// We should not use heartbeat if we finished a transaction
6769
private _finished: boolean = false;
6870

69-
private _finishCallback?: (transactionSpan: IdleTransaction) => void;
71+
private readonly _beforeFinishCallbacks: BeforeFinishCallback[] = [];
7072

7173
public constructor(
7274
transactionContext: TransactionContext,
@@ -142,8 +144,8 @@ export class IdleTransaction extends Transaction {
142144
if (this.spanRecorder) {
143145
logger.log('[Tracing] finishing IdleTransaction', new Date(endTimestamp * 1000).toISOString(), this.op);
144146

145-
if (this._finishCallback) {
146-
this._finishCallback(this);
147+
for (const callback of this._beforeFinishCallbacks) {
148+
callback(this);
147149
}
148150

149151
this.spanRecorder.spans = this.spanRecorder.spans.filter((span: Span) => {
@@ -224,8 +226,8 @@ export class IdleTransaction extends Transaction {
224226
* This is exposed because users have no other way of running something before an idle transaction
225227
* finishes.
226228
*/
227-
public beforeFinish(callback: (transactionSpan: IdleTransaction) => void): void {
228-
this._finishCallback = callback;
229+
public registerBeforeFinishCallback(callback: BeforeFinishCallback): void {
230+
this._beforeFinishCallbacks.push(callback);
229231
}
230232

231233
/**

packages/tracing/test/hub.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ describe('Hub', () => {
5757
expect(child.sampled).toBeFalsy();
5858
});
5959
});
60+
});
6061
});

packages/tracing/test/idletransaction.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,24 @@ describe('IdleTransaction', () => {
9595
});
9696

9797
it('calls beforeFinish callback before finishing', () => {
98-
const mockCallback = jest.fn();
98+
const mockCallback1 = jest.fn();
99+
const mockCallback2 = jest.fn();
99100
const transaction = new IdleTransaction({ name: 'foo' }, hub, 1000);
100101
transaction.initSpanRecorder(10);
101-
transaction.beforeFinish(mockCallback);
102+
transaction.registerBeforeFinishCallback(mockCallback1);
103+
transaction.registerBeforeFinishCallback(mockCallback2);
102104

103-
expect(mockCallback).toHaveBeenCalledTimes(0);
105+
expect(mockCallback1).toHaveBeenCalledTimes(0);
106+
expect(mockCallback2).toHaveBeenCalledTimes(0);
104107

105108
const span = transaction.startChild();
106109
span.finish();
107110

108111
jest.runOnlyPendingTimers();
109-
expect(mockCallback).toHaveBeenCalledTimes(1);
110-
expect(mockCallback).toHaveBeenLastCalledWith(transaction);
112+
expect(mockCallback1).toHaveBeenCalledTimes(1);
113+
expect(mockCallback1).toHaveBeenLastCalledWith(transaction);
114+
expect(mockCallback2).toHaveBeenCalledTimes(1);
115+
expect(mockCallback2).toHaveBeenLastCalledWith(transaction);
111116
});
112117

113118
it('filters spans on finish', () => {

0 commit comments

Comments
 (0)