Skip to content

Commit 35d8c6d

Browse files
authored
fix(logging): do not accumulate telemetry events that are never sent (#2106)
Compass currently does not emit `evaluate-started`/`evaluate-finished` events for shell API calls. We should not gather data for the corresponding telemetry events if we never get to send them.
1 parent 5125498 commit 35d8c6d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

packages/logging/src/setup-logger-and-telemetry.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable mocha/max-top-level-suites */
12
import { expect } from 'chai';
23
import { MongoLogWriter } from 'mongodb-log-writer';
34
import { setupLoggerAndTelemetry } from './';
@@ -561,6 +562,7 @@ describe('setupLoggerAndTelemetry', function () {
561562
expect(analyticsOutput).to.be.empty;
562563

563564
bus.emit('mongosh:new-user', { userId, anonymousId: userId });
565+
bus.emit('mongosh:evaluate-started');
564566

565567
logOutput = [];
566568
analyticsOutput = [];
@@ -704,6 +706,7 @@ describe('setupLoggerAndTelemetry', function () {
704706
logOutput = [];
705707
analyticsOutput = [];
706708

709+
bus.emit('mongosh:evaluate-started');
707710
bus.emit('mongosh:api-call', {
708711
method: 'cloneDatabase',
709712
class: 'Database',
@@ -724,4 +727,27 @@ describe('setupLoggerAndTelemetry', function () {
724727
});
725728
expect(analyticsOutput).to.have.lengthOf(2);
726729
});
730+
731+
it('does not track database calls outside of evaluate-{started,finished}', function () {
732+
setupLoggerAndTelemetry(bus, logger, analytics, {}, '1.0.0');
733+
expect(logOutput).to.have.lengthOf(0);
734+
expect(analyticsOutput).to.be.empty;
735+
736+
bus.emit('mongosh:new-user', { userId, anonymousId: userId });
737+
738+
logOutput = [];
739+
analyticsOutput = [];
740+
741+
bus.emit('mongosh:api-call', {
742+
method: 'cloneDatabase',
743+
class: 'Database',
744+
deprecated: true,
745+
callDepth: 0,
746+
isAsync: true,
747+
});
748+
bus.emit('mongosh:evaluate-finished');
749+
750+
expect(logOutput).to.have.lengthOf(0);
751+
expect(analyticsOutput).to.be.empty;
752+
});
727753
});

packages/logging/src/setup-logger-and-telemetry.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,10 @@ export function setupLoggerAndTelemetry(
632632

633633
const deprecatedApiCalls = new MultiSet<Pick<ApiEvent, 'class' | 'method'>>();
634634
const apiCalls = new MultiSet<Pick<ApiEvent, 'class' | 'method'>>();
635+
let apiCallTrackingEnabled = false;
635636
bus.on('mongosh:api-call', function (ev: ApiEvent) {
637+
// Only track if we have previously seen a mongosh:evaluate-started call
638+
if (!apiCallTrackingEnabled) return;
636639
if (ev.deprecated) {
637640
deprecatedApiCalls.add({ class: ev.class, method: ev.method });
638641
}
@@ -641,6 +644,7 @@ export function setupLoggerAndTelemetry(
641644
}
642645
});
643646
bus.on('mongosh:evaluate-started', function () {
647+
apiCallTrackingEnabled = true;
644648
// Clear API calls before evaluation starts. This is important because
645649
// some API calls are also emitted by mongosh CLI repl internals,
646650
// but we only care about those emitted from user code (i.e. during
@@ -680,6 +684,7 @@ export function setupLoggerAndTelemetry(
680684
}
681685
deprecatedApiCalls.clear();
682686
apiCalls.clear();
687+
apiCallTrackingEnabled = false;
683688
});
684689

685690
// Log ids 1_000_000_034 through 1_000_000_042 are reserved for the

0 commit comments

Comments
 (0)