-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ken Howard
committed
Nov 21, 2018
1 parent
3c9c0d7
commit 9b91159
Showing
10 changed files
with
208 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// tslint:disable:no-any no-unsafe-any | ||
const mockedImplementation = { | ||
sendTelemetryEvent: jest.fn() | ||
}; | ||
|
||
// tslint:disable-next-line:no-default-export | ||
export default jest.fn(() => mockedImplementation); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
// tslint:disable:no-magic-numbers | ||
import { Buffer } from 'buffer'; | ||
|
||
export const DEBUG = process.env.DEBUG === 'true'; | ||
export const GISTS_PER_PAGE = 9999; | ||
export const LOGGER_LEVEL = 3; | ||
export const TELEMETRY_COHORT_RANGE = [0, 10]; | ||
export const TELEMETRY_COHORT_RANGE = [0, 25]; | ||
export const TELEMETRY_WRITE_KEY = Buffer.from( | ||
'OTgzNDBjN2UtOTExOS00OGM3LWI2OWMtOTY3NTE3MTZiOTg4', | ||
'base64' | ||
).toString(); | ||
export const TMP_DIRECTORY_PREFIX = 'vscode_gist'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// tslint:disable:no-any no-unsafe-any no-require-imports ordered-imports | ||
import TelemetryReporter from 'vscode-extension-telemetry'; | ||
|
||
jest.genMockFromModule<TelemetryReporter>('vscode-extension-telemetry'); | ||
jest.mock('vscode-extension-telemetry'); | ||
|
||
const sendTelemetryEvent = jest.fn(); | ||
|
||
(TelemetryReporter as any).mockImplementation( | ||
(): any => ({ sendTelemetryEvent }) | ||
); | ||
|
||
import { telemetry } from '../telemetry-service'; | ||
|
||
describe('Telemetry Service Tests', () => { | ||
beforeEach(() => { | ||
(telemetry as any).enabled = true; | ||
}); | ||
afterEach(() => { | ||
(telemetry as any).enabled = true; | ||
jest.resetAllMocks(); | ||
}); | ||
describe('#exception', () => { | ||
test('should send telemetry event to app insights', () => { | ||
telemetry.exception('telemetry-test', { message: 'testing' }); | ||
expect(sendTelemetryEvent).toHaveBeenCalledWith( | ||
'exception', | ||
{ context: 'telemetry-test', message: 'testing' }, | ||
{} | ||
); | ||
}); | ||
}); | ||
describe('#track', () => { | ||
test('should send telemetry event to app insights', () => { | ||
telemetry.track('telemetry-test', { message: 'testing' }); | ||
expect(sendTelemetryEvent).toHaveBeenCalledWith( | ||
'telemetry-test', | ||
{ message: 'testing' }, | ||
{} | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { telemetry as insights } from './telemetry-service'; | ||
|
||
export { insights }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { extensions } from 'vscode'; | ||
import TelemetryReporter from 'vscode-extension-telemetry'; | ||
|
||
import { | ||
DEBUG, | ||
TELEMETRY_COHORT_RANGE, | ||
TELEMETRY_WRITE_KEY | ||
} from '../constants'; | ||
|
||
interface Extension { | ||
packageJSON: { version: string }; | ||
} | ||
|
||
const extensionId = 'kenhowardpdx.vscode-gist'; | ||
const extension = extensions.getExtension(extensionId) as Extension; | ||
const extensionVersion = | ||
(extension && extension.packageJSON && extension.packageJSON.version) || ''; | ||
|
||
const telemetryCohortMax = 100; | ||
const telemetryCohortMin = 1; | ||
const telemetryCohort = | ||
Math.floor(Math.random() * telemetryCohortMax) + telemetryCohortMin; | ||
|
||
const enabled = ((): boolean => { | ||
if (DEBUG) { | ||
return true; | ||
} else if ( | ||
telemetryCohort >= TELEMETRY_COHORT_RANGE[0] && | ||
telemetryCohort <= TELEMETRY_COHORT_RANGE[1] | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
})(); | ||
|
||
class TelemetryService { | ||
public static getInstance = (): TelemetryService => | ||
// TODO: permanently disable the semicolon rule | ||
// tslint:disable-next-line:semicolon | ||
TelemetryService.instance | ||
? TelemetryService.instance | ||
: new TelemetryService() | ||
|
||
private static readonly instance?: TelemetryService; | ||
|
||
private readonly enabled = enabled; | ||
private reporter: TelemetryReporter; | ||
|
||
private constructor() { | ||
this.reporter = new TelemetryReporter( | ||
extensionId, | ||
extensionVersion, | ||
TELEMETRY_WRITE_KEY | ||
); | ||
} | ||
|
||
public configure(id: string, version: string, key: string): void { | ||
this.reporter = new TelemetryReporter(id, version, key); | ||
} | ||
|
||
public exception( | ||
context: string, | ||
properties?: { [x: string]: string }, | ||
measurements?: { [x: string]: number } | ||
): void { | ||
this.track('exception', { ...properties, context }, { ...measurements }); | ||
} | ||
|
||
public track( | ||
eventName: string, | ||
properties?: { [x: string]: string }, | ||
measurements?: { [x: string]: number } | ||
): void { | ||
if (!this.enabled) { | ||
return; | ||
} | ||
|
||
this.reporter.sendTelemetryEvent( | ||
eventName, | ||
{ ...properties }, | ||
{ ...measurements } | ||
); | ||
} | ||
} | ||
|
||
export const telemetry = TelemetryService.getInstance(); |