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

Add output for tail being in sampling mode #3146

Merged
merged 1 commit into from
May 8, 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
5 changes: 5 additions & 0 deletions .changeset/witty-pumpkins-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Added output for tail being in "sampling mode"
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
ScheduledEvent,
AlarmEvent,
EmailEvent,
TailInfo,
} from "../tail/createTail";
import type { RequestInit } from "undici";
import type WebSocket from "ws";
Expand Down Expand Up @@ -655,6 +656,7 @@ function isRequest(
| RequestEvent
| AlarmEvent
| EmailEvent
| TailInfo
| undefined
| null
): event is RequestEvent {
Expand Down
45 changes: 38 additions & 7 deletions packages/wrangler/src/__tests__/tail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
ScheduledEvent,
AlarmEvent,
EmailEvent,
TailInfo,
} from "../tail/createTail";
import type { RequestInit } from "undici";
import type WebSocket from "ws";
Expand Down Expand Up @@ -56,10 +57,10 @@ describe("tail", () => {
await runWrangler("tail durable-object--websocket--response");
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`
"▲ [WARNING] Beginning log collection requires restarting the Durable Objects associated with durable-object--websocket--response. Any WebSocket connections or other non-persisted state will be lost as part of this restart.
"▲ [WARNING] Beginning log collection requires restarting the Durable Objects associated with durable-object--websocket--response. Any WebSocket connections or other non-persisted state will be lost as part of this restart.

"
`);
"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
it("creates and then delete tails", async () => {
Expand Down Expand Up @@ -503,10 +504,31 @@ describe("tail", () => {
)
.replace(mockTailExpiration.toISOString(), "[mock expiration date]")
).toMatchInlineSnapshot(`
"Successfully created tail, expires at [mock expiration date]
Connected to test-worker, waiting for logs...
Email from:${mockEmailEventFrom} to:${mockEmailEventTo} size:${mockEmailEventSize} @ [mock event timestamp] - Ok"
`);
"Successfully created tail, expires at [mock expiration date]
Connected to test-worker, waiting for logs...
Email from:[email protected] to:[email protected] size:45416 @ [mock event timestamp] - Ok"
`);
});

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

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

api.ws.send(serializedMessage);
expect(
std.out.replace(
mockTailExpiration.toISOString(),
"[mock expiration date]"
)
).toMatchInlineSnapshot(`
"Successfully created tail, expires at [mock expiration date]
Connected to test-worker, waiting for logs...
Tail is currently in sampling mode due to the high volume of messages. To prevent messages from being dropped consider adding filters."
`);
});

it("should not crash when the tail message has a void event", async () => {
Expand Down Expand Up @@ -675,6 +697,7 @@ function isRequest(
| RequestEvent
| AlarmEvent
| EmailEvent
| TailInfo
| undefined
| null
): event is RequestEvent {
Expand Down Expand Up @@ -956,3 +979,11 @@ function generateMockEmailEvent(opts?: Partial<EmailEvent>): EmailEvent {
rawSize: opts?.rawSize || mockEmailEventSize,
};
}

function generateTailInfo(): TailInfo {
return {
message:
"Tail is currently in sampling mode due to the high volume of messages. To prevent messages from being dropped consider adding filters.",
type: "overload",
};
}
9 changes: 9 additions & 0 deletions packages/wrangler/src/tail/createTail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export type TailEventMessage = {
| ScheduledEvent
| AlarmEvent
| EmailEvent
| TailInfo
| undefined
| null;
};
Expand Down Expand Up @@ -404,3 +405,11 @@ export type EmailEvent = {
*/
rawSize: number;
};

/**
* Message from tail with information about the tail itself
*/
export type TailInfo = {
message: string;
type: string;
};
10 changes: 10 additions & 0 deletions packages/wrangler/src/tail/printing.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import chalk from "chalk";
import { logger } from "../logger";
import type {
AlarmEvent,
EmailEvent,
RequestEvent,
ScheduledEvent,
TailInfo,
TailEventMessage,
} from "./createTail";
import type { Outcome } from "./filters";
Expand Down Expand Up @@ -48,6 +50,10 @@ export function prettyPrintLogs(data: WebSocket.RawData): void {
).toLocaleString();

logger.log(`Alarm @ ${datetime} - ${outcome}`);
} else if (isTailInfo(eventMessage.event)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of else if's starting to getting added, we may want to start thinking about the code design here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a switch/reducer kind of pattern, or something to define the event state.

if (eventMessage.event.type === "overload") {
logger.log(`${chalk.red.bold(eventMessage.event.message)}`);
}
} else {
// Unknown event type
const outcome = prettifyOutcome(eventMessage.outcome);
Expand Down Expand Up @@ -103,6 +109,10 @@ function isAlarmEvent(event: TailEventMessage["event"]): event is AlarmEvent {
return Boolean(event && "scheduledTime" in event && !("cron" in event));
}

function isTailInfo(event: TailEventMessage["event"]): event is TailInfo {
return Boolean(event && "message" in event && "type" in event);
}
Comment on lines +112 to +114
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Predicate Type Check, nice.


function prettifyOutcome(outcome: Outcome): string {
switch (outcome) {
case "ok":
Expand Down