Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statsbeat do not count failed request when throttle #911

Merged
merged 5 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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