Skip to content

Commit

Permalink
Add check for invalid iKey and tests. (#1243)
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonWeber authored Nov 15, 2023
1 parent 0217324 commit 2b142c8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion AutoCollection/Statsbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ class Statsbeat {
}

private _shutdownStatsbeat() {
this.enable(false);// Disable Statsbeat as is it failed 3 times cosnecutively during initialization, is possible SDK is running in private or restricted network
this.enable(false);// Disable Statsbeat as is it failed 3 times consecutively during initialization, is possible SDK is running in private or restricted network
}

private _getConnectionString(config: Config): string {
Expand Down
5 changes: 5 additions & 0 deletions Library/Sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { FileAccessControl } from "./FileAccessControl";
const legacyThrottleStatusCode = 439; // - Too many requests and refresh cache
const throttleStatusCode = 402; // Monthly Quota Exceeded (new SDK)
const RESPONSE_CODES_INDICATING_REACHED_BREEZE = [200, 206, 402, 408, 429, 439, 500];
const INVALID_IKEY = "Invalid instrumentation key";

class Sender {
private static TAG = "Sender";
Expand Down Expand Up @@ -197,6 +198,10 @@ class Sender {
let endTime = +new Date();
let duration = endTime - startTime;
this._numConsecutiveFailures = 0;
if (responseString.includes(INVALID_IKEY) && res.statusCode === 400) {
Logging.warn("Instrumentation key was invalid, please check the iKey");
this._shutdownStatsbeat();
}
// Handling of Statsbeat instance sending data, should turn it off if is not able to reach ingestion endpoint
if (this._isStatsbeatSender && !this._statsbeatHasReachedIngestionAtLeastOnce) {
if (RESPONSE_CODES_INDICATING_REACHED_BREEZE.includes(res.statusCode)) {
Expand Down
24 changes: 23 additions & 1 deletion Tests/Library/Sender.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,23 @@ describe("Library/Sender", () => {
errors: []
};

const invalidIKeyResponse: Contracts.BreezeResponse = {
itemsAccepted: 0,
itemsReceived: 1,
errors: [{
index: 0,
statusCode: 400,
message: "Invalid instrumentation key"
}]
}

let config = new Config("2bb22222-bbbb-1ccc-8ddd-eeeeffff3333");
let statsbeat = new Statsbeat(config);
let statsbeatSender = new Sender(config, null, null, null, statsbeat);
let shutdownCalled = false;
let shutdown = () => {
shutdownCalled = true;
};
let statsbeatSender = new Sender(config, null, null, null, statsbeat, false, shutdown);
let statsbeatError: Error = {name: "Statsbeat", message: "Statsbeat error" };

it("Succesful requests", (done) => {
Expand Down Expand Up @@ -480,6 +494,14 @@ describe("Library/Sender", () => {
});
});

it("Statsbeat should shutdown upon invalid iKey", (done) => {
nockScope = interceptor.reply(400, invalidIKeyResponse);
statsbeatSender.send([testEnvelope], () => {
assert.strictEqual(shutdownCalled, true);
done();
});
});

it("Retry counts", (done) => {
statsbeatSender.setDiskRetryMode(true);
var statsbeatSpy = sandbox.spy(statsbeat, "countRequest");
Expand Down

0 comments on commit 2b142c8

Please sign in to comment.