UNPKG

autotel-cloudflare

Version:

The #1 OpenTelemetry package for Cloudflare Workers - complete bindings coverage, native CF OTel integration, advanced sampling

65 lines (63 loc) 2.17 kB
import { f as member } from "./values-BC6rdpGG.js"; import { getExecutionLogger } from "autotel-edge/logger"; //#region src/execution-logger.ts function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } function collectHeaders(headers, include) { if (!include || include.length === 0) return void 0; const allowlist = new Set(include.map((h) => h.toLowerCase())); const out = {}; headers.forEach((value, key) => { if (allowlist.has(key.toLowerCase())) out[key] = value; }); return Object.keys(out).length > 0 ? out : void 0; } function pickCfContext(request) { const cf = member(request, "cf"); if (!isRecord(cf)) return {}; const out = {}; if (typeof cf.colo === "string") out.colo = cf.colo; if (typeof cf.country === "string") out.country = cf.country; if (typeof cf.asn === "number") out.asn = cf.asn; if (typeof cf.city === "string") out.city = cf.city; if (typeof cf.region === "string") out.region = cf.region; return out; } /** * Create an execution logger pre-populated with common request context. * Best used from fetch handlers that already run inside autotel span context. */ function createWorkersLogger(request, options = {}, ctx) { const log = getExecutionLogger(ctx); const url = new URL(request.url); const cfRay = request.headers.get("cf-ray") ?? void 0; const traceparent = request.headers.get("traceparent") ?? void 0; log.set({ request: { method: request.method, path: url.pathname, url: request.url, requestId: options.requestId ?? cfRay, headers: collectHeaders(request.headers, options.headers) }, cfRay, traceparent, ...pickCfContext(request) }); return log; } function getRequestLogger(ctx, options) { return getExecutionLogger(ctx, options); } function getQueueLogger(ctx, options) { return getExecutionLogger(ctx, options); } function getWorkflowLogger(ctx, options) { return getExecutionLogger(ctx, options); } function getActorLogger(ctx, options) { return getExecutionLogger(ctx, options); } //#endregion export { getWorkflowLogger as a, getRequestLogger as i, getActorLogger as n, getQueueLogger as r, createWorkersLogger as t };