Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/DuckBug/DuckBugService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { DuckBugErrorEvent, DuckBugLogEvent } from "../contract";
import type { DuckBugConfig } from "./DuckBugConfig";
import { ensureEventId } from "./ensureEventId";
import {
ingestErrorsBatchUrl,
ingestErrorsUrl,
Expand Down Expand Up @@ -44,13 +45,14 @@ const STATUS_NOT_IMPLEMENTED = 501;
* event id with a Postgres primary key and ON CONFLICT DO NOTHING, with no
* expiry, on the single and the batch route alike.
*
* That guarantee is only as strong as the id. It has to be supplied by the
* caller: when a payload reaches ingest without an `eventId`, the server mints
* a fresh one per request and a retry does store the event twice.
* `finalizeIngestEvent` runs `ensureEventId` on everything `DuckBugProvider`
* sends, so the normal path is safe; code driving `DuckBugService` directly
* owns that field itself. The server validates it as uuid4, so a non-UUID
* idempotency key is rejected with 400 rather than honoured.
* That guarantee is only as strong as the id, and the id is optional to
* ingest: a payload that arrives without an `eventId` gets a fresh one minted
* per request, and a retry then stores the event twice. Nothing leaves this
* class in that state - `sendLog` and `sendError` run `ensureEventId` on every
* event they queue, which is what makes the guarantee hold for code driving
* `DuckBugService` directly as well as for the `DuckBugProvider` path. The
* server validates it as uuid4, so a non-UUID idempotency key is rejected with
* 400 rather than honoured.
*
* 408 is retried because it is the edge timing out the request body (nginx
* client_body_timeout and friends), never a verdict on the payload; RFC 9110
Expand Down Expand Up @@ -128,16 +130,30 @@ export class DuckBugService {
this.onTransportError = config.onTransportError;
}

/**
* The id is assigned here, on the way into the queue, because this is the
* one point every event passes through before a request is built: ingest
* deduplicates retries on `eventId` and mints a throwaway one per request
* when the field is missing, so an event queued without it is stored once
* per attempt of `postJsonWithRetry`. `finalizeIngestEvent` covers the
* `DuckBugProvider` pipeline, but not a caller holding a service directly,
* not `sendLog(event, { skipPrivacyPipeline: true })`, and not a
* `beforeSend` hook that returned an event with the field dropped.
* `ensureEventId` keeps an id the caller supplied.
*/
sendLog(logInfo: DuckBugLogEvent): void {
const event = ensureEventId(logInfo);
void this.enqueueLogTransport(async () => {
this.logQueue.push(logInfo);
this.logQueue.push(event);
await this.pumpLogsAfterEnqueue();
});
}

/** See {@link DuckBugService.sendLog} for why the id is assigned here. */
sendError(errorRequest: DuckBugErrorEvent): void {
const event = ensureEventId(errorRequest);
void this.enqueueErrorTransport(async () => {
this.errorQueue.push(errorRequest);
this.errorQueue.push(event);
await this.pumpErrorsAfterEnqueue();
});
}
Expand Down
119 changes: 105 additions & 14 deletions tests/DuckBug/DuckBugService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ describe("DuckBugService", () => {
await service.flush();

expect(mockFetch).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledWith(logsUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(logInfo),
const callArgs = mockFetch.mock.calls[0];
expect(callArgs[0]).toBe(logsUrl);
expect(callArgs[1]?.method).toBe("POST");
expect(callArgs[1]?.headers).toEqual({
"Content-Type": "application/json",
});
// The body is the caller's event plus the eventId the service mints for
// retry idempotency; the id itself is covered by the "eventId" tests.
expect(JSON.parse(callArgs[1]?.body as string)).toEqual({
...JSON.parse(JSON.stringify(logInfo)),
eventId: expect.any(String),
});
});

Expand All @@ -59,12 +64,17 @@ describe("DuckBugService", () => {
await service.flush();

expect(mockFetch).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledWith(logsUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(logInfo),
const callArgs = mockFetch.mock.calls[0];
expect(callArgs[0]).toBe(logsUrl);
expect(callArgs[1]?.method).toBe("POST");
expect(callArgs[1]?.headers).toEqual({
"Content-Type": "application/json",
});
// The body is the caller's event plus the eventId the service mints for
// retry idempotency; the id itself is covered by the "eventId" tests.
expect(JSON.parse(callArgs[1]?.body as string)).toEqual({
...JSON.parse(JSON.stringify(logInfo)),
eventId: expect.any(String),
});
});

Expand Down Expand Up @@ -146,7 +156,10 @@ describe("DuckBugService", () => {
});

const requestBody = JSON.parse(callArgs[1]?.body as string);
expect(requestBody).toEqual(errorRequest);
expect(requestBody).toEqual({
...errorRequest,
eventId: expect.any(String),
});
});

it("should send error request without context", async () => {
Expand All @@ -166,7 +179,10 @@ describe("DuckBugService", () => {

const callArgs = mockFetch.mock.calls[0];
const requestBody = JSON.parse(callArgs[1]?.body as string);
expect(requestBody).toEqual(errorRequest);
expect(requestBody).toEqual({
...errorRequest,
eventId: expect.any(String),
});
});

it("should send error request with all fields", async () => {
Expand Down Expand Up @@ -220,6 +236,81 @@ describe("DuckBugService", () => {
});
});

describe("eventId", () => {
// The shape ingest validates the field against (`omitempty,uuid4`): an id
// in any other shape is answered with 400, not deduplicated.
const UUID4 =
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;

const bodyOf = (call: unknown[]): Record<string, unknown> =>
JSON.parse((call[1] as RequestInit).body as string);

it("mints a uuid4 eventId for a log sent without one", async () => {
service.sendLog({
message: "no id from the caller",
level: logLevel.INFO,
time: 1234567890,
});
await service.flush();

expect(bodyOf(mockFetch.mock.calls[0]).eventId).toMatch(UUID4);
});

it("mints a uuid4 eventId for an error sent without one", async () => {
service.sendError({
time: 1234567890,
message: "no id from the caller",
stacktrace: { raw: "", frames: [] },
file: "test.js",
line: 1,
});
await service.flush();

expect(bodyOf(mockFetch.mock.calls[0]).eventId).toMatch(UUID4);
});

it("keeps an eventId the caller supplied", async () => {
const callerId = "550e8400-e29b-41d4-a716-446655440000";

service.sendLog({
eventId: callerId,
message: "caller owns the id",
level: logLevel.INFO,
time: 1234567890,
});
await service.flush();

expect(bodyOf(mockFetch.mock.calls[0]).eventId).toBe(callerId);
});

it("mints a distinct eventId per batched event", async () => {
const batching = new DuckBugService({
dsn: TEST_DSN,
transport: { maxBatchSize: 2 },
});

batching.sendLog({
message: "first",
level: logLevel.INFO,
time: 1,
});
batching.sendLog({
message: "second",
level: logLevel.INFO,
time: 2,
});
await batching.flush();

const batch = JSON.parse(
(mockFetch.mock.calls[0][1] as RequestInit).body as string,
) as Array<Record<string, unknown>>;
expect(batch).toHaveLength(2);
expect(batch[0].eventId).toMatch(UUID4);
expect(batch[1].eventId).toMatch(UUID4);
expect(batch[0].eventId).not.toBe(batch[1].eventId);
});
});

describe("integration", () => {
it("should handle multiple consecutive calls", async () => {
const logInfo: Log = {
Expand Down
Loading