From 5a54a946f5c45d2ddcefe36768b0ca7fec8c5d66 Mon Sep 17 00:00:00 2001 From: Jun Han Date: Tue, 13 Aug 2024 14:35:43 +0800 Subject: [PATCH] chore: add subscription id and resource name in telemetry (#231) --- src/common/telemetryEvent.ts | 3 +- src/extension.ts | 5 ++- src/test/unit/utils/telemetryUtils.test.ts | 39 ++++++++++++++++++++++ src/tree/SubscriptionTreeItem.ts | 7 ++-- src/utils/telemetryUtils.ts | 18 ++++++++++ 5 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 src/test/unit/utils/telemetryUtils.test.ts create mode 100644 src/utils/telemetryUtils.ts diff --git a/src/common/telemetryEvent.ts b/src/common/telemetryEvent.ts index 06d70bac..0cac6fc9 100644 --- a/src/common/telemetryEvent.ts +++ b/src/common/telemetryEvent.ts @@ -11,7 +11,8 @@ export enum TelemetryProperties { duration = "duration", slashCommand = 'slashCommand', option = 'option', - treeItemFullId = 'treeItemFullId', + subscriptionId = 'subscriptionId', + resourceName = 'resourceName', }; export enum ErrorProperties { diff --git a/src/extension.ts b/src/extension.ts index b94d132a..db65b80c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -40,6 +40,7 @@ import { ext } from './extensionVariables'; import { ApiVersionDefinitionTreeItem } from './tree/ApiVersionDefinitionTreeItem'; import { createAzureAccountTreeItem } from "./tree/AzureAccountTreeItem"; import { OpenApiEditor } from './tree/Editors/openApi/OpenApiEditor'; +import { TelemetryUtils } from './utils/telemetryUtils'; export async function activate(context: vscode.ExtensionContext) { console.log('Congratulations, your extension "azure-api-center" is now active!'); @@ -157,9 +158,7 @@ async function registerCommandWithTelemetry(commandId: string, callback: Command } finally { const end: number = Date.now(); properties[TelemetryProperties.duration] = ((end - start) / 1000).toString(); - if (args[0] && args[0] instanceof AzExtTreeItem && args[0].fullId) { - properties[TelemetryProperties.treeItemFullId] = args[0].fullId; - } + TelemetryUtils.setAzureResourcesInfo(properties, args[0]); if (parsedError) { properties[ErrorProperties.errorType] = parsedError.errorType; properties[ErrorProperties.errorMessage] = parsedError.message; diff --git a/src/test/unit/utils/telemetryUtils.test.ts b/src/test/unit/utils/telemetryUtils.test.ts new file mode 100644 index 00000000..65e6d699 --- /dev/null +++ b/src/test/unit/utils/telemetryUtils.test.ts @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import { AzExtTreeItem, ISubscriptionContext } from "@microsoft/vscode-azext-utils"; +import * as assert from "assert"; +import { TelemetryUtils } from "../../../utils/telemetryUtils"; + +class MockAzExtTreeItem extends AzExtTreeItem { + public label: string; + public contextValue: string; + constructor(subscriptionContext: ISubscriptionContext) { + super(undefined); + this.label = 'mockLabel'; + this.contextValue = 'mockContext'; + this._subscription = subscriptionContext; + } + + private _subscription: ISubscriptionContext; + + public get subscription(): ISubscriptionContext { + return this._subscription; + } +} + +describe("telemetryUtils", () => { + it("set Azure Resources Info", () => { + const mockSubscriptionContext = { + subscriptionId: "mockSubscriptionId", + }; + const mockItem = new MockAzExtTreeItem(mockSubscriptionContext as ISubscriptionContext); + + const properties: { [key: string]: string; } = {}; + + TelemetryUtils.setAzureResourcesInfo(properties, mockItem); + + assert.strictEqual(properties["subscriptionId"], "mockSubscriptionId"); + assert.strictEqual(properties["resourceName"], "mockLabel"); + }); +}); diff --git a/src/tree/SubscriptionTreeItem.ts b/src/tree/SubscriptionTreeItem.ts index 3163689b..020883f1 100644 --- a/src/tree/SubscriptionTreeItem.ts +++ b/src/tree/SubscriptionTreeItem.ts @@ -3,7 +3,8 @@ import { AzExtParentTreeItem, AzExtTreeItem, ISubscriptionContext } from "@microsoft/vscode-azext-utils"; import { ResourceGraphService } from "../azure/ResourceGraph/ResourceGraphService"; import { TelemetryClient } from "../common/telemetryClient"; -import { TelemetryEvent, TelemetryProperties } from "../common/telemetryEvent"; +import { TelemetryEvent } from "../common/telemetryEvent"; +import { TelemetryUtils } from "../utils/telemetryUtils"; import { treeUtils } from "../utils/treeUtils"; import { ApiCenterTreeItem } from "./ApiCenterTreeItem"; @@ -49,7 +50,9 @@ class SubscriptionTreeItem extends AzExtParentTreeItem { } public async loadMoreChildrenImpl(): Promise { - TelemetryClient.sendEvent(TelemetryEvent.treeviewListApiCenters, { [TelemetryProperties.treeItemFullId]: this.fullId }); + const properties: { [key: string]: string; } = {}; + TelemetryUtils.setAzureResourcesInfo(properties, this); + TelemetryClient.sendEvent(TelemetryEvent.treeviewListApiCenters, properties); const resourceGraphService = new ResourceGraphService(this.subscription); diff --git a/src/utils/telemetryUtils.ts b/src/utils/telemetryUtils.ts new file mode 100644 index 00000000..488f666a --- /dev/null +++ b/src/utils/telemetryUtils.ts @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import { AzExtTreeItem } from "@microsoft/vscode-azext-utils"; +import { TelemetryProperties } from "../common/telemetryEvent"; + +export namespace TelemetryUtils { + export function setAzureResourcesInfo(properties: { [key: string]: string; }, arg: any): void { + if (arg && arg instanceof AzExtTreeItem) { + if (arg.subscription.subscriptionId) { + properties[TelemetryProperties.subscriptionId] = arg.subscription.subscriptionId; + } + if (arg.label) { + properties[TelemetryProperties.resourceName] = arg.label; + } + } + } +}