-
-
Notifications
You must be signed in to change notification settings - Fork 862
presets(cloudflare): bridge tracing channel events to observability custom spans #4413
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
Open
RihanArfan
wants to merge
7
commits into
main
Choose a base branch
from
cloudflare-tracingchannel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f01479
presets(cloudflare): bridge tracing channel events to observability cβ¦
RihanArfan 4b495d2
Merge branch 'main' into cloudflare-tracingchannel
pi0 06d5d05
fix(telemetry): report operations that throw synchronously
RihanArfan 7e90370
feat(cloudflare): open observability spans via tracing channel onStarβ¦
RihanArfan cc93487
test(telemetry): cover subscribeTracedChannels
RihanArfan e259c68
fix(cloudflare): record exceptions even when the describer fails at cβ¦
RihanArfan 0668713
fix(cloudflare): record exceptions even when the describer fails at cβ¦
RihanArfan 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
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,85 @@ | ||
| import { definePlugin } from "nitro"; | ||
| // Handle older compatibility dates without the custom-spans API | ||
| import * as cloudflare from "cloudflare:workers"; | ||
| import type { Span, Tracing } from "@cloudflare/workers-types"; | ||
| import type { IAnyValue } from "#nitro/runtime/telemetry/types"; | ||
| import { subscribeTracedChannels } from "#nitro/runtime/telemetry/subscribe"; | ||
|
|
||
| // https://developers.cloudflare.com/workers/observability/traces/custom-spans/ | ||
| const tracing = (cloudflare as { tracing?: Tracing }).tracing; | ||
|
|
||
| interface PendingSpan { | ||
| span: Span; | ||
| close: () => void; | ||
| } | ||
|
|
||
| /** | ||
| * Exports Nitro tracing-channel events as Cloudflare Workers custom spans, | ||
| * alongside Cloudflare's automatic instrumentation (fetch, KV, D1, β¦) | ||
| */ | ||
| export default definePlugin(() => { | ||
| if (typeof tracing?.enterSpan !== "function") return; | ||
|
|
||
| subscribeTracedChannels<PendingSpan>( | ||
| (info, _startTimeUnixNano, error, entry) => { | ||
| if (!entry) return; | ||
| try { | ||
| // Skip attribute work for unsampled requests (`head_sampling_rate`). | ||
| if (entry.span.isTraced) { | ||
| // `info` is undefined when the describer failed on the completed | ||
| // payload β the error is still recorded on the (already named) span. | ||
| if (info) { | ||
| for (const { key, value } of info.attributes) { | ||
| entry.span.setAttribute(key, attributeValue(value)); | ||
| } | ||
| } | ||
| if (error !== undefined) { | ||
| recordException(entry.span, error); | ||
| } | ||
| } | ||
| } finally { | ||
| entry.close(); | ||
| } | ||
| }, | ||
| { | ||
| onStart(info) { | ||
| let close!: () => void; | ||
| const done = new Promise<void>((resolve) => { | ||
| close = resolve; | ||
| }); | ||
| let entry: PendingSpan | undefined; | ||
| tracing.enterSpan(info.name, (span) => { | ||
| entry = { span, close }; | ||
| return done; | ||
| }); | ||
| return entry; | ||
| }, | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| /** OTLP `IAnyValue` (from the shared describers) β Cloudflare attribute value. */ | ||
| function attributeValue(value: IAnyValue): string | number | boolean | undefined { | ||
| if (value.stringValue != null) return value.stringValue; | ||
| if (value.intValue != null) return value.intValue; | ||
| if (value.doubleValue != null) return value.doubleValue; | ||
| if (value.boolValue != null) return value.boolValue; | ||
| } | ||
|
|
||
| /** | ||
| * OTEL exception semconv, flattened onto span attributes β the Cloudflare API | ||
| * has no span events, and `setOutcome` is not available yet. | ||
| */ | ||
| function recordException(span: Span, error: unknown): void { | ||
| const err = error as Partial<Error> | undefined; | ||
| if (typeof err?.name === "string") { | ||
| span.setAttribute("exception.type", err.name); | ||
| } | ||
| span.setAttribute( | ||
| "exception.message", | ||
| typeof err?.message === "string" ? err.message : String(error) | ||
| ); | ||
| if (typeof err?.stack === "string") { | ||
| span.setAttribute("exception.stacktrace", err.stack); | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -47,6 +47,9 @@ const pendingSpans = new Map<string, Span[]>(); | |
| */ | ||
| export default definePlugin(() => { | ||
| subscribeTracedChannels((info, startTimeUnixNano, error) => { | ||
| // Describer failed on the completed payload β nothing to report. | ||
| if (!info) return; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like a bug. Why are we even calling callback in this case? |
||
|
|
||
| const context = (globalThis as Record<symbol, RequestContextReader | undefined>)[ | ||
| REQUEST_CONTEXT_SYMBOL | ||
| ]?.get?.(); | ||
|
|
||
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.
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.