-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: webhook analytics timeouts (#1717)
## Description Creates analytics around webhook timeouts in the form of Prom metrics. Ex: Pepr Module ```ts async function sleep(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); } When(a.Pod) .IsCreatedOrUpdated() .Mutate(async po => { await sleep(5000); po.SetLabel("hello", "world"); }) ``` _run `npx pepr@v0.42.3 dev`_ Patch the webhook timeoutSeconds and failurePolicy ```bash kubectl patch mutatingwebhookconfiguration pepr-static-test --type='json' -p='[ { "op": "replace", "path": "/webhooks/0/failurePolicy", "value": "Fail" }, { "op": "replace", "path": "/webhooks/0/timeoutSeconds", "value": 3 } ]' ``` Create a pod ```bash > k run a --image=nginx Error from server (InternalError): Internal error occurred: failed calling webhook "pepr-static-test.pepr.dev": failed to call webhook: Post "https://host.k3d.internal:3000/mutate/72e209af76fec1700f45afd06d9f121d14e2727beef9d67f537bd3bffe5eda41?timeout=3s": context deadline exceeded ``` Curl the metrics ```bash > curl -k https://localhost:3000/metrics ... yada yada yada... # HELP pepr_mutate_timeouts Number of mutate webhook timeouts # TYPE pepr_mutate_timeouts counter pepr_mutate_timeouts 1 # HELP pepr_validate_timeouts Number of validate webhook timeouts # TYPE pepr_validate_timeouts counter pepr_validate_timeouts 0 ``` ## Related Issue Fixes #358 <!-- or --> Relates to # ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Other (security config, docs update, etc) ## Checklist before merging - [x] Unit, [Journey](https://github.com/defenseunicorns/pepr/tree/main/journey), [E2E Tests](https://github.com/defenseunicorns/pepr-excellent-examples), [docs](https://github.com/defenseunicorns/pepr/tree/main/docs), [adr](https://github.com/defenseunicorns/pepr/tree/main/adr) added or updated as needed - [x] [Contributor Guide Steps](https://docs.pepr.dev/main/contribute/#submitting-a-pull-request) followed --------- Signed-off-by: Case Wylie <cmwylie19@defenseunicorns.com>
- Loading branch information
Showing
8 changed files
with
152 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors | ||
|
||
import { describe, expect, it } from "@jest/globals"; | ||
import { getNow } from "./timeUtils"; | ||
describe("getNow", () => { | ||
it("should return the current time in milliseconds", () => { | ||
const perfNow = getNow(); | ||
const performanceNow = performance.now(); | ||
|
||
expect(performanceNow).toBeGreaterThanOrEqual(perfNow); | ||
}); | ||
}); |
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 @@ | ||
export const getNow = (): number => performance.now(); |
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,63 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors | ||
|
||
import { describe, expect, it, jest, beforeEach } from "@jest/globals"; | ||
import { MeasureWebhookTimeout } from "./webhookTimeouts"; | ||
import { metricsCollector } from "./metrics"; | ||
import { getNow } from "./timeUtils"; | ||
import { WebhookType } from "../enums"; | ||
|
||
jest.mock("./metrics", () => ({ | ||
metricsCollector: { | ||
addCounter: jest.fn(), | ||
incCounter: jest.fn(), | ||
}, | ||
})); | ||
|
||
jest.mock("./timeUtils", () => ({ | ||
getNow: jest.fn(), | ||
})); | ||
|
||
describe("MeasureWebhookTimeout", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should initialize a timeout counter for the webhook type", () => { | ||
const webhookType = WebhookType.MUTATE; | ||
new MeasureWebhookTimeout(webhookType); | ||
|
||
expect(metricsCollector.addCounter).toHaveBeenCalledWith( | ||
`${WebhookType.MUTATE}_timeouts`, | ||
"Number of mutate webhook timeouts", | ||
); | ||
}); | ||
|
||
it("should throw an error if stop is called before start", () => { | ||
const webhook = new MeasureWebhookTimeout(WebhookType.MUTATE); | ||
|
||
expect(() => webhook.stop()).toThrow("Timer was not started before calling stop."); | ||
}); | ||
|
||
it("should not increment the timeout counter if elapsed time is less than the timeout", () => { | ||
(getNow as jest.Mock).mockReturnValueOnce(1000).mockReturnValueOnce(1500); | ||
|
||
const webhook = new MeasureWebhookTimeout(WebhookType.MUTATE); | ||
webhook.start(1000); | ||
|
||
webhook.stop(); | ||
|
||
expect(metricsCollector.incCounter).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should increment the timeout counter if elapsed time exceeds the timeout", () => { | ||
(getNow as jest.Mock).mockReturnValueOnce(1000).mockReturnValueOnce(2000); | ||
|
||
const webhook = new MeasureWebhookTimeout(WebhookType.MUTATE); | ||
webhook.start(500); | ||
|
||
webhook.stop(); | ||
|
||
expect(metricsCollector.incCounter).toHaveBeenCalledWith(`${WebhookType.MUTATE}_timeouts`); | ||
}); | ||
}); |
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,34 @@ | ||
import { metricsCollector } from "./metrics"; | ||
import { getNow } from "./timeUtils"; | ||
import Log from "./logger"; | ||
import { WebhookType } from "../enums"; | ||
export class MeasureWebhookTimeout { | ||
#startTime: number | null = null; | ||
#webhookType: string; | ||
timeout: number = 0; | ||
|
||
constructor(webhookType: WebhookType) { | ||
this.#webhookType = webhookType; | ||
metricsCollector.addCounter(`${webhookType}_timeouts`, `Number of ${webhookType} webhook timeouts`); | ||
} | ||
|
||
start(timeout: number = 10): void { | ||
this.#startTime = getNow(); | ||
this.timeout = timeout; | ||
Log.info(`Starting timer at ${this.#startTime}`); | ||
} | ||
|
||
stop(): void { | ||
if (this.#startTime === null) { | ||
throw new Error("Timer was not started before calling stop."); | ||
} | ||
|
||
const elapsedTime = getNow() - this.#startTime; | ||
Log.info(`Webhook ${this.#startTime} took ${elapsedTime}ms`); | ||
this.#startTime = null; | ||
|
||
if (elapsedTime > this.timeout) { | ||
metricsCollector.incCounter(`${this.#webhookType}_timeouts`); | ||
} | ||
} | ||
} |