From 1a7252affb6168fab7c8464d731894294bb9419f Mon Sep 17 00:00:00 2001 From: lcampos Date: Thu, 16 Aug 2018 16:58:28 -0700 Subject: [PATCH] Prevent metrics being logged from development environments (#566) --- .../salesforcedx-vscode-core/src/index.ts | 4 +- .../src/telemetry/telemetry.ts | 13 +- .../test/telemetry/index.test.ts | 271 +++++++++++------- 3 files changed, 179 insertions(+), 109 deletions(-) diff --git a/packages/salesforcedx-vscode-core/src/index.ts b/packages/salesforcedx-vscode-core/src/index.ts index 7b84abba68..ff143ddbe7 100644 --- a/packages/salesforcedx-vscode-core/src/index.ts +++ b/packages/salesforcedx-vscode-core/src/index.ts @@ -352,7 +352,9 @@ export async function activate(context: vscode.ExtensionContext) { console.log('SFDX CLI Extension Activated'); // Telemetry - telemetryService.initializeService(context); + const machineId = + vscode && vscode.env ? vscode.env.machineId : 'someValue.machineId'; + telemetryService.initializeService(context, machineId); telemetryService.showTelemetryMessage(); telemetryService.sendExtensionActivationEvent(); diff --git a/packages/salesforcedx-vscode-core/src/telemetry/telemetry.ts b/packages/salesforcedx-vscode-core/src/telemetry/telemetry.ts index ebc7e718dd..5eeaface9e 100644 --- a/packages/salesforcedx-vscode-core/src/telemetry/telemetry.ts +++ b/packages/salesforcedx-vscode-core/src/telemetry/telemetry.ts @@ -25,11 +25,18 @@ export class TelemetryService { return TelemetryService.instance; } - public initializeService(context: vscode.ExtensionContext): void { + public initializeService( + context: vscode.ExtensionContext, + machineId: string + ): void { this.context = context; - + const isDevMode = machineId === 'someValue.machineId'; // TelemetryReporter is not initialized if user has disabled telemetry setting. - if (this.reporter === undefined && this.isTelemetryEnabled()) { + if ( + this.reporter === undefined && + this.isTelemetryEnabled() && + !isDevMode + ) { const extensionPackage = require(this.context.asAbsolutePath( './package.json' )); diff --git a/packages/salesforcedx-vscode-core/test/telemetry/index.test.ts b/packages/salesforcedx-vscode-core/test/telemetry/index.test.ts index 479a0e9fdc..e3393642f4 100644 --- a/packages/salesforcedx-vscode-core/test/telemetry/index.test.ts +++ b/packages/salesforcedx-vscode-core/test/telemetry/index.test.ts @@ -13,131 +13,192 @@ import { TelemetryService } from '../../src/telemetry/telemetry'; import { MockContext } from './MockContext'; describe('Telemetry', () => { + const machineId = '45678903'; let mShowInformation: SinonStub; let settings: SinonStub; let mockContext: MockContext; let reporter: SinonStub; - beforeEach(() => { - mShowInformation = stub(window, 'showInformationMessage').returns( - Promise.resolve(null) - ); - settings = stub(SfdxCoreSettings.prototype, 'getTelemetryEnabled').returns( - true - ); - reporter = stub(TelemetryReporter.prototype, 'sendTelemetryEvent'); - }); - - afterEach(() => { - mShowInformation.restore(); - settings.restore(); - reporter.restore(); - }); - - it('Should show telemetry info message', async () => { - // create vscode extensionContext in which telemetry msg has never been previously shown - mockContext = new MockContext(false); - - const telemetryService = TelemetryService.getInstance(); - telemetryService.initializeService(mockContext); - - const telemetryEnabled = telemetryService.isTelemetryEnabled(); - expect(telemetryEnabled).to.be.eql(true); - - telemetryService.showTelemetryMessage(); - assert.calledOnce(mShowInformation); - }); - - it('Should not show telemetry info message', async () => { - // create vscode extensionContext in which telemetry msg has been previously shown - mockContext = new MockContext(true); - - const telemetryService = TelemetryService.getInstance(); - telemetryService.initializeService(mockContext); + describe('in dev mode', () => { + beforeEach(() => { + mShowInformation = stub(window, 'showInformationMessage').returns( + Promise.resolve(null) + ); + settings = stub( + SfdxCoreSettings.prototype, + 'getTelemetryEnabled' + ).returns(true); + }); - const telemetryEnabled = telemetryService.isTelemetryEnabled(); - expect(telemetryEnabled).to.be.eql(true); + afterEach(() => { + mShowInformation.restore(); + settings.restore(); + }); - telemetryService.showTelemetryMessage(); - assert.notCalled(mShowInformation); - }); - - it('Should send telemetry data', async () => { - // create vscode extensionContext in which telemetry msg has been previously shown - mockContext = new MockContext(true); - - const telemetryService = TelemetryService.getInstance(); - telemetryService.initializeService(mockContext); - - telemetryService.sendExtensionActivationEvent(); - assert.calledOnce(reporter); - }); + it('Should not initialize telemetry reporter', async () => { + // create vscode extensionContext + mockContext = new MockContext(true); - it('Should not send telemetry data', async () => { - // create vscode extensionContext - mockContext = new MockContext(true); - // user has updated settings for not sending telemetry data. - settings.restore(); - settings = stub(SfdxCoreSettings.prototype, 'getTelemetryEnabled').returns( - false - ); + const telemetryService = TelemetryService.getInstance(); + telemetryService.initializeService(mockContext, 'someValue.machineId'); - const telemetryService = TelemetryService.getInstance(); - telemetryService.initializeService(mockContext); + const telemetryReporter = telemetryService.getReporter(); + expect(typeof telemetryReporter).to.be.eql('undefined'); + }); - const telemetryEnabled = telemetryService.isTelemetryEnabled(); - expect(telemetryEnabled).to.be.eql(false); + it('Should show telemetry info message', async () => { + // create vscode extensionContext in which telemetry msg has never been previously shown + mockContext = new MockContext(false); - telemetryService.sendCommandEvent('create_apex_class_command'); - assert.notCalled(reporter); - }); - - it('Should send correct data format on sendExtensionActivationEvent', async () => { - // create vscode extensionContext - mockContext = new MockContext(true); + const telemetryService = TelemetryService.getInstance(); + telemetryService.initializeService(mockContext, 'someValue.machineId'); - const telemetryService = TelemetryService.getInstance(); - telemetryService.initializeService(mockContext); + const telemetryEnabled = telemetryService.isTelemetryEnabled(); + expect(telemetryEnabled).to.be.eql(true); - telemetryService.sendExtensionActivationEvent(); - assert.calledOnce(reporter); + telemetryService.showTelemetryMessage(); + assert.calledOnce(mShowInformation); + }); - const expectedData = { - extensionName: 'salesforcedx-vscode-core' - }; - assert.calledWith(reporter, 'activationEvent', expectedData); - }); + it('Should not show telemetry info message', async () => { + // create vscode extensionContext in which telemetry msg has been previously shown + mockContext = new MockContext(true); - it('Should send correct data format on sendExtensionDeactivationEvent', async () => { - // create vscode extensionContext - mockContext = new MockContext(true); + const telemetryService = TelemetryService.getInstance(); + telemetryService.initializeService(mockContext, 'someValue.machineId'); - const telemetryService = TelemetryService.getInstance(); - telemetryService.initializeService(mockContext); + const telemetryEnabled = telemetryService.isTelemetryEnabled(); + expect(telemetryEnabled).to.be.eql(true); - telemetryService.sendExtensionDeactivationEvent(); - assert.calledOnce(reporter); - - const expectedData = { - extensionName: 'salesforcedx-vscode-core' - }; - assert.calledWith(reporter, 'deactivationEvent', expectedData); + telemetryService.showTelemetryMessage(); + assert.notCalled(mShowInformation); + }); }); - it('Should send correct data format on sendCommandEvent', async () => { - // create vscode extensionContext - mockContext = new MockContext(true); - - const telemetryService = TelemetryService.getInstance(); - telemetryService.initializeService(mockContext); + describe('production mode', () => { + beforeEach(() => { + mShowInformation = stub(window, 'showInformationMessage').returns( + Promise.resolve(null) + ); + settings = stub( + SfdxCoreSettings.prototype, + 'getTelemetryEnabled' + ).returns(true); + reporter = stub(TelemetryReporter.prototype, 'sendTelemetryEvent'); + }); + + afterEach(() => { + mShowInformation.restore(); + settings.restore(); + reporter.restore(); + }); + + it('Should show telemetry info message', async () => { + // create vscode extensionContext in which telemetry msg has never been previously shown + mockContext = new MockContext(false); + + const telemetryService = TelemetryService.getInstance(); + telemetryService.initializeService(mockContext, machineId); + + const telemetryEnabled = telemetryService.isTelemetryEnabled(); + expect(telemetryEnabled).to.be.eql(true); + + telemetryService.showTelemetryMessage(); + assert.calledOnce(mShowInformation); + }); + + it('Should not show telemetry info message', async () => { + // create vscode extensionContext in which telemetry msg has been previously shown + mockContext = new MockContext(true); + + const telemetryService = TelemetryService.getInstance(); + telemetryService.initializeService(mockContext, machineId); + + const telemetryEnabled = telemetryService.isTelemetryEnabled(); + expect(telemetryEnabled).to.be.eql(true); + + telemetryService.showTelemetryMessage(); + assert.notCalled(mShowInformation); + }); + + it('Should send telemetry data', async () => { + // create vscode extensionContext in which telemetry msg has been previously shown + mockContext = new MockContext(true); + + const telemetryService = TelemetryService.getInstance(); + telemetryService.initializeService(mockContext, machineId); + + telemetryService.sendExtensionActivationEvent(); + assert.calledOnce(reporter); + }); + + it('Should not send telemetry data', async () => { + // create vscode extensionContext + mockContext = new MockContext(true); + // user has updated settings for not sending telemetry data. + settings.restore(); + settings = stub( + SfdxCoreSettings.prototype, + 'getTelemetryEnabled' + ).returns(false); + + const telemetryService = TelemetryService.getInstance(); + telemetryService.initializeService(mockContext, machineId); + + const telemetryEnabled = telemetryService.isTelemetryEnabled(); + expect(telemetryEnabled).to.be.eql(false); + + telemetryService.sendCommandEvent('create_apex_class_command'); + assert.notCalled(reporter); + }); + + it('Should send correct data format on sendExtensionActivationEvent', async () => { + // create vscode extensionContext + mockContext = new MockContext(true); + + const telemetryService = TelemetryService.getInstance(); + telemetryService.initializeService(mockContext, machineId); + + telemetryService.sendExtensionActivationEvent(); + assert.calledOnce(reporter); + + const expectedData = { + extensionName: 'salesforcedx-vscode-core' + }; + assert.calledWith(reporter, 'activationEvent', expectedData); + }); + + it('Should send correct data format on sendExtensionDeactivationEvent', async () => { + // create vscode extensionContext + mockContext = new MockContext(true); + + const telemetryService = TelemetryService.getInstance(); + telemetryService.initializeService(mockContext, machineId); + + telemetryService.sendExtensionDeactivationEvent(); + assert.calledOnce(reporter); + + const expectedData = { + extensionName: 'salesforcedx-vscode-core' + }; + assert.calledWith(reporter, 'deactivationEvent', expectedData); + }); + + it('Should send correct data format on sendCommandEvent', async () => { + // create vscode extensionContext + mockContext = new MockContext(true); + + const telemetryService = TelemetryService.getInstance(); + telemetryService.initializeService(mockContext, machineId); - telemetryService.sendCommandEvent('create_apex_class_command'); - assert.calledOnce(reporter); + telemetryService.sendCommandEvent('create_apex_class_command'); + assert.calledOnce(reporter); - const expectedData = { - extensionName: 'salesforcedx-vscode-core', - commandName: 'create_apex_class_command' - }; - assert.calledWith(reporter, 'commandExecution', expectedData); + const expectedData = { + extensionName: 'salesforcedx-vscode-core', + commandName: 'create_apex_class_command' + }; + assert.calledWith(reporter, 'commandExecution', expectedData); + }); }); });