From d24463ddaa961a6119ea517b4a52f733e25ee940 Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Mon, 27 Nov 2023 15:26:50 +0100 Subject: [PATCH 01/10] chore: analytics sampler --- .../logging/src/analytics-helpers.spec.ts | 59 ++++++++++++++++- packages/logging/src/analytics-helpers.ts | 66 +++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/packages/logging/src/analytics-helpers.spec.ts b/packages/logging/src/analytics-helpers.spec.ts index 04454db14..5ae787802 100644 --- a/packages/logging/src/analytics-helpers.spec.ts +++ b/packages/logging/src/analytics-helpers.spec.ts @@ -4,7 +4,11 @@ import fs from 'fs'; import { promisify } from 'util'; import { expect } from 'chai'; import type { MongoshAnalytics } from './analytics-helpers'; -import { ToggleableAnalytics, ThrottledAnalytics } from './analytics-helpers'; +import { + ToggleableAnalytics, + ThrottledAnalytics, + SampledAnalytics, +} from './analytics-helpers'; const wait = promisify(setTimeout); @@ -245,4 +249,57 @@ describe('analytics helpers', function () { ).to.match(/^(hi,hi,hi|bye,bye,bye)$/); }); }); + + describe('SampledAnalytics', function () { + const userId = `u-${Date.now()}`; + const iEvt = { userId, traits: { platform: 'what', session_id: 'abc' } }; + const tEvt = { + userId, + event: 'hi', + properties: { mongosh_version: '1.2.3', session_id: 'abc' }, + }; + + it('should sample by default a 30% of open sessions with an error margin of ±1%', function () { + const expectedPercentage = 30; + // sampling is random based, so we must consider an error margin, for example, ±1% + const errorMargin = 1; + + let totalSample = 0; + let enabledSamples = 0; + + for (let i = 0; i < 100_000; i++) { + // evenly distributed randoms are more evenly with more samples + totalSample++; + enabledSamples += SampledAnalytics.default(target).enabled ? 1 : 0; + } + + const sampledPercentage = (enabledSamples / totalSample) * 100; + expect(sampledPercentage).to.be.greaterThan( + expectedPercentage - errorMargin + ); + expect(sampledPercentage).to.be.lessThan( + expectedPercentage + errorMargin + ); + }); + + it('should send the event forward when sampled', function () { + const analytics = SampledAnalytics.enabledForAll(target); + expect(analytics.enabled).to.be.true; + + analytics.identify(iEvt); + analytics.track(tEvt); + + expect(events.length).to.equal(2); + }); + + it('should not send the event forward when not sampled', function () { + const analytics = SampledAnalytics.disabledForAll(target); + expect(analytics.enabled).to.be.false; + + analytics.identify(iEvt); + analytics.track(tEvt); + + expect(events.length).to.equal(0); + }); + }); }); diff --git a/packages/logging/src/analytics-helpers.ts b/packages/logging/src/analytics-helpers.ts index 546f3b85c..84ff58b4a 100644 --- a/packages/logging/src/analytics-helpers.ts +++ b/packages/logging/src/analytics-helpers.ts @@ -378,3 +378,69 @@ export class ThrottledAnalytics implements MongoshAnalytics { }); } } + +type SampledAnalyticsOptions = { + target: MongoshAnalytics; + /** + * Sampling options. If not provided, sampling will be defaulted to 100% of sessions. + * Also, from an exposed environment standpoint, providing a MONGOSH_ANALYTICS_SAMPLE + * environment variable with a truthy value will force the sampling to 100%. + * + * The sampling configuration can be changed, however, by default it will be a Math.random + * bounded from 0 to 100. If you are going to configure it use a evenly distributed random function. + */ + sampling: { + percentage: number; + samplingFunction: () => number; + } | null; +}; + +export class SampledAnalytics implements MongoshAnalytics { + private isEnabled: boolean; + + private constructor(private configuration: SampledAnalyticsOptions) { + this.configuration.sampling ??= { + percentage: 30, + samplingFunction: () => Math.random() * 100, + }; + + const shouldBeSampled = + this.configuration.sampling.samplingFunction() <= + this.configuration.sampling.percentage; + this.isEnabled = !!process.env.MONGOSH_ANALYTICS_SAMPLE || shouldBeSampled; + } + + static default(target: MongoshAnalytics): SampledAnalytics { + return new SampledAnalytics({ target, sampling: null }); + } + + static enabledForAll(target: MongoshAnalytics): SampledAnalytics { + return new SampledAnalytics({ + target, + sampling: { percentage: 100, samplingFunction: () => 1 }, + }); + } + + static disabledForAll(target: MongoshAnalytics): SampledAnalytics { + return new SampledAnalytics({ + target, + sampling: { percentage: 0, samplingFunction: () => 1 }, + }); + } + + get enabled(): boolean { + return this.isEnabled; + } + + identify(message: AnalyticsIdentifyMessage): void { + this.isEnabled && this.configuration.target.identify(message); + } + + track(message: AnalyticsTrackMessage): void { + this.isEnabled && this.configuration.target.track(message); + } + + flush(callback: (err?: Error | undefined) => void): void { + this.isEnabled && this.configuration.target.flush(callback); + } +} From d7dc58c8e17b21360280b780b7e487fa6bc21ae6 Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Mon, 27 Nov 2023 16:30:59 +0100 Subject: [PATCH 02/10] chore: connect to the cli-repl --- packages/cli-repl/src/cli-repl.ts | 17 +++++---- .../logging/src/analytics-helpers.spec.ts | 38 ++++++++----------- packages/logging/src/analytics-helpers.ts | 36 ++++++++++-------- 3 files changed, 46 insertions(+), 45 deletions(-) diff --git a/packages/cli-repl/src/cli-repl.ts b/packages/cli-repl/src/cli-repl.ts index 07d7e4394..59bce7dd6 100644 --- a/packages/cli-repl/src/cli-repl.ts +++ b/packages/cli-repl/src/cli-repl.ts @@ -43,6 +43,7 @@ import { promisify } from 'util'; import { getOsInfo } from './get-os-info'; import { UpdateNotificationManager } from './update-notification-manager'; import { markTime } from './startup-timing'; +import { SampledAnalytics } from '@mongosh/logging/lib/analytics-helpers'; /** * Connecting text key. @@ -500,13 +501,15 @@ export class CliRepl implements MongoshIOProvider { } as any /* axiosConfig and axiosRetryConfig are existing options, but don't have type definitions */ ); this.toggleableAnalytics = new ToggleableAnalytics( - new ThrottledAnalytics({ - target: this.segmentAnalytics, - throttle: { - rate: 30, - metadataPath: this.shellHomeDirectory.paths.shellLocalDataPath, - }, - }) + SampledAnalytics.default( + new ThrottledAnalytics({ + target: this.segmentAnalytics, + throttle: { + rate: 30, + metadataPath: this.shellHomeDirectory.paths.shellLocalDataPath, + }, + }) + ) ); } diff --git a/packages/logging/src/analytics-helpers.spec.ts b/packages/logging/src/analytics-helpers.spec.ts index 5ae787802..58811bef9 100644 --- a/packages/logging/src/analytics-helpers.spec.ts +++ b/packages/logging/src/analytics-helpers.spec.ts @@ -259,31 +259,23 @@ describe('analytics helpers', function () { properties: { mongosh_version: '1.2.3', session_id: 'abc' }, }; - it('should sample by default a 30% of open sessions with an error margin of ±1%', function () { - const expectedPercentage = 30; - // sampling is random based, so we must consider an error margin, for example, ±1% - const errorMargin = 1; - - let totalSample = 0; - let enabledSamples = 0; - - for (let i = 0; i < 100_000; i++) { - // evenly distributed randoms are more evenly with more samples - totalSample++; - enabledSamples += SampledAnalytics.default(target).enabled ? 1 : 0; - } + afterEach(function () { + delete process.env.MONGOSH_ANALYTICS_SAMPLE; + }); - const sampledPercentage = (enabledSamples / totalSample) * 100; - expect(sampledPercentage).to.be.greaterThan( - expectedPercentage - errorMargin - ); - expect(sampledPercentage).to.be.lessThan( - expectedPercentage + errorMargin - ); + it('should override sampling with the MONGOSH_ANALYTICS_SAMPLE environment variable', function () { + process.env.MONGOSH_ANALYTICS_SAMPLE = 'true'; + const analytics = SampledAnalytics.disabledForAll( + target + ) as SampledAnalytics; + + expect(analytics.enabled).to.be.true; }); it('should send the event forward when sampled', function () { - const analytics = SampledAnalytics.enabledForAll(target); + const analytics = SampledAnalytics.enabledForAll( + target + ) as SampledAnalytics; expect(analytics.enabled).to.be.true; analytics.identify(iEvt); @@ -293,7 +285,9 @@ describe('analytics helpers', function () { }); it('should not send the event forward when not sampled', function () { - const analytics = SampledAnalytics.disabledForAll(target); + const analytics = SampledAnalytics.disabledForAll( + target + ) as SampledAnalytics; expect(analytics.enabled).to.be.false; analytics.identify(iEvt); diff --git a/packages/logging/src/analytics-helpers.ts b/packages/logging/src/analytics-helpers.ts index 84ff58b4a..09a32ef9f 100644 --- a/packages/logging/src/analytics-helpers.ts +++ b/packages/logging/src/analytics-helpers.ts @@ -1,5 +1,6 @@ import fs from 'fs'; import path from 'path'; +import { performance } from 'perf_hooks'; export type MongoshAnalyticsIdentity = | { @@ -380,7 +381,7 @@ export class ThrottledAnalytics implements MongoshAnalytics { } type SampledAnalyticsOptions = { - target: MongoshAnalytics; + target?: MongoshAnalytics; /** * Sampling options. If not provided, sampling will be defaulted to 100% of sessions. * Also, from an exposed environment standpoint, providing a MONGOSH_ANALYTICS_SAMPLE @@ -391,40 +392,43 @@ type SampledAnalyticsOptions = { */ sampling: { percentage: number; - samplingFunction: () => number; + samplingFunction: (percentage: number) => boolean; } | null; }; export class SampledAnalytics implements MongoshAnalytics { private isEnabled: boolean; + private target: MongoshAnalytics; - private constructor(private configuration: SampledAnalyticsOptions) { - this.configuration.sampling ??= { + private constructor(configuration: SampledAnalyticsOptions) { + configuration.sampling ??= { percentage: 30, - samplingFunction: () => Math.random() * 100, + samplingFunction: (percentage) => Math.random() * 100 < percentage, }; - const shouldBeSampled = - this.configuration.sampling.samplingFunction() <= - this.configuration.sampling.percentage; + const shouldBeSampled = configuration.sampling.samplingFunction( + configuration.sampling.percentage + ); + this.isEnabled = !!process.env.MONGOSH_ANALYTICS_SAMPLE || shouldBeSampled; + this.target = configuration.target || new NoopAnalytics(); } - static default(target: MongoshAnalytics): SampledAnalytics { + static default(target?: MongoshAnalytics): MongoshAnalytics { return new SampledAnalytics({ target, sampling: null }); } - static enabledForAll(target: MongoshAnalytics): SampledAnalytics { + static enabledForAll(target?: MongoshAnalytics): MongoshAnalytics { return new SampledAnalytics({ target, - sampling: { percentage: 100, samplingFunction: () => 1 }, + sampling: { percentage: 100, samplingFunction: () => true }, }); } - static disabledForAll(target: MongoshAnalytics): SampledAnalytics { + static disabledForAll(target?: MongoshAnalytics): MongoshAnalytics { return new SampledAnalytics({ target, - sampling: { percentage: 0, samplingFunction: () => 1 }, + sampling: { percentage: 0, samplingFunction: () => false }, }); } @@ -433,14 +437,14 @@ export class SampledAnalytics implements MongoshAnalytics { } identify(message: AnalyticsIdentifyMessage): void { - this.isEnabled && this.configuration.target.identify(message); + this.isEnabled && this.target.identify(message); } track(message: AnalyticsTrackMessage): void { - this.isEnabled && this.configuration.target.track(message); + this.isEnabled && this.target.track(message); } flush(callback: (err?: Error | undefined) => void): void { - this.isEnabled && this.configuration.target.flush(callback); + this.isEnabled && this.target.flush(callback); } } From 6ddbeea15cf9eebdeb5e7dc0381662178229de7c Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Mon, 27 Nov 2023 16:37:33 +0100 Subject: [PATCH 03/10] chore: fix docs --- packages/logging/src/analytics-helpers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logging/src/analytics-helpers.ts b/packages/logging/src/analytics-helpers.ts index 09a32ef9f..1332d287b 100644 --- a/packages/logging/src/analytics-helpers.ts +++ b/packages/logging/src/analytics-helpers.ts @@ -383,7 +383,7 @@ export class ThrottledAnalytics implements MongoshAnalytics { type SampledAnalyticsOptions = { target?: MongoshAnalytics; /** - * Sampling options. If not provided, sampling will be defaulted to 100% of sessions. + * Sampling options. If not provided, sampling will be defaulted to 30% of sessions. * Also, from an exposed environment standpoint, providing a MONGOSH_ANALYTICS_SAMPLE * environment variable with a truthy value will force the sampling to 100%. * From 79b1ad74689b8d9d90eb1e1e4a8f41648fe8550d Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Mon, 27 Nov 2023 16:38:16 +0100 Subject: [PATCH 04/10] chore: optimise imports --- packages/logging/src/analytics-helpers.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/logging/src/analytics-helpers.ts b/packages/logging/src/analytics-helpers.ts index 1332d287b..982434c2a 100644 --- a/packages/logging/src/analytics-helpers.ts +++ b/packages/logging/src/analytics-helpers.ts @@ -1,6 +1,5 @@ import fs from 'fs'; import path from 'path'; -import { performance } from 'perf_hooks'; export type MongoshAnalyticsIdentity = | { From 34e5a21e305c8ffa93c096eb75be7fc8d6423203 Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Mon, 27 Nov 2023 17:19:19 +0100 Subject: [PATCH 05/10] chore: refactoring --- packages/cli-repl/src/cli-repl.ts | 12 ++++--- .../logging/src/analytics-helpers.spec.ts | 13 ------- packages/logging/src/analytics-helpers.ts | 34 +++---------------- packages/logging/src/index.ts | 1 + 4 files changed, 13 insertions(+), 47 deletions(-) diff --git a/packages/cli-repl/src/cli-repl.ts b/packages/cli-repl/src/cli-repl.ts index 59bce7dd6..bdb40aa3e 100644 --- a/packages/cli-repl/src/cli-repl.ts +++ b/packages/cli-repl/src/cli-repl.ts @@ -34,6 +34,7 @@ import { setupLoggerAndTelemetry, ToggleableAnalytics, ThrottledAnalytics, + SampledAnalytics, } from '@mongosh/logging'; import type { MongoshBus } from '@mongosh/types'; import { CliUserConfig, CliUserConfigValidator } from '@mongosh/types'; @@ -43,7 +44,6 @@ import { promisify } from 'util'; import { getOsInfo } from './get-os-info'; import { UpdateNotificationManager } from './update-notification-manager'; import { markTime } from './startup-timing'; -import { SampledAnalytics } from '@mongosh/logging/lib/analytics-helpers'; /** * Connecting text key. @@ -501,15 +501,17 @@ export class CliRepl implements MongoshIOProvider { } as any /* axiosConfig and axiosRetryConfig are existing options, but don't have type definitions */ ); this.toggleableAnalytics = new ToggleableAnalytics( - SampledAnalytics.default( - new ThrottledAnalytics({ + new SampledAnalytics({ + target: new ThrottledAnalytics({ target: this.segmentAnalytics, throttle: { rate: 30, metadataPath: this.shellHomeDirectory.paths.shellLocalDataPath, }, - }) - ) + }), + sampling: () => + !!process.env.MONGOSH_ANALYTICS_SAMPLE || Math.random() < 0.3, + }) ); } diff --git a/packages/logging/src/analytics-helpers.spec.ts b/packages/logging/src/analytics-helpers.spec.ts index 58811bef9..e83c20c24 100644 --- a/packages/logging/src/analytics-helpers.spec.ts +++ b/packages/logging/src/analytics-helpers.spec.ts @@ -259,19 +259,6 @@ describe('analytics helpers', function () { properties: { mongosh_version: '1.2.3', session_id: 'abc' }, }; - afterEach(function () { - delete process.env.MONGOSH_ANALYTICS_SAMPLE; - }); - - it('should override sampling with the MONGOSH_ANALYTICS_SAMPLE environment variable', function () { - process.env.MONGOSH_ANALYTICS_SAMPLE = 'true'; - const analytics = SampledAnalytics.disabledForAll( - target - ) as SampledAnalytics; - - expect(analytics.enabled).to.be.true; - }); - it('should send the event forward when sampled', function () { const analytics = SampledAnalytics.enabledForAll( target diff --git a/packages/logging/src/analytics-helpers.ts b/packages/logging/src/analytics-helpers.ts index 982434c2a..b5f8b7f73 100644 --- a/packages/logging/src/analytics-helpers.ts +++ b/packages/logging/src/analytics-helpers.ts @@ -381,53 +381,29 @@ export class ThrottledAnalytics implements MongoshAnalytics { type SampledAnalyticsOptions = { target?: MongoshAnalytics; - /** - * Sampling options. If not provided, sampling will be defaulted to 30% of sessions. - * Also, from an exposed environment standpoint, providing a MONGOSH_ANALYTICS_SAMPLE - * environment variable with a truthy value will force the sampling to 100%. - * - * The sampling configuration can be changed, however, by default it will be a Math.random - * bounded from 0 to 100. If you are going to configure it use a evenly distributed random function. - */ - sampling: { - percentage: number; - samplingFunction: (percentage: number) => boolean; - } | null; + sampling: () => boolean; }; export class SampledAnalytics implements MongoshAnalytics { private isEnabled: boolean; private target: MongoshAnalytics; - private constructor(configuration: SampledAnalyticsOptions) { - configuration.sampling ??= { - percentage: 30, - samplingFunction: (percentage) => Math.random() * 100 < percentage, - }; - - const shouldBeSampled = configuration.sampling.samplingFunction( - configuration.sampling.percentage - ); - - this.isEnabled = !!process.env.MONGOSH_ANALYTICS_SAMPLE || shouldBeSampled; + constructor(configuration: SampledAnalyticsOptions) { + this.isEnabled = configuration.sampling(); this.target = configuration.target || new NoopAnalytics(); } - static default(target?: MongoshAnalytics): MongoshAnalytics { - return new SampledAnalytics({ target, sampling: null }); - } - static enabledForAll(target?: MongoshAnalytics): MongoshAnalytics { return new SampledAnalytics({ target, - sampling: { percentage: 100, samplingFunction: () => true }, + sampling: () => true, }); } static disabledForAll(target?: MongoshAnalytics): MongoshAnalytics { return new SampledAnalytics({ target, - sampling: { percentage: 0, samplingFunction: () => false }, + sampling: () => false, }); } diff --git a/packages/logging/src/index.ts b/packages/logging/src/index.ts index 672187842..263682d08 100644 --- a/packages/logging/src/index.ts +++ b/packages/logging/src/index.ts @@ -2,6 +2,7 @@ export { setupLoggerAndTelemetry } from './setup-logger-and-telemetry'; export { MongoshAnalytics, ToggleableAnalytics, + SampledAnalytics, NoopAnalytics, ThrottledAnalytics, } from './analytics-helpers'; From 8b21e62dc46d18f176833a361c850958f9db1fae Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Mon, 27 Nov 2023 17:31:10 +0100 Subject: [PATCH 06/10] chore: refactor so tests are moved to cli-repl --- packages/cli-repl/src/cli-repl.spec.ts | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/packages/cli-repl/src/cli-repl.spec.ts b/packages/cli-repl/src/cli-repl.spec.ts index b479ad351..83ac2a3d5 100644 --- a/packages/cli-repl/src/cli-repl.spec.ts +++ b/packages/cli-repl/src/cli-repl.spec.ts @@ -1236,6 +1236,7 @@ describe('CliRepl', function () { }); afterEach(async function () { + delete process.env.MONGOSH_ANALYTICS_SAMPLE; srv.close(); await once(srv, 'close'); setTelemetryDelay(0); @@ -1271,6 +1272,40 @@ describe('CliRepl', function () { expect(requests[0].body).to.include(process.platform); }); + it('posts analytics if the environment variable MONGOSH_ANALYTICS_SAMPLE is provided', async function () { + process.env.MONGOSH_ANALYTICS_SAMPLE = 'true'; + await cliRepl.start(await testServer.connectionString(), {}); + input.write('use somedb;\n'); + await waitEval(cliRepl.bus); + // There are warnings generated by the driver if exit is used to close + // the REPL too early. That might be worth investigating at some point. + await delay(100); + input.write('exit\n'); + await waitBus(cliRepl.bus, 'mongosh:closed'); + const useEvents = requests.flatMap((req) => + JSON.parse(req.body).batch.filter((entry) => entry.event === 'Use') + ); + expect(useEvents).to.have.lengthOf(1); + }); + + it('does not post analytics if the environment variable MONGOSH_ANALYTICS_SAMPLE is true but user disabled telemetry', async function () { + process.env.MONGOSH_ANALYTICS_SAMPLE = 'true'; + await cliRepl.start(await testServer.connectionString(), {}); + input.write('disableTelemetry()\n'); + await waitEval(cliRepl.bus); + input.write('use somedb;\n'); + await waitEval(cliRepl.bus); + // There are warnings generated by the driver if exit is used to close + // the REPL too early. That might be worth investigating at some point. + await delay(100); + input.write('exit\n'); + await waitBus(cliRepl.bus, 'mongosh:closed'); + const useEvents = requests.flatMap((req) => + JSON.parse(req.body).batch.filter((entry) => entry.event === 'Use') + ); + expect(useEvents).to.have.lengthOf(0); + }); + it('stops posting analytics data after disableTelemetry()', async function () { await cliRepl.start(await testServer.connectionString(), {}); input.write('use somedb;\n'); From 85f54da883378961d34479da3f9a41d14eb8847b Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Mon, 27 Nov 2023 17:32:55 +0100 Subject: [PATCH 07/10] chore: reuse interface for tests --- packages/logging/src/analytics-helpers.spec.ts | 16 ++++++++++------ packages/logging/src/analytics-helpers.ts | 14 -------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/packages/logging/src/analytics-helpers.spec.ts b/packages/logging/src/analytics-helpers.spec.ts index e83c20c24..9efda4e1e 100644 --- a/packages/logging/src/analytics-helpers.spec.ts +++ b/packages/logging/src/analytics-helpers.spec.ts @@ -260,9 +260,11 @@ describe('analytics helpers', function () { }; it('should send the event forward when sampled', function () { - const analytics = SampledAnalytics.enabledForAll( - target - ) as SampledAnalytics; + const analytics = new SampledAnalytics({ + target, + sampling: () => true, + }); + expect(analytics.enabled).to.be.true; analytics.identify(iEvt); @@ -272,9 +274,11 @@ describe('analytics helpers', function () { }); it('should not send the event forward when not sampled', function () { - const analytics = SampledAnalytics.disabledForAll( - target - ) as SampledAnalytics; + const analytics = new SampledAnalytics({ + target, + sampling: () => false, + }); + expect(analytics.enabled).to.be.false; analytics.identify(iEvt); diff --git a/packages/logging/src/analytics-helpers.ts b/packages/logging/src/analytics-helpers.ts index b5f8b7f73..390287146 100644 --- a/packages/logging/src/analytics-helpers.ts +++ b/packages/logging/src/analytics-helpers.ts @@ -393,20 +393,6 @@ export class SampledAnalytics implements MongoshAnalytics { this.target = configuration.target || new NoopAnalytics(); } - static enabledForAll(target?: MongoshAnalytics): MongoshAnalytics { - return new SampledAnalytics({ - target, - sampling: () => true, - }); - } - - static disabledForAll(target?: MongoshAnalytics): MongoshAnalytics { - return new SampledAnalytics({ - target, - sampling: () => false, - }); - } - get enabled(): boolean { return this.isEnabled; } From d240074e35348bfcbdce48b0fc0844f5fbeec642 Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Mon, 27 Nov 2023 17:38:25 +0100 Subject: [PATCH 08/10] chore: forgot to force the analytics on the test --- packages/cli-repl/src/cli-repl.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/cli-repl/src/cli-repl.spec.ts b/packages/cli-repl/src/cli-repl.spec.ts index 83ac2a3d5..f8eb21669 100644 --- a/packages/cli-repl/src/cli-repl.spec.ts +++ b/packages/cli-repl/src/cli-repl.spec.ts @@ -1208,6 +1208,7 @@ describe('CliRepl', function () { }; beforeEach(async function () { + process.env.MONGOSH_ANALYTICS_SAMPLE = 'true'; requests = []; srv = http .createServer((req, res) => { From c610435e2e81e15f4ecf6ed1495014b949437a40 Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Mon, 27 Nov 2023 18:19:21 +0100 Subject: [PATCH 09/10] chore: always flush --- packages/logging/src/analytics-helpers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logging/src/analytics-helpers.ts b/packages/logging/src/analytics-helpers.ts index 390287146..dc0b162d8 100644 --- a/packages/logging/src/analytics-helpers.ts +++ b/packages/logging/src/analytics-helpers.ts @@ -406,6 +406,6 @@ export class SampledAnalytics implements MongoshAnalytics { } flush(callback: (err?: Error | undefined) => void): void { - this.isEnabled && this.target.flush(callback); + this.target.flush(callback); } } From d5405b59d180d95465352553a277c9d106be3899 Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Tue, 28 Nov 2023 17:05:22 +0100 Subject: [PATCH 10/10] chore: only sample 1% of sessions --- packages/cli-repl/src/cli-repl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli-repl/src/cli-repl.ts b/packages/cli-repl/src/cli-repl.ts index bdb40aa3e..03483b86e 100644 --- a/packages/cli-repl/src/cli-repl.ts +++ b/packages/cli-repl/src/cli-repl.ts @@ -510,7 +510,7 @@ export class CliRepl implements MongoshIOProvider { }, }), sampling: () => - !!process.env.MONGOSH_ANALYTICS_SAMPLE || Math.random() < 0.3, + !!process.env.MONGOSH_ANALYTICS_SAMPLE || Math.random() <= 0.01, }) ); }