feat(tracing): Span.startChild + getActiveSpan + honoTracing helper#9
Open
stackbilt-admin wants to merge 1 commit intomainfrom
Open
feat(tracing): Span.startChild + getActiveSpan + honoTracing helper#9stackbilt-admin wants to merge 1 commit intomainfrom
stackbilt-admin wants to merge 1 commit intomainfrom
Conversation
Three dogfood-driven ergonomics lifts, all surfaced while instrumenting tarotscript-worker (tarotscript#163). Closes worker-observability#3, #4. What's new 1. Span.startChild(name, attributes?) - Convenience wrapper around `this.tracer.startSpan(name, { parent })` - Saves ~3 lines of mechanical plumbing at every child-span call site - NoOpSpan override returns itself so sampled-out spans are still safe 2. AsyncLocalStorage-backed active span context - Tracer.runWithSpan(span, fn) scopes the span for a code region - getActiveSpan() returns the scoped span anywhere in the call stack, across async boundaries, without threading a parent through every function signature - Import guarded via @ts-expect-error; node:async_hooks is pulled in at runtime by the Workers `nodejs_als` / `nodejs_compat` flag. When ALS is unavailable the storage is null and both runWithSpan and getActiveSpan degrade gracefully (identity wrapper / undefined) - This is the scalability lever for deep instrumentation — library code (interpreters, LLM providers, scaffold materializers) can now create child spans without taking a hard dep on observability 3. honoTracing() — batteries-included Hono middleware - Wraps the full root-span lifecycle pattern every dogfood worker was rewriting: span start, HTTP attributes, recordError on throw, error status on 5xx, publish to c.get('rootSpan'), automatic active-span scope via runWithSpan, and ctx.waitUntil-based flush of tracer+metrics so ingest doesn't block the response - Options: skip predicate, extra attributes callback, custom spanNamer, contextKey override - Accepts a minimal HonoTracingMonitoring shape ({ tracer, metrics }) so it doesn't couple to the full createMonitoring bundle - Leaves the existing tracingMiddleware in place; honoTracing is the replacement for dogfood consumers Tests Added src/__tests__/tracing-ergonomics.test.ts (11 tests): - startChild: trace-id inheritance, parent-span linkage, attributes - runWithSpan/getActiveSpan: synchronous, async, deep-stack scenarios - honoTracing: null-safety, skip predicate, root span publication, active-span scoping, 5xx → error, thrown error → recordError, custom spanNamer + attributes 82/82 tests passing (71 existing redact + 11 new ergonomics). Exports added to src/index.ts: - honoTracing, getActiveSpan - HonoTracingMonitoring, HonoTracingOptions types Closes #3 Closes #4 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Three dogfood-driven ergonomics lifts surfaced while instrumenting `tarotscript-worker` (tarotscript#163).
Why it matters
Every Stackbilt worker instrumented with this library (stackbilt-web, edge-auth, tarotscript, img-forge, aegis, pro-tier customer workers) was rewriting the same ~30-line middleware. Centralizing it means bug fixes propagate automatically, new workers integrate in 2 lines instead of 30, and deep code (interpreters, LLM providers, scaffold materializers) can now participate in tracing without taking a hard dep on observability.
AsyncLocalStorage notes
Requires `nodejs_als` or `nodejs_compat` compat flag in `wrangler.toml`. When unavailable, `getActiveSpan()` returns `undefined` and `runWithSpan` becomes an identity wrapper — no crashes, just no-op.
honoTracing features
Test plan
Depends on
Independent of #7 (span-drops fix in #8) — they touch different methods, can land in either order.
Closes #3
Closes #4
🤖 Generated with Claude Code