Skip to content

Commit

Permalink
Prevent metrics being logged from development environments (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcampos authored Aug 16, 2018
1 parent 25b7038 commit 1a7252a
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 109 deletions.
4 changes: 3 additions & 1 deletion packages/salesforcedx-vscode-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
13 changes: 10 additions & 3 deletions packages/salesforcedx-vscode-core/src/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
));
Expand Down
271 changes: 166 additions & 105 deletions packages/salesforcedx-vscode-core/test/telemetry/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 1a7252a

Please sign in to comment.