Skip to content

Commit

Permalink
Moves collecting telemetry to a separate process
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz committed Jul 19, 2023
1 parent 05cab2f commit 185bdcd
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 90 deletions.
6 changes: 0 additions & 6 deletions src/appInsights.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,4 @@ describe('appInsights', () => {
const i: any = require('./appInsights');
assert(i.default.commonProperties.env === 'docker');
});

it(`sets shell to empty string if couldn't resolve name from pid`, () => {
sinon.stub(pid, 'getProcessName').callsFake(() => undefined);
const i: any = require('./appInsights');
assert.strictEqual(i.default.commonProperties.shell, '');
});
});
4 changes: 0 additions & 4 deletions src/appInsights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import * as appInsights from 'applicationinsights';
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as path from 'path';
import { pid } from './utils/pid';
import { session } from './utils/session';

const config = appInsights.setup('6b908c80-d09f-4cf6-8274-e54349a0149a');
config.setInternalLogging(false, false);
Expand All @@ -21,12 +19,10 @@ const env: string = process.env.CLIMICROSOFT365_ENV !== undefined ? process.env.
appInsights.defaultClient.commonProperties = {
version: version,
node: process.version,
shell: pid.getProcessName(process.ppid) || '',
env: env,
ci: Boolean(process.env.CI).toString()
};

appInsights.defaultClient.context.tags['ai.session.id'] = session.getId(process.ppid);
appInsights.defaultClient.context.tags['ai.cloud.roleInstance'] = crypto.createHash('sha256').update(appInsights.defaultClient.context.tags['ai.cloud.roleInstance']).digest('hex');
delete appInsights.defaultClient.context.tags['ai.cloud.role'];

Expand Down
6 changes: 6 additions & 0 deletions src/m365/base/AppCommand.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { sinonUtil } from '../../utils/sinonUtil';
import AppCommand from './AppCommand';
import sinon = require('sinon');
import Command, { CommandError } from '../../Command';
import { telemetry } from '../../telemetry';

