Skip to content

Commit 7d138d9

Browse files
authoredJan 23, 2025··
Fix RPC method invocations showing up as unknown events (#7811)
1 parent 3886065 commit 7d138d9

File tree

5 files changed

+90
-41
lines changed

5 files changed

+90
-41
lines changed
 

‎.changeset/fifty-pots-nail.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Fix RPC method invocations showing up as unknown events

‎packages/wrangler/src/__tests__/pages/pages-deployment-tail.test.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ import type {
1515
QueueEvent,
1616
RequestEvent,
1717
ScheduledEvent,
18-
TailEvent,
1918
TailEventMessage,
20-
TailInfo,
19+
TailEventMessageType,
2120
} from "../../tail/createTail";
2221
import type { RequestInit } from "undici";
2322
import type WebSocket from "ws";
@@ -807,18 +806,7 @@ function serialize(message: TailEventMessage): WebSocket.RawData {
807806
* @param event A TailEvent
808807
* @returns true if `event` is a RequestEvent
809808
*/
810-
function isRequest(
811-
event:
812-
| ScheduledEvent
813-
| RequestEvent
814-
| AlarmEvent
815-
| EmailEvent
816-
| TailEvent
817-
| TailInfo
818-
| QueueEvent
819-
| undefined
820-
| null
821-
): event is RequestEvent {
809+
function isRequest(event: TailEventMessageType): event is RequestEvent {
822810
return Boolean(event && "request" in event);
823811
}
824812

‎packages/wrangler/src/__tests__/tail.test.ts

+39-12
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import type {
1616
EmailEvent,
1717
QueueEvent,
1818
RequestEvent,
19+
RpcEvent,
1920
ScheduledEvent,
2021
TailEvent,
2122
TailEventMessage,
23+
TailEventMessageType,
2224
TailInfo,
2325
} from "../tail/createTail";
2426
import type { RequestInit } from "undici";
@@ -91,6 +93,7 @@ describe("tail", () => {
9193
await api.closeHelper();
9294
expect(api.requests.deletion.count).toStrictEqual(1);
9395
});
96+
9497
it("should connect to the worker assigned to a given route", async () => {
9598
api = mockWebsocketAPIs();
9699
expect(api.requests.creation.length).toStrictEqual(0);
@@ -532,6 +535,33 @@ describe("tail", () => {
532535
await api.closeHelper();
533536
});
534537

538+
it("logs rpc messages in pretty format", async () => {
539+
api = mockWebsocketAPIs();
540+
await runWrangler("tail test-worker --format pretty");
541+
542+
const event = generateMockRpcEvent();
543+
const message = generateMockEventMessage({
544+
entrypoint: "MyDurableObject",
545+
event,
546+
});
547+
const serializedMessage = serialize(message);
548+
549+
api.ws.send(serializedMessage);
550+
expect(
551+
std.out
552+
.replace(
553+
new Date(mockEventTimestamp).toLocaleString(),
554+
"[mock event timestamp]"
555+
)
556+
.replace(mockTailExpiration.toISOString(), "[mock expiration date]")
557+
).toMatchInlineSnapshot(`
558+
"Successfully created tail, expires at [mock expiration date]
559+
Connected to test-worker, waiting for logs...
560+
MyDurableObject.foo - Ok @ [mock event timestamp]"
561+
`);
562+
await api.closeHelper();
563+
});
564+
535565
it("logs scheduled messages in pretty format", async () => {
536566
api = mockWebsocketAPIs();
537567
await runWrangler("tail test-worker --format pretty");
@@ -908,18 +938,7 @@ function serialize(message: TailEventMessage): WebSocket.RawData {
908938
* @param event A TailEvent
909939
* @returns true if `event` is a RequestEvent
910940
*/
911-
function isRequest(
912-
event:
913-
| ScheduledEvent
914-
| RequestEvent
915-
| AlarmEvent
916-
| EmailEvent
917-
| TailEvent
918-
| TailInfo
919-
| QueueEvent
920-
| undefined
921-
| null
922-
): event is RequestEvent {
941+
function isRequest(event: TailEventMessageType): event is RequestEvent {
923942
return Boolean(event && "request" in event);
924943
}
925944

@@ -1133,13 +1152,15 @@ function mockWebsocketAPIs(
11331152
*/
11341153
function generateMockEventMessage({
11351154
outcome = "ok",
1155+
entrypoint = undefined,
11361156
exceptions = [],
11371157
logs = [],
11381158
eventTimestamp = mockEventTimestamp,
11391159
event = generateMockRequestEvent(),
11401160
}: Partial<TailEventMessage>): TailEventMessage {
11411161
return {
11421162
outcome,
1163+
entrypoint,
11431164
exceptions,
11441165
logs,
11451166
eventTimestamp,
@@ -1229,3 +1250,9 @@ function generateMockQueueEvent(opts?: Partial<QueueEvent>): QueueEvent {
12291250
batchSize: opts?.batchSize || 7,
12301251
};
12311252
}
1253+
1254+
function generateMockRpcEvent(opts?: Partial<RpcEvent>): RpcEvent {
1255+
return {
1256+
rpcMethod: opts?.rpcMethod || "foo",
1257+
};
1258+
}

‎packages/wrangler/src/tail/createTail.ts

+32-15
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ export async function createTail(
198198
return { tail, expiration, deleteTail };
199199
}
200200

201+
export type TailEventMessageType =
202+
| RequestEvent
203+
| ScheduledEvent
204+
| AlarmEvent
205+
| EmailEvent
206+
| TailEvent
207+
| TailInfo
208+
| QueueEvent
209+
| RpcEvent
210+
| undefined
211+
| null;
212+
201213
/**
202214
* Everything captured by the trace worker and sent to us via
203215
* `wrangler tail` is structured JSON that deserializes to this type.
@@ -213,6 +225,11 @@ export type TailEventMessage = {
213225
*/
214226
scriptName?: string;
215227

228+
/**
229+
* The name of the entrypoint invoked by the Worker
230+
*/
231+
entrypoint?: string;
232+
216233
/**
217234
* Any exceptions raised by the worker
218235
*/
@@ -250,29 +267,19 @@ export type TailEventMessage = {
250267
/**
251268
* The event that triggered the worker. In the case of an HTTP request,
252269
* this will be a RequestEvent. If it's a cron trigger, it'll be a
253-
* ScheduledEvent. If it's a durable object alarm, it's an AlarmEvent.
270+
* ScheduledEvent. If it's a Durable Object alarm, it's an AlarmEvent.
254271
* If it's a email, it'a an EmailEvent. If it's a Queue consumer event,
255272
* it's a QueueEvent.
256273
*
257274
* Until workers-types exposes individual types for export, we'll have
258275
* to just re-define these types ourselves.
259276
*/
260-
event:
261-
| RequestEvent
262-
| ScheduledEvent
263-
| AlarmEvent
264-
| EmailEvent
265-
| TailEvent
266-
| TailInfo
267-
| QueueEvent
268-
| undefined
269-
| null;
277+
event: TailEventMessageType;
270278
};
271279

272280
/**
273281
* A request that triggered worker execution
274282
*/
275-
276283
export type RequestEvent = {
277284
request: Pick<Request, "url" | "method" | "headers"> & {
278285
/**
@@ -386,7 +393,7 @@ export type ScheduledEvent = {
386393
};
387394

388395
/**
389-
* A event that was triggered from a durable object alarm
396+
* An event that was triggered from a Durable Object alarm
390397
*/
391398
export type AlarmEvent = {
392399
/**
@@ -442,8 +449,8 @@ export type TailInfo = {
442449
type: string;
443450
};
444451

445-
/*
446-
* A event that was triggered by receiving a batch of messages from a Queue for consumption.
452+
/**
453+
* An event that was triggered by receiving a batch of messages from a Queue for consumption.
447454
*/
448455
export type QueueEvent = {
449456
/**
@@ -456,3 +463,13 @@ export type QueueEvent = {
456463
*/
457464
batchSize: number;
458465
};
466+
467+
/**
468+
* An RPC method that was invoked
469+
*/
470+
export type RpcEvent = {
471+
/**
472+
* The name of the RPC method that was invoked
473+
*/
474+
rpcMethod: string;
475+
};

‎packages/wrangler/src/tail/printing.ts

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
EmailEvent,
66
QueueEvent,
77
RequestEvent,
8+
RpcEvent,
89
ScheduledEvent,
910
TailEvent,
1011
TailEventMessage,
@@ -82,6 +83,13 @@ export function prettyPrintLogs(data: WebSocket.RawData): void {
8283
logger.log(
8384
`Queue ${queueName} (${batchSizeMsg}) - ${outcome} @ ${datetime}`
8485
);
86+
} else if (isRpcEvent(eventMessage.event)) {
87+
const outcome = prettifyOutcome(eventMessage.outcome);
88+
const datetime = new Date(eventMessage.eventTimestamp).toLocaleString();
89+
90+
logger.log(
91+
`${eventMessage.entrypoint}.${eventMessage.event.rpcMethod} - ${outcome} @ ${datetime}`
92+
);
8593
} else {
8694
// Unknown event type
8795
const outcome = prettifyOutcome(eventMessage.outcome);
@@ -127,6 +135,10 @@ function isQueueEvent(event: TailEventMessage["event"]): event is QueueEvent {
127135
return Boolean(event && "queue" in event);
128136
}
129137

138+
function isRpcEvent(event: TailEventMessage["event"]): event is RpcEvent {
139+
return Boolean(event && "rpcMethod" in event);
140+
}
141+
130142
/**
131143
* Check to see if an event sent from a worker is an AlarmEvent.
132144
*

0 commit comments

Comments
 (0)
Please sign in to comment.