Skip to content

Commit

Permalink
Merge pull request #97 from Microsoft/OperationName
Browse files Browse the repository at this point in the history
Log Operation name
  • Loading branch information
Kamil Szostak authored Jun 24, 2016
2 parents 3616101 + f4484ca commit bc303da
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
9 changes: 8 additions & 1 deletion AutoCollection/RequestDataHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class RequestDataHelper {
newTags[RequestDataHelper.keys.sessionId] = this._getId("ai_session");
newTags[RequestDataHelper.keys.userId] = this._getId("ai_user");
newTags[RequestDataHelper.keys.userAgent] = this.userAgent;
newTags[RequestDataHelper.keys.operationName] = this.method + " " + url.parse(this.url).pathname;

return newTags;
}

Expand All @@ -97,11 +99,16 @@ class RequestDataHelper {
}

var encrypted = <any>request.connection ? (<any>request.connection).encrypted : null;
var requestUrl = url.parse(request.url);

var pathName = requestUrl.pathname;
var search = requestUrl.search;

var absoluteUrl = url.format({
protocol: encrypted ? "https" : "http",
host: request.headers.host,
pathname: request.url
pathname: pathName,
search: search
});

return absoluteUrl;
Expand Down
57 changes: 54 additions & 3 deletions Tests/AutoCollection/RequestDataHelper.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("AutoCollection/RequestDataHelper", () => {

describe("#getRequestData()", () => {
var request = {
method: "method",
method: "GET",
url: "/search?q=test",
connection: {
encrypted: false
Expand All @@ -33,15 +33,66 @@ describe("AutoCollection/RequestDataHelper", () => {
it("should return an absolute url", () => {
var helper = new RequestDataHelper(<any>request);
var requestData = helper.getRequestData();
assert.equal(requestData.baseData.url, "http://bing.com/search%3Fq=test");
assert.equal(requestData.baseData.url, "http://bing.com/search?q=test");
});

it("should return an absolute url for encrypted traffic", () => {
request.connection.encrypted = true;

var helper = new RequestDataHelper(<any>request);
var requestData = helper.getRequestData();
assert.equal(requestData.baseData.url, "https://bing.com/search%3Fq=test");
assert.equal(requestData.baseData.url, "https://bing.com/search?q=test");
});

var requestComplex = {
method: "GET",
url: "/a/b/c/?q=test&test2",
connection: {
encrypted: false
},
headers: {
host: "bing.com"
}
}

it("should return an absolute url for complex urls", () => {
var helper = new RequestDataHelper(<any>requestComplex);
var requestData = helper.getRequestData();
assert.equal(requestData.baseData.url, "http://bing.com/a/b/c/?q=test&test2");
});

var requestNoSearchParam = {
method: "method",
url: "/a/",
connection: {
encrypted: false
},
headers: {
host: "bing.com"
}
}

it("should return an absolute url when url does not have search part", () => {
var helper = new RequestDataHelper(<any>requestNoSearchParam);
var requestData = helper.getRequestData();
assert.equal(requestData.baseData.url, "http://bing.com/a/");
});

var requestNoPathName = {
method: "method",
url: "/",
connection: {
encrypted: false
},
headers: {
host: "bing.com"
}
}

it("should return an absolute url when url does not have path name", () => {
var helper = new RequestDataHelper(<any>requestNoPathName);
var requestData = helper.getRequestData();
assert.equal(requestData.baseData.url, "http://bing.com/");
});
});
});
8 changes: 5 additions & 3 deletions Tests/Library/Client.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ describe("Library/Client", () => {
this.errorCallback = callback;
}
},
method: "method",
method: "GET",
url: "/search?q=test",
connection: {
encrypted: false
},
headers: {
host: "http://bing.com"
host: "bing.com"
}
};

Expand Down Expand Up @@ -247,7 +247,7 @@ describe("Library/Client", () => {
assert.equal(duration, 10);
});

it('should track request with correct tags on response finish event', function () {
it('should track request with correct tags on response finish event', () => {
trackStub.reset();
clock.reset();
client.trackRequest(<any>request, <any>response, properties);
Expand All @@ -258,6 +258,8 @@ describe("Library/Client", () => {
// validate
var args = trackStub.args;
var tags = args[0][1];

assert.equal(tags["ai.operation.name"], "GET /search");
assert.equal(tags["ai.device.id"], "node");
assert.equal(tags["ai.device.type"], null);
});
Expand Down

0 comments on commit bc303da

Please sign in to comment.