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

Update readme + support connection string #211

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ This module provides a consistent way for extensions to report telemetry
over Application Insights. The module respects the user's decision about whether or
not to send telemetry data. See [telemetry extension guidelines](https://code.visualstudio.com/api/extension-guides/telemetry) for more information on using telemetry in your extension.

Follow [guide to set up Application Insights](https://docs.microsoft.com/en-us/azure/azure-monitor/app/create-new-resource) in Azure and get your key. Don't worry about hardcoding it, it is not sensitive.
Follow [guide to set up Application Insights](https://learn.microsoft.com/en-us/azure/azure-monitor/app/create-workspace-resource) in Azure and get your connection string. Don't worry about hardcoding it, it is not sensitive.

# Install
With npm:
`npm install @vscode/extension-telemetry`

With yarn:
`yarn add @vscode/extension-telemetry`

Expand All @@ -18,15 +19,15 @@ With yarn:
import * as vscode from 'vscode';
import TelemetryReporter from '@vscode/extension-telemetry';

// the application insights key (also known as instrumentation key)
const key = '<your key>';
// the connection string
const connectionString = '<your connection string>';

// telemetry reporter
let reporter;

function activate(context: vscode.ExtensionContext) {
// create telemetry reporter on extension activation
reporter = new TelemetryReporter(key);
reporter = new TelemetryReporter(connectionString);
// ensure it gets properly disposed. Upon disposal the events will be flushed
context.subscriptions.push(reporter);
}
Expand Down
4 changes: 2 additions & 2 deletions dist/telemetryReporter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export interface ReplacementOption {

export default class TelemetryReporter {
/**
* @param key The app insights key
* @param connectionString The app insights connection string
* @param replacementOptions A list of replacement options for the app insights client. This allows the sender to filter out any sensitive or unnecessary information from the telemetry server.
*/
constructor(key: string, replacementOptions?: ReplacementOption[]);
constructor(connectionString: string, replacementOptions?: ReplacementOption[]);

/**
* A string representation of the current level of telemetry being collected
Expand Down
10 changes: 5 additions & 5 deletions src/browser/telemetryReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ function getBrowserRelease(navigator: Navigator): string {
}

export default class TelemetryReporter extends BaseTelemetryReporter {
constructor(key: string, replacementOptions?: ReplacementOption[]) {
let clientFactory = (key: string) => appInsightsClientFactory(key, vscode.env.machineId, undefined, replacementOptions);
constructor(connectionString: string, replacementOptions?: ReplacementOption[]) {
let clientFactory = (connectionString: string) => appInsightsClientFactory(connectionString, vscode.env.machineId, undefined, replacementOptions);
// If key is usable by 1DS use the 1DS SDk
if (TelemetryUtil.shouldUseOneDataSystemSDK(key)) {
if (TelemetryUtil.shouldUseOneDataSystemSDK(connectionString)) {
clientFactory = (key: string) => oneDataSystemClientFactory(key, vscode);
}

Expand All @@ -34,9 +34,9 @@ export default class TelemetryReporter extends BaseTelemetryReporter {
architecture: "web",
};

const sender = new BaseTelemetrySender(key, clientFactory);
const sender = new BaseTelemetrySender(connectionString, clientFactory);
// AIF is no longer supported
if (key && (key.indexOf("AIF") === 0)) {
if (connectionString && (connectionString.indexOf("AIF") === 0)) {
throw new Error("AIF keys are no longer supported. Please switch to 1DS keys for 1st party extensions");
}
super(sender, vscode, { additionalCommonProperties: TelemetryUtil.getAdditionalCommonProperties(osShim) });
Expand Down
4 changes: 2 additions & 2 deletions src/common/appInsightsClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ReplacementOption, SenderData } from "./baseTelemetryReporter";
import { BaseTelemetryClient } from "./baseTelemetrySender";
import { TelemetryUtil } from "./util";

export const appInsightsClientFactory = async (key: string, machineId: string, xhrOverride?: IXHROverride, replacementOptions?: ReplacementOption[]): Promise<BaseTelemetryClient> => {
export const appInsightsClientFactory = async (connectionString: string, machineId: string, xhrOverride?: IXHROverride, replacementOptions?: ReplacementOption[]): Promise<BaseTelemetryClient> => {
let appInsightsClient: ApplicationInsights | undefined;
try {
const basicAISDK = await import/* webpackMode: "eager" */("@microsoft/applicationinsights-web-basic");
Expand All @@ -26,7 +26,7 @@ export const appInsightsClientFactory = async (key: string, machineId: string, x
}

appInsightsClient = new basicAISDK.ApplicationInsights({
instrumentationKey: key,
connectionString: connectionString,
disableAjaxTracking: true,
disableExceptionTracking: true,
disableFetchTracking: true,
Expand Down
2 changes: 1 addition & 1 deletion src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class TelemetryUtil {
}

/**
* Given a key checks if it is a valid 1DS key
* Given a key / connection string checks if it is a valid 1DS key
* @param key The key to check if it's a valid 1DS key
*/
public static shouldUseOneDataSystemSDK(key: string): boolean {
Expand Down
12 changes: 6 additions & 6 deletions src/node/telemetryReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ function getXHROverride() {
}

export default class TelemetryReporter extends BaseTelemetryReporter {
constructor(key: string, replacementOptions?: ReplacementOption[]) {
let clientFactory = (key: string) => appInsightsClientFactory(key, vscode.env.machineId, getXHROverride(), replacementOptions);
// If key is usable by 1DS use the 1DS SDk
if (TelemetryUtil.shouldUseOneDataSystemSDK(key)) {
constructor(connectionString: string, replacementOptions?: ReplacementOption[]) {
let clientFactory = (connectionString: string) => appInsightsClientFactory(connectionString, vscode.env.machineId, getXHROverride(), replacementOptions);
// If connection string is usable by 1DS use the 1DS SDk
if (TelemetryUtil.shouldUseOneDataSystemSDK(connectionString)) {
clientFactory = (key: string) => oneDataSystemClientFactory(key, vscode, getXHROverride());
}

Expand All @@ -67,8 +67,8 @@ export default class TelemetryReporter extends BaseTelemetryReporter {
architecture: os.arch(),
};

const sender = new BaseTelemetrySender(key, clientFactory,);
if (key && key.indexOf("AIF-") === 0) {
const sender = new BaseTelemetrySender(connectionString, clientFactory,);
if (connectionString && connectionString.indexOf("AIF-") === 0) {
throw new Error("AIF keys are no longer supported. Please switch to 1DS keys for 1st party extensions");
}
super(sender, vscode, { additionalCommonProperties: TelemetryUtil.getAdditionalCommonProperties(osShim) });
Expand Down
Loading