Skip to content

Commit ecf5653

Browse files
authored
chore(cli-repl): log telemetry flushing errors and duration MONGOSH-1038 (#1145)
1 parent 32ea20f commit ecf5653

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

packages/cli-repl/src/cli-repl.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,17 @@ describe('CliRepl', () => {
817817
expect(apiEvents[0].properties.count).to.equal(1);
818818
});
819819

820+
it('includes a statement about flushed telemetry in the log', async() => {
821+
await cliRepl.start(await testServer.connectionString(), {});
822+
const { logFilePath } = cliRepl.logWriter;
823+
input.write('db.hello()\n');
824+
input.write('exit\n');
825+
await waitBus(cliRepl.bus, 'mongosh:closed');
826+
const flushEntry = (await readReplLogfile(logFilePath)).find(entry => entry.id === 1_000_000_045);
827+
expect(flushEntry.attr.flushError).to.equal(null);
828+
expect(flushEntry.attr.flushDuration).to.be.a('number');
829+
});
830+
820831
context('with a 5.0+ server', () => {
821832
skipIfServerVersion(testServer, '<= 4.4');
822833

packages/cli-repl/src/cli-repl.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,25 @@ class CliRepl {
500500
}
501501
this.closing = true;
502502
const analytics = this.analytics;
503+
let flushError: string | null = null;
504+
let flushDuration: number | null = null;
503505
if (analytics) {
506+
const flushStart = Date.now();
504507
try {
505508
await promisify(analytics.flush.bind(analytics))();
506-
} catch { /* ignore */ }
509+
} catch (err: any) {
510+
flushError = err.message;
511+
} finally {
512+
flushDuration = Date.now() - flushStart;
513+
}
507514
}
508515
this.mongocryptdManager.close();
509-
await this.logWriter?.flush?.();
516+
// eslint-disable-next-line chai-friendly/no-unused-expressions
517+
this.logWriter?.info('MONGOSH', mongoLogId(1_000_000_045), 'analytics', 'Flushed outstanding data', {
518+
flushError,
519+
flushDuration
520+
});
521+
await this.logWriter?.flush();
510522
this.bus.emit('mongosh:closed');
511523
}
512524

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,6 @@ export function setupLoggerAndTelemetry(
488488
error: ev.error.message
489489
});
490490
});
491+
492+
// NB: mongoLogId(1_000_000_045) is used in cli-repl itself
491493
}

0 commit comments

Comments
 (0)