-
Notifications
You must be signed in to change notification settings - Fork 604
Backend observability and hygiene for Durable Object resets #56
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
4 commits
Select commit
Hold shift + click to select a range
9123bdc
Restore visibility of API session aborts
ndisidore bf835a7
Restore intended 64 KiB snapshot threshold
ndisidore cdac2c3
Enable Workers Traces on workshop-backend
ndisidore 010dd53
Add custom trace spans and heavy-operation duration logs
ndisidore 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Runs against the real workerd tracing API (vitest-pool-workers), pinning the platform | ||
| // contract `traced` depends on: enterSpan ends its span when the returned promise settles, not | ||
| // when the synchronous callback returns (docs: workers/observability/traces/custom-spans/). | ||
| // | ||
| // Caveats: | ||
| // - Invocations are only traced when a tail consumer is attached, so vitest.config.ts wires a | ||
| // no-op streaming tail sink behind the experimental `streaming_tail_worker` flag. If workerd | ||
| // renames or removes that flag, setup fails loudly — a mechanical config fix, not a tracer bug. | ||
| // - Attributes are write-only, so "span still open" is inferred from the documented guarantee | ||
| // that `span.isTraced` flips to false once the span ends. The entry assertion in each test | ||
| // guards the signal itself: if invocations stop being traced, isTraced starts false and the | ||
| // test fails rather than silently passing. | ||
|
|
||
| import { describe, expect, it } from "vitest"; | ||
| import { createTracer } from "../src/tracing"; | ||
|
|
||
| const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
| const traced = createTracer(() => ({})); | ||
|
|
||
| describe("traced span lifetime", () => { | ||
| it("keeps the span open across awaits in an async callback", async () => { | ||
| const observed: boolean[] = []; | ||
| await traced("probe", async (span) => { | ||
| observed.push(span.isTraced); // must start true — guards the isTraced signal | ||
| await sleep(50); | ||
| observed.push(span.isTraced); // still true ⇒ span did not end at the sync return | ||
| }); | ||
| expect(observed).toEqual([true, true]); | ||
| }); | ||
|
|
||
| it("records the error attribute before the span can close on rejection", async () => { | ||
| let openWhenErrorSet: boolean | undefined; | ||
| await expect( | ||
| traced("probe-reject", (span) => { | ||
| // Shadow setAttribute to observe the instant traced()'s catch marks the failure. | ||
| const original = span.setAttribute.bind(span); | ||
| span.setAttribute = (key, value) => { | ||
| if (key === "error") openWhenErrorSet = span.isTraced; | ||
| original(key, value); | ||
| }; | ||
| return (async () => { | ||
| await sleep(50); | ||
| throw new Error("boom"); | ||
| })(); | ||
| }), | ||
| ).rejects.toThrow("boom"); | ||
| expect(openWhenErrorSet).toBe(true); | ||
| }); | ||
| }); |
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,46 @@ | ||
| import { tracing } from "cloudflare:workers"; | ||
|
|
||
| type Attribute = boolean | number | string; | ||
|
|
||
| // The span surface exposed to callbacks. Lifetime is managed by `traced`, so no `end()`. | ||
| export interface TraceSpan { | ||
| readonly isTraced: boolean; | ||
| setAttribute(key: string, value?: Attribute): void; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a span helper that stamps the ambient observability context onto each span as | ||
| * attributes. Tracing only: never logs, never modifies context. Exceptions propagate | ||
| * unchanged, marked on the span via an `error` attribute (the beta API has no outcome). | ||
| * Sync and async callbacks both get correct spans: enterSpan ends the span only when the | ||
| * returned promise settles, not at the synchronous return (pinned by __tests__/tracing.test.ts). | ||
| */ | ||
| export function createTracer(getContext: () => Readonly<Record<string, unknown>>) { | ||
| return function traced<Result>(name: string, callback: (span: TraceSpan) => Result): Result { | ||
| return tracing.enterSpan(name, (span) => { | ||
| if (span.isTraced) { | ||
| for (const [key, value] of Object.entries(getContext())) { | ||
| if (typeof value === "boolean" || typeof value === "number" || typeof value === "string") { | ||
| span.setAttribute(key, value); | ||
| } | ||
| } | ||
| } | ||
| // Boolean marker only: error text is unbounded and possibly sensitive, so it belongs to | ||
| // logs/reporting, not trace attributes. | ||
| const fail = () => span.setAttribute("error", true); | ||
| try { | ||
| const result = callback(span); | ||
| // enterSpan keeps the span open until the returned promise settles, so async work gets | ||
| // its real duration and fail() runs before the span can close (the runtime watches the | ||
| // .catch-wrapped promise returned here). That wrapper is a new promise, not `result` — | ||
| // fine for data results; don't wrap pipelined RPC stubs in `traced`. | ||
| return result instanceof Promise | ||
| ? result.catch((err) => { fail(); throw err; }) as Result | ||
| : result; | ||
| } catch (err) { | ||
| fail(); | ||
| throw err; | ||
| } | ||
| }); | ||
| }; | ||
| } |
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
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
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
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.