Skip to content

Commit

Permalink
chore: add subscription id and resource name in telemetry (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
formulahendry authored Aug 13, 2024
1 parent a6f8a4e commit 5a54a94
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/common/telemetryEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export enum TelemetryProperties {
duration = "duration",
slashCommand = 'slashCommand',
option = 'option',
treeItemFullId = 'treeItemFullId',
subscriptionId = 'subscriptionId',
resourceName = 'resourceName',
};

export enum ErrorProperties {
Expand Down
5 changes: 2 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand Down Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions src/test/unit/utils/telemetryUtils.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
7 changes: 5 additions & 2 deletions src/tree/SubscriptionTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -49,7 +50,9 @@ class SubscriptionTreeItem extends AzExtParentTreeItem {
}

public async loadMoreChildrenImpl(): Promise<AzExtTreeItem[]> {
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);

Expand Down
18 changes: 18 additions & 0 deletions src/utils/telemetryUtils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}

0 comments on commit 5a54a94

Please sign in to comment.