From 811975de18b35f05a2d3bfa648a443fc8a10c64a Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Tue, 15 Feb 2022 10:59:48 -0800 Subject: [PATCH] Statsbeat do not count failed request when throttle (#911) * Statsbeat do not count failed request when throttle * Fix responde code for throttling * Add new status code handling --- Library/Sender.ts | 23 +++++++++++++---------- Tests/Library/Sender.tests.ts | 4 ++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Library/Sender.ts b/Library/Sender.ts index 56dd9069d..4714b0ad8 100644 --- a/Library/Sender.ts +++ b/Library/Sender.ts @@ -15,8 +15,9 @@ import Util = require("./Util"); import { URL } from "url"; import Logging = require("./Logging"); import { FileAccessControl } from "./FileAccessControl"; -import { Envelope } from "../Declarations/Contracts"; +const legacyThrottleStatusCode = 439; // - Too many requests and refresh cache +const throttleStatusCode = 402; // Monthly Quota Exceeded (new SDK) class Sender { private static TAG = "Sender"; @@ -185,6 +186,14 @@ class Sender { let endTime = +new Date(); let duration = endTime - startTime; this._numConsecutiveFailures = 0; + if (this._statsbeat) { + if (res.statusCode == throttleStatusCode || res.statusCode == legacyThrottleStatusCode) { // Throttle + this._statsbeat.countThrottle(Constants.StatsbeatNetworkCategory.Breeze, endpointHost); + } + else { + this._statsbeat.countRequest(Constants.StatsbeatNetworkCategory.Breeze, endpointHost, duration, res.statusCode === 200); + } + } if (this._enableDiskRetryMode) { // try to send any cached events if the user is back online if (res.statusCode === 200) { @@ -199,9 +208,6 @@ class Sender { try { if (this._statsbeat) { this._statsbeat.countRetry(Constants.StatsbeatNetworkCategory.Breeze, endpointHost); - if (res.statusCode === 429) { - this._statsbeat.countThrottle(Constants.StatsbeatNetworkCategory.Breeze, endpointHost); - } } const breezeResponse = JSON.parse(responseString) as Contracts.BreezeResponse; let filteredEnvelopes: Contracts.EnvelopeTelemetry[] = []; @@ -247,9 +253,6 @@ class Sender { } else { - if (this._statsbeat) { - this._statsbeat.countRequest(Constants.StatsbeatNetworkCategory.Breeze, endpointHost, duration, res.statusCode === 200); - } this._numConsecutiveRedirects = 0; if (typeof callback === "function") { callback(responseString); @@ -314,13 +317,13 @@ class Sender { private _isRetriable(statusCode: number) { return ( - statusCode === 206 || // Retriable + statusCode === 206 || // Partial Accept statusCode === 401 || // Unauthorized statusCode === 403 || // Forbidden statusCode === 408 || // Timeout - statusCode === 429 || // Throttle + statusCode === 429 || // Too many requests statusCode === 500 || // Server Error - statusCode === 503 // Server Unavilable + statusCode === 503 // Server Unavailable ); } diff --git a/Tests/Library/Sender.tests.ts b/Tests/Library/Sender.tests.ts index 0a48ee3ef..443fbff7d 100644 --- a/Tests/Library/Sender.tests.ts +++ b/Tests/Library/Sender.tests.ts @@ -420,9 +420,9 @@ describe("Library/Sender", () => { statsbeatSender.setDiskRetryMode(true); var statsbeatSpy = sandbox.spy(statsbeat, "countRequest"); var throttleSpy = sandbox.spy(statsbeat, "countThrottle"); - nockScope = interceptor.reply(429, breezeResponse); + nockScope = interceptor.reply(439, breezeResponse); statsbeatSender.send([testEnvelope], () => { - assert.ok(statsbeatSpy.calledOnce); + assert.ok(statsbeatSpy.notCalled); assert.ok(throttleSpy.calledOnce); done(); });