Skip to content
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

Support Queue events in tail #2397

Merged
merged 1 commit into from
Oct 10, 2023
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
8 changes: 8 additions & 0 deletions .changeset/warm-queens-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"wrangler": minor
---

feature: Support Queue consumer events in tail

So that it's less confusing when tailing a worker that consumes events from a
Queue.
49 changes: 49 additions & 0 deletions packages/wrangler/src/__tests__/pages-deployment-tail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
EmailEvent,
TailEvent,
TailInfo,
QueueEvent,
} from "../tail/createTail";
import type { RequestInit } from "undici";
import type WebSocket from "ws";
Expand Down Expand Up @@ -379,6 +380,20 @@ describe("pages deployment tail", () => {
expect(std.out).toMatch(deserializeToJson(serializedMessage));
});

it("logs queue messages in json format", async () => {
const api = mockTailAPIs();
await runWrangler(
"pages deployment tail mock-deployment-id --project-name mock-project --format json"
);

const event = generateMockQueueEvent();
const message = generateMockEventMessage({ event });
const serializedMessage = serialize(message);

api.ws.send(serializedMessage);
expect(std.out).toMatch(deserializeToJson(serializedMessage));
});

it("logs request messages in pretty format", async () => {
const api = mockTailAPIs();
await runWrangler(
Expand Down Expand Up @@ -487,6 +502,33 @@ describe("pages deployment tail", () => {
`);
});

it("logs queue messages in pretty format", async () => {
const api = mockTailAPIs();
await runWrangler(
"pages deployment tail mock-deployment-id --project-name mock-project --format pretty"
);

const event = generateMockQueueEvent();
const message = generateMockEventMessage({ event });
const serializedMessage = serialize(message);

api.ws.send(serializedMessage);
expect(
std.out
.replace(
new Date(mockEventTimestamp).toLocaleString(),
"[mock timestamp string]"
)
.replace(
mockTailExpiration.toLocaleString(),
"[mock expiration date]"
)
).toMatchInlineSnapshot(`
"Connected to deployment mock-deployment-id, waiting for logs...
Queue my-queue123 (7 messages) - Ok @ [mock timestamp string]"
`);
});

it("should not crash when the tail message has a void event", async () => {
const api = mockTailAPIs();
await runWrangler(
Expand Down Expand Up @@ -666,6 +708,7 @@ function isRequest(
| EmailEvent
| TailEvent
| TailInfo
| QueueEvent
| undefined
| null
): event is RequestEvent {
Expand Down Expand Up @@ -964,3 +1007,9 @@ function generateMockEmailEvent(opts?: Partial<EmailEvent>): EmailEvent {
rawSize: opts?.rawSize || mockEmailEventSize,
};
}
function generateMockQueueEvent(opts?: Partial<QueueEvent>): QueueEvent {
return {
queue: opts?.queue || "my-queue123",
batchSize: opts?.batchSize || 7,
};
}
44 changes: 44 additions & 0 deletions packages/wrangler/src/__tests__/tail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
EmailEvent,
TailEvent,
TailInfo,
QueueEvent,
} from "../tail/createTail";
import type { RequestInit } from "undici";
import type WebSocket from "ws";
Expand Down Expand Up @@ -439,6 +440,18 @@ describe("tail", () => {
expect(std.out).toMatch(deserializeToJson(serializedMessage));
});

it("logs queue messages in json format", async () => {
const api = mockWebsocketAPIs();
await runWrangler("tail test-worker --format json");

const event = generateMockQueueEvent();
const message = generateMockEventMessage({ event });
const serializedMessage = serialize(message);

api.ws.send(serializedMessage);
expect(std.out).toMatch(deserializeToJson(serializedMessage));
});

it("logs request messages in pretty format", async () => {
const api = mockWebsocketAPIs();
await runWrangler("tail test-worker --format pretty");
Expand Down Expand Up @@ -583,6 +596,29 @@ describe("tail", () => {
`);
});

it("logs queue messages in pretty format", async () => {
const api = mockWebsocketAPIs();
await runWrangler("tail test-worker --format pretty");

const event = generateMockQueueEvent();
const message = generateMockEventMessage({ event });
const serializedMessage = serialize(message);

api.ws.send(serializedMessage);
expect(
std.out
.replace(
new Date(mockEventTimestamp).toLocaleString(),
"[mock timestamp string]"
)
.replace(mockTailExpiration.toISOString(), "[mock expiration date]")
).toMatchInlineSnapshot(`
"Successfully created tail, expires at [mock expiration date]
Connected to test-worker, waiting for logs...
Queue my-queue123 (7 messages) - Ok @ [mock timestamp string]"
`);
});

it("should not crash when the tail message has a void event", async () => {
const api = mockWebsocketAPIs();
await runWrangler("tail test-worker --format pretty");
Expand Down Expand Up @@ -751,6 +787,7 @@ function isRequest(
| EmailEvent
| TailEvent
| TailInfo
| QueueEvent
| undefined
| null
): event is RequestEvent {
Expand Down Expand Up @@ -1054,3 +1091,10 @@ function generateTailInfo(overload: boolean): TailInfo {
type: "overload-stop",
};
}

function generateMockQueueEvent(opts?: Partial<QueueEvent>): QueueEvent {
return {
queue: opts?.queue || "my-queue123",
batchSize: opts?.batchSize || 7,
};
}
19 changes: 18 additions & 1 deletion packages/wrangler/src/tail/createTail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ export type TailEventMessage = {
* The event that triggered the worker. In the case of an HTTP request,
* this will be a RequestEvent. If it's a cron trigger, it'll be a
* ScheduledEvent. If it's a durable object alarm, it's an AlarmEvent.
* If it's a email, it'a an EmailEvent
* If it's a email, it'a an EmailEvent. If it's a Queue consumer event,
* it's a QueueEvent.
*
* Until workers-types exposes individual types for export, we'll have
* to just re-define these types ourselves.
Expand All @@ -254,6 +255,7 @@ export type TailEventMessage = {
| EmailEvent
| TailEvent
| TailInfo
| QueueEvent
| undefined
| null;
};
Expand Down Expand Up @@ -430,3 +432,18 @@ export type TailInfo = {
message: string;
type: string;
};

/*
* A event that was triggered by receiving a batch of messages from a Queue for consumption.
*/
export type QueueEvent = {
/**
* The name of the queue that the message batch came from.
*/
queue: string;

/**
* The number of messages in the batch.
*/
batchSize: number;
};
15 changes: 15 additions & 0 deletions packages/wrangler/src/tail/printing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { logger } from "../logger";
import type {
AlarmEvent,
EmailEvent,
QueueEvent,
RequestEvent,
ScheduledEvent,
TailEvent,
Expand Down Expand Up @@ -71,6 +72,16 @@ export function prettyPrintLogs(data: WebSocket.RawData): void {
} else if (eventMessage.event.type === "overload-stop") {
logger.log(`${chalk.yellow.bold(eventMessage.event.message)}`);
}
} else if (isQueueEvent(eventMessage.event)) {
const outcome = prettifyOutcome(eventMessage.outcome);
const datetime = new Date(eventMessage.eventTimestamp).toLocaleString();
const queueName = eventMessage.event.queue;
const batchSize = eventMessage.event.batchSize;
const batchSizeMsg = `${batchSize} message${batchSize !== 1 ? "s" : ""}`;

logger.log(
`Queue ${queueName} (${batchSizeMsg}) - ${outcome} @ ${datetime}`
);
} else {
// Unknown event type
const outcome = prettifyOutcome(eventMessage.outcome);
Expand Down Expand Up @@ -112,6 +123,10 @@ function isEmailEvent(event: TailEventMessage["event"]): event is EmailEvent {
return Boolean(event && "mailFrom" in event);
}

function isQueueEvent(event: TailEventMessage["event"]): event is QueueEvent {
return Boolean(event && "queue" in event);
}

/**
* Check to see if an event sent from a worker is an AlarmEvent.
*
Expand Down