feat: inject traceparent meta tag when OpenTelemetry is active (#3729)

Closes #2830

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bartek Iwańczuk
2026-03-29 18:51:12 +00:00
committed by GitHub
co-authored by Claude Opus 4.6
parent 92e07f03b3
commit 075dfb73b2
2 changed files with 39 additions and 0 deletions
@@ -35,6 +35,7 @@ import { getCodeFrame } from "../../dev/middlewares/error_overlay/code_frame.ts"
import { escapeScript } from "../../utils.ts";
import { HeadContext } from "../head.ts";
import { useContext } from "preact/hooks";
import { isSpanContextValid, trace } from "@opentelemetry/api";
interface InternalPreactOptions extends PreactOptions {
[OptionsType.ATTR](name: string, value: unknown): string | void;
@@ -295,6 +296,22 @@ options[OptionsType.DIFF] = (vnode) => {
}
}
// Inject W3C traceparent meta tag when OpenTelemetry is active,
// enabling client-side tracing to connect to the server span.
const activeSpan = trace.getActiveSpan();
if (activeSpan) {
const spanCtx = activeSpan.spanContext();
if (isSpanContextValid(spanCtx)) {
const flags = (spanCtx.traceFlags & 1) ? "01" : "00";
const traceparent =
`00-${spanCtx.traceId}-${spanCtx.spanId}-${flags}`;
items.push(
// deno-lint-ignore no-explicit-any
h("meta", { name: "traceparent", content: traceparent }) as any,
);
}
}
// deno-lint-ignore no-explicit-any
items.push(h(RemainingHead, null) as VNode<any>);