From cb1d4d430f6aaefcd0349f4c1a76ee29bdad0537 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 19 May 2021 14:58:15 +0200 Subject: [PATCH] chore(cli-repl): include version in logging MONGOSH-774 --- packages/cli-repl/src/cli-repl.ts | 3 +- .../src/setup-logger-and-telemetry.spec.ts | 89 ++++++++++--------- .../src/setup-logger-and-telemetry.ts | 11 ++- packages/types/src/index.ts | 6 +- 4 files changed, 61 insertions(+), 48 deletions(-) diff --git a/packages/cli-repl/src/cli-repl.ts b/packages/cli-repl/src/cli-repl.ts index ab9597c515..a0f81b1b7a 100644 --- a/packages/cli-repl/src/cli-repl.ts +++ b/packages/cli-repl/src/cli-repl.ts @@ -114,6 +114,7 @@ class CliRepl { * @param {MongoClientOptions} driverOptions - The driver options. */ async start(driverUri: string, driverOptions: MongoClientOptions): Promise { + const { version } = require('../package.json'); await this.verifyNodeVersion(); if (this.isPasswordMissing(driverOptions)) { await this.requirePassword(driverUri, driverOptions); @@ -175,7 +176,7 @@ class CliRepl { } } await this.loadRcFiles(); - this.bus.emit('mongosh:start-mongosh-repl'); + this.bus.emit('mongosh:start-mongosh-repl', { version }); await this.mongoshRepl.startRepl(initialized); } diff --git a/packages/cli-repl/src/setup-logger-and-telemetry.spec.ts b/packages/cli-repl/src/setup-logger-and-telemetry.spec.ts index 0caecb62d0..cc66262266 100644 --- a/packages/cli-repl/src/setup-logger-and-telemetry.spec.ts +++ b/packages/cli-repl/src/setup-logger-and-telemetry.spec.ts @@ -29,7 +29,7 @@ describe('setupLoggerAndTelemetry', () => { it('works', () => { setupLoggerAndTelemetry(logId, bus, () => logger, () => analytics); - expect(logOutput).to.be.empty; + expect(logOutput).to.have.lengthOf(1); expect(analyticsOutput).to.be.empty; bus.emit('mongosh:new-user', userId, false); @@ -58,51 +58,52 @@ describe('setupLoggerAndTelemetry', () => { bus.emit('mongosh:start-loading-cli-scripts', { usesShellOption: true }); bus.emit('mongosh:api-load-file', { nested: true, filename: 'foobar.js' }); - bus.emit('mongosh:start-mongosh-repl'); + bus.emit('mongosh:start-mongosh-repl', { version: '1.0.0' }); bus.emit('mongosh:api-load-file', { nested: false, filename: 'foobar.js' }); bus.emit('mongosh:mongoshrc-load'); bus.emit('mongosh:mongoshrc-mongorc-warn'); bus.emit('mongosh:eval-cli-script'); - expect(logOutput).to.have.lengthOf(24); - expect(logOutput[0].msg).to.equal('mongosh:update-user {"enableTelemetry":false}'); - expect(logOutput[1].msg).to.match(/^mongosh:connect/); - expect(logOutput[1].msg).to.match(/"session_id":"5fb3c20ee1507e894e5340f3"/); - expect(logOutput[1].msg).to.match(/"userId":"53defe995fa47e6c13102d9d"/); - expect(logOutput[1].msg).to.match(/"connectionUri":"mongodb:\/\/localhost\/"/); - expect(logOutput[1].msg).to.match(/"is_localhost":true/); - expect(logOutput[1].msg).to.match(/"is_atlas":false/); - expect(logOutput[1].msg).to.match(/"node_version":"v12\.19\.0"/); - expect(logOutput[2].type).to.equal('Error'); - expect(logOutput[2].msg).to.match(/meow/); - expect(logOutput[3].msg).to.equal('mongosh:help'); - expect(logOutput[4].msg).to.equal('mongosh:use {"db":"admin"}'); - expect(logOutput[5].msg).to.equal('mongosh:show {"method":"dbs"}'); - expect(logOutput[6].msg).to.equal('mongosh:update-user {"enableTelemetry":true}'); - expect(logOutput[7].msg).to.match(/^mongosh:connect/); - expect(logOutput[8].type).to.equal('Error'); - expect(logOutput[8].msg).to.match(/meow/); - expect(logOutput[9].msg).to.equal('mongosh:help'); - expect(logOutput[10].msg).to.equal('mongosh:use {"db":"admin"}'); - expect(logOutput[11].msg).to.equal('mongosh:show {"method":"dbs"}'); - expect(logOutput[12].msg).to.equal('mongosh:setCtx {"method":"setCtx"}'); - expect(logOutput[13].msg).to.match(/^mongosh:api-call/); - expect(logOutput[13].msg).to.match(/"db":"test-1603986682000"/); + expect(logOutput).to.have.lengthOf(25); + expect(logOutput[0].msg).to.match(/^mongosh:start-logging \{"version":".+","execPath":".+","isCompiledBinary":.+\}$/); + expect(logOutput[1].msg).to.equal('mongosh:update-user {"enableTelemetry":false}'); + expect(logOutput[2].msg).to.match(/^mongosh:connect/); + expect(logOutput[2].msg).to.match(/"session_id":"5fb3c20ee1507e894e5340f3"/); + expect(logOutput[2].msg).to.match(/"userId":"53defe995fa47e6c13102d9d"/); + expect(logOutput[2].msg).to.match(/"connectionUri":"mongodb:\/\/localhost\/"/); + expect(logOutput[2].msg).to.match(/"is_localhost":true/); + expect(logOutput[2].msg).to.match(/"is_atlas":false/); + expect(logOutput[2].msg).to.match(/"node_version":"v12\.19\.0"/); + expect(logOutput[3].type).to.equal('Error'); + expect(logOutput[3].msg).to.match(/meow/); + expect(logOutput[4].msg).to.equal('mongosh:help'); + expect(logOutput[5].msg).to.equal('mongosh:use {"db":"admin"}'); + expect(logOutput[6].msg).to.equal('mongosh:show {"method":"dbs"}'); + expect(logOutput[7].msg).to.equal('mongosh:update-user {"enableTelemetry":true}'); + expect(logOutput[8].msg).to.match(/^mongosh:connect/); + expect(logOutput[9].type).to.equal('Error'); + expect(logOutput[9].msg).to.match(/meow/); + expect(logOutput[10].msg).to.equal('mongosh:help'); + expect(logOutput[11].msg).to.equal('mongosh:use {"db":"admin"}'); + expect(logOutput[12].msg).to.equal('mongosh:show {"method":"dbs"}'); + expect(logOutput[13].msg).to.equal('mongosh:setCtx {"method":"setCtx"}'); expect(logOutput[14].msg).to.match(/^mongosh:api-call/); - expect(logOutput[14].msg).to.match(/"email":""/); - expect(logOutput[15].msg).to.match(/^mongosh:evaluate-input/); - expect(logOutput[15].msg).to.match(/"input":"1\+1"/); - expect(logOutput[16].msg).to.match(/"version":"3.6.1"/); - expect(logOutput[17].msg).to.equal('mongosh:start-loading-cli-scripts'); - expect(logOutput[18].msg).to.match(/^mongosh:api-load-file/); - expect(logOutput[18].msg).to.match(/"nested":true/); - expect(logOutput[18].msg).to.match(/"filename":"foobar.js"/); - expect(logOutput[19].msg).to.equal('mongosh:start-mongosh-repl'); - expect(logOutput[20].msg).to.match(/"nested":false/); - expect(logOutput[20].msg).to.match(/"filename":"foobar.js"/); - expect(logOutput[21].msg).to.equal('mongosh:mongoshrc-load'); - expect(logOutput[22].msg).to.equal('mongosh:mongoshrc-mongorc-warn'); - expect(logOutput[23].msg).to.equal('mongosh:eval-cli-script'); + expect(logOutput[14].msg).to.match(/"db":"test-1603986682000"/); + expect(logOutput[15].msg).to.match(/^mongosh:api-call/); + expect(logOutput[15].msg).to.match(/"email":""/); + expect(logOutput[16].msg).to.match(/^mongosh:evaluate-input/); + expect(logOutput[16].msg).to.match(/"input":"1\+1"/); + expect(logOutput[17].msg).to.match(/"version":"3.6.1"/); + expect(logOutput[18].msg).to.equal('mongosh:start-loading-cli-scripts'); + expect(logOutput[19].msg).to.match(/^mongosh:api-load-file/); + expect(logOutput[19].msg).to.match(/"nested":true/); + expect(logOutput[19].msg).to.match(/"filename":"foobar.js"/); + expect(logOutput[20].msg).to.equal('mongosh:start-mongosh-repl {"version":"1.0.0"}'); + expect(logOutput[21].msg).to.match(/"nested":false/); + expect(logOutput[21].msg).to.match(/"filename":"foobar.js"/); + expect(logOutput[22].msg).to.equal('mongosh:mongoshrc-load'); + expect(logOutput[23].msg).to.equal('mongosh:mongoshrc-mongorc-warn'); + expect(logOutput[24].msg).to.equal('mongosh:eval-cli-script'); const mongosh_version = require('../package.json').version; @@ -243,11 +244,11 @@ describe('setupLoggerAndTelemetry', () => { setupLoggerAndTelemetry('5fb3c20ee1507e894e5340f3', bus, () => logger, () => { throw new Error(); }); bus.emit('mongosh:new-user', userId, true); expect(analyticsOutput).to.be.empty; - expect(logOutput).to.have.lengthOf(1); - expect(logOutput[0].type).to.equal('Error'); - expect(logOutput[0].name).to.equal('mongosh'); + expect(logOutput).to.have.lengthOf(2); + expect(logOutput[1].type).to.equal('Error'); + expect(logOutput[1].name).to.equal('mongosh'); bus.emit('mongosh:help'); expect(analyticsOutput).to.be.empty; - expect(logOutput).to.have.lengthOf(2); + expect(logOutput).to.have.lengthOf(3); }); }); diff --git a/packages/cli-repl/src/setup-logger-and-telemetry.ts b/packages/cli-repl/src/setup-logger-and-telemetry.ts index 1de430565a..9374bf9280 100644 --- a/packages/cli-repl/src/setup-logger-and-telemetry.ts +++ b/packages/cli-repl/src/setup-logger-and-telemetry.ts @@ -11,6 +11,7 @@ import type { ConnectEvent, ScriptLoadFileEvent, StartLoadingCliScriptsEvent, + StartMongoshReplEvent, MongocryptdTrySpawnEvent, MongocryptdLogEvent, MongocryptdErrorEvent @@ -52,6 +53,12 @@ export default function setupLoggerAndTelemetry( arch: process.arch }; + log.info('mongosh:start-logging', { + version: mongosh_version, + execPath: process.execPath, + isCompiledBinary: process.execPath === process.argv[1] + }); + let analytics: MongoshAnalytics = new NoopAnalytics(); try { analytics = makeAnalytics(); @@ -64,8 +71,8 @@ export default function setupLoggerAndTelemetry( // state here so that the places where the events are emitted don't have to // be aware of this distinction. let hasStartedMongoshRepl = false; - bus.on('mongosh:start-mongosh-repl', () => { - log.info('mongosh:start-mongosh-repl'); + bus.on('mongosh:start-mongosh-repl', (ev: StartMongoshReplEvent) => { + log.info('mongosh:start-mongosh-repl', ev); hasStartedMongoshRepl = true; }); diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 214d407e8d..55f4496946 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -72,6 +72,10 @@ export interface MongocryptdLogEvent { logEntry: any; } +export interface StartMongoshReplEvent { + version: string; +} + export interface MongoshBusEventsMap { 'mongosh:connect': (ev: ConnectEvent) => void; 'mongosh:driver-initialized': (driverMetadata: any) => void; @@ -88,7 +92,7 @@ export interface MongoshBusEventsMap { 'mongosh:warn': (ev: ApiWarning) => void; 'mongosh:api-load-file': (ev: ScriptLoadFileEvent) => void; 'mongosh:start-loading-cli-scripts': (event: StartLoadingCliScriptsEvent) => void; - 'mongosh:start-mongosh-repl': () => void; + 'mongosh:start-mongosh-repl': (ev: StartMongoshReplEvent) => void; 'mongosh:mongoshrc-load': () => void; 'mongosh:mongoshrc-mongorc-warn': () => void; 'mongosh:eval-cli-script': () => void;