Skip to content

Commit

Permalink
Share telemetry from core extension. (#20893)
Browse files Browse the repository at this point in the history
it turns out the new telemetry API removed a way to set extension id and
version when telemetry reporter is created and it implicitly sets from
extension reporter is created. the same way how LSP client is working.

since we want to keep using the same extension id and etc for our
telemetry, we need the reporter created from core ext.
  • Loading branch information
heejaechang authored Mar 22, 2023
1 parent 807b9fe commit 01e798a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
18 changes: 4 additions & 14 deletions src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import { ILanguageServerOutputChannel } from './activation/types';
import { IExtensionApi } from './apiTypes';
import { isTestExecution, PYTHON_LANGUAGE } from './common/constants';
import { IConfigurationService, Resource } from './common/types';
import { IEnvironmentVariablesProvider } from './common/variables/types';
import { getDebugpyLauncherArgs, getDebugpyPackagePath } from './debugger/extension/adapter/remoteLaunchers';
import { IInterpreterService } from './interpreter/contracts';
import { IServiceContainer, IServiceManager } from './ioc/types';
import { JupyterExtensionIntegration } from './jupyter/jupyterIntegration';
import { traceError } from './logging';
import { IDiscoveryAPI } from './pythonEnvironments/base/locator';
import { buildEnvironmentApi } from './environmentApi';
import { ApiForPylance } from './pylanceApi';
import { getTelemetryReporter } from './telemetry';

export function buildApi(
ready: Promise<any>,
Expand All @@ -31,21 +32,14 @@ export function buildApi(
const interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
serviceManager.addSingleton<JupyterExtensionIntegration>(JupyterExtensionIntegration, JupyterExtensionIntegration);
const jupyterIntegration = serviceContainer.get<JupyterExtensionIntegration>(JupyterExtensionIntegration);
const envService = serviceContainer.get<IEnvironmentVariablesProvider>(IEnvironmentVariablesProvider);
const outputChannel = serviceContainer.get<ILanguageServerOutputChannel>(ILanguageServerOutputChannel);

const api: IExtensionApi & {
/**
* @deprecated Temporarily exposed for Pylance until we expose this API generally. Will be removed in an
* iteration or two.
*/
pylance: {
getPythonPathVar: (resource?: Uri) => Promise<string | undefined>;
readonly onDidEnvironmentVariablesChange: Event<Uri | undefined>;
createClient(...args: any[]): BaseLanguageClient;
start(client: BaseLanguageClient): Promise<void>;
stop(client: BaseLanguageClient): Promise<void>;
};
pylance: ApiForPylance;
} & {
/**
* @deprecated Use IExtensionApi.environments API instead.
Expand Down Expand Up @@ -126,11 +120,6 @@ export function buildApi(
: (noop as any),
},
pylance: {
getPythonPathVar: async (resource?: Uri) => {
const envs = await envService.getEnvironmentVariables(resource);
return envs.PYTHONPATH;
},
onDidEnvironmentVariablesChange: envService.onDidEnvironmentVariablesChange,
createClient: (...args: any[]): BaseLanguageClient => {
// Make sure we share output channel so that we can share one with
// Jedi as well.
Expand All @@ -141,6 +130,7 @@ export function buildApi(
},
start: (client: BaseLanguageClient): Promise<void> => client.start(),
stop: (client: BaseLanguageClient): Promise<void> => client.stop(),
getTelemetryReporter: () => getTelemetryReporter(),
},
environments: buildEnvironmentApi(discoveryApi, serviceContainer),
};
Expand Down
11 changes: 4 additions & 7 deletions src/client/browser/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,25 @@
import { BaseLanguageClient } from 'vscode-languageclient';
import { LanguageClient } from 'vscode-languageclient/browser';
import { PYTHON_LANGUAGE } from '../common/constants';
import { ApiForPylance, TelemetryReporter } from '../pylanceApi';

export interface IBrowserExtensionApi {
/**
* @deprecated Temporarily exposed for Pylance until we expose this API generally. Will be removed in an
* iteration or two.
*/
pylance: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
createClient(...args: any[]): BaseLanguageClient;
start(client: BaseLanguageClient): Promise<void>;
stop(client: BaseLanguageClient): Promise<void>;
};
pylance: ApiForPylance;
}

export function buildApi(): IBrowserExtensionApi {
export function buildApi(reporter: TelemetryReporter): IBrowserExtensionApi {
const api: IBrowserExtensionApi = {
pylance: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
createClient: (...args: any[]): BaseLanguageClient =>
new LanguageClient(PYTHON_LANGUAGE, 'Python Language Server', args[0], args[1]),
start: (client: BaseLanguageClient): Promise<void> => client.start(),
stop: (client: BaseLanguageClient): Promise<void> => client.stop(),
getTelemetryReporter: () => reporter,
},
};

Expand Down
6 changes: 4 additions & 2 deletions src/client/browser/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ let languageClient: LanguageClient | undefined;
let pylanceApi: PylanceApi | undefined;

export async function activate(context: vscode.ExtensionContext): Promise<IBrowserExtensionApi> {
const reporter = getTelemetryReporter();

const pylanceExtension = vscode.extensions.getExtension<PylanceApi>(PYLANCE_EXTENSION_ID);
if (pylanceExtension) {
await runPylance(context, pylanceExtension);
return buildApi();
return buildApi(reporter);
}

const changeDisposable = vscode.extensions.onDidChange(async () => {
Expand All @@ -35,7 +37,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<IBrows
}
});

return buildApi();
return buildApi(reporter);
}

export async function deactivate(): Promise<void> {
Expand Down
26 changes: 26 additions & 0 deletions src/client/pylanceApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { TelemetryEventMeasurements, TelemetryEventProperties } from '@vscode/extension-telemetry';
import { BaseLanguageClient } from 'vscode-languageclient';

export interface TelemetryReporter {
sendTelemetryEvent(
eventName: string,
properties?: TelemetryEventProperties,
measurements?: TelemetryEventMeasurements,
): void;
sendTelemetryErrorEvent(
eventName: string,
properties?: TelemetryEventProperties,
measurements?: TelemetryEventMeasurements,
): void;
}

export interface ApiForPylance {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
createClient(...args: any[]): BaseLanguageClient;
start(client: BaseLanguageClient): Promise<void>;
stop(client: BaseLanguageClient): Promise<void>;
getTelemetryReporter(): TelemetryReporter;
}
2 changes: 1 addition & 1 deletion src/client/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function _resetSharedProperties(): void {
}

let telemetryReporter: TelemetryReporter | undefined;
function getTelemetryReporter() {
export function getTelemetryReporter(): TelemetryReporter {
if (!isTestExecution() && telemetryReporter) {
return telemetryReporter;
}
Expand Down

0 comments on commit 01e798a

Please sign in to comment.