Skip to content

Commit

Permalink
fix(logging): do not accumulate telemetry events that are never sent (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
addaleax authored Jul 31, 2024
1 parent 5125498 commit 35d8c6d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/logging/src/setup-logger-and-telemetry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable mocha/max-top-level-suites */
import { expect } from 'chai';
import { MongoLogWriter } from 'mongodb-log-writer';
import { setupLoggerAndTelemetry } from './';
Expand Down Expand Up @@ -561,6 +562,7 @@ describe('setupLoggerAndTelemetry', function () {
expect(analyticsOutput).to.be.empty;

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

logOutput = [];
analyticsOutput = [];
Expand Down Expand Up @@ -704,6 +706,7 @@ describe('setupLoggerAndTelemetry', function () {
logOutput = [];
analyticsOutput = [];

bus.emit('mongosh:evaluate-started');
bus.emit('mongosh:api-call', {
method: 'cloneDatabase',
class: 'Database',
Expand All @@ -724,4 +727,27 @@ describe('setupLoggerAndTelemetry', function () {
});
expect(analyticsOutput).to.have.lengthOf(2);
});

it('does not track database calls outside of evaluate-{started,finished}', function () {
setupLoggerAndTelemetry(bus, logger, analytics, {}, '1.0.0');
expect(logOutput).to.have.lengthOf(0);
expect(analyticsOutput).to.be.empty;

bus.emit('mongosh:new-user', { userId, anonymousId: userId });

logOutput = [];
analyticsOutput = [];

bus.emit('mongosh:api-call', {
method: 'cloneDatabase',
class: 'Database',
deprecated: true,
callDepth: 0,
isAsync: true,
});
bus.emit('mongosh:evaluate-finished');

expect(logOutput).to.have.lengthOf(0);
expect(analyticsOutput).to.be.empty;
});
});
5 changes: 5 additions & 0 deletions packages/logging/src/setup-logger-and-telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,10 @@ export function setupLoggerAndTelemetry(

const deprecatedApiCalls = new MultiSet<Pick<ApiEvent, 'class' | 'method'>>();
const apiCalls = new MultiSet<Pick<ApiEvent, 'class' | 'method'>>();
let apiCallTrackingEnabled = false;
bus.on('mongosh:api-call', function (ev: ApiEvent) {
// Only track if we have previously seen a mongosh:evaluate-started call
if (!apiCallTrackingEnabled) return;
if (ev.deprecated) {
deprecatedApiCalls.add({ class: ev.class, method: ev.method });
}
Expand All @@ -641,6 +644,7 @@ export function setupLoggerAndTelemetry(
}
});
bus.on('mongosh:evaluate-started', function () {
apiCallTrackingEnabled = true;
// Clear API calls before evaluation starts. This is important because
// some API calls are also emitted by mongosh CLI repl internals,
// but we only care about those emitted from user code (i.e. during
Expand Down Expand Up @@ -680,6 +684,7 @@ export function setupLoggerAndTelemetry(
}
deprecatedApiCalls.clear();
apiCalls.clear();
apiCallTrackingEnabled = false;
});

// Log ids 1_000_000_034 through 1_000_000_042 are reserved for the
Expand Down

0 comments on commit 35d8c6d

Please sign in to comment.