diff --git a/.vscode/settings.json b/.vscode/settings.json index c22468949..12e68f782 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,7 +2,8 @@ { "files.exclude": { "**/*.js": { "when": "$(basename).ts"}, - "**/*.js.map": true + "**/*.js.map": true, + "**/*.d.ts": true }, "typescript.tsdk": "./node_modules/typescript/lib" } \ No newline at end of file diff --git a/AutoCollection/ClientRequests.ts b/AutoCollection/ClientRequests.ts index 4a2dc5faa..5895fc5fe 100644 --- a/AutoCollection/ClientRequests.ts +++ b/AutoCollection/ClientRequests.ts @@ -98,11 +98,13 @@ class AutoCollectClientRequests { if (request.on) { request.on('response', (response: http.ClientResponse) => { requestParser.onResponse(response, properties); - client.track(requestParser.getDependencyData()); + var context : { [name: string]: any; } = { "http.RequestOptions": requestOptions, "http.ClientRequest": request, "http.ClientResponse": response }; + client.track(requestParser.getDependencyData(), null, context); }); request.on('error', (e: Error) => { requestParser.onError(e, properties); - client.track(requestParser.getDependencyData()); + var context : { [name: string]: any; } = { "http.RequestOptions": requestOptions, "http.ClientRequest": request, "Error": e }; + client.track(requestParser.getDependencyData(), null, context); }); } } diff --git a/AutoCollection/ServerRequests.ts b/AutoCollection/ServerRequests.ts index 8a96bac45..d927a87b1 100644 --- a/AutoCollection/ServerRequests.ts +++ b/AutoCollection/ServerRequests.ts @@ -84,7 +84,7 @@ class AutoCollectServerRequests { // store data about the request var requestParser = new ServerRequestParser(request); - AutoCollectServerRequests.endRequest(client, requestParser, response, ellapsedMilliseconds, properties, error); + AutoCollectServerRequests.endRequest(client, requestParser, request, response, ellapsedMilliseconds, properties, error); } /** @@ -106,14 +106,14 @@ class AutoCollectServerRequests { // response listeners if (response.once) { response.once("finish", () => { - AutoCollectServerRequests.endRequest(client, requestParser, response, null, properties, null); + AutoCollectServerRequests.endRequest(client, requestParser, request, response, null, properties, null); }); } // track a failed request if an error is emitted if (request.on) { request.on("error", (error:any) => { - AutoCollectServerRequests.endRequest(client, requestParser, response, null, properties, error); + AutoCollectServerRequests.endRequest(client, requestParser, request, response, null, properties, error); }); } } @@ -131,7 +131,7 @@ class AutoCollectServerRequests { } } - private static endRequest(client: Client, requestParser: ServerRequestParser, response: http.ServerResponse, ellapsedMilliseconds?: number, properties?: { [key: string]: string}, error?: any) { + private static endRequest(client: Client, requestParser: ServerRequestParser, request: http.ServerRequest, response: http.ServerResponse, ellapsedMilliseconds?: number, properties?: { [key: string]: string}, error?: any) { if (error) { requestParser.onError(error, properties, ellapsedMilliseconds); } else { @@ -140,7 +140,8 @@ class AutoCollectServerRequests { var data = requestParser.getRequestData(); var tags = requestParser.getRequestTags(client.context.tags); - client.track(data, tags); + var context : { [name: string]: any; } = {"http.ServerRequest": request, "http.ServerResponse": response}; + client.track(data, tags, context); } public dispose() { diff --git a/Library/Client.ts b/Library/Client.ts index d26a21a46..557cfd23c 100644 --- a/Library/Client.ts +++ b/Library/Client.ts @@ -235,10 +235,11 @@ class Client { */ public track( data: ContractsModule.Contracts.Data, - tagOverrides?: { [key: string]: string; }) { + tagOverrides?: { [key: string]: string; }, + contextObjects?: { [name: string]: any; }) { var envelope = this.getEnvelope(data, tagOverrides); - var accepted = this.runTelemetryProcessors(envelope); + var accepted = this.runTelemetryProcessors(envelope, contextObjects); if (accepted) { this.channel.send(envelope); @@ -249,9 +250,9 @@ class Client { * Adds telemetry processor to the collection. Telemetry processors will be called one by one * before telemetry item is pushed for sending and in the order they were added. * - * @param telemetryProcessor function, takes Envelope, returns boolean + * @param telemetryProcessor function, takes Envelope, and optional context object and returns boolean */ - public addTelemetryProcessor(telemetryProcessor: (envelope: ContractsModule.Contracts.Envelope) => boolean) { + public addTelemetryProcessor(telemetryProcessor: (envelope: ContractsModule.Contracts.Envelope, contextObjects?: { [name: string]: any; }) => boolean) { this._telemetryProcessors.push(telemetryProcessor); } @@ -270,7 +271,7 @@ class Client { return [array[0], parseInt(array[1])]; } - private runTelemetryProcessors(envelope: ContractsModule.Contracts.Envelope): boolean { + private runTelemetryProcessors(envelope: ContractsModule.Contracts.Envelope, contextObjects: { [name: string]: any; }): boolean { var accepted = true; var telemetryProcessorsCount = this._telemetryProcessors.length; @@ -283,7 +284,7 @@ class Client { try { var processor = this._telemetryProcessors[i]; if (processor) { - if (processor.apply(null, [envelope]) === false) { + if (processor.apply(null, [envelope, contextObjects]) === false) { accepted = false; break; } diff --git a/Tests/Library/Client.tests.ts b/Tests/Library/Client.tests.ts index 8cb84a9b3..9a6c13a49 100644 --- a/Tests/Library/Client.tests.ts +++ b/Tests/Library/Client.tests.ts @@ -713,6 +713,23 @@ describe("Library/Client", () => { assert.equal(actualData.name, expectedName, "envelope name should be changed by the processor"); }); + it("telemetry processor can access the context object", () => { + trackStub.restore(); + var expectedName = "I was here"; + + client.addTelemetryProcessor((env, contextObjects) => { + env.name = contextObjects["name"]; + return true; + }); + + client.track(mockData, null, {"name": expectedName}); + + assert.equal(sendStub.callCount, 1, "send called once"); + + var actualData = sendStub.firstCall.args[0] as ContractsModule.Contracts.Envelope; + assert.equal(actualData.name, expectedName, "envelope name should be changed by the processor"); + }); + it("telemetry processors are executed in a right order", () => { trackStub.restore();