Skip to content

Commit

Permalink
fix(cli-repl): properly report API telemetry for plain-vm eval (#1859)
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax authored Mar 7, 2024
1 parent b3780c6 commit 491f5b0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/cli-repl/src/async-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function start(opts: AsyncREPLOptions): REPLServer {
(opts as ReplOptions).breakEvalOnSigint = true;
}

const repl = (opts.start ?? originalStart)(opts);
const repl: REPLServer = (opts.start ?? originalStart)(opts);
const originalEval = promisify(
wrapPauseInput(repl.input, wrapNoSyncDomainError(repl.eval.bind(repl)))
// string, Context, string is not string, any, string
Expand Down
37 changes: 35 additions & 2 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1288,15 +1288,16 @@ describe('CliRepl', function () {
setTelemetryDelay(0);
});

it('timeouts fast', async function () {
it('times out fast', async function () {
const testStartMs = Date.now();
// The `httpRequestTimeout` of our Segment Analytics is set to
// 1000ms which makes these requests fail as they exceed the timeout.
// Segment will silently fail http request errors when tracking and flushing.
// This will cause the test to fail if the system running the tests is
// unable to flush telemetry in 1500ms.
this.timeout(2500);
setTelemetryDelay(5000);
await cliRepl.start(await testServer.connectionString(), {});
this.timeout(Date.now() - testStartMs + 2500); // Do not include connection time in 2.5s timeout
input.write('use somedb;\n');
input.write('exit\n');
await waitBus(cliRepl.bus, 'mongosh:closed');
Expand Down Expand Up @@ -1451,6 +1452,38 @@ describe('CliRepl', function () {
expect(totalEventsTracked).to.equal(5);
});

it('sends out telemetry data for multiple command line scripts', async function () {
cliReplOptions.shellCliOptions.eval = [
'db.hello(); db.hello();',
'db.hello()',
];
cliRepl = new CliRepl(cliReplOptions);
await startWithExpectedImmediateExit(
cliRepl,
await testServer.connectionString()
);
expect(totalEventsTracked).to.equal(7);

const apiEvents = requests
.map((req) =>
JSON.parse(req.body).batch.filter(
(entry) => entry.event === 'API Call'
)
)
.flat();
expect(apiEvents).to.have.lengthOf(2);
expect(
apiEvents.map((e) => [
e.properties.class,
e.properties.method,
e.properties.count,
])
).to.deep.equal([
['Database', 'hello', 2],
['Database', 'hello', 1],
]);
});

it('sends out telemetry if the repl is running in an interactive mode in a containerized environment', async function () {
cliRepl = new CliRepl(cliReplOptions);
cliRepl.getIsContainerizedEnvironment = () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ class MongoshNodeRepl implements EvaluationListener {
}

markTime(TimingCategories.UserConfigLoading, 'set up history file');

// Similar calls are added for plain-vm evaluation below
(repl as any).on(asyncRepl.evalStart, () => {
this.bus.emit('mongosh:evaluate-started');
});
Expand Down Expand Up @@ -746,6 +748,7 @@ class MongoshNodeRepl implements EvaluationListener {
);
};
});
this.bus.emit('mongosh:evaluate-started');
try {
process.addListener('SIGINT', asyncSigintHandler);
return await Promise.race([
Expand All @@ -762,6 +765,7 @@ class MongoshNodeRepl implements EvaluationListener {
]);
} finally {
process.removeListener('SIGINT', asyncSigintHandler);
this.bus.emit('mongosh:evaluate-finished');
}
}

Expand Down

0 comments on commit 491f5b0

Please sign in to comment.