diff --git a/package-lock.json b/package-lock.json index 6a2ef2512..952e07058 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,7 +4,6 @@ "requires": true, "packages": { "": { - "name": "mongosh", "license": "Apache-2.0", "bin": { "mongosh": "packages/cli-repl/bin/mongosh.js" diff --git a/packages/cli-repl/src/cli-repl.spec.ts b/packages/cli-repl/src/cli-repl.spec.ts index a7708199f..70119e90c 100644 --- a/packages/cli-repl/src/cli-repl.spec.ts +++ b/packages/cli-repl/src/cli-repl.spec.ts @@ -946,6 +946,18 @@ describe('CliRepl', () => { expect(requests).to.have.lengthOf(2); }); + it('sends out telemetry if the repl is running in an interactive mode in a containerized environment', async() => { + cliRepl = new CliRepl(cliReplOptions); + cliRepl.getIsContainerizedEnvironment = () => { + return Promise.resolve(true); + }; + await cliRepl.start(await testServer.connectionString(), {}); + input.write('db.hello()\n'); + input.write('exit\n'); + await waitBus(cliRepl.bus, 'mongosh:closed'); + expect(requests).to.have.lengthOf(2); + }); + it('does not send out telemetry if the user starts with a no-telemetry config', async() => { await fs.writeFile(path.join(tmpdir.path, 'config'), EJSON.stringify({ enableTelemetry: false })); await cliRepl.start(await testServer.connectionString(), {}); @@ -986,6 +998,19 @@ describe('CliRepl', () => { expect(requests).to.have.lengthOf(0); }); + it('does not send out telemetry if the repl is running in non-interactive mode in a containerized environment', async() => { + cliReplOptions.shellCliOptions.eval = ['db.hello()']; + cliRepl = new CliRepl(cliReplOptions); + cliRepl.getIsContainerizedEnvironment = () => { + return Promise.resolve(true); + }; + await startWithExpectedImmediateExit( + cliRepl, + await testServer.connectionString() + ); + expect(requests).to.have.lengthOf(0); + }); + context('with a 5.0+ server', () => { skipIfServerVersion(testServer, '<= 4.4'); diff --git a/packages/cli-repl/src/cli-repl.ts b/packages/cli-repl/src/cli-repl.ts index 994b95ae3..3525435e7 100644 --- a/packages/cli-repl/src/cli-repl.ts +++ b/packages/cli-repl/src/cli-repl.ts @@ -95,6 +95,7 @@ class CliRepl implements MongoshIOProvider { warnedAboutInaccessibleFiles = false; onExit: (code?: number) => Promise; closing = false; + isContainerizedEnvironment = false; /** * Instantiate the new CLI Repl. @@ -155,6 +156,29 @@ class CliRepl implements MongoshIOProvider { }); } + async getIsContainerizedEnvironment() { + // Check for dockerenv file first + try { + await fs.stat('/.dockerenv'); + return true; + } catch { + try { + // Check if there is any mention of docker / lxc / k8s in control groups + const cgroup = await fs.readFile('/proc/self/cgroup', 'utf8'); + return /\b(docker|lxc|kubepods)\b/.test(cgroup); + } catch { + return false; + } + } + } + + get forceDisableTelemetry(): boolean { + return ( + this.globalConfig?.forceDisableTelemetry || + (this.isContainerizedEnvironment && !this.mongoshRepl.isInteractive) + ); + } + /** * Setup CLI environment: serviceProvider, ShellEvaluator, log connection * information, external editor, and finally start the repl. @@ -167,6 +191,9 @@ class CliRepl implements MongoshIOProvider { const { version } = require('../package.json'); await this.verifyNodeVersion(); + this.isContainerizedEnvironment = + await this.getIsContainerizedEnvironment(); + if (!this.cliOptions.nodb) { const cs = new ConnectionString(driverUri); const searchParams = cs.typedSearchParams(); @@ -215,7 +242,8 @@ class CliRepl implements MongoshIOProvider { this.toggleableAnalytics, { platform: process.platform, - arch: process.arch + arch: process.arch, + is_containerized: this.isContainerizedEnvironment, }, require('../package.json').version); @@ -329,7 +357,7 @@ class CliRepl implements MongoshIOProvider { // case. return; } - if (enabled && !this.globalConfig.forceDisableTelemetry) { + if (enabled && !this.forceDisableTelemetry) { this.toggleableAnalytics.enable(); } else { this.toggleableAnalytics.disable(); diff --git a/packages/cli-repl/src/mongosh-repl.ts b/packages/cli-repl/src/mongosh-repl.ts index ed204845d..6cbd465e6 100644 --- a/packages/cli-repl/src/mongosh-repl.ts +++ b/packages/cli-repl/src/mongosh-repl.ts @@ -167,6 +167,10 @@ class MongoshNodeRepl implements EvaluationListener { this.runtimeState().instanceState.isInteractive = value; } + get isInteractive(): boolean { + return this.runtimeState().instanceState.isInteractive; + } + /** * Create a Node.js REPL instance that can run mongosh commands, * print greeting messages, and set up autocompletion and