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(logging): do not accumulate telemetry events that are never sent #2106

Merged
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
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
Loading