Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend telemetry client to pass the context #165

Merged
merged 5 commits into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
6 changes: 4 additions & 2 deletions AutoCollection/ClientRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
Expand Down
11 changes: 6 additions & 5 deletions AutoCollection/ServerRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
});
}
}
Expand All @@ -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 {
Expand All @@ -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() {
Expand Down
13 changes: 7 additions & 6 deletions Library/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,11 @@ class Client {
*/
public track(
data: ContractsModule.Contracts.Data<ContractsModule.Contracts.Domain>,
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);
Expand All @@ -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);
}

Expand All @@ -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;

Expand All @@ -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;
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/Library/Client.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down