class MockCommand extends AppCommand {
public get name(): string {
Expand All @@ -32,6 +33,7 @@ describe('AppCommand', () => {

before(() => {
commandInfo = Cli.getCommandInfo(new MockCommand());
sinon.stub(telemetry, 'trackEvent').callsFake(() => { });
});

beforeEach(() => {
Expand All @@ -58,6 +60,10 @@ describe('AppCommand', () => {
]);
});

after(() => {
sinon.restore();
});

it('defines correct resource', () => {
assert.strictEqual((cmd as any).resource, 'https://graph.microsoft.com');
});
Expand Down
7 changes: 2 additions & 5 deletions src/m365/cli/commands/config/config-reset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ describe(commands.CONFIG_RESET, () => {
assert.notStrictEqual(command.description, null);
});

it(`Resets a specific configuration option to its default value`, async () => {
it('resets a specific configuration option to its default value', async () => {
const output = undefined;
const config = Cli.getInstance().config;

let actualKey: string = '', actualValue: any;

sinon.restore();
sinon.stub(config, 'delete').callsFake(((key: string) => {
actualKey = key;
actualValue = undefined;
Expand All @@ -67,15 +66,13 @@ describe(commands.CONFIG_RESET, () => {
assert.strictEqual(actualValue, undefined, 'Invalid value');
});

it(`Resets all configuration settings to default`, async () => {
it('resets all configuration settings to default', async () => {
const config = Cli.getInstance().config;
let errorOutputKey: string = '', errorOutputValue: any
, outputKey: string = '', outputValue: any
, printErrorsAsPlainTextKey: string = '', printErrorsAsPlainTextValue: any
, showHelpOnFailureKey: string = '', showHelpOnFailureValue: any;

sinon.restore();

sinon.stub(config, 'clear').callsFake((() => {
errorOutputKey = settingsNames.errorOutput;
errorOutputValue = undefined;
Expand Down
3 changes: 0 additions & 3 deletions src/m365/cli/commands/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ describe(commands.CONFIG_SET, () => {
const output = "text";
const config = Cli.getInstance().config;
let actualKey: string = '', actualValue: any;
sinon.restore();
sinon.stub(config, 'set').callsFake(((key: string, value: any) => {
actualKey = key;
actualValue = value;
Expand All @@ -98,7 +97,6 @@ describe(commands.CONFIG_SET, () => {
const output = "json";
const config = Cli.getInstance().config;
let actualKey: string = '', actualValue: any;
sinon.restore();
sinon.stub(config, 'set').callsFake(((key: string, value: any) => {
actualKey = key;
actualValue = value;
Expand All @@ -113,7 +111,6 @@ describe(commands.CONFIG_SET, () => {
const output = "csv";
const config = Cli.getInstance().config;
let actualKey: string = '', actualValue: any;
sinon.restore();
sinon.stub(config, 'set').callsFake(((key: string, value: any) => {
actualKey = key;
actualValue = value;
Expand Down
6 changes: 3 additions & 3 deletions src/m365/context/commands/context-init.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as assert from 'assert';
import * as fs from 'fs';
import * as sinon from 'sinon';
import appInsights from '../../../appInsights';
import { Logger } from '../../../cli/Logger';
import Command, { CommandError } from '../../../Command';
import { Logger } from '../../../cli/Logger';
import { telemetry } from '../../../telemetry';
import { sinonUtil } from '../../../utils/sinonUtil';
import commands from '../commands';
const command: Command = require('./context-init');
Expand All @@ -13,7 +13,7 @@ describe(commands.INIT, () => {
let logger: Logger;

before(() => {
sinon.stub(appInsights, 'trackEvent').callsFake(() => { });
sinon.stub(telemetry, 'trackEvent').callsFake(() => { });
});

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import appInsights from '../../../../appInsights';
import auth from '../../../../Auth';
import Command, { CommandError } from '../../../../Command';
import { Cli } from '../../../../cli/Cli';
import { CommandInfo } from '../../../../cli/CommandInfo';
import { Logger } from '../../../../cli/Logger';
import Command, { CommandError } from '../../../../Command';
import request from '../../../../request';
import { telemetry } from '../../../../telemetry';
import { pid } from '../../../../utils/pid';
import { session } from '../../../../utils/session';
import { sinonUtil } from '../../../../utils/sinonUtil';
Expand All @@ -23,7 +23,7 @@ describe(commands.RETENTIONEVENT_REMOVE, () => {

before(() => {
sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve());
sinon.stub(appInsights, 'trackEvent').callsFake(() => { });
sinon.stub(telemetry, 'trackEvent').callsFake(() => { });
sinon.stub(pid, 'getProcessName').callsFake(() => '');
sinon.stub(session, 'getId').callsFake(() => '');
auth.service.connected = true;
Expand Down
6 changes: 3 additions & 3 deletions src/m365/spo/commands/listitem/listitem-record-lock.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import appInsights from '../../../../appInsights';
import auth from '../../../../Auth';
import Command, { CommandError } from '../../../../Command';
import { Cli } from '../../../../cli/Cli';
import { CommandInfo } from '../../../../cli/CommandInfo';
import { Logger } from '../../../../cli/Logger';
import Command, { CommandError } from '../../../../Command';
import request from '../../../../request';
import { telemetry } from '../../../../telemetry';
import { pid } from '../../../../utils/pid';
import { session } from '../../../../utils/session';
import { sinonUtil } from '../../../../utils/sinonUtil';
Expand Down Expand Up @@ -40,7 +40,7 @@ describe(commands.LISTITEM_RECORD_LOCK, () => {
before(() => {
cli = Cli.getInstance();
sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve());
sinon.stub(appInsights, 'trackEvent').callsFake(() => { });
sinon.stub(telemetry, 'trackEvent').callsFake(() => { });
sinon.stub(pid, 'getProcessName').callsFake(() => '');
sinon.stub(session, 'getId').callsFake(() => '');
auth.service.connected = true;
Expand Down
6 changes: 3 additions & 3 deletions src/m365/spo/commands/listitem/listitem-record-unlock.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import appInsights from '../../../../appInsights';
import auth from '../../../../Auth';
import Command, { CommandError } from '../../../../Command';
import { Cli } from '../../../../cli/Cli';
import { CommandInfo } from '../../../../cli/CommandInfo';
import { Logger } from '../../../../cli/Logger';
import Command, { CommandError } from '../../../../Command';
import request from '../../../../request';
import { telemetry } from '../../../../telemetry';
import { pid } from '../../../../utils/pid';
import { session } from '../../../../utils/session';
import { sinonUtil } from '../../../../utils/sinonUtil';
Expand Down Expand Up @@ -40,7 +40,7 @@ describe(commands.LISTITEM_RECORD_UNLOCK, () => {
before(() => {
cli = Cli.getInstance();
sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve());
sinon.stub(appInsights, 'trackEvent').callsFake(() => { });
sinon.stub(telemetry, 'trackEvent').callsFake(() => { });
sinon.stub(pid, 'getProcessName').callsFake(() => '');
sinon.stub(session, 'getId').callsFake(() => '');
auth.service.connected = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import appInsights from '../../../../appInsights';
import auth from '../../../../Auth';
import Command, { CommandError } from '../../../../Command';
import { Cli } from '../../../../cli/Cli';
import { CommandInfo } from '../../../../cli/CommandInfo';
import { Logger } from '../../../../cli/Logger';
import Command, { CommandError } from '../../../../Command';
import request from '../../../../request';
import { telemetry } from '../../../../telemetry';
import { formatting } from '../../../../utils/formatting';
import { pid } from '../../../../utils/pid';
import { session } from '../../../../utils/session';
Expand Down Expand Up @@ -64,7 +64,7 @@ describe(commands.LISTITEM_RETENTIONLABEL_ENSURE, () => {
before(() => {
cli = Cli.getInstance();
sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve());
sinon.stub(appInsights, 'trackEvent').callsFake(() => { });
sinon.stub(telemetry, 'trackEvent').callsFake(() => { });
sinon.stub(pid, 'getProcessName').callsFake(() => '');
sinon.stub(session, 'getId').callsFake(() => '');
auth.service.connected = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import appInsights from '../../../../appInsights';
import auth from '../../../../Auth';
import Command, { CommandError } from '../../../../Command';
import { Cli } from '../../../../cli/Cli';
import { CommandInfo } from '../../../../cli/CommandInfo';
import { Logger } from '../../../../cli/Logger';
import Command, { CommandError } from '../../../../Command';
import { telemetry } from '../../../../telemetry';
import { pid } from '../../../../utils/pid';
import { session } from '../../../../utils/session';
import { sinonUtil } from '../../../../utils/sinonUtil';
import { urlUtil } from '../../../../utils/urlUtil';
import commands from '../../commands';
import * as spoTenantAppCatalogUrlGetCommand from '../tenant/tenant-appcatalogurl-get';
import * as spoListItemListCommand from '../listitem/listitem-list';
import * as spoListItemAddCommand from '../listitem/listitem-add';
import { urlUtil } from '../../../../utils/urlUtil';
import * as spoListItemListCommand from '../listitem/listitem-list';
import * as spoTenantAppCatalogUrlGetCommand from '../tenant/tenant-appcatalogurl-get';
const command: Command = require('./tenant-applicationcustomizer-add');

describe(commands.TENANT_APPLICATIONCUSTOMIZER_ADD, () => {
Expand All @@ -36,7 +36,7 @@ describe(commands.TENANT_APPLICATIONCUSTOMIZER_ADD, () => {

before(() => {
sinon.stub(auth, 'restoreAuth').resolves();
sinon.stub(appInsights, 'trackEvent').returns();
sinon.stub(telemetry, 'trackEvent').returns();
sinon.stub(pid, 'getProcessName').returns('');
sinon.stub(session, 'getId').returns('');
auth.service.connected = true;
Expand Down
12 changes: 6 additions & 6 deletions src/m365/spo/commands/tenant/tenant-commandset-add.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import appInsights from '../../../../appInsights';
import auth from '../../../../Auth';
import Command, { CommandError } from '../../../../Command';
import { Cli } from '../../../../cli/Cli';
import { CommandInfo } from '../../../../cli/CommandInfo';
import { Logger } from '../../../../cli/Logger';
import Command, { CommandError } from '../../../../Command';
import { telemetry } from '../../../../telemetry';
import { pid } from '../../../../utils/pid';
import { session } from '../../../../utils/session';
import { sinonUtil } from '../../../../utils/sinonUtil';
import { urlUtil } from '../../../../utils/urlUtil';
import commands from '../../commands';
import * as spoTenantAppCatalogUrlGetCommand from './tenant-appcatalogurl-get';
import * as spoListItemListCommand from '../listitem/listitem-list';
import * as spoListItemAddCommand from '../listitem/listitem-add';
import { urlUtil } from '../../../../utils/urlUtil';
import * as spoListItemListCommand from '../listitem/listitem-list';
import * as spoTenantAppCatalogUrlGetCommand from './tenant-appcatalogurl-get';
const command: Command = require('./tenant-commandset-add');

describe(commands.TENANT_COMMANDSET_ADD, () => {
Expand All @@ -36,7 +36,7 @@ describe(commands.TENANT_COMMANDSET_ADD, () => {

before(() => {
sinon.stub(auth, 'restoreAuth').resolves();
sinon.stub(appInsights, 'trackEvent').returns();
sinon.stub(telemetry, 'trackEvent').returns();
sinon.stub(pid, 'getProcessName').returns('');
sinon.stub(session, 'getId').returns('');
auth.service.connected = true;
Expand Down
8 changes: 4 additions & 4 deletions src/m365/spo/commands/tenant/tenant-commandset-set.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import appInsights from '../../../../appInsights';
import auth from '../../../../Auth';
import Command, { CommandError } from '../../../../Command';
import { Cli } from '../../../../cli/Cli';
import { CommandInfo } from '../../../../cli/CommandInfo';
import { Logger } from '../../../../cli/Logger';
import Command, { CommandError } from '../../../../Command';
import request from '../../../../request';
import { telemetry } from '../../../../telemetry';
import { pid } from '../../../../utils/pid';
import { session } from '../../../../utils/session';
import { sinonUtil } from '../../../../utils/sinonUtil';
import commands from '../../commands';
import request from '../../../../request';
const command: Command = require('./tenant-commandset-set');

describe(commands.TENANT_COMMANDSET_SET, () => {
Expand Down Expand Up @@ -52,7 +52,7 @@ describe(commands.TENANT_COMMANDSET_SET, () => {

before(() => {
sinon.stub(auth, 'restoreAuth').resolves();
sinon.stub(appInsights, 'trackEvent').returns();
sinon.stub(telemetry, 'trackEvent').returns();
sinon.stub(pid, 'getProcessName').returns('');
sinon.stub(session, 'getId').returns('');
auth.service.connected = true;
Expand Down
6 changes: 3 additions & 3 deletions src/m365/spo/commands/web/web-retentionlabel-list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import appInsights from '../../../../appInsights';
import auth from '../../../../Auth';
import Command, { CommandError } from '../../../../Command';
import { Cli } from '../../../../cli/Cli';
import { CommandInfo } from '../../../../cli/CommandInfo';
import { Logger } from '../../../../cli/Logger';
import Command, { CommandError } from '../../../../Command';
import request from '../../../../request';
import { telemetry } from '../../../../telemetry';
import { formatting } from '../../../../utils/formatting';
import { pid } from '../../../../utils/pid';
import { session } from '../../../../utils/session';
Expand Down Expand Up @@ -59,7 +59,7 @@ describe(commands.WEB_RETENTIONLABEL_LIST, () => {

before(() => {
sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve());
sinon.stub(appInsights, 'trackEvent').callsFake(() => { });
sinon.stub(telemetry, 'trackEvent').callsFake(() => { });
sinon.stub(pid, 'getProcessName').callsFake(() => '');
sinon.stub(session, 'getId').callsFake(() => '');
auth.service.connected = true;
Expand Down
Loading

0 comments on commit 185bdcd

Please sign in to comment.