Skip to content

Commit

Permalink
update code base
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Aug 1, 2023
1 parent 1d7d40e commit 1e7c2d1
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 175 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import vscode from "vscode";
import { TelemetryReporter } from 'vscode-telemetry';

export async function activate(context: vscode.ExtensionContext) {
TelemetryReporter.configure(context, process.env.INSTRUMENTATION_KEY!);
TelemetryReporter.configure(process.env.INSTRUMENTATION_KEY!);
// ...
}

Expand Down
2 changes: 0 additions & 2 deletions __mocks__/@vscode/extension-telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export default class ExtensionTelemetryReporter {
constructor (
public extensionId: string,
public version: string,
public key: string
) {}
}
195 changes: 108 additions & 87 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,22 @@
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.3",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.1.0",
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.1.2",
"@types/node": "^20.4.5",
"@typescript-eslint/eslint-plugin": "^6.2.0",
"@typescript-eslint/parser": "^6.2.0",
"@vitest/coverage-v8": "^0.33.0",
"@typescript-eslint/eslint-plugin": "^6.2.1",
"@typescript-eslint/parser": "^6.2.1",
"@vitest/coverage-v8": "^0.34.0",
"eslint": "^8.46.0",
"eslint-plugin-import": "^2.28.0",
"npm-run-all": "^4.1.5",
"release-it": "^16.1.3",
"rollup": "^3.27.0",
"rollup-plugin-cleanup": "^3.2.1",
"typescript": "^5.1.6",
"vitest": "^0.33.0"
"vitest": "^0.34.0"
},
"dependencies": {
"@types/vscode": "1.80.0",
Expand Down
4 changes: 4 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import resolve from '@rollup/plugin-node-resolve'
import typescript from '@rollup/plugin-typescript'
import commonjs from '@rollup/plugin-commonjs'
import terser from '@rollup/plugin-terser'
import json from '@rollup/plugin-json'

import cleanup from 'rollup-plugin-cleanup'

Expand Down Expand Up @@ -54,6 +55,7 @@ const esm = {
},
plugins: [
resolve({ extensions }),
json(),
commonjs({ defaultIsModuleExports: false }),
typescript({
outputToFilesystem: true,
Expand All @@ -78,6 +80,7 @@ const cjs = {
},
plugins: [
resolve({ extensions }),
json(),
commonjs({ defaultIsModuleExports: false }),
typescript({
outputToFilesystem: true,
Expand All @@ -101,6 +104,7 @@ const browser = {
},
plugins: [
resolve({ extensions, browser: true }),
json(),
commonjs({ defaultIsModuleExports: false }),
typescript({
outputToFilesystem: true,
Expand Down
45 changes: 13 additions & 32 deletions src/BaseTelemetryReporter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Disposable } from 'vscode'
import type TelemetryReporter from '@vscode/extension-telemetry'
import type {
TelemetryEventProperties,
RawTelemetryEventProperties,
TelemetryEventMeasurements
} from '@vscode/extension-telemetry'

Expand All @@ -17,6 +17,13 @@ export class BaseTelemetryReporter {
}
}

/**
* An event that is fired when the telemetry level is changed
*/
static onDidChangeTelemetryLevel (listener: (ev: 'all' | 'error' | 'crash' | 'off', thisArgs?: any, disposables?: Disposable[]) => any) {
return this.reporter!.onDidChangeTelemetryLevel(listener)
}

/**
* Sends a telemetry event with the given properties and measurements
* Properties are sanitized on best-effort basis to remove sensitive data prior to sending.
Expand All @@ -35,7 +42,7 @@ export class BaseTelemetryReporter {
* @param properties The set of properties to add to the event in the form of a string key value pair
* @param measurements The set of measurements to add to the event in the form of a string key number value pair
*/
static sendRawTelemetryEvent(eventName: string, properties?: RawTelemetryEventProperties, measurements?: TelemetryEventMeasurements) {
static sendRawTelemetryEvent(eventName: string, properties?: TelemetryEventProperties, measurements?: TelemetryEventMeasurements) {
this.ensureToBeConfigured()
this.reporter!.sendRawTelemetryEvent(eventName, properties, measurements)
}
Expand All @@ -46,11 +53,10 @@ export class BaseTelemetryReporter {
* @param eventName The name of the event
* @param properties The properties to send with the event
* @param measurements The measurements (numeric values) to send with the event
* @param sanitize Whether or not to sanitize to the properties and measures, defaults to true
*/
static sendDangerousTelemetryEvent(eventName: string, properties?: TelemetryEventProperties, measurements?: TelemetryEventMeasurements, sanitize?: boolean) {
static sendDangerousTelemetryEvent(eventName: string, properties?: TelemetryEventProperties, measurements?: TelemetryEventMeasurements) {
this.ensureToBeConfigured()
this.reporter!.sendDangerousTelemetryEvent(eventName, properties, measurements, sanitize)
this.reporter!.sendDangerousTelemetryEvent(eventName, properties, measurements)
}

/**
Expand All @@ -71,34 +77,9 @@ export class BaseTelemetryReporter {
* @param eventName The name of the event
* @param properties The properties to send with the event
* @param measurements The measurements (numeric values) to send with the event
* @param sanitize Whether or not to run the properties and measures through sanitiziation, defaults to true
*/
static sendDangerousTelemetryErrorEvent(eventName: string, properties?: TelemetryEventProperties, measurements?: TelemetryEventMeasurements, sanitize?: boolean) {
this.ensureToBeConfigured()
this.reporter!.sendDangerousTelemetryErrorEvent(eventName, properties, measurements, sanitize)
}

/**
* Sends an exception which includes the error stack, properties, and measurements
* @param error The error to send
* @param properties The set of properties to add to the event in the form of a string key value pair
* @param measurements The set of measurements to add to the event in the form of a string key number value pair
*/
static sendTelemetryException(error: Error, properties?: TelemetryEventProperties, measurements?: TelemetryEventMeasurements) {
this.ensureToBeConfigured()
this.reporter!.sendTelemetryException(error, properties, measurements)
}

/**
* **DANGEROUS** Given an error, properties, and measurements. Sends an exception event without checking the telemetry setting
* Do not use unless in a controlled environment i.e. sending telmetry from a CI pipeline or testing during development
* @param eventName The name of the event
* @param properties The properties to send with the event
* @param measurements The measurements (numeric values) to send with the event
* @param sanitize Whether or not to sanitize to the properties and measures, defaults to true
*/
static sendDangerousTelemetryException(error: Error, properties?: TelemetryEventProperties, measurements?: TelemetryEventMeasurements, sanitize?: boolean) {
static sendDangerousTelemetryErrorEvent(eventName: string, properties?: TelemetryEventProperties, measurements?: TelemetryEventMeasurements) {
this.ensureToBeConfigured()
this.reporter!.sendDangerousTelemetryException(error, properties, measurements, sanitize)
this.reporter!.sendDangerousTelemetryErrorEvent(eventName, properties, measurements)
}
}
13 changes: 7 additions & 6 deletions src/extension/TelemetryReporter.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type vscode from 'vscode'
import ExtensionTelemetryReporter from '@vscode/extension-telemetry'
import ExtensionTelemetryReporter, { type ReplacementOption } from '@vscode/extension-telemetry'

import { BaseTelemetryReporter } from '../BaseTelemetryReporter.js'

/**
* Telemetry reporter to be imported within a VS Code extension
*/
export class TelemetryReporter extends BaseTelemetryReporter {
static configure (context: vscode.ExtensionContext, key: string) {
const { name, publisher, version } = context.extension.packageJSON
const extensionId = `${publisher}.${name}`
this.reporter = new ExtensionTelemetryReporter(extensionId, version, key)
/**
* @param key The app insights key
* @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.
*/
static configure (key: string, replacementOptions: ReplacementOption[] = []) {
this.reporter = new ExtensionTelemetryReporter(key, replacementOptions)
return this.reporter
}
}
3 changes: 1 addition & 2 deletions src/extension/TelemetryViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ export class TelemetryViewProvider implements vscode.WebviewViewProvider {
TelemetryReporter[payload.eventType](
payload.eventName,
payload.properties,
payload.measurements,
payload.sanitize
payload.measurements
)
})
}
Expand Down
11 changes: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import vscode from 'vscode'

import { TelemetryReporter } from './extension/TelemetryReporter'
import { PAYLOAD_KEY } from './constants'
import { TelemetryReporter } from './extension/TelemetryReporter.js'
import { PAYLOAD_KEY } from './constants.js'
import type { TelemetryEvent } from './types'

export const createWebviewTelemetryPanel = (
Expand All @@ -21,13 +21,12 @@ export const createWebviewTelemetryPanel = (
TelemetryReporter[payload.eventType](
payload.eventName,
payload.properties,
payload.measurements,
payload.sanitize
payload.measurements
)
})

return panel
}

export * from './extension/TelemetryReporter'
export * from './extension/TelemetryViewProvider'
export * from './extension/TelemetryReporter.js'
export * from './extension/TelemetryViewProvider.js'
6 changes: 2 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import type {
TelemetryEventProperties,
RawTelemetryEventProperties,
TelemetryEventMeasurements
} from '@vscode/extension-telemetry'

export type EventTypes = 'sendTelemetryEvent' | 'sendRawTelemetryEvent' | 'sendDangerousTelemetryEvent' | 'sendTelemetryErrorEvent' | 'sendDangerousTelemetryErrorEvent' | 'sendTelemetryException' | 'sendDangerousTelemetryException'
export type EventTypes = 'sendTelemetryEvent' | 'sendRawTelemetryEvent' | 'sendDangerousTelemetryEvent' | 'sendTelemetryErrorEvent' | 'sendDangerousTelemetryErrorEvent'
export interface TelemetryPayload {
eventType: EventTypes
eventName: string & Error
properties?: TelemetryEventProperties | RawTelemetryEventProperties
properties?: TelemetryEventProperties
measurements?: TelemetryEventMeasurements
sanitize?: boolean
}

export interface TelemetryEvent {
Expand Down
13 changes: 6 additions & 7 deletions src/webview/TelemetryReporter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { WebviewApi } from 'vscode-webview'
import type {
TelemetryEventProperties,
RawTelemetryEventProperties,
TelemetryEventMeasurements
} from '@vscode/extension-telemetry'

Expand All @@ -14,24 +13,24 @@ export class TangleTelemetryReporter extends BaseTelemetryReporter {
function handlerFunction (eventType: EventTypes) {
return (
eventName: string | Error,
properties?: TelemetryEventProperties | RawTelemetryEventProperties,
measurements?: TelemetryEventMeasurements,
sanitize?: boolean
properties?: TelemetryEventProperties,
measurements?: TelemetryEventMeasurements
) => vscode.postMessage(<TelemetryEvent>{
[PAYLOAD_KEY]: { eventType, eventName, properties, measurements, sanitize }
[PAYLOAD_KEY]: { eventType, eventName, properties, measurements }
})
}

this.reporter = {
telemetryLevel: 'all',
dispose: () => Promise.resolve({}),
onDidChangeTelemetryLevel: () => {
throw new Error('onDidChangeTelemetryLevel not supported')
},
sendTelemetryEvent: handlerFunction('sendTelemetryEvent'),
sendRawTelemetryEvent: handlerFunction('sendRawTelemetryEvent'),
sendDangerousTelemetryEvent: handlerFunction('sendDangerousTelemetryEvent'),
sendTelemetryErrorEvent: handlerFunction('sendTelemetryErrorEvent'),
sendDangerousTelemetryErrorEvent: handlerFunction('sendDangerousTelemetryErrorEvent'),
sendTelemetryException: handlerFunction('sendTelemetryException'),
sendDangerousTelemetryException: handlerFunction('sendDangerousTelemetryException')
}
return this.reporter
}
Expand Down
4 changes: 0 additions & 4 deletions tests/BaseTelemetryReporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,4 @@ test('BaseTelemetryReporter', () => {
expect(reporter.sendTelemetryErrorEvent).toBeCalledTimes(1)
BaseTelemetryReporter.sendDangerousTelemetryErrorEvent('sendDangerousTelemetryErrorEvent')
expect(reporter.sendDangerousTelemetryErrorEvent).toBeCalledTimes(1)
BaseTelemetryReporter.sendTelemetryException(new Error('sendTelemetryException'))
expect(reporter.sendTelemetryException).toBeCalledTimes(1)
BaseTelemetryReporter.sendDangerousTelemetryException(new Error('sendDangerousTelemetryException'))
expect(reporter.sendDangerousTelemetryException).toBeCalledTimes(1)
})
15 changes: 1 addition & 14 deletions tests/extension/TelemetryReporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,7 @@ import { TelemetryReporter } from '../../src/extension/TelemetryReporter'
vi.mock('@vscode/extension-telemetry')

test('TelemetryReporter', () => {
const context: any = {
extension: {
packageJSON: {
name: 'foo',
publisher: 'bar',
version: '1.2.3'
}
}
}
const reporter = TelemetryReporter.configure(context, 'somekey')
// @ts-expect-error mock feature
expect(reporter.extensionId).toBe('bar.foo')
// @ts-expect-error mock feature
expect(reporter.version).toBe('1.2.3')
const reporter = TelemetryReporter.configure('somekey')
// @ts-expect-error mock feature
expect(reporter.key).toBe('somekey')
})
2 changes: 1 addition & 1 deletion tests/extension/TelemetryViewProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ test('TelemetryViewProvider.resolveWebviewView', async () => {
expect(TelemetryReporter.sendTelemetryEvent).toBeCalledTimes(0)

handler({ __telemetryEvent__: { eventType: 'sendTelemetryEvent', eventName: 'foobar' } })
expect(TelemetryReporter.sendTelemetryEvent).toBeCalledWith('foobar', undefined, undefined, undefined)
expect(TelemetryReporter.sendTelemetryEvent).toBeCalledWith('foobar', undefined, undefined)
})
2 changes: 1 addition & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ test('createWebviewTelemetryPanel', () => {
expect(TelemetryReporter.sendTelemetryEvent).toBeCalledTimes(0)

handler({ __telemetryEvent__: { eventType: 'sendTelemetryEvent', eventName: 'foobar' } })
expect(TelemetryReporter.sendTelemetryEvent).toBeCalledWith('foobar', undefined, undefined, undefined)
expect(TelemetryReporter.sendTelemetryEvent).toBeCalledWith('foobar', undefined, undefined)
})
7 changes: 3 additions & 4 deletions tests/webview/TelemetryReporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ vi.mock('vscode')

test('TangleTelemetryReporter', () => {
const reporter = TangleTelemetryReporter.configure(vscode as any as WebviewApi<any>)
reporter.sendDangerousTelemetryEvent('foobar', { some: 'props' }, { mesaurements: 42 }, true)
reporter!.sendTelemetryErrorEvent('foobar', { some: 'props' }, { mesaurements: 42 })
expect((vscode as any as WebviewApi<any>).postMessage).toBeCalledWith({
__telemetryEvent__: {
eventName: 'foobar',
eventType: 'sendDangerousTelemetryEvent',
eventType: 'sendTelemetryErrorEvent',
measurements: { mesaurements: 42 },
properties: { some: 'props' },
sanitize: true
properties: { some: 'props' }
}
})
})

0 comments on commit 1e7c2d1

Please sign in to comment.