Skip to content

Commit

Permalink
fix: telemetry user id is now properly filled (#5857)
Browse files Browse the repository at this point in the history
* fix: telemetry user id is now properly filled

@W-16742418@
Telemetry user Id is now being populated correctly

* chore: apply review suggestions
  • Loading branch information
peternhale authored Sep 19, 2024
1 parent b2c1d92 commit 581b63c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 39 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/salesforcedx-utils-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@salesforce/core-bundle": "8.5.7",
"@salesforce/source-deploy-retrieve-bundle": "12.6.3",
"@salesforce/source-tracking-bundle": "7.0.6",
"@salesforce/vscode-service-provider": "1.1.1",
"@salesforce/vscode-service-provider": "1.2.1",
"applicationinsights": "1.0.7",
"cross-spawn": "7.0.3",
"rxjs": "^5.4.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/salesforcedx-utils-vscode/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export {
SETTING_CLEAR_OUTPUT_TAB,
SFDX_CORE_CONFIGURATION_NAME,
SFDX_CORE_EXTENSION_NAME,
SFDX_LWC_EXTENSION_NAME
SFDX_LWC_EXTENSION_NAME,
TELEMETRY_GLOBAL_USER_ID
} from './constants';
export {
OrgUserInfo,
Expand Down
26 changes: 7 additions & 19 deletions packages/salesforcedx-utils-vscode/src/services/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ActivationInfo
} from '@salesforce/vscode-service-provider';
import * as util from 'util';
import { ExtensionContext, ExtensionMode, workspace, extensions } from 'vscode';
import { ExtensionContext, ExtensionMode, workspace } from 'vscode';
import {
DEFAULT_AIKEY,
SFDX_CORE_CONFIGURATION_NAME,
Expand Down Expand Up @@ -105,26 +105,22 @@ export class TelemetryService implements TelemetryServiceInterface {
* Initialize Telemetry Service during extension activation.
* @param extensionContext extension context
*/
public initializeService(
public async initializeService(
extensionContext: ExtensionContext
): Promise<void> {
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 = getExtensionContextByName(name);
this.extensionContext = extensionContext;
this.extensionName = name;
this.version = version ?? '';
this.aiKey = aiKey || this.aiKey;
this.isInternal = isInternalHost();
this.isDevMode = extensionMode !== ExtensionMode.Production;
this.isDevMode = extensionContext.extensionMode !== ExtensionMode.Production;

this.checkCliTelemetry()
.then(cliEnabled => {
Expand All @@ -136,7 +132,7 @@ export class TelemetryService implements TelemetryServiceInterface {
console.log('Error initializing telemetry service: ' + error);
});

if(this.reporters.length === 0 && (await this.isTelemetryEnabled())) {
if (this.reporters.length === 0 && (await this.isTelemetryEnabled())) {
const userId = this.extensionContext ? await UserService.getTelemetryUserId(this.extensionContext) : 'unknown';
const reporterConfig: TelemetryReporterConfig = {
extName: this.extensionName,
Expand Down Expand Up @@ -171,8 +167,8 @@ export class TelemetryService implements TelemetryServiceInterface {

public async isTelemetryEnabled(): Promise<boolean> {
return this.isInternal
? true
: (this.isTelemetryExtensionConfigurationEnabled() && await this.checkCliTelemetry());
? true
: (this.isTelemetryExtensionConfigurationEnabled() && await this.checkCliTelemetry());
}

public async checkCliTelemetry(): Promise<boolean> {
Expand Down Expand Up @@ -353,11 +349,3 @@ export class TelemetryService implements TelemetryServiceInterface {
}
}
}

const getExtensionContextByName = (extensionName: string): ExtensionContext | undefined => {
const extension = extensions.getExtension(extensionName);
if (extension) {
// Access the extension's context
return extension.exports as ExtensionContext;
}
};
2 changes: 1 addition & 1 deletion packages/salesforcedx-vscode-apex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@salesforce/apex-tmlanguage": "1.8.0",
"@salesforce/core-bundle": "8.5.7",
"@salesforce/salesforcedx-utils-vscode": "61.14.0",
"@salesforce/vscode-service-provider": "1.1.1",
"@salesforce/vscode-service-provider": "1.2.1",
"expand-home-dir": "0.0.3",
"find-java-home": "0.2.0",
"shelljs": "0.8.5",
Expand Down
7 changes: 4 additions & 3 deletions packages/salesforcedx-vscode-apex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export const activate = async (extensionContext: vscode.ExtensionContext) => {
if (!telemetryService) {
throw new Error('Could not fetch a telemetry service instance');
}

// Telemetry
await telemetryService.initializeService(extensionContext);

const activationTracker = new ActivationTracker(
extensionContext,
telemetryService
Expand Down Expand Up @@ -86,9 +90,6 @@ export const activate = async (extensionContext: vscode.ExtensionContext) => {
// Workspace Context
await workspaceContext.initialize(extensionContext);

// Telemetry
await telemetryService.initializeService(extensionContext);

// start the language server and client
await createLanguageClient(extensionContext, languageServerStatusBarItem);

Expand Down
2 changes: 1 addition & 1 deletion packages/salesforcedx-vscode-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@salesforce/source-deploy-retrieve-bundle": "12.6.3",
"@salesforce/templates": "61.4.10",
"@salesforce/ts-types": "2.0.12",
"@salesforce/vscode-service-provider": "1.1.1",
"@salesforce/vscode-service-provider": "1.2.1",
"adm-zip": "0.5.10",
"applicationinsights": "1.0.7",
"glob": "^7.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { TELEMETRY_GLOBAL_USER_ID } from '@salesforce/salesforcedx-utils-vscode';
import * as os from 'os';
import { extensions, ExtensionMode, window, Extension, ExtensionContext } from 'vscode';
import { extensions, window, Extension } from 'vscode';
import { TELEMETRY_GLOBAL_VALUE, TELEMETRY_INTERNAL_VALUE, TELEMETRY_OPT_OUT_LINK } from '../../../src/constants';
import { nls } from '../../../src/messages';
import { SalesforceCoreSettings } from '../../../src/settings/salesforceCoreSettings';
Expand Down Expand Up @@ -43,11 +44,14 @@ describe('Telemetry', () => {
const showMessage = nls.localize('telemetry_legal_dialog_message', TELEMETRY_OPT_OUT_LINK);
const internalMessage = nls.localize('telemetry_internal_user_message');

const handleTelemetryMsgShown = (key:string, globalMsgShown:boolean, internalMsgShown:boolean) => {
if(key === TELEMETRY_GLOBAL_VALUE) {
const handleTelemetryMsgShown = (key: string, globalMsgShown: boolean, internalMsgShown: boolean) => {
if (key === TELEMETRY_GLOBAL_USER_ID) {
return key;
}
if (key === TELEMETRY_GLOBAL_VALUE) {
return globalMsgShown;
}
if(key === TELEMETRY_INTERNAL_VALUE) {
if (key === TELEMETRY_INTERNAL_VALUE) {
return internalMsgShown;
}
throw new Error('unknown key');
Expand All @@ -56,8 +60,6 @@ describe('Telemetry', () => {
beforeEach(() => {
// create vscode extensionContext
mockExtensionContext = new MockExtensionContext();
jest.spyOn(extensions, 'getExtension')
.mockReturnValue(mockExtensionContext as unknown as Extension<any>);
globalStateTelemetrySpy = jest.spyOn(mockExtensionContext.globalState, 'get');
});

Expand Down Expand Up @@ -95,7 +97,8 @@ describe('Telemetry', () => {
expect(telemetryEnabled).toEqual(true);

await showTelemetryMessage(mockExtensionContext);
expect(globalStateTelemetrySpy).toHaveBeenCalledTimes(2);
expect(globalStateTelemetrySpy).toHaveBeenCalledTimes(3);
expect(globalStateTelemetrySpy).toHaveBeenCalledWith(TELEMETRY_GLOBAL_USER_ID);
expect(globalStateTelemetrySpy).toHaveBeenCalledWith(TELEMETRY_GLOBAL_VALUE);
expect(globalStateTelemetrySpy).toHaveBeenLastCalledWith(TELEMETRY_INTERNAL_VALUE);
expect(mShowInformation).not.toHaveBeenCalled();
Expand Down

0 comments on commit 581b63c

Please sign in to comment.