Skip to content

Commit

Permalink
Statsbeat do not count failed request when throttle (#911)
Browse files Browse the repository at this point in the history
* Statsbeat do not count failed request when throttle

* Fix responde code for throttling

* Add new status code handling
  • Loading branch information
hectorhdzg authored Feb 15, 2022
1 parent 48f1281 commit 811975d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 13 additions & 10 deletions Library/Sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand All @@ -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[] = [];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
);
}

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

0 comments on commit 811975d

Please sign in to comment.