Skip to content

Commit db41a95

Browse files
authored
Update readme + support connection string (#211)
1 parent 6019c88 commit db41a95

6 files changed

+21
-20
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ This module provides a consistent way for extensions to report telemetry
33
over Application Insights. The module respects the user's decision about whether or
44
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.
55

6-
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.
6+
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.
77

88
# Install
99
With npm:
1010
`npm install @vscode/extension-telemetry`
11+
1112
With yarn:
1213
`yarn add @vscode/extension-telemetry`
1314

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

21-
// the application insights key (also known as instrumentation key)
22-
const key = '<your key>';
22+
// the connection string
23+
const connectionString = '<your connection string>';
2324

2425
// telemetry reporter
2526
let reporter;
2627

2728
function activate(context: vscode.ExtensionContext) {
2829
// create telemetry reporter on extension activation
29-
reporter = new TelemetryReporter(key);
30+
reporter = new TelemetryReporter(connectionString);
3031
// ensure it gets properly disposed. Upon disposal the events will be flushed
3132
context.subscriptions.push(reporter);
3233
}

dist/telemetryReporter.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ export interface ReplacementOption {
2828

2929
export default class TelemetryReporter {
3030
/**
31-
* @param key The app insights key
31+
* @param connectionString The app insights connection string
3232
* @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.
3333
*/
34-
constructor(key: string, replacementOptions?: ReplacementOption[]);
34+
constructor(connectionString: string, replacementOptions?: ReplacementOption[]);
3535

3636
/**
3737
* A string representation of the current level of telemetry being collected

src/browser/telemetryReporter.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ function getBrowserRelease(navigator: Navigator): string {
2121
}
2222

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

@@ -34,9 +34,9 @@ export default class TelemetryReporter extends BaseTelemetryReporter {
3434
architecture: "web",
3535
};
3636

37-
const sender = new BaseTelemetrySender(key, clientFactory);
37+
const sender = new BaseTelemetrySender(connectionString, clientFactory);
3838
// AIF is no longer supported
39-
if (key && (key.indexOf("AIF") === 0)) {
39+
if (connectionString && (connectionString.indexOf("AIF") === 0)) {
4040
throw new Error("AIF keys are no longer supported. Please switch to 1DS keys for 1st party extensions");
4141
}
4242
super(sender, vscode, { additionalCommonProperties: TelemetryUtil.getAdditionalCommonProperties(osShim) });

src/common/appInsightsClientFactory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ReplacementOption, SenderData } from "./baseTelemetryReporter";
1111
import { BaseTelemetryClient } from "./baseTelemetrySender";
1212
import { TelemetryUtil } from "./util";
1313

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

2828
appInsightsClient = new basicAISDK.ApplicationInsights({
29-
instrumentationKey: key,
29+
connectionString: connectionString,
3030
disableAjaxTracking: true,
3131
disableExceptionTracking: true,
3232
disableFetchTracking: true,

src/common/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class TelemetryUtil {
2828
}
2929

3030
/**
31-
* Given a key checks if it is a valid 1DS key
31+
* Given a key / connection string checks if it is a valid 1DS key
3232
* @param key The key to check if it's a valid 1DS key
3333
*/
3434
public static shouldUseOneDataSystemSDK(key: string): boolean {

src/node/telemetryReporter.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ function getXHROverride() {
5454
}
5555

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

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

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

0 commit comments

Comments
 (0)