Skip to content

Commit bd2093f

Browse files
authored
chore(cli-repl): include version in logging MONGOSH-774 (#884)
1 parent ca86afc commit bd2093f

File tree

4 files changed

+61
-48
lines changed

4 files changed

+61
-48
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class CliRepl {
114114
* @param {MongoClientOptions} driverOptions - The driver options.
115115
*/
116116
async start(driverUri: string, driverOptions: MongoClientOptions): Promise<void> {
117+
const { version } = require('../package.json');
117118
await this.verifyNodeVersion();
118119
if (this.isPasswordMissing(driverOptions)) {
119120
await this.requirePassword(driverUri, driverOptions);
@@ -175,7 +176,7 @@ class CliRepl {
175176
}
176177
}
177178
await this.loadRcFiles();
178-
this.bus.emit('mongosh:start-mongosh-repl');
179+
this.bus.emit('mongosh:start-mongosh-repl', { version });
179180
await this.mongoshRepl.startRepl(initialized);
180181
}
181182

packages/cli-repl/src/setup-logger-and-telemetry.spec.ts

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('setupLoggerAndTelemetry', () => {
3030

3131
it('works', () => {
3232
setupLoggerAndTelemetry(logId, bus, () => logger, () => analytics);
33-
expect(logOutput).to.be.empty;
33+
expect(logOutput).to.have.lengthOf(1);
3434
expect(analyticsOutput).to.be.empty;
3535

3636
bus.emit('mongosh:new-user', userId, false);
@@ -59,51 +59,52 @@ describe('setupLoggerAndTelemetry', () => {
5959

6060
bus.emit('mongosh:start-loading-cli-scripts', { usesShellOption: true });
6161
bus.emit('mongosh:api-load-file', { nested: true, filename: 'foobar.js' });
62-
bus.emit('mongosh:start-mongosh-repl');
62+
bus.emit('mongosh:start-mongosh-repl', { version: '1.0.0' });
6363
bus.emit('mongosh:api-load-file', { nested: false, filename: 'foobar.js' });
6464
bus.emit('mongosh:mongoshrc-load');
6565
bus.emit('mongosh:mongoshrc-mongorc-warn');
6666
bus.emit('mongosh:eval-cli-script');
6767

68-
expect(logOutput).to.have.lengthOf(24);
69-
expect(logOutput[0].msg).to.equal('mongosh:update-user {"enableTelemetry":false}');
70-
expect(logOutput[1].msg).to.match(/^mongosh:connect/);
71-
expect(logOutput[1].msg).to.match(/"session_id":"5fb3c20ee1507e894e5340f3"/);
72-
expect(logOutput[1].msg).to.match(/"userId":"53defe995fa47e6c13102d9d"/);
73-
expect(logOutput[1].msg).to.match(/"connectionUri":"mongodb:\/\/localhost\/"/);
74-
expect(logOutput[1].msg).to.match(/"is_localhost":true/);
75-
expect(logOutput[1].msg).to.match(/"is_atlas":false/);
76-
expect(logOutput[1].msg).to.match(/"node_version":"v12\.19\.0"/);
77-
expect(logOutput[2].type).to.equal('Error');
78-
expect(logOutput[2].msg).to.match(/meow/);
79-
expect(logOutput[3].msg).to.equal('mongosh:help');
80-
expect(logOutput[4].msg).to.equal('mongosh:use {"db":"admin"}');
81-
expect(logOutput[5].msg).to.equal('mongosh:show {"method":"dbs"}');
82-
expect(logOutput[6].msg).to.equal('mongosh:update-user {"enableTelemetry":true}');
83-
expect(logOutput[7].msg).to.match(/^mongosh:connect/);
84-
expect(logOutput[8].type).to.equal('Error');
85-
expect(logOutput[8].msg).to.match(/meow/);
86-
expect(logOutput[9].msg).to.equal('mongosh:help');
87-
expect(logOutput[10].msg).to.equal('mongosh:use {"db":"admin"}');
88-
expect(logOutput[11].msg).to.equal('mongosh:show {"method":"dbs"}');
89-
expect(logOutput[12].msg).to.equal('mongosh:setCtx {"method":"setCtx"}');
90-
expect(logOutput[13].msg).to.match(/^mongosh:api-call/);
91-
expect(logOutput[13].msg).to.match(/"db":"test-1603986682000"/);
68+
expect(logOutput).to.have.lengthOf(25);
69+
expect(logOutput[0].msg).to.match(/^mongosh:start-logging \{"version":".+","execPath":".+","isCompiledBinary":.+\}$/);
70+
expect(logOutput[1].msg).to.equal('mongosh:update-user {"enableTelemetry":false}');
71+
expect(logOutput[2].msg).to.match(/^mongosh:connect/);
72+
expect(logOutput[2].msg).to.match(/"session_id":"5fb3c20ee1507e894e5340f3"/);
73+
expect(logOutput[2].msg).to.match(/"userId":"53defe995fa47e6c13102d9d"/);
74+
expect(logOutput[2].msg).to.match(/"connectionUri":"mongodb:\/\/localhost\/"/);
75+
expect(logOutput[2].msg).to.match(/"is_localhost":true/);
76+
expect(logOutput[2].msg).to.match(/"is_atlas":false/);
77+
expect(logOutput[2].msg).to.match(/"node_version":"v12\.19\.0"/);
78+
expect(logOutput[3].type).to.equal('Error');
79+
expect(logOutput[3].msg).to.match(/meow/);
80+
expect(logOutput[4].msg).to.equal('mongosh:help');
81+
expect(logOutput[5].msg).to.equal('mongosh:use {"db":"admin"}');
82+
expect(logOutput[6].msg).to.equal('mongosh:show {"method":"dbs"}');
83+
expect(logOutput[7].msg).to.equal('mongosh:update-user {"enableTelemetry":true}');
84+
expect(logOutput[8].msg).to.match(/^mongosh:connect/);
85+
expect(logOutput[9].type).to.equal('Error');
86+
expect(logOutput[9].msg).to.match(/meow/);
87+
expect(logOutput[10].msg).to.equal('mongosh:help');
88+
expect(logOutput[11].msg).to.equal('mongosh:use {"db":"admin"}');
89+
expect(logOutput[12].msg).to.equal('mongosh:show {"method":"dbs"}');
90+
expect(logOutput[13].msg).to.equal('mongosh:setCtx {"method":"setCtx"}');
9291
expect(logOutput[14].msg).to.match(/^mongosh:api-call/);
93-
expect(logOutput[14].msg).to.match(/"email":"<email>"/);
94-
expect(logOutput[15].msg).to.match(/^mongosh:evaluate-input/);
95-
expect(logOutput[15].msg).to.match(/"input":"1\+1"/);
96-
expect(logOutput[16].msg).to.match(/"version":"3.6.1"/);
97-
expect(logOutput[17].msg).to.equal('mongosh:start-loading-cli-scripts');
98-
expect(logOutput[18].msg).to.match(/^mongosh:api-load-file/);
99-
expect(logOutput[18].msg).to.match(/"nested":true/);
100-
expect(logOutput[18].msg).to.match(/"filename":"foobar.js"/);
101-
expect(logOutput[19].msg).to.equal('mongosh:start-mongosh-repl');
102-
expect(logOutput[20].msg).to.match(/"nested":false/);
103-
expect(logOutput[20].msg).to.match(/"filename":"foobar.js"/);
104-
expect(logOutput[21].msg).to.equal('mongosh:mongoshrc-load');
105-
expect(logOutput[22].msg).to.equal('mongosh:mongoshrc-mongorc-warn');
106-
expect(logOutput[23].msg).to.equal('mongosh:eval-cli-script');
92+
expect(logOutput[14].msg).to.match(/"db":"test-1603986682000"/);
93+
expect(logOutput[15].msg).to.match(/^mongosh:api-call/);
94+
expect(logOutput[15].msg).to.match(/"email":"<email>"/);
95+
expect(logOutput[16].msg).to.match(/^mongosh:evaluate-input/);
96+
expect(logOutput[16].msg).to.match(/"input":"1\+1"/);
97+
expect(logOutput[17].msg).to.match(/"version":"3.6.1"/);
98+
expect(logOutput[18].msg).to.equal('mongosh:start-loading-cli-scripts');
99+
expect(logOutput[19].msg).to.match(/^mongosh:api-load-file/);
100+
expect(logOutput[19].msg).to.match(/"nested":true/);
101+
expect(logOutput[19].msg).to.match(/"filename":"foobar.js"/);
102+
expect(logOutput[20].msg).to.equal('mongosh:start-mongosh-repl {"version":"1.0.0"}');
103+
expect(logOutput[21].msg).to.match(/"nested":false/);
104+
expect(logOutput[21].msg).to.match(/"filename":"foobar.js"/);
105+
expect(logOutput[22].msg).to.equal('mongosh:mongoshrc-load');
106+
expect(logOutput[23].msg).to.equal('mongosh:mongoshrc-mongorc-warn');
107+
expect(logOutput[24].msg).to.equal('mongosh:eval-cli-script');
107108

108109

109110
const mongosh_version = require('../package.json').version;
@@ -311,11 +312,11 @@ describe('setupLoggerAndTelemetry', () => {
311312
setupLoggerAndTelemetry('5fb3c20ee1507e894e5340f3', bus, () => logger, () => { throw new Error(); });
312313
bus.emit('mongosh:new-user', userId, true);
313314
expect(analyticsOutput).to.be.empty;
314-
expect(logOutput).to.have.lengthOf(1);
315-
expect(logOutput[0].type).to.equal('Error');
316-
expect(logOutput[0].name).to.equal('mongosh');
315+
expect(logOutput).to.have.lengthOf(2);
316+
expect(logOutput[1].type).to.equal('Error');
317+
expect(logOutput[1].name).to.equal('mongosh');
317318
bus.emit('mongosh:help');
318319
expect(analyticsOutput).to.be.empty;
319-
expect(logOutput).to.have.lengthOf(2);
320+
expect(logOutput).to.have.lengthOf(3);
320321
});
321322
});

packages/cli-repl/src/setup-logger-and-telemetry.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
ConnectEvent,
1212
ScriptLoadFileEvent,
1313
StartLoadingCliScriptsEvent,
14+
StartMongoshReplEvent,
1415
MongocryptdTrySpawnEvent,
1516
MongocryptdLogEvent,
1617
MongocryptdErrorEvent
@@ -52,6 +53,12 @@ export default function setupLoggerAndTelemetry(
5253
arch: process.arch
5354
};
5455

56+
log.info('mongosh:start-logging', {
57+
version: mongosh_version,
58+
execPath: process.execPath,
59+
isCompiledBinary: process.execPath === process.argv[1]
60+
});
61+
5562
let analytics: MongoshAnalytics = new NoopAnalytics();
5663
try {
5764
analytics = makeAnalytics();
@@ -64,8 +71,8 @@ export default function setupLoggerAndTelemetry(
6471
// state here so that the places where the events are emitted don't have to
6572
// be aware of this distinction.
6673
let hasStartedMongoshRepl = false;
67-
bus.on('mongosh:start-mongosh-repl', () => {
68-
log.info('mongosh:start-mongosh-repl');
74+
bus.on('mongosh:start-mongosh-repl', (ev: StartMongoshReplEvent) => {
75+
log.info('mongosh:start-mongosh-repl', ev);
6976
hasStartedMongoshRepl = true;
7077
});
7178

packages/types/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ export interface MongocryptdLogEvent {
7272
logEntry: any;
7373
}
7474

75+
export interface StartMongoshReplEvent {
76+
version: string;
77+
}
78+
7579
export interface MongoshBusEventsMap {
7680
'mongosh:connect': (ev: ConnectEvent) => void;
7781
'mongosh:driver-initialized': (driverMetadata: any) => void;
@@ -90,7 +94,7 @@ export interface MongoshBusEventsMap {
9094
'mongosh:warn': (ev: ApiWarning) => void;
9195
'mongosh:api-load-file': (ev: ScriptLoadFileEvent) => void;
9296
'mongosh:start-loading-cli-scripts': (event: StartLoadingCliScriptsEvent) => void;
93-
'mongosh:start-mongosh-repl': () => void;
97+
'mongosh:start-mongosh-repl': (ev: StartMongoshReplEvent) => void;
9498
'mongosh:mongoshrc-load': () => void;
9599
'mongosh:mongoshrc-mongorc-warn': () => void;
96100
'mongosh:eval-cli-script': () => void;

0 commit comments

Comments
 (0)