-
Notifications
You must be signed in to change notification settings - Fork 0
feat(observability): emit structured idempotency events on all 4 claimDelivery outcomes #242
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
1c945d3
feat(observability): emit structured idempotency events on all 4 clai…
chrisleekr cad2db6
fix(observability): tighten idempotency schema and doc per review
chrisleekr 63b2ee7
fix(observability): claimed at debug per #232 volume policy; validate…
chrisleekr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,58 @@ | ||
| /** | ||
| * Canonical pino log-field schema for `claimDelivery` observability (issue #232). | ||
| * | ||
| * Mirrors `src/utils/retry-log-fields.ts`: a strict Zod shape pins the | ||
| * structured `idempotency.*` event family so the emit sites in `claimDelivery` | ||
| * cannot drift on a field name (e.g. `reason` value or `failed_open` vs | ||
| * `failed-open`) without the co-located test catching it. Emitters log plain | ||
| * objects via `log.info` / `log.warn`; the schema is the drift-prevention | ||
| * contract, not a runtime validator on the hot path. | ||
| * | ||
| * The schema is a union of strict per-outcome objects so per-event field | ||
| * presence is pinned exactly: `claimed` / `duplicate_skipped` carry only | ||
| * `deliveryId`; `failed_open` splits into a `reason: "unavailable"` branch | ||
| * (no `err`) and a `reason: "error"` branch (`err` required). The fail-open | ||
| * split is two branches rather than a `reason` enum + optional `err` so the | ||
| * contract "`err` only on the `error` path" is enforced by the schema itself, | ||
| * not just by emit-site discipline: a future emit attaching `err` to an | ||
| * `unavailable` line is rejected here and trips the co-located test. | ||
| */ | ||
| import { z } from "zod"; | ||
|
|
||
| export const IDEMPOTENCY_LOG_EVENTS = { | ||
| claimed: "idempotency.claimed", | ||
| duplicateSkipped: "idempotency.duplicate_skipped", | ||
| failedOpen: "idempotency.failed_open", | ||
| } as const; | ||
|
|
||
| // `deliveryId` stays camelCase because it is the established repo-wide | ||
| // child-logger delivery identifier binding; new metric-style fields use snake_case. | ||
| const deliveryId = z.string().min(1); | ||
|
|
||
| export const IdempotencyLogFieldsSchema = z.union([ | ||
| /** Info: the SET-NX won the claim (first delivery seen); the caller proceeds. */ | ||
| z.strictObject({ | ||
| event: z.literal(IDEMPOTENCY_LOG_EVENTS.claimed), | ||
| deliveryId, | ||
| }), | ||
| /** Info: the SET-NX found an existing key (a redelivery); the caller skips. */ | ||
| z.strictObject({ | ||
| event: z.literal(IDEMPOTENCY_LOG_EVENTS.duplicateSkipped), | ||
| deliveryId, | ||
| }), | ||
| /** Warn: Valkey unconfigured/disconnected, no SET issued. The caller proceeds. */ | ||
| z.strictObject({ | ||
| event: z.literal(IDEMPOTENCY_LOG_EVENTS.failedOpen), | ||
| deliveryId, | ||
| reason: z.literal("unavailable"), | ||
| }), | ||
| /** Warn: the SET threw; `err` carries the message. The caller proceeds. */ | ||
| z.strictObject({ | ||
| event: z.literal(IDEMPOTENCY_LOG_EVENTS.failedOpen), | ||
| deliveryId, | ||
| reason: z.literal("error"), | ||
| err: z.string(), | ||
| }), | ||
| ]); | ||
|
chrisleekr marked this conversation as resolved.
|
||
|
|
||
| export type IdempotencyLogFields = z.infer<typeof IdempotencyLogFieldsSchema>; | ||
This file contains hidden or 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 hidden or 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,114 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
|
|
||
| import { | ||
| IDEMPOTENCY_LOG_EVENTS, | ||
| IdempotencyLogFieldsSchema, | ||
| } from "../../src/webhook/idempotency-log-fields"; | ||
|
|
||
| describe("IDEMPOTENCY_LOG_EVENTS", () => { | ||
| it("pins the three canonical event strings", () => { | ||
| expect(IDEMPOTENCY_LOG_EVENTS.claimed).toBe("idempotency.claimed"); | ||
| expect(IDEMPOTENCY_LOG_EVENTS.duplicateSkipped).toBe("idempotency.duplicate_skipped"); | ||
| expect(IDEMPOTENCY_LOG_EVENTS.failedOpen).toBe("idempotency.failed_open"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("IdempotencyLogFieldsSchema: accepts well-formed events", () => { | ||
| it("accepts a valid idempotency.claimed record", () => { | ||
| const result = IdempotencyLogFieldsSchema.safeParse({ | ||
| event: IDEMPOTENCY_LOG_EVENTS.claimed, | ||
| deliveryId: "d1", | ||
| }); | ||
| expect(result.success).toBe(true); | ||
| }); | ||
|
|
||
| it("accepts a valid idempotency.duplicate_skipped record", () => { | ||
| const result = IdempotencyLogFieldsSchema.safeParse({ | ||
| event: IDEMPOTENCY_LOG_EVENTS.duplicateSkipped, | ||
| deliveryId: "d1", | ||
| }); | ||
| expect(result.success).toBe(true); | ||
| }); | ||
|
|
||
| it("accepts a valid idempotency.failed_open record with reason unavailable", () => { | ||
| const result = IdempotencyLogFieldsSchema.safeParse({ | ||
| event: IDEMPOTENCY_LOG_EVENTS.failedOpen, | ||
| deliveryId: "d1", | ||
| reason: "unavailable", | ||
| }); | ||
| expect(result.success).toBe(true); | ||
| }); | ||
|
|
||
| it("accepts a valid idempotency.failed_open record with reason error and err", () => { | ||
| const result = IdempotencyLogFieldsSchema.safeParse({ | ||
| event: IDEMPOTENCY_LOG_EVENTS.failedOpen, | ||
| deliveryId: "d1", | ||
| reason: "error", | ||
| err: "ECONNREFUSED", | ||
| }); | ||
| expect(result.success).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("IdempotencyLogFieldsSchema: rejects drift and bad input", () => { | ||
| it("rejects an unknown extra field (strict)", () => { | ||
| const result = IdempotencyLogFieldsSchema.safeParse({ | ||
| event: IDEMPOTENCY_LOG_EVENTS.claimed, | ||
| deliveryId: "d1", | ||
| surprise: "boo", | ||
| }); | ||
| expect(result.success).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects an unknown event literal", () => { | ||
| const result = IdempotencyLogFieldsSchema.safeParse({ | ||
| event: "idempotency.bogus", | ||
| deliveryId: "d1", | ||
| }); | ||
| expect(result.success).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects an invalid reason on failed_open", () => { | ||
| const result = IdempotencyLogFieldsSchema.safeParse({ | ||
| event: IDEMPOTENCY_LOG_EVENTS.failedOpen, | ||
| deliveryId: "d1", | ||
| reason: "weird", | ||
| }); | ||
| expect(result.success).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects a failed_open record missing reason", () => { | ||
| const result = IdempotencyLogFieldsSchema.safeParse({ | ||
| event: IDEMPOTENCY_LOG_EVENTS.failedOpen, | ||
| deliveryId: "d1", | ||
| }); | ||
| expect(result.success).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects err on the unavailable branch (err only valid with reason error)", () => { | ||
| const result = IdempotencyLogFieldsSchema.safeParse({ | ||
| event: IDEMPOTENCY_LOG_EVENTS.failedOpen, | ||
| deliveryId: "d1", | ||
| reason: "unavailable", | ||
| err: "boom", | ||
| }); | ||
| expect(result.success).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects a failed_open record with reason error but no err", () => { | ||
| const result = IdempotencyLogFieldsSchema.safeParse({ | ||
| event: IDEMPOTENCY_LOG_EVENTS.failedOpen, | ||
| deliveryId: "d1", | ||
| reason: "error", | ||
| }); | ||
| expect(result.success).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects an empty deliveryId", () => { | ||
| const result = IdempotencyLogFieldsSchema.safeParse({ | ||
| event: IDEMPOTENCY_LOG_EVENTS.claimed, | ||
| deliveryId: "", | ||
| }); | ||
| expect(result.success).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.