Skip to content

Commit

Permalink
fix: implement provider for telemetry (#5786)
Browse files Browse the repository at this point in the history
* fix: add get telemetry service provider cmd

@W-16507598@

* chore: wip

* chore: wip

* chore: wip

* chore: wip

* chore: wip

* chore: wip

* chore: wip

* chore: apex unit tests

---------

Co-authored-by: Daphne Yang <[email protected]>
  • Loading branch information
peternhale and daphne-sfdc authored Sep 4, 2024
1 parent 931cc54 commit f8232f1
Show file tree
Hide file tree
Showing 33 changed files with 312 additions and 163 deletions.
10 changes: 6 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/salesforcedx-utils-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@salesforce/core-bundle": "8.2.0",
"@salesforce/source-deploy-retrieve-bundle": "12.1.5",
"@salesforce/source-tracking-bundle": "7.0.6",
"@salesforce/vscode-service-provider": "1.1.1",
"applicationinsights": "1.0.7",
"cross-spawn": "7.0.3",
"rxjs": "^5.4.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Properties, Measurements, TelemetryData } from '@salesforce/vscode-service-provider';
import * as vscode from 'vscode';
import { CliCommandExecutor, Command, CommandExecution } from '../cli';
import {
Measurements,
Properties,
TelemetryBuilder,
TelemetryData,
TelemetryService
} from '../index';
import { nls } from '../messages';
Expand Down Expand Up @@ -120,8 +118,7 @@ export abstract class SfCommandletExecutor<T> implements CommandletExecutor<T> {
}

export abstract class LibraryCommandletExecutor<T>
implements CommandletExecutor<T>
{
implements CommandletExecutor<T> {
protected cancellable: boolean = false;
private cancelled: boolean = false;
private readonly executionName: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,18 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { ExtensionContext, ExtensionKind, Uri } from 'vscode';
import { TelemetryService } from '..';
import { ActivationInfo, TelemetryServiceInterface } from '@salesforce/vscode-service-provider';
import { ExtensionContext } from 'vscode';
import { getExtensionInfo } from './activationTrackerUtils';

export type ExtensionInfo = {
isActive: boolean;
path: string;
kind: ExtensionKind;
uri: Uri;
loadStartDate: Date;
};

export type ExtensionsInfo = {
[extensionId: string]: ExtensionInfo;
};

export type ActivationInfo = Partial<ExtensionInfo> & {
startActivateHrTime: [number, number];
activateStartDate: Date;
activateEndDate?: Date;
extensionActivationTime: number;
markEndTime?: number;
};

export class ActivationTracker {
private extensionContext: ExtensionContext;
private telemetryService: TelemetryService;
private telemetryService: TelemetryServiceInterface;
private _activationInfo: ActivationInfo;

constructor(
extensionContext: ExtensionContext,
telemetryService: TelemetryService
telemetryService: TelemetryServiceInterface
) {
this.extensionContext = extensionContext;
this.telemetryService = telemetryService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { ExtensionInfo, ExtensionsInfo } from '@salesforce/vscode-service-provider';
import { readFile } from 'fs/promises';
import { EOL } from 'os';
import { join, sep } from 'path';
import { extensions, ExtensionContext, Uri } from 'vscode';
import { ExtensionInfo, ExtensionsInfo } from './activationTracker';

type ParsedLog = {
dateTime: Date;
Expand Down
7 changes: 3 additions & 4 deletions packages/salesforcedx-utils-vscode/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ export {
} from './validations';
export { isSFContainerMode } from './env';
export {
ActivationInfo,
ActivationTracker,
ExtensionInfo,
ExtensionsInfo
ActivationTracker
} from './activationTracker';


5 changes: 1 addition & 4 deletions packages/salesforcedx-utils-vscode/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ export {
} from './context/workspaceContextUtil';
export {
TelemetryService,
TelemetryBuilder,
TelemetryData,
Properties,
Measurements
TelemetryBuilder
} from './services/telemetry';
export * from './helpers';
export { TraceFlags } from './helpers/traceFlags';
Expand Down
71 changes: 38 additions & 33 deletions packages/salesforcedx-utils-vscode/src/services/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {
Properties,
Measurements,
TelemetryData,
TelemetryServiceInterface,
TelemetryReporter,
ActivationInfo
} from '@salesforce/vscode-service-provider';
import * as util from 'util';
import { ExtensionContext, ExtensionMode, workspace } from 'vscode';
import { ActivationInfo } from '..';
import { ExtensionContext, ExtensionMode, workspace, extensions } from 'vscode';
import {
DEFAULT_AIKEY,
SFDX_CORE_CONFIGURATION_NAME,
Expand All @@ -18,7 +25,6 @@ import {
disableCLITelemetry,
isCLITelemetryAllowed
} from '../telemetry/cliConfiguration';
import { TelemetryReporter } from '../telemetry/interfaces/telemetryReporter';
import { AppInsights } from '../telemetry/reporters/appInsights';
import { LogStream } from '../telemetry/reporters/logStream';
import { LogStreamConfig } from '../telemetry/reporters/logStreamConfig';
Expand All @@ -31,19 +37,6 @@ type CommandMetric = {
executionTime?: string;
};

export type Measurements = {
[key: string]: number;
};

export type Properties = {
[key: string]: string;
};

export type TelemetryData = {
properties?: Properties;
measurements?: Measurements;
};

export class TelemetryBuilder {
private properties?: Properties;
private measurements?: Measurements;
Expand Down Expand Up @@ -75,7 +68,7 @@ export class TelemetryBuilder {
// export only for unit test
export class TelemetryServiceProvider {
public static instances = new Map<string, TelemetryService>(); // public only for unit test
public static getInstance(extensionName?: string): TelemetryService {
public static getInstance(extensionName?: string): TelemetryServiceInterface {
// default if not present
const name = extensionName || SFDX_CORE_EXTENSION_NAME;
let service = TelemetryServiceProvider.instances.get(name);
Expand All @@ -87,7 +80,7 @@ export class TelemetryServiceProvider {
}
}

export class TelemetryService {
export class TelemetryService implements TelemetryServiceInterface {
private extensionContext: ExtensionContext | undefined;
private reporters: TelemetryReporter[] = [];
private aiKey = DEFAULT_AIKEY;
Expand All @@ -97,7 +90,7 @@ export class TelemetryService {
* If no extension name provided, return the instance for core extension by default
* @param extensionName extension name
*/
public static getInstance(extensionName?: string) {
public static getInstance(extensionName?: string): TelemetryServiceInterface {
return TelemetryServiceProvider.getInstance(extensionName);
}
/**
Expand All @@ -111,23 +104,27 @@ export class TelemetryService {
* @param extensionContext extension context
* @param extensionName extension name
*/
public async initializeService(
public initializeService(
extensionContext: ExtensionContext
): Promise<void> {
const { name, version, aiKey } = extensionContext.extension.packageJSON;
const { name, version, aiKey } = extensionContext.extension.packageJSON as { name: string; version: string; aiKey: string };
return this.initializeServiceWithAttributes(name, aiKey, version, extensionContext.extensionMode);
}

public async initializeServiceWithAttributes(name: string, aiKey?: string, version?: string, extensionMode?: ExtensionMode): Promise<void> {
if (!name) {
console.log('Extension name is not defined in package.json');
}
if (!version) {
console.log('Extension version is not defined in package.json');
}
this.extensionContext = extensionContext;
this.extensionContext = getExtensionContextByName(name);
this.extensionName = name;
this.version = version;
this.version = version ?? '';
this.aiKey = aiKey || this.aiKey;

this.checkCliTelemetry()
.then(async cliEnabled => {
.then(cliEnabled => {
this.setCliTelemetryEnabled(
this.isTelemetryExtensionConfigurationEnabled() && cliEnabled
);
Expand All @@ -137,13 +134,13 @@ export class TelemetryService {
});

const isDevMode =
extensionContext.extensionMode !== ExtensionMode.Production;
extensionMode !== ExtensionMode.Production;

// TelemetryReporter is not initialized if user has disabled telemetry setting.
if (this.reporters.length === 0 && (await this.isTelemetryEnabled())) {
if (!isDevMode) {
console.log('adding AppInsights reporter.');
const userId = await UserService.getTelemetryUserId(this.extensionContext);
const userId = this.extensionContext ? await UserService.getTelemetryUserId(this.extensionContext) : 'unknown';
this.reporters.push(
new AppInsights(
this.getTelemetryReporterName(),
Expand Down Expand Up @@ -182,7 +179,7 @@ export class TelemetryService {
}
}

this.extensionContext.subscriptions.push(...this.reporters);
this.extensionContext?.subscriptions.push(...this.reporters);
}

/**
Expand Down Expand Up @@ -333,12 +330,12 @@ export class TelemetryService {
} catch (error) {
console.log(
'There was an error sending an exception report to: ' +
typeof reporter +
' ' +
'name: ' +
name +
' message: ' +
message
typeof reporter +
' ' +
'name: ' +
name +
' message: ' +
message
);
}
});
Expand Down Expand Up @@ -386,3 +383,11 @@ export class TelemetryService {
}
}
}

const getExtensionContextByName = (extensionName: string): ExtensionContext | undefined => {
const extension = extensions.getExtension(extensionName);
if (extension) {
// Access the extension's context
return extension.exports as ExtensionContext;
}
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

'use strict';

import { TelemetryReporter } from '@salesforce/vscode-service-provider';
import * as appInsights from 'applicationinsights';
import * as os from 'os';
import { Disposable, env, UIKind, version, workspace } from 'vscode';
import { WorkspaceContextUtil } from '../../context/workspaceContextUtil';
import { TelemetryReporter } from '../interfaces';

type CommonProperties = {
'common.os': string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/
'use strict';

import { TelemetryReporter } from '@salesforce/vscode-service-provider';
import * as fs from 'fs';
import * as path from 'path';
import { Disposable, workspace } from 'vscode';
import { WorkspaceContextUtil } from '../../context/workspaceContextUtil';
import { TelemetryReporter } from '../interfaces';

/**
* Represents a telemetry reporter that logs telemetry events to a file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { TelemetryReporter } from '@salesforce/vscode-service-provider';
import * as fs from 'fs';
import * as path from 'path';
import { getRootWorkspacePath } from '../..';
import { LOCAL_TELEMETRY_FILE } from '../../constants';
import { TelemetryReporter } from '../interfaces';

/**
* Represents a telemetry file that logs telemetry events by appending to a local file.
Expand Down
1 change: 1 addition & 0 deletions packages/salesforcedx-vscode-apex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@salesforce/apex-tmlanguage": "1.8.0",
"@salesforce/core-bundle": "8.2.0",
"@salesforce/salesforcedx-utils-vscode": "61.12.0",
"@salesforce/vscode-service-provider": "1.1.1",
"expand-home-dir": "0.0.3",
"find-java-home": "0.2.0",
"shelljs": "0.8.5",
Expand Down
Loading

0 comments on commit f8232f1

Please sign in to comment.