Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Clear activeTransaction from the scope and always start idle timers #3215

Merged
merged 3 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/tracing/src/browser/browsertracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ export class BrowserTracing implements Integration {
logger.log(`[Tracing] Will not send ${finalContext.op} transaction because of beforeNavigate.`);
}

logger.log(`[Tracing] Starting ${finalContext.op} transaction on scope`);

const hub = this._getCurrentHub();
const { location } = getGlobalObject() as WindowOrWorkerGlobalScope & { location: Location };

Expand All @@ -218,7 +220,6 @@ export class BrowserTracing implements Integration {
true,
{ location }, // for use in the tracesSampler
);
logger.log(`[Tracing] Starting ${finalContext.op} transaction on scope`);
idleTransaction.registerBeforeFinishCallback((transaction, endTimestamp) => {
this._metrics.addPerformanceEntries(transaction);
adjustTransactionDuration(secToMs(maxTransactionDuration), transaction, endTimestamp);
Expand Down
22 changes: 11 additions & 11 deletions packages/tracing/src/idletransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ export class IdleTransaction extends Transaction {
logger.log(`Setting idle transaction on scope. Span ID: ${this.spanId}`);
_idleHub.configureScope(scope => scope.setSpan(this));
}

this._initTimeout = setTimeout(() => {
if (!this._finished) {
this.finish();
}
}, this._idleTimeout);
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -130,16 +136,16 @@ export class IdleTransaction extends Transaction {
return keepSpan;
});

// this._onScope is true if the transaction was previously on the scope.
if (this._onScope) {
clearActiveTransaction(this._idleHub);
}

logger.log('[Tracing] flushing IdleTransaction');
} else {
logger.log('[Tracing] No active IdleTransaction');
}

// this._onScope is true if the transaction was previously on the scope.
if (this._onScope) {
clearActiveTransaction(this._idleHub);
}

return super.finish(endTimestamp);
}

Expand All @@ -159,12 +165,6 @@ export class IdleTransaction extends Transaction {
*/
public initSpanRecorder(maxlen?: number): void {
if (!this.spanRecorder) {
this._initTimeout = setTimeout(() => {
if (!this._finished) {
this.finish();
}
}, this._idleTimeout);

const pushActivity = (id: string): void => {
if (this._finished) {
return;
Expand Down
33 changes: 25 additions & 8 deletions packages/tracing/test/idletransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('IdleTransaction', () => {
});
});

it('removes transaction from scope on finish if onScope is true', () => {
it('removes sampled transaction from scope on finish if onScope is true', () => {
const transaction = new IdleTransaction({ name: 'foo' }, hub, 1000, true);
transaction.initSpanRecorder(10);

Expand All @@ -41,6 +41,17 @@ describe('IdleTransaction', () => {
expect(s.getTransaction()).toBe(undefined);
});
});

it('removes unsampled transaction from scope on finish if onScope is true', () => {
const transaction = new IdleTransaction({ name: 'foo', sampled: false }, hub, 1000, true);

transaction.finish();
jest.runAllTimers();

hub.configureScope(s => {
expect(s.getTransaction()).toBe(undefined);
});
});
});

beforeEach(() => {
Expand Down Expand Up @@ -150,7 +161,7 @@ describe('IdleTransaction', () => {
const transaction = new IdleTransaction({ name: 'foo', startTimestamp: 1234 }, hub, 1000);
transaction.initSpanRecorder(10);

jest.runTimersToTime(DEFAULT_IDLE_TIMEOUT);
jest.advanceTimersByTime(DEFAULT_IDLE_TIMEOUT);
expect(transaction.endTimestamp).toBeDefined();
});

Expand All @@ -159,28 +170,34 @@ describe('IdleTransaction', () => {
transaction.initSpanRecorder(10);
transaction.startChild({});

jest.runTimersToTime(DEFAULT_IDLE_TIMEOUT);
jest.advanceTimersByTime(DEFAULT_IDLE_TIMEOUT);
expect(transaction.endTimestamp).toBeUndefined();
});
});

describe('heartbeat', () => {
it('does not start heartbeat if there is no span recorder', () => {
const transaction = new IdleTransaction({ name: 'foo' }, hub, 1000);
it('does not mark transaction as `DeadlineExceeded` if idle timeout has not been reached', () => {
const HEARTBEAT_INTERVAL = 5000;
// 20s to exceed 3 heartbeats
const transaction = new IdleTransaction({ name: 'foo' }, hub, 20000);
const mockFinish = jest.spyOn(transaction, 'finish');

expect(transaction.status).not.toEqual(SpanStatus.DeadlineExceeded);
expect(mockFinish).toHaveBeenCalledTimes(0);

// Beat 1
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(HEARTBEAT_INTERVAL);
expect(transaction.status).not.toEqual(SpanStatus.DeadlineExceeded);
expect(mockFinish).toHaveBeenCalledTimes(0);

// Beat 2
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(HEARTBEAT_INTERVAL);
expect(transaction.status).not.toEqual(SpanStatus.DeadlineExceeded);
expect(mockFinish).toHaveBeenCalledTimes(0);

// Beat 3
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(HEARTBEAT_INTERVAL);
expect(transaction.status).not.toEqual(SpanStatus.DeadlineExceeded);
expect(mockFinish).toHaveBeenCalledTimes(0);
});

Expand Down