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
Prev Previous commit
Next Next commit
refine handling of telemetry key
  • Loading branch information
RodgeFu committed Feb 27, 2025
commit d302d59b2fe1bc3ab9c8c8e5dd927da14fda6d1f
4 changes: 0 additions & 4 deletions eng/tsp-core/pipelines/templates/build.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Template building all packages with parallelism.

steps:
- script: pnpm run update-telemetry-key $(vscode.telemetryKey)
workingDirectory: $(Build.SourcesDirectory)/packages/typespec-vscode
displayName: Update vscode telemetry key

- script: pnpm run build
displayName: Build
2 changes: 1 addition & 1 deletion packages/typespec-vscode/package.json
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@
"workspaceContains:**/tspconfig.yaml"
],
"icon": "./icons/logo.png",
"telemetryKey": "19cdc7e5-ff34-485f-b2cb-9538638cba7c",
"telemetryKey": "00000000-0000-0000-0000-000000000000",
"contributes": {
"viewsWelcome": [
{
1 change: 1 addition & 0 deletions packages/typespec-vscode/src/const.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const StartFileName = "main.tsp";
export const TspConfigFileName = "tspconfig.yaml";
export const EmptyGuid = "00000000-0000-0000-0000-000000000000";
28 changes: 25 additions & 3 deletions packages/typespec-vscode/src/telemetry/telemetry-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import TelemetryReporter from "@vscode/extension-telemetry";
import pkgJson from "../../package.json" assert { type: "json" };
import { EmptyGuid } from "../const.js";
import logger from "../log/logger.js";
import { ResultCode } from "../types.js";
import { isWhitespaceStringOrUndefined } from "../utils.js";
@@ -19,17 +20,38 @@ class TelemetryClient {
private _logTelemetryErrorCount = 0;

constructor() {
this._client = new (TelemetryReporter as any)(this.getConnectionString());
this.initClient();
}

private getConnectionString() {
private initClient() {
const cs = this.getConnectionString();
if (!cs) {
this.logErrorWhenLoggingTelemetry(
"Skip initializing telemetry client because no telemetry key is provided",
);
this._client = undefined;
} else {
this._client = new (TelemetryReporter as any)(cs);
}
}

private getConnectionString(): string | undefined {
let key: string | undefined = pkgJson.telemetryKey;
if (!key || key === EmptyGuid) {
// try to check environment variable VSCODE_TELEMETRY_KEY if the key is not provided in package.json
key = process.env.VSCODE_TELEMETRY_KEY;
}
if (!key || key === EmptyGuid) {
return undefined;
}
return `InstrumentationKey=${pkgJson.telemetryKey}`;
}

public async flush() {
// flush function is not exposed by the telemetry client, so we leverage dispose to trigger the flush and recreate the client
await this._client?.dispose();
this._client = new (TelemetryReporter as any)(this.getConnectionString());
this._client = undefined;
this.initClient();
}

private sendEvent(
5 changes: 4 additions & 1 deletion packages/typespec-vscode/src/telemetry/telemetry-event.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EmptyGuid } from "../const.js";
import { ResultCode } from "../types.js";

export enum TelemetryEventName {
@@ -26,6 +27,8 @@ export interface OperationTelemetryEvent extends TelemetryEventBase {
export enum OperationDetailPropertyName {
error,
emitterPackage,
emitterVersion,
emitResult,
compilerLocation,
compilerVersion,
}
@@ -34,4 +37,4 @@ export function generateActivityId() {
return crypto.randomUUID();
}

export const emptyActivityId = "00000000-0000-0000-0000-000000000000";
export const emptyActivityId = EmptyGuid;
Loading
Oops, something went wrong.