Skip to content

Commit

Permalink
Shorten host property in network statsbeat (#918)
Browse files Browse the repository at this point in the history
* Shorten host property in network statsbeat

* Addressing comments
  • Loading branch information
hectorhdzg authored Feb 28, 2022
1 parent a594935 commit e8da01c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
20 changes: 18 additions & 2 deletions AutoCollection/Statsbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,17 @@ class Statsbeat {
}

private _getNetworkStatsbeatCounter(endpoint: number, host: string): Network.NetworkStatsbeat {
let shortHost = this._getShortHost(host);
// Check if counter is available
for (let i = 0; i < this._networkStatsbeatCollection.length; i++) {
// Same object
if (endpoint === this._networkStatsbeatCollection[i].endpoint &&
host === this._networkStatsbeatCollection[i].host) {
shortHost === this._networkStatsbeatCollection[i].host) {
return this._networkStatsbeatCollection[i];
}
}
// Create a new one if not found
let newCounter = new Network.NetworkStatsbeat(endpoint, host);
let newCounter = new Network.NetworkStatsbeat(endpoint, shortHost);
this._networkStatsbeatCollection.push(newCounter);
return newCounter;
}
Expand All @@ -242,6 +243,21 @@ class Statsbeat {
}
}

private _getShortHost(originalHost: string) {
let shortHost = originalHost;
try {
let hostRegex = new RegExp(/^https?:\/\/(?:www\.)?([^\/.-]+)/);
let res = hostRegex.exec(originalHost);
if (res != null && res.length > 1) {
shortHost = res[1];
}
}
catch (error) {
// Ignore error
}
return shortHost;
}

private _trackRequestsCount(commonProperties: {}) {
for (let i = 0; i < this._networkStatsbeatCollection.length; i++) {
var currentCounter = this._networkStatsbeatCollection[i];
Expand Down
15 changes: 11 additions & 4 deletions Tests/AutoCollection/Statsbeat.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,20 @@ describe("AutoCollection/Statsbeat", () => {
it("init should use non EU connection string", () => {
let testConfig = new Config("InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333;IngestionEndpoint=https://westus-0.in.applicationinsights.azure.com/");
let statsbeat = new Statsbeat(testConfig);
assert.equal(statsbeat["_statsbeatConfig"].endpointUrl, "https://westus-0.in.applicationinsights.azure.com/v2.1/track")
assert.equal(statsbeat["_statsbeatConfig"].endpointUrl, "https://westus-0.in.applicationinsights.azure.com/v2.1/track");
});

it("init should use EU connection string", () => {
let testConfig = new Config("InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/");
let statsbeat = new Statsbeat(testConfig);
assert.equal(statsbeat["_statsbeatConfig"].endpointUrl, "https://westeurope-5.in.applicationinsights.azure.com/v2.1/track")
assert.equal(statsbeat["_statsbeatConfig"].endpointUrl, "https://westeurope-5.in.applicationinsights.azure.com/v2.1/track");
});

it("_getShortHost", () => {
assert.equal(statsBeat["_getShortHost"]("http://westus02-1.in.applicationinsights.azure.com"), "westus02");
assert.equal(statsBeat["_getShortHost"]("https://westus02-1.in.applicationinsights.azure.com"), "westus02");
assert.equal(statsBeat["_getShortHost"]("https://dc.services.visualstudio.com"), "dc");
assert.equal(statsBeat["_getShortHost"]("https://www.test.com"), "test");
});
});

Expand Down Expand Up @@ -288,8 +295,8 @@ describe("AutoCollection/Statsbeat", () => {
it("Multiple network categories and endpoints", (done) => {
statsBeat.enable(true);
const sendStub = sandbox.stub(statsBeat, "_sendStatsbeats");
statsBeat.countRequest(0, "breezeFirstEndpoint", 100, true);
statsBeat.countRequest(1, "quickpulseEndpoint", 200, true);
statsBeat.countRequest(0, "https://breezeFirstEndpoint.something.com", 100, true);
statsBeat.countRequest(1, "http://quickpulseEndpoint.something.com", 200, true);
statsBeat.countRequest(0, "breezeSecondEndpoint", 400, true);
statsBeat.trackShortIntervalStatsbeats().then(() => {
assert.ok(sendStub.called, "should call _sendStatsbeats");
Expand Down

0 comments on commit e8da01c

Please sign in to comment.