UNPKG

autotel-cloudflare

Version:

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

63 lines (61 loc) 2.07 kB
import { getExecutionLogger } from 'autotel-edge/logger'; // 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 = Reflect.get(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; } 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); } export { createWorkersLogger, getActorLogger, getQueueLogger, getRequestLogger, getWorkflowLogger }; //# sourceMappingURL=chunk-RVVMMPWN.js.map //# sourceMappingURL=chunk-RVVMMPWN.js.map