Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] Initial check-in for supporting telemetry in vscode #6123

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
init check-in for telemetry
  • Loading branch information
RodgeFu committed Feb 16, 2025
commit b131f23a3b6fde3e8fdb8a3fdad920e20c920afd
68 changes: 63 additions & 5 deletions packages/typespec-vscode/ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
@@ -8,11 +8,69 @@ original copyright notices and the licenses under which Microsoft received such
components are set forth below. Microsoft reserves all rights not expressly
granted herein, whether by implication, estoppel or otherwise.

1. balanced-match version 1.0.2 (https://github.com/juliangruber/balanced-match)
2. brace-expansion version 2.0.1 (https://github.com/juliangruber/brace-expansion)
3. minimatch version 5.1.6 (https://github.com/isaacs/minimatch)
4. semver version 7.6.3 (https://github.com/npm/node-semver)
5. yaml version 2.5.1 (github:eemeli/yaml)
1. @nevware21/ts-async version 0.5.4 (https://github.com/nevware21/ts-async)
2. @nevware21/ts-utils version 0.11.6 (https://github.com/nevware21/ts-utils)
3. balanced-match version 1.0.2 (https://github.com/juliangruber/balanced-match)
4. brace-expansion version 2.0.1 (https://github.com/juliangruber/brace-expansion)
5. minimatch version 5.1.6 (https://github.com/isaacs/minimatch)
6. semver version 7.6.3 (https://github.com/npm/node-semver)
7. yaml version 2.5.1 (github:eemeli/yaml)


%% @nevware21/ts-async NOTICES AND INFORMATION BEGIN HERE
=====================================================
MIT License

Copyright (c) 2022 NevWare21 Solutions LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=====================================================");
END OF @nevware21/ts-async NOTICES AND INFORMATION


%% @nevware21/ts-utils NOTICES AND INFORMATION BEGIN HERE
=====================================================
MIT License

Copyright (c) 2022 NevWare21 Solutions LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=====================================================");
END OF @nevware21/ts-utils NOTICES AND INFORMATION


%% balanced-match NOTICES AND INFORMATION BEGIN HERE
3 changes: 3 additions & 0 deletions packages/typespec-vscode/package.json
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@
"workspaceContains:**/tspconfig.yaml"
],
"icon": "./icons/logo.png",
"telemetryKey": "19cdc7e5-ff34-485f-b2cb-9538638cba7c",
"contributes": {
"viewsWelcome": [
{
@@ -231,6 +232,7 @@
},
"devDependencies": {
"@rollup/plugin-commonjs": "~28.0.0",
"@rollup/plugin-json": "~6.1.0",
"@rollup/plugin-node-resolve": "~15.3.0",
"@rollup/plugin-typescript": "~12.1.0",
"@types/mocha": "^10.0.9",
@@ -241,6 +243,7 @@
"@typespec/internal-build-utils": "workspace:~",
"@vitest/coverage-v8": "^2.1.5",
"@vitest/ui": "^2.1.2",
"@vscode/extension-telemetry": "^0.9.8",
"@vscode/test-web": "^0.0.62",
"@vscode/vsce": "~3.1.1",
"c8": "^10.1.2",
3 changes: 2 additions & 1 deletion packages/typespec-vscode/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -3,11 +3,12 @@ import resolve from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import { dirname } from "path";

import json from "@rollup/plugin-json";
import { defineConfig } from "rollup";
import { fileURLToPath } from "url";
const projDir = dirname(fileURLToPath(import.meta.url));

const plugins = [(resolve as any)({ preferBuiltins: true }), (commonjs as any)()];
const plugins = [(resolve as any)({ preferBuiltins: true }), (commonjs as any)(), (json as any)()];
const baseConfig = defineConfig({
input: "src/extension.ts",
output: {
72 changes: 55 additions & 17 deletions packages/typespec-vscode/src/extension.ts
Original file line number Diff line number Diff line change
@@ -6,11 +6,16 @@ import { ExtensionLogListener, getPopupAction } from "./log/extension-log-listen
import logger from "./log/logger.js";
import { TypeSpecLogOutputChannel } from "./log/typespec-log-output-channel.js";
import { createTaskProvider } from "./task-provider.js";
import telemetryClient from "./telemetry/telemetry-client.js";
import { OperationTelemetryEvent, TelemetryEventName } from "./telemetry/telemetry-event.js";
import { TspLanguageClient } from "./tsp-language-client.js";
import {
CommandName,
InstallGlobalCliCommandArgs,
RestartServerCommandArgs,
RestartServerCommandResult,
Result,
ResultCode,
SettingName,
} from "./types.js";
import { isWhitespaceStringOrUndefined } from "./utils.js";
@@ -29,6 +34,8 @@ logger.registerLogListener("extension-log", new ExtensionLogListener(outputChann
export async function activate(context: ExtensionContext) {
const stateManager = new ExtensionStateManager(context);

context.subscriptions.push(telemetryClient);

context.subscriptions.push(createTaskProvider());

context.subscriptions.push(createCodeActionProvider());
@@ -66,26 +73,37 @@ export async function activate(context: ExtensionContext) {
context.subscriptions.push(
commands.registerCommand(
CommandName.RestartServer,
async (args: RestartServerCommandArgs | undefined): Promise<TspLanguageClient> => {
async (args: RestartServerCommandArgs | undefined): Promise<RestartServerCommandResult> => {
return vscode.window.withProgress(
{
title: args?.notificationMessage ?? "Restarting TypeSpec language service...",
location: vscode.ProgressLocation.Notification,
},
async () => {
if (args?.forceRecreate === true) {
logger.info("Forcing to recreate TypeSpec LSP server...");
return await recreateLSPClient(context);
}
if (client && client.state === State.Running) {
await client.restart();
return client;
} else {
logger.info(
"TypeSpec LSP server is not running which is not expected, try to recreate and start...",
);
return recreateLSPClient(context);
}
return await telemetryClient.doOperationWithTelemetry(
TelemetryEventName.RestartServer,
async (tel) => {
if (args?.forceRecreate === true) {
logger.info("Forcing to recreate TypeSpec LSP server...");
const c = await recreateLSPClient(context, tel.activityId);
tel.lastStep = "Recreate LSP client";
return getResultFromLSPClient(c);
}
if (client && client.state === State.Running) {
await client.restart(tel.activityId);
tel.lastStep = "Restart LSP client";
return getResultFromLSPClient(client);
} else {
logger.info(
"TypeSpec LSP server is not running which is not expected, try to recreate and start...",
);
const c = await recreateLSPClient(context, tel.activityId);
tel.lastStep = "Create LSP client";
return getResultFromLSPClient(c);
}
},
args?.activityId,
);
},
);
},
@@ -153,7 +171,13 @@ export async function activate(context: ExtensionContext) {
location: vscode.ProgressLocation.Notification,
},
async () => {
await recreateLSPClient(context);
await telemetryClient.doOperationWithTelemetry(
TelemetryEventName.StartExtension,
async (tel: OperationTelemetryEvent) => {
await recreateLSPClient(context, tel.activityId);
return getResultFromLSPClient(client);
},
);
},
);
} else {
@@ -166,15 +190,29 @@ export async function deactivate() {
await client?.stop();
}

async function recreateLSPClient(context: ExtensionContext) {
async function recreateLSPClient(context: ExtensionContext, activityId?: string) {
logger.info("Recreating TypeSpec LSP server...");
const oldClient = client;
client = await TspLanguageClient.create(context, outputChannel);
await oldClient?.stop();
await client.start();
await client.start(activityId);
return client;
}

function getResultFromLSPClient(c: TspLanguageClient | undefined): Result<TspLanguageClient> {
if (c?.state === State.Running) {
return {
code: ResultCode.Success,
value: c,
};
} else {
return {
code: ResultCode.Fail,
details: "TspLanguageClient is not running. Please check previous log for details.",
};
}
}

function showStartUpMessages(stateManager: ExtensionStateManager) {
vscode.workspace.workspaceFolders?.forEach((workspaceFolder) => {
const msg = stateManager.loadStartUpMessage(workspaceFolder.uri.fsPath);
118 changes: 118 additions & 0 deletions packages/typespec-vscode/src/telemetry/telemetry-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import TelemetryReporter from "@vscode/extension-telemetry";
import pkgJson from "../../package.json" assert { type: "json" };
import logger from "../log/logger.js";
import { Result } from "../types.js";
import { isWhitespaceStringOrUndefined } from "../utils.js";
import {
createOperationTelemetryEvent,
emptyActivityId,
OperationTelemetryEvent,
TelemetryEventName,
} from "./telemetry-event.js";

class TelemetryClient {
private _client: TelemetryReporter.default | undefined;
// The maximum number of telemetry error to log to avoid too much noise from it when
// the telemetry doesn't work for some reason
private readonly MAX_LOG_TELEMETRY_ERROR = 5;
private _logTelemetryErrorCount = 0;

constructor() {
const cs = `InstrumentationKey=${pkgJson.telemetryKey}`;
this._client = new (TelemetryReporter as any)(cs);
}

private sendEvent(
eventName: string,
properties?: { [key: string]: string },
measurements?: { [key: string]: number },
): void {
try {
this._client?.sendTelemetryEvent(eventName, properties, measurements);
} catch (e) {
this.logErrorWhenLoggingTelemetry(e);
}
}

private sendErrorEvent(
eventName: string,
properties?: { [key: string]: string },
measurements?: { [key: string]: number },
): void {
try {
this._client?.sendTelemetryErrorEvent(eventName, properties, measurements);
} catch (e) {
this.logErrorWhenLoggingTelemetry(e);
}
}

public async doOperationWithTelemetry<T>(
eventName: TelemetryEventName,
operation: (opTelemetryEvent: OperationTelemetryEvent) => Promise<Result<T>>,
activityId?: string,
): Promise<Result<T>> {
const opTelemetryEvent = createOperationTelemetryEvent(eventName, activityId);
try {
const result = await operation(opTelemetryEvent);
opTelemetryEvent.result ??= result.code;
return result;
} finally {
opTelemetryEvent.endTime ??= new Date();
this.logOperationTelemetryEvent(opTelemetryEvent);
}
}

public logOperationTelemetryEvent(event: OperationTelemetryEvent) {
const telFunc =
event.result === "success" || event.result === "cancelled"
? this.sendEvent
: this.sendErrorEvent;
telFunc.call(this, event.eventName, {
activityId: isWhitespaceStringOrUndefined(event.activityId)
? emptyActivityId
: event.activityId!,
// ISO format: YYYY-MM-DDTHH:mm:ss.sssZ
startTime: event.startTime.toISOString(),
endTime: event.endTime?.toISOString() ?? "",
lastStep: event.lastStep ?? "undefined",
result: event.result ?? "undefined",
});
}

/**
* Use this method to send log to telemetry.
* IMPORTANT: make sure to:
* - Collect as *little* telemetry as possible.
* - Do not include any personal or sensitive information.
* Detail guidance can be found at: https://code.visualstudio.com/api/extension-guides/telemetry
* @param level
* @param message
* @param activityId
*/
public log(level: "error", message: string, activityId?: string) {
const telFunc = level === "error" ? this.sendErrorEvent : this.sendEvent;
telFunc.call(this, TelemetryEventName.Log, {
activityId: isWhitespaceStringOrUndefined(activityId) ? emptyActivityId : activityId!,
level: level,
message: message,
});
}

private logErrorWhenLoggingTelemetry(error: any) {
if (this._logTelemetryErrorCount++ < this.MAX_LOG_TELEMETRY_ERROR) {
logger.error("Failed to log telemetry event\n", [error]);
}
if (this._logTelemetryErrorCount === this.MAX_LOG_TELEMETRY_ERROR) {
logger.error(
`Failed to log telemetry event more than ${this.MAX_LOG_TELEMETRY_ERROR} times, will stop logging more error`,
);
}
}

async dispose() {
await this._client?.dispose();
}
}

const telemetryClient = new TelemetryClient();
export default telemetryClient;
Loading
Oops, something went wrong.