diff --git a/AutoCollection/PreAggregatedMetrics.ts b/AutoCollection/PreAggregatedMetrics.ts index fde91477..3f96d472 100644 --- a/AutoCollection/PreAggregatedMetrics.ts +++ b/AutoCollection/PreAggregatedMetrics.ts @@ -172,17 +172,15 @@ class AutoCollectPreAggregatedMetrics { var elapsedMs = currentCounter.time - currentCounter.lastTime; var averageRequestExecutionTime = ((currentCounter.intervalExecutionTime - currentCounter.lastIntervalExecutionTime) / intervalRequests) || 0; currentCounter.lastIntervalExecutionTime = currentCounter.intervalExecutionTime; // reset - if (elapsedMs > 0) { - if (intervalRequests > 0) { - this._trackPreAggregatedMetric({ - name: "Server response time", - dimensions: currentCounter.dimensions, - value: averageRequestExecutionTime, - count: intervalRequests, - aggregationInterval: elapsedMs, - metricType: Constants.MetricId.REQUESTS_DURATION, - }); - } + if (elapsedMs > 0 && intervalRequests > 0) { + this._trackPreAggregatedMetric({ + name: "Server response time", + dimensions: currentCounter.dimensions, + value: averageRequestExecutionTime, + count: intervalRequests, + aggregationInterval: elapsedMs, + metricType: Constants.MetricId.REQUESTS_DURATION, + }); } // Set last counters currentCounter.lastTotalCount = currentCounter.totalCount; @@ -198,17 +196,15 @@ class AutoCollectPreAggregatedMetrics { var elapsedMs = currentCounter.time - currentCounter.lastTime; var averageDependencyExecutionTime = ((currentCounter.intervalExecutionTime - currentCounter.lastIntervalExecutionTime) / intervalDependencies) || 0; currentCounter.lastIntervalExecutionTime = currentCounter.intervalExecutionTime; // reset - if (elapsedMs > 0) { - if (intervalDependencies > 0) { - this._trackPreAggregatedMetric({ - name: "Dependency duration", - dimensions: currentCounter.dimensions, - value: averageDependencyExecutionTime, - count: intervalDependencies, - aggregationInterval: elapsedMs, - metricType: Constants.MetricId.DEPENDENCIES_DURATION, - }); - } + if (elapsedMs > 0 && intervalDependencies > 0) { + this._trackPreAggregatedMetric({ + name: "Dependency duration", + dimensions: currentCounter.dimensions, + value: averageDependencyExecutionTime, + count: intervalDependencies, + aggregationInterval: elapsedMs, + metricType: Constants.MetricId.DEPENDENCIES_DURATION, + }); } // Set last counters currentCounter.lastTotalCount = currentCounter.totalCount; @@ -219,17 +215,19 @@ class AutoCollectPreAggregatedMetrics { private _trackExceptionMetrics() { for (let i = 0; i < AutoCollectPreAggregatedMetrics._exceptionCountersCollection.length; i++) { var currentCounter = AutoCollectPreAggregatedMetrics._exceptionCountersCollection[i]; + currentCounter.time = +new Date; var intervalExceptions = (currentCounter.totalCount - currentCounter.lastTotalCount) || 0; var elapsedMs = currentCounter.time - currentCounter.lastTime; - this._trackPreAggregatedMetric({ - name: "Exceptions", - dimensions: currentCounter.dimensions, - value: intervalExceptions, - count: intervalExceptions, - aggregationInterval: elapsedMs, - metricType: Constants.MetricId.EXCEPTIONS_COUNT, - }); - + if (elapsedMs > 0 && intervalExceptions > 0) { + this._trackPreAggregatedMetric({ + name: "Exceptions", + dimensions: currentCounter.dimensions, + value: intervalExceptions, + count: intervalExceptions, + aggregationInterval: elapsedMs, + metricType: Constants.MetricId.EXCEPTIONS_COUNT, + }); + } // Set last counters currentCounter.lastTotalCount = currentCounter.totalCount; currentCounter.lastTime = currentCounter.time; @@ -239,17 +237,19 @@ class AutoCollectPreAggregatedMetrics { private _trackTraceMetrics() { for (let i = 0; i < AutoCollectPreAggregatedMetrics._traceCountersCollection.length; i++) { var currentCounter = AutoCollectPreAggregatedMetrics._traceCountersCollection[i]; + currentCounter.time = +new Date; var intervalTraces = (currentCounter.totalCount - currentCounter.lastTotalCount) || 0; var elapsedMs = currentCounter.time - currentCounter.lastTime; - this._trackPreAggregatedMetric({ - name: "Traces", - dimensions: currentCounter.dimensions, - value: intervalTraces, - count: intervalTraces, - aggregationInterval: elapsedMs, - metricType: Constants.MetricId.TRACES_COUNT, - }); - + if (elapsedMs > 0 && intervalTraces > 0) { + this._trackPreAggregatedMetric({ + name: "Traces", + dimensions: currentCounter.dimensions, + value: intervalTraces, + count: intervalTraces, + aggregationInterval: elapsedMs, + metricType: Constants.MetricId.TRACES_COUNT, + }); + } // Set last counters currentCounter.lastTotalCount = currentCounter.totalCount; currentCounter.lastTime = currentCounter.time; diff --git a/Tests/AutoCollection/Statsbeat.tests.ts b/Tests/AutoCollection/Statsbeat.tests.ts index b7162922..0a24879e 100644 --- a/Tests/AutoCollection/Statsbeat.tests.ts +++ b/Tests/AutoCollection/Statsbeat.tests.ts @@ -111,8 +111,7 @@ describe("AutoCollection/Statsbeat", () => { const spy = sandbox.spy(statsBeat["_sender"], "send"); statsBeat.countRequest(123, "testEndpointHost", 123, true); statsBeat.setCodelessAttach(); - statsBeat.trackShortIntervalStatsbeats(); - setTimeout(() => { + statsBeat.trackShortIntervalStatsbeats().then(() => { assert.equal(spy.callCount, 2, "should call sender"); let envelope = spy.args[1][0][0]; assert.equal(envelope.name, "Statsbeat"); @@ -130,7 +129,7 @@ describe("AutoCollection/Statsbeat", () => { assert.ok(baseData.properties["version"]); statsBeat.enable(false); done(); - }, 10); + }); }); it("Track duration", (done) => { @@ -139,8 +138,7 @@ describe("AutoCollection/Statsbeat", () => { const spy = sandbox.spy(statsBeat["_sender"], "send"); statsBeat.countRequest(0, "test", 1000, true); statsBeat.countRequest(0, "test", 500, false); - statsBeat.trackShortIntervalStatsbeats(); - setImmediate(() => { + statsBeat.trackShortIntervalStatsbeats().then((error) => { assert.equal(spy.callCount, 2, "should call sender"); let envelope = spy.args[1][0][0]; let baseData: Contracts.MetricData = envelope.data.baseData; @@ -166,8 +164,7 @@ describe("AutoCollection/Statsbeat", () => { statsBeat.countRetry(0, "test"); statsBeat.countThrottle(0, "test"); statsBeat.countException(0, "test"); - statsBeat.trackShortIntervalStatsbeats(); - setImmediate(() => { + statsBeat.trackShortIntervalStatsbeats().then(() => { assert.equal(spy.callCount, 2, "should call sender"); let envelope = spy.args[1][0][1]; let baseData: Contracts.MetricData = envelope.data.baseData; @@ -198,7 +195,7 @@ describe("AutoCollection/Statsbeat", () => { const statsBeat: Statsbeat = new Statsbeat(config); statsBeat.enable(true); const spy = sandbox.spy(statsBeat["_sender"], "send"); - setTimeout(() => { + setImmediate(() => { let envelope = spy.args[0][0][0]; let baseData: Contracts.MetricData = envelope.data.baseData; assert.equal(baseData.metrics[0].name, "Attach"); @@ -213,7 +210,7 @@ describe("AutoCollection/Statsbeat", () => { assert.ok(baseData.properties["version"]); statsBeat.enable(false); done(); - }, 10) + }) }); it("Track feature Statbeat", (done) => { @@ -221,7 +218,7 @@ describe("AutoCollection/Statsbeat", () => { statsBeat.enable(true); statsBeat.addFeature(Constants.StatsbeatFeature.DISK_RETRY); const spy = sandbox.spy(statsBeat["_sender"], "send"); - setTimeout(() => { + setImmediate(() => { let envelope = spy.args[0][0][2]; let baseData: Contracts.MetricData = envelope.data.baseData; assert.equal(baseData.metrics[0].name, "Feature"); @@ -237,7 +234,7 @@ describe("AutoCollection/Statsbeat", () => { assert.ok(baseData.properties["version"]); statsBeat.enable(false); done(); - }, 10) + }) }); it("Track instrumentation Statbeat", (done) => { @@ -245,7 +242,7 @@ describe("AutoCollection/Statsbeat", () => { statsBeat.enable(true); statsBeat.addInstrumentation(Constants.StatsbeatInstrumentation.AZURE_CORE_TRACING); const spy = sandbox.spy(statsBeat["_sender"], "send"); - setTimeout(() => { + setImmediate(() => { let envelope = spy.args[0][0][1]; let baseData: Contracts.MetricData = envelope.data.baseData; assert.equal(baseData.metrics[0].name, "Feature"); @@ -261,7 +258,7 @@ describe("AutoCollection/Statsbeat", () => { assert.ok(baseData.properties["version"]); statsBeat.enable(false); done(); - }, 10) + }) }); it("Instrumentations", () => { @@ -295,8 +292,7 @@ describe("AutoCollection/Statsbeat", () => { statsBeat.countRequest(0, "breezeFirstEndpoint", 100, true); statsBeat.countRequest(1, "quickpulseEndpoint", 200, true); statsBeat.countRequest(0, "breezeSecondEndpoint", 400, true); - statsBeat.trackShortIntervalStatsbeats(); - setTimeout(() => { + statsBeat.trackShortIntervalStatsbeats().then(() => { assert.equal(spy.callCount, 2, "should call sender"); let envelope = spy.args[1][0][0]; let baseData: Contracts.MetricData = envelope.data.baseData; @@ -318,7 +314,7 @@ describe("AutoCollection/Statsbeat", () => { assert.equal(baseData.properties["host"], "breezeSecondEndpoint"); statsBeat.enable(false); done(); - }, 100); + }); }); }); }); \ No newline at end of file