-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support sending metrics before handler initialization (#524)
* add `warning` level to logger * export `logWarning` from `utils` * add `queue.ts` for `MetricsQueue` * export `MetricsQueue` from `metrics` * use `MetricsQueue` for global metrics queue for later processing when using on handler not initialized * remove unused import * format and lint * remove unused import * format and lint * add entry to integration tets * fix `return` to `continue` * update `shift` to `pop` reducing time complexity to `O(2n)`
- Loading branch information
1 parent
dadad52
commit 6810d0c
Showing
12 changed files
with
269 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { MetricsConfig, MetricsListener } from "./listener"; | ||
export { MetricsQueue } from "./queue"; | ||
export { KMSService } from "./kms-service"; | ||
export { incrementErrorsMetric, incrementInvocationsMetric } from "./enhanced-metrics"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { LogLevel, logDebug, setLogLevel, setLogger } from "../utils"; | ||
import { METRICS_QUEUE_LIMIT, MetricsQueue } from "./queue"; | ||
|
||
describe("MetricsQueue", () => { | ||
const logger = { | ||
debug: jest.fn(), | ||
error: jest.fn(), | ||
warn: jest.fn(), | ||
}; | ||
describe("push", () => { | ||
beforeEach(() => { | ||
setLogLevel(LogLevel.NONE); | ||
setLogger(logger); | ||
}); | ||
it("resets metrics queue when its full", () => { | ||
setLogLevel(LogLevel.WARNING); | ||
const queue = new MetricsQueue(); | ||
for (let i = 0; i < METRICS_QUEUE_LIMIT + 1; i++) { | ||
queue.push({ name: "metric", tags: [], value: i }); | ||
} | ||
|
||
// The queue should have been reset and only contain the last metric | ||
expect(queue.length).toBe(1); | ||
expect(logger.warn).toHaveBeenLastCalledWith( | ||
'{"status":"warning","message":"datadog:Metrics queue is full, dropping all metrics."}', | ||
); | ||
}); | ||
|
||
it("enqueue metric", () => { | ||
setLogLevel(LogLevel.DEBUG); | ||
const queue = new MetricsQueue(); | ||
queue.push({ name: "metric", tags: [], value: 1 }); | ||
expect(queue.length).toBe(1); | ||
expect(logger.debug).toHaveBeenLastCalledWith( | ||
'{"status":"debug","message":"datadog:Metrics Listener was not initialized. Enqueuing metric for later processing."}', | ||
); | ||
}); | ||
}); | ||
|
||
describe("pop", () => { | ||
it("returns undefined when queue is empty", () => { | ||
const queue = new MetricsQueue(); | ||
expect(queue.pop()).toBeUndefined(); | ||
}); | ||
|
||
it("returns the first element in the queue", () => { | ||
const queue = new MetricsQueue(); | ||
queue.push({ name: "metric", tags: [], value: 1 }); | ||
queue.push({ name: "metric", tags: [], value: 2 }); | ||
expect(queue.pop()).toEqual({ name: "metric", tags: [], value: 2 }); | ||
}); | ||
}); | ||
|
||
it("reverses the queue", () => { | ||
const queue = new MetricsQueue(); | ||
queue.push({ name: "metric", tags: [], value: 1 }); | ||
queue.push({ name: "metric", tags: [], value: 2 }); | ||
queue.reverse(); | ||
expect(queue.pop()).toEqual({ name: "metric", tags: [], value: 1 }); | ||
}); | ||
|
||
it("resets the queue", () => { | ||
const queue = new MetricsQueue(); | ||
queue.push({ name: "metric", tags: [], value: 1 }); | ||
queue.push({ name: "metric", tags: [], value: 2 }); | ||
queue.reset(); | ||
expect(queue.length).toBe(0); | ||
}); | ||
|
||
it("returns the length of the queue", () => { | ||
const queue = new MetricsQueue(); | ||
queue.push({ name: "metric", tags: [], value: 1 }); | ||
queue.push({ name: "metric", tags: [], value: 2 }); | ||
expect(queue.length).toBe(2); | ||
}); | ||
}); |
Oops, something went wrong.