-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created isolated app insights client for tests
- Loading branch information
Showing
8 changed files
with
1,265 additions
and
9 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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,20 +1,43 @@ | ||
import { Context } from 'mocha'; | ||
import { Telemetry } from '../platform/common/constants'; | ||
import { sendTelemetryEvent } from '../telemetry'; | ||
import { AppinsightsKey, JVSC_EXTENSION_ID, Telemetry } from '../platform/common/constants'; | ||
import { IS_CI_SERVER } from './ciConstants.node'; | ||
import { extensions } from 'vscode'; | ||
import TelemetryReporter from './utils/ciTelemetry/node/telemetryReporter.node'; | ||
import { sleep } from './core'; | ||
|
||
|
||
let telemetryReporter: TelemetryReporter | undefined; | ||
|
||
export const rootHooks = { | ||
beforeAll() { | ||
if (!IS_CI_SERVER) { | ||
return; | ||
} | ||
|
||
const extensionVersion = extensions.getExtension(JVSC_EXTENSION_ID)?.packageJSON.version; | ||
telemetryReporter = new TelemetryReporter(JVSC_EXTENSION_ID, extensionVersion, AppinsightsKey, true); | ||
}, | ||
afterEach(this: Context) { | ||
if (!IS_CI_SERVER) { | ||
return; | ||
} | ||
|
||
let result = this.currentTest?.isFailed() ? 'failed' : this.currentTest?.isPassed() ? 'passed' : 'skipped'; | ||
if (this.currentTest?.title) { | ||
sendTelemetryEvent(Telemetry.RunTest, this.currentTest?.duration, { | ||
const duration = this.currentTest?.duration; | ||
const measures = typeof duration === 'number' ? { duration: duration } : duration ? duration : undefined; | ||
telemetryReporter?.sendRawTelemetryEvent(Telemetry.RunTest, { | ||
testName: this.currentTest?.title, | ||
testResult: result | ||
}); | ||
}, measures); | ||
} | ||
}, | ||
afterAll: async function () { | ||
if (!IS_CI_SERVER) { | ||
return; | ||
} | ||
|
||
await telemetryReporter?.dispose(); | ||
await sleep(2000); | ||
} | ||
}; |
100 changes: 100 additions & 0 deletions
100
src/test/utils/ciTelemetry/common/baseTelemetryAppender.ts
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,100 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright (C) Microsoft Corporation. All rights reserved. | ||
*--------------------------------------------------------*/ | ||
|
||
import { AppenderData, ITelemetryAppender } from './baseTelemetryReporter'; | ||
|
||
export interface BaseTelemetryClient { | ||
logEvent(eventName: string, data?: AppenderData): void; | ||
logException(exception: Error, data?: AppenderData): void; | ||
flush(): void | Promise<void>; | ||
} | ||
|
||
export class BaseTelemetryAppender implements ITelemetryAppender { | ||
private _telemetryClient: BaseTelemetryClient | undefined; | ||
private _clientInitialization: Promise<void> | undefined; | ||
|
||
// Queues used to store events until the appender is ready | ||
private _eventQueue: Array<{ eventName: string; data: AppenderData | undefined }> = []; | ||
private _exceptionQueue: Array<{ exception: Error; data: AppenderData | undefined }> = []; | ||
|
||
// Necessary information to create a telemetry client | ||
private _clientFactory: (key: string) => Promise<BaseTelemetryClient>; | ||
private _key: string; | ||
|
||
constructor(key: string, clientFactory: (key: string) => Promise<BaseTelemetryClient>) { | ||
this._clientFactory = clientFactory; | ||
this._key = key; | ||
this.instantiateAppender(); | ||
} | ||
|
||
/** | ||
* Sends the event to the passed in telemetry client | ||
* @param eventName The named of the event to log | ||
* @param data The data contanied in the event | ||
*/ | ||
logEvent(eventName: string, data?: AppenderData): void { | ||
if (this._telemetryClient) { | ||
this._telemetryClient.logEvent(eventName, data); | ||
} | ||
else{ | ||
this._eventQueue.push({ eventName, data }); | ||
} | ||
} | ||
|
||
/** | ||
* Sends an exception to the passed in telemetry client | ||
* @param exception The exception to collect | ||
* @param data Data associated with the exception | ||
*/ | ||
logException(exception: Error, data?: AppenderData): void { | ||
if (this._telemetryClient) { | ||
this._telemetryClient.logException(exception, data); | ||
} | ||
else{ | ||
this._exceptionQueue.push({ exception, data }); | ||
} | ||
} | ||
|
||
/** | ||
* Flushes the buffered telemetry data | ||
*/ | ||
async flush(): Promise<void> { | ||
if (this._clientInitialization) { | ||
await this._clientInitialization; | ||
if (this._telemetryClient) { | ||
await this._telemetryClient.flush(); | ||
this._telemetryClient = undefined; | ||
} | ||
} | ||
return; | ||
} | ||
|
||
/** | ||
* Flushes the queued events that existed before the client was instantiated | ||
*/ | ||
private _flushQueues(): void { | ||
this._eventQueue.forEach(({ eventName, data }) => this.logEvent(eventName, data)); | ||
this._eventQueue = []; | ||
this._exceptionQueue.forEach(({ exception, data }) => this.logException(exception, data)); | ||
this._exceptionQueue = []; | ||
} | ||
|
||
/** | ||
* Instantiates the telemetry client to make the appender "active" | ||
*/ | ||
instantiateAppender(): void { | ||
if (this._clientInitialization) { | ||
return; | ||
} | ||
// Call the client factory to get the client and then let it know it's instatntiated | ||
this._clientInitialization = this._clientFactory(this._key) | ||
.then((client) => { | ||
this._telemetryClient = client; | ||
this._flushQueues(); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
}); | ||
} | ||
} |
Oops, something went wrong.