autotel-cloudflare
Version:
The #1 OpenTelemetry package for Cloudflare Workers - complete bindings coverage, native CF OTel integration, advanced sampling
32 lines (31 loc) • 1.15 kB
JavaScript
//#region src/native/native-tracing.ts
function readTracing(carrier) {
const tracing = carrier?.tracing;
return typeof tracing?.enterSpan === "function" ? tracing : void 0;
}
/**
* Returns `true` when Cloudflare native custom-span tracing is available on the
* given ExecutionContext (i.e. tracing is enabled for this Worker).
*/
function isNativeTracingAvailable(ctx) {
return readTracing(ctx) !== void 0;
}
/**
* Wrap Cloudflare's `ctx.tracing` as an autotel {@link NativeTracer}, or return
* `null` when native tracing is unavailable on this context.
*
* @param correlationId Optional per-request id (e.g. the `cf-ray` header)
* surfaced as `ctx.correlationId` and a `correlation.id` span attribute, so
* logs, custom spans, and the Cloudflare dashboard share one queryable key
* today — before Cloudflare exposes in-code trace/span ids.
*/
function getNativeTracerFromCtx(ctx, correlationId) {
const tracing = readTracing(ctx);
if (!tracing) return null;
return {
correlationId,
enterSpan: (name, callback) => tracing.enterSpan(name, callback)
};
}
//#endregion
export { isNativeTracingAvailable as n, getNativeTracerFromCtx as t };