diff --git a/AutoCollection/Statsbeat.ts b/AutoCollection/Statsbeat.ts index 05db7ce37..52fff1f39 100644 --- a/AutoCollection/Statsbeat.ts +++ b/AutoCollection/Statsbeat.ts @@ -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 { diff --git a/Library/Sender.ts b/Library/Sender.ts index d96821ca3..4649c7406 100644 --- a/Library/Sender.ts +++ b/Library/Sender.ts @@ -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"; @@ -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)) { diff --git a/Tests/Library/Sender.tests.ts b/Tests/Library/Sender.tests.ts index 1a467d79d..337adda16 100644 --- a/Tests/Library/Sender.tests.ts +++ b/Tests/Library/Sender.tests.ts @@ -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) => { @@ -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");