autotel-edge 3.16.3 → 3.16.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -252,6 +252,27 @@ init((env) => ({
252
252
  }))
253
253
  ```
254
254
 
255
+ ### Fetch Route Controls
256
+
257
+ Use fetch handler route controls to include/exclude paths and set per-route service names.
258
+
259
+ ```typescript
260
+ init({
261
+ service: { name: 'edge-app' },
262
+ exporter: { url: 'https://otlp.example.com/v1/traces' },
263
+ handlers: {
264
+ fetch: {
265
+ include: ['/api/**'],
266
+ exclude: ['/api/internal/**', '/health'],
267
+ routes: {
268
+ '/api/auth/**': { service: 'auth-service' },
269
+ '/api/**': { service: 'api-service' },
270
+ },
271
+ },
272
+ },
273
+ })
274
+ ```
275
+
255
276
  ## API Reference
256
277
 
257
278
  ### Core Functions
@@ -1,4 +1,4 @@
1
- import { SpanStatusCode, trace } from '@opentelemetry/api';
1
+ import { trace, SpanStatusCode } from '@opentelemetry/api';
2
2
 
3
3
  // src/execution-logger.ts
4
4
 
@@ -27,6 +27,35 @@ function setSpanName(span, name) {
27
27
  }
28
28
 
29
29
  // src/execution-logger.ts
30
+ var POST_EMIT_FORK_HINT = "For intentional background work tied to this execution, use log.fork('label', fn) when available.";
31
+ function warnPostEmit(method, detail) {
32
+ console.warn(
33
+ `[autotel-edge] ${method} called after the execution event was emitted - ${detail} This data will not appear in observability. ${POST_EMIT_FORK_HINT}`
34
+ );
35
+ }
36
+ function mergeInto(target, source) {
37
+ for (const key in source) {
38
+ const sourceVal = source[key];
39
+ if (sourceVal === void 0) continue;
40
+ const targetVal = target[key];
41
+ if (sourceVal !== null && typeof sourceVal === "object" && !Array.isArray(sourceVal) && targetVal !== null && typeof targetVal === "object" && !Array.isArray(targetVal)) {
42
+ mergeInto(
43
+ targetVal,
44
+ sourceVal
45
+ );
46
+ } else if (Array.isArray(targetVal) && Array.isArray(sourceVal)) {
47
+ target[key] = [...targetVal, ...sourceVal];
48
+ } else {
49
+ target[key] = sourceVal;
50
+ }
51
+ }
52
+ }
53
+ function generateCorrelationId() {
54
+ if (typeof globalThis.crypto !== "undefined" && typeof globalThis.crypto.randomUUID === "function") {
55
+ return globalThis.crypto.randomUUID();
56
+ }
57
+ return `exec-${Date.now()}-${Math.random().toString(16).slice(2)}`;
58
+ }
30
59
  function toAttributeValue(value) {
31
60
  if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
32
61
  return value;
@@ -119,6 +148,8 @@ function resolveContext(ctx) {
119
148
  function getExecutionLogger(ctx, options) {
120
149
  const activeContext = resolveContext(ctx);
121
150
  let contextState = {};
151
+ let emitted = false;
152
+ let lastSnapshot = null;
122
153
  const addLogEvent = (level, message, fields) => {
123
154
  const attrs = fields ? flattenToAttributes(fields) : void 0;
124
155
  activeContext.addEvent(`log.${level}`, {
@@ -126,36 +157,46 @@ function getExecutionLogger(ctx, options) {
126
157
  ...attrs
127
158
  });
128
159
  };
160
+ const sealCheck = (method, keys) => {
161
+ if (emitted) {
162
+ warnPostEmit(
163
+ method,
164
+ `Keys dropped: ${keys.length ? keys.join(", ") : "(empty)"}.`
165
+ );
166
+ }
167
+ };
129
168
  return {
130
169
  set(fields) {
131
- contextState = {
132
- ...contextState,
133
- ...fields
134
- };
170
+ sealCheck("log.set()", Object.keys(fields));
171
+ if (emitted) return;
172
+ mergeInto(contextState, fields);
135
173
  activeContext.setAttributes(flattenToAttributes(fields));
136
174
  },
137
175
  info(message, fields) {
176
+ const keys = fields ? ["message", ...Object.keys(fields).filter((k) => k !== "requestLogs")] : ["message"];
177
+ sealCheck("log.info()", keys);
178
+ if (emitted) return;
138
179
  addLogEvent("info", message, fields);
139
180
  if (fields) {
140
- contextState = {
141
- ...contextState,
142
- ...fields
143
- };
181
+ mergeInto(contextState, fields);
144
182
  activeContext.setAttributes(flattenToAttributes(fields));
145
183
  }
146
184
  },
147
185
  warn(message, fields) {
186
+ const keys = fields ? ["message", ...Object.keys(fields).filter((k) => k !== "requestLogs")] : ["message"];
187
+ sealCheck("log.warn()", keys);
188
+ if (emitted) return;
148
189
  addLogEvent("warn", message, fields);
149
190
  activeContext.setAttribute("autotel.log.level", "warn");
150
191
  if (fields) {
151
- contextState = {
152
- ...contextState,
153
- ...fields
154
- };
192
+ mergeInto(contextState, fields);
155
193
  activeContext.setAttributes(flattenToAttributes(fields));
156
194
  }
157
195
  },
158
196
  error(error, fields) {
197
+ const keys = fields ? [...Object.keys(fields), "error"] : ["error"];
198
+ sealCheck("log.error()", keys);
199
+ if (emitted) return;
159
200
  const err = typeof error === "string" ? new Error(error) : error;
160
201
  activeContext.recordException(err);
161
202
  activeContext.setStatus({
@@ -165,10 +206,7 @@ function getExecutionLogger(ctx, options) {
165
206
  activeContext.setAttributes(getErrorAttributes(err));
166
207
  addLogEvent("error", err.message, fields);
167
208
  if (fields) {
168
- contextState = {
169
- ...contextState,
170
- ...fields
171
- };
209
+ mergeInto(contextState, fields);
172
210
  activeContext.setAttributes(flattenToAttributes(fields));
173
211
  }
174
212
  activeContext.setAttribute("autotel.log.level", "error");
@@ -177,6 +215,10 @@ function getExecutionLogger(ctx, options) {
177
215
  return { ...contextState };
178
216
  },
179
217
  emitNow(overrides) {
218
+ if (emitted) {
219
+ warnPostEmit("log.emitNow()", "Ignoring duplicate emit.");
220
+ return lastSnapshot;
221
+ }
180
222
  const mergedContext = {
181
223
  ...contextState,
182
224
  ...overrides ?? {}
@@ -198,11 +240,42 @@ function getExecutionLogger(ctx, options) {
198
240
  console.warn("[autotel-edge] execution logger onEmit failed:", error);
199
241
  });
200
242
  }
243
+ emitted = true;
244
+ lastSnapshot = snapshot;
201
245
  return snapshot;
246
+ },
247
+ fork(label, fn) {
248
+ const parentCorrelationId = activeContext.correlationId;
249
+ if (typeof parentCorrelationId !== "string" || parentCorrelationId.length === 0) {
250
+ throw new Error(
251
+ "[autotel-edge] log.fork() requires the parent logger to have a correlationId. Ensure execution context was created by autotel trace instrumentation."
252
+ );
253
+ }
254
+ const tracer = trace.getTracer("autotel-edge.execution-logger");
255
+ void tracer.startActiveSpan(`execution.fork:${label}`, (childSpan) => {
256
+ const childContext = {
257
+ ...createTraceContext(childSpan),
258
+ correlationId: generateCorrelationId()
259
+ };
260
+ const childLog = getExecutionLogger(childContext);
261
+ childLog.set({
262
+ operation: label,
263
+ _parentCorrelationId: parentCorrelationId
264
+ });
265
+ return Promise.resolve().then(() => fn()).then(() => {
266
+ childLog.emitNow();
267
+ }).catch((err) => {
268
+ const error = err instanceof Error ? err : new Error(String(err));
269
+ childLog.error(error);
270
+ childLog.emitNow();
271
+ }).finally(() => {
272
+ childSpan.end();
273
+ });
274
+ });
202
275
  }
203
276
  };
204
277
  }
205
278
 
206
279
  export { createTraceContext, getExecutionLogger, setSpanName };
207
- //# sourceMappingURL=chunk-A2AH5BBJ.js.map
208
- //# sourceMappingURL=chunk-A2AH5BBJ.js.map
280
+ //# sourceMappingURL=chunk-FCYVL2I7.js.map
281
+ //# sourceMappingURL=chunk-FCYVL2I7.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/core/trace-context.ts","../src/execution-logger.ts"],"names":["otelTrace"],"mappings":";;;;;AAgBA,IAAM,WAAA,uBAAkB,OAAA,EAAsB;AAsDvC,SAAS,mBAAmB,IAAA,EAA0B;AAC3D,EAAA,MAAM,WAAA,GAAc,KAAK,WAAA,EAAY;AACrC,EAAA,OAAO;AAAA,IACL,SAAS,WAAA,CAAY,OAAA;AAAA,IACrB,QAAQ,WAAA,CAAY,MAAA;AAAA,IACpB,aAAA,EAAe,WAAA,CAAY,OAAA,CAAQ,KAAA,CAAM,GAAG,EAAE,CAAA;AAAA,IAC9C,eAAA,EAAiB,WAAA,CAAY,GAAA,CAAI,IAAI,CAAA;AAAA,IACrC,YAAA,EAAc,IAAA,CAAK,YAAA,CAAa,IAAA,CAAK,IAAI,CAAA;AAAA,IACzC,aAAA,EAAe,IAAA,CAAK,aAAA,CAAc,IAAA,CAAK,IAAI,CAAA;AAAA,IA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AmB,SAAS,CAAA;AAAA,UAC/B,eAAe,qBAAA;AAAsB,SACvC;AAEA,QAAA,MAAM,QAAA,GAAW,mBAAmB,YAAY,CAAA;AAChD,QAAA,QAAA,CAAS,GAAA,CAAI;AAAA,UACX,SAAA,EAAW,KAAA;AAAA,UACX,oBAAA,EAAsB;AAAA,SACvB,CAAA;AAED,QAAA,OAAO,OAAA,CAAQ,SAAQ,CACpB,IAAA,CAAK,MAAM,EAAA,EAAI,CAAA,CACf,IAAA,CAAK,MAAM;AACV,UAAA,QAAA,CAAS,OAAA,EAAQ;AAAA,QACnB,CAAC,CAAA,CACA,KAAA,CAAM,CAAC,GAAA,KAAiB;AACvB,UAAA,MAAM,KAAA,GAAQ,eAAe,KAAA,GAAQ,GAAA,GAAM,IAAI,KAAA,CAAM,MAAA,CAAO,GAAG,CAAC,CAAA;AAChE,UAAA,QAAA,CAAS,MAAM,KAAK,CAAA;AACpB,UAAA,QAAA,CAAS,OAAA,EAAQ;AAAA,QACnB,CAAC,CAAA,CACA,OAAA,CAAQ,MAAM;AACb,UAAA,SAAA,CAAU,GAAA,EAAI;AAAA,QAChB,CAAC,CAAA;AAAA,MACL,CAAC,CAAA;AAAA,IACH;AAAA,GACF;AACF","file":"chunk-FCYVL2I7.js","sourcesContent":["/**\n * Trace context types and utilities\n */\n\nimport type {\n AttributeValue,\n Link,\n Span,\n SpanStatusCode,\n TimeInput,\n} from '@opentelemetry/api';\n\n/**\n * WeakMap to store span names for active spans.\n * Enables retrieving span names for correlation helpers.\n */\nconst spanNameMap = new WeakMap<Span, string>();\n\n/**\n * Base trace context containing trace identifiers\n */\nexport interface TraceContextBase {\n traceId: string;\n spanId: string;\n correlationId: string;\n 'code.function'?: string;\n}\n\n/**\n * Span methods available on trace context\n */\nexport interface SpanMethods {\n /** Set a single attribute on the span */\n setAttribute(key: string, value: AttributeValue): void;\n /** Set multiple attributes on the span */\n setAttributes(attrs: Record<string, AttributeValue>): void;\n /** Set the status of the span */\n setStatus(status: { code: SpanStatusCode; message?: string }): void;\n /** Record an exception on the span */\n recordException(exception: Error, time?: TimeInput): void;\n /** Add an event to the span (for logging milestones/checkpoints) */\n addEvent(\n name: string,\n attributesOrStartTime?: Record<string, AttributeValue> | TimeInput,\n startTime?: TimeInput,\n ): void;\n /** Add a link to another span */\n addLink(link: Link): void;\n /** Add multiple links to other spans */\n addLinks(links: Link[]): void;\n /** Update the span name dynamically */\n updateName(name: string): void;\n /** Check if the span is recording */\n isRecording(): boolean;\n}\n\n/**\n * Complete trace context that merges base context and span methods\n *\n * This is the ctx parameter passed to factory functions in trace().\n * It provides access to trace IDs and span manipulation methods.\n */\nexport type TraceContext = TraceContextBase & SpanMethods;\n\n/**\n * Create a TraceContext from an OpenTelemetry Span\n *\n * This utility extracts trace context information from a span\n * and provides span manipulation methods in a consistent format.\n */\nexport function createTraceContext(span: Span): TraceContext {\n const spanContext = span.spanContext();\n return {\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n correlationId: spanContext.traceId.slice(0, 16),\n 'code.function': spanNameMap.get(span),\n setAttribute: span.setAttribute.bind(span),\n setAttributes: span.setAttributes.bind(span),\n setStatus: span.setStatus.bind(span),\n recordException: span.recordException.bind(span),\n addEvent: span.addEvent.bind(span),\n addLink: span.addLink.bind(span),\n addLinks: span.addLinks.bind(span),\n updateName: span.updateName.bind(span),\n isRecording: span.isRecording.bind(span),\n };\n}\n\n/**\n * Store the span name for later retrieval via trace context helpers.\n */\nexport function setSpanName(span: Span, name: string): void {\n spanNameMap.set(span, name);\n}\n","import { trace as otelTrace, SpanStatusCode } from '@opentelemetry/api';\nimport type { AttributeValue } from '@opentelemetry/api';\nimport type { TraceContext } from './functional';\nimport { createTraceContext } from './core/trace-context';\n\nconst POST_EMIT_FORK_HINT =\n \"For intentional background work tied to this execution, use log.fork('label', fn) when available.\";\n\nfunction warnPostEmit(method: string, detail: string): void {\n console.warn(\n `[autotel-edge] ${method} called after the execution event was emitted - ${detail} This data will not appear in observability. ${POST_EMIT_FORK_HINT}`,\n );\n}\n\nfunction mergeInto(\n target: Record<string, unknown>,\n source: Record<string, unknown>,\n): void {\n for (const key in source) {\n const sourceVal = source[key];\n if (sourceVal === undefined) continue;\n const targetVal = target[key];\n if (\n sourceVal !== null &&\n typeof sourceVal === 'object' &&\n !Array.isArray(sourceVal) &&\n targetVal !== null &&\n typeof targetVal === 'object' &&\n !Array.isArray(targetVal)\n ) {\n mergeInto(\n targetVal as Record<string, unknown>,\n sourceVal as Record<string, unknown>,\n );\n } else if (Array.isArray(targetVal) && Array.isArray(sourceVal)) {\n target[key] = [...targetVal, ...sourceVal];\n } else {\n target[key] = sourceVal;\n }\n }\n}\n\nfunction generateCorrelationId(): string {\n if (\n typeof globalThis.crypto !== 'undefined' &&\n typeof globalThis.crypto.randomUUID === 'function'\n ) {\n return globalThis.crypto.randomUUID();\n }\n\n return `exec-${Date.now()}-${Math.random().toString(16).slice(2)}`;\n}\n\nexport interface ExecutionLogger {\n set(fields: Record<string, unknown>): void;\n info(message: string, fields?: Record<string, unknown>): void;\n warn(message: string, fields?: Record<string, unknown>): void;\n error(error: Error | string, fields?: Record<string, unknown>): void;\n getContext(): Record<string, unknown>;\n emitNow(overrides?: Record<string, unknown>): ExecutionLogSnapshot;\n fork(label: string, fn: () => void | Promise<void>): void;\n}\n\nexport interface ExecutionLogSnapshot {\n timestamp: string;\n traceId: string;\n spanId: string;\n correlationId: string;\n context: Record<string, unknown>;\n}\n\nexport interface ExecutionLoggerOptions {\n onEmit?: (snapshot: ExecutionLogSnapshot) => void | Promise<void>;\n}\n\nfunction toAttributeValue(value: unknown): AttributeValue | undefined {\n if (\n typeof value === 'string' ||\n typeof value === 'number' ||\n typeof value === 'boolean'\n ) {\n return value;\n }\n\n if (Array.isArray(value)) {\n if (\n value.every((item) => typeof item === 'string') ||\n value.every((item) => typeof item === 'number') ||\n value.every((item) => typeof item === 'boolean')\n ) {\n return value as AttributeValue;\n }\n\n try {\n return JSON.stringify(value);\n } catch {\n return '<serialization-failed>';\n }\n }\n\n if (value instanceof Date) {\n return value.toISOString();\n }\n\n if (value instanceof Error) {\n return value.message;\n }\n\n return undefined;\n}\n\nfunction flattenToAttributes(\n fields: Record<string, unknown>,\n prefix = '',\n): Record<string, AttributeValue> {\n const out: Record<string, AttributeValue> = {};\n const seen = new WeakSet<object>();\n\n function flatten(obj: Record<string, unknown>, currentPrefix: string): void {\n for (const [key, value] of Object.entries(obj)) {\n if (value == null) continue;\n\n const nextKey = currentPrefix ? `${currentPrefix}.${key}` : key;\n const attr = toAttributeValue(value);\n\n if (attr !== undefined) {\n out[nextKey] = attr;\n continue;\n }\n\n if (typeof value === 'object' && value.constructor === Object) {\n if (seen.has(value)) {\n out[nextKey] = '<circular-reference>';\n continue;\n }\n\n seen.add(value);\n flatten(value as Record<string, unknown>, nextKey);\n continue;\n }\n\n try {\n out[nextKey] = JSON.stringify(value);\n } catch {\n out[nextKey] = '<serialization-failed>';\n }\n }\n }\n\n flatten(fields, prefix);\n return out;\n}\n\nfunction getErrorAttributes(error: Error): Record<string, AttributeValue> {\n const attributes: Record<string, AttributeValue> = {\n 'error.type': error.name || 'Error',\n 'error.message': error.message,\n };\n\n if (error.stack) {\n attributes['error.stack'] = error.stack;\n }\n\n const structured = error as Error & {\n why?: string;\n fix?: string;\n link?: string;\n code?: string | number;\n status?: number;\n details?: Record<string, unknown>;\n };\n\n if (structured.why) attributes['error.why'] = structured.why;\n if (structured.fix) attributes['error.fix'] = structured.fix;\n if (structured.link) attributes['error.link'] = structured.link;\n if (structured.code !== undefined) {\n attributes['error.code'] =\n typeof structured.code === 'string'\n ? structured.code\n : String(structured.code);\n }\n if (structured.status !== undefined) {\n attributes['error.status'] = structured.status;\n }\n if (structured.details) {\n Object.assign(\n attributes,\n flattenToAttributes(structured.details, 'error.details'),\n );\n }\n\n return attributes;\n}\n\nfunction resolveContext(ctx?: TraceContext): TraceContext {\n if (ctx) return ctx;\n\n const span = otelTrace.getActiveSpan();\n if (!span) {\n throw new Error(\n '[autotel-edge] getExecutionLogger() requires an active span or explicit TraceContext. Wrap your handler with trace() or pass ctx directly.',\n );\n }\n\n return createTraceContext(span);\n}\n\nexport function getExecutionLogger(\n ctx?: TraceContext,\n options?: ExecutionLoggerOptions,\n): ExecutionLogger {\n const activeContext = resolveContext(ctx);\n let contextState: Record<string, unknown> = {};\n let emitted = false;\n let lastSnapshot: ExecutionLogSnapshot | null = null;\n\n const addLogEvent = (\n level: 'info' | 'warn' | 'error',\n message: string,\n fields?: Record<string, unknown>,\n ) => {\n const attrs = fields ? flattenToAttributes(fields) : undefined;\n activeContext.addEvent(`log.${level}`, {\n message,\n ...attrs,\n });\n };\n\n const sealCheck = (method: string, keys: string[]): void => {\n if (emitted) {\n warnPostEmit(\n method,\n `Keys dropped: ${keys.length ? keys.join(', ') : '(empty)'}.`,\n );\n }\n };\n\n return {\n set(fields: Record<string, unknown>) {\n sealCheck('log.set()', Object.keys(fields));\n if (emitted) return;\n mergeInto(contextState, fields);\n activeContext.setAttributes(flattenToAttributes(fields));\n },\n\n info(message: string, fields?: Record<string, unknown>) {\n const keys = fields\n ? ['message', ...Object.keys(fields).filter((k) => k !== 'requestLogs')]\n : ['message'];\n sealCheck('log.info()', keys);\n if (emitted) return;\n addLogEvent('info', message, fields);\n if (fields) {\n mergeInto(contextState, fields);\n activeContext.setAttributes(flattenToAttributes(fields));\n }\n },\n\n warn(message: string, fields?: Record<string, unknown>) {\n const keys = fields\n ? ['message', ...Object.keys(fields).filter((k) => k !== 'requestLogs')]\n : ['message'];\n sealCheck('log.warn()', keys);\n if (emitted) return;\n addLogEvent('warn', message, fields);\n activeContext.setAttribute('autotel.log.level', 'warn');\n if (fields) {\n mergeInto(contextState, fields);\n activeContext.setAttributes(flattenToAttributes(fields));\n }\n },\n\n error(error: Error | string, fields?: Record<string, unknown>) {\n const keys = fields ? [...Object.keys(fields), 'error'] : ['error'];\n sealCheck('log.error()', keys);\n if (emitted) return;\n const err = typeof error === 'string' ? new Error(error) : error;\n\n activeContext.recordException(err);\n activeContext.setStatus({\n code: SpanStatusCode.ERROR,\n message: err.message,\n });\n activeContext.setAttributes(getErrorAttributes(err));\n addLogEvent('error', err.message, fields);\n\n if (fields) {\n mergeInto(contextState, fields);\n activeContext.setAttributes(flattenToAttributes(fields));\n }\n\n activeContext.setAttribute('autotel.log.level', 'error');\n },\n\n getContext() {\n return { ...contextState };\n },\n\n emitNow(overrides?: Record<string, unknown>): ExecutionLogSnapshot {\n if (emitted) {\n warnPostEmit('log.emitNow()', 'Ignoring duplicate emit.');\n return lastSnapshot as ExecutionLogSnapshot;\n }\n\n const mergedContext = {\n ...contextState,\n ...(overrides ?? {}),\n };\n const flattened = flattenToAttributes(mergedContext);\n activeContext.setAttributes(flattened);\n\n const snapshot: ExecutionLogSnapshot = {\n timestamp: new Date().toISOString(),\n traceId: activeContext.traceId,\n spanId: activeContext.spanId,\n correlationId: activeContext.correlationId,\n context: mergedContext,\n };\n\n activeContext.addEvent('log.emit.manual', {\n ...flattened,\n });\n\n if (options?.onEmit) {\n Promise.resolve(options.onEmit(snapshot)).catch((error) => {\n console.warn('[autotel-edge] execution logger onEmit failed:', error);\n });\n }\n\n emitted = true;\n lastSnapshot = snapshot;\n return snapshot;\n },\n\n fork(label: string, fn: () => void | Promise<void>): void {\n const parentCorrelationId = activeContext.correlationId;\n if (\n typeof parentCorrelationId !== 'string' ||\n parentCorrelationId.length === 0\n ) {\n throw new Error(\n '[autotel-edge] log.fork() requires the parent logger to have a correlationId. ' +\n 'Ensure execution context was created by autotel trace instrumentation.',\n );\n }\n\n const tracer = otelTrace.getTracer('autotel-edge.execution-logger');\n void tracer.startActiveSpan(`execution.fork:${label}`, (childSpan) => {\n const childContext: TraceContext = {\n ...createTraceContext(childSpan),\n correlationId: generateCorrelationId(),\n };\n\n const childLog = getExecutionLogger(childContext);\n childLog.set({\n operation: label,\n _parentCorrelationId: parentCorrelationId,\n });\n\n return Promise.resolve()\n .then(() => fn())\n .then(() => {\n childLog.emitNow();\n })\n .catch((err: unknown) => {\n const error = err instanceof Error ? err : new Error(String(err));\n childLog.error(error);\n childLog.emitNow();\n })\n .finally(() => {\n childSpan.end();\n });\n });\n },\n };\n}\n"]}
@@ -5,7 +5,7 @@ import { ParentBasedSampler, AlwaysOnSampler } from '@opentelemetry/sdk-trace-ba
5
5
  import { createContextKey, context } from '@opentelemetry/api';
6
6
 
7
7
  // src/core/exporter.ts
8
- var PACKAGE_VERSION = "3.16.3";
8
+ var PACKAGE_VERSION = "3.16.4";
9
9
  var defaultHeaders = {
10
10
  accept: "application/json",
11
11
  "content-type": "application/json",
@@ -306,5 +306,5 @@ function createInitialiser(config) {
306
306
  }
307
307
 
308
308
  export { OTLPExporter, createInitialiser, getActiveConfig, parseConfig, setConfig };
309
- //# sourceMappingURL=chunk-D7K4WLCX.js.map
310
- //# sourceMappingURL=chunk-D7K4WLCX.js.map
309
+ //# sourceMappingURL=chunk-UJGJ663T.js.map
310
+ //# sourceMappingURL=chunk-UJGJ663T.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/core/exporter.ts","../src/types.ts","../src/core/spanprocessor.ts","../src/core/config.ts"],"names":["api_context"],"mappings":";;;;;;;AAiBA,IAAM,eAAA,GAAkB,QAAA;AAExB,IAAM,cAAA,GAAyC;AAAA,EAC7C,MAAA,EAAQ,kBAAA;AAAA,EACR,cAAA,EAAgB,kBAAA;AAAA,EAChB,YAAA,EAAc,iBAAiB,eAAe,CAAA;AAChD,CAAA;AAKO,IAAM,eAAN,MAA2C;AAAA,EACxC,OAAA;AAAA,EACA,GAAA;AAAA,EAER,YAAY,MAAA,EAA4B;AACtC,IAAA,IAAA,CAAK,MAAM,MAAA,CAAO,GAAA;AAClB,IAAA,IAAA,CAAK,UAAU,MAAA,CAAO,MAAA,CAAO,EAAC,EAAG,cAAA,EAAgB,OAAO,OAAO,CAAA;AAAA,EACjE;AAAA,EAEA,MAAA,CAAO,OAAc,cAAA,EAAsD;AACzE,IAAA,IAAA,CAAK,OAAA,CAA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AA,EAAc;AAChB,IAAA,OAAO,IAAI,kBAAA,CAAmB,EAAE,IAAA,EAAM,cAAqB,CAAA;AAAA,EAC7D;AAEA,EAAA,OAAO,YAAA;AACT;AAKO,SAAS,kBAAkB,MAAA,EAA0C;AAC1E,EAAA,IAAI,OAAO,WAAW,UAAA,EAAY;AAChC,IAAA,OAAO,CAAC,KAAK,OAAA,KAAY;AACvB,MAAA,MAAM,IAAA,GAAO,WAAA,CAAY,MAAA,CAAO,GAAA,EAAK,OAAO,CAAC,CAAA;AAC7C,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,EACF,CAAA,MAAO;AACL,IAAA,MAAM,MAAA,GAAS,YAAY,MAAM,CAAA;AACjC,IAAA,OAAO,MAAM,MAAA;AAAA,EACf;AACF","file":"chunk-UJGJ663T.js","sourcesContent":["/**\n * Lightweight OTLP exporter for edge environments\n * Ported and adapted from @microlabs/\n *\n * This exporter is much smaller than the standard @opentelemetry/exporter-trace-otlp-http\n * because it uses fetch() directly instead of Node.js http/https modules.\n */\n\nimport type { ExportResult } from '@opentelemetry/core';\nimport { ExportResultCode } from '@opentelemetry/core';\nimport { OTLPExporterError } from '@opentelemetry/otlp-exporter-base';\nimport { JsonTraceSerializer } from '@opentelemetry/otlp-transformer';\nimport type { SpanExporter } from '@opentelemetry/sdk-trace-base';\nimport type { OTLPExporterConfig } from '../types';\n\n// Version is injected at build time via tsup define\n// This avoids runtime filesystem access which isn't available in edge environments\nconst PACKAGE_VERSION = process.env.AUTOTEL_EDGE_VERSION || '0.1.1';\n\nconst defaultHeaders: Record<string, string> = {\n accept: 'application/json',\n 'content-type': 'application/json',\n 'user-agent': `autotel-edge v${PACKAGE_VERSION}`,\n};\n\n/**\n * Minimal OTLP exporter using fetch()\n */\nexport class OTLPExporter implements SpanExporter {\n private headers: Record<string, string>;\n private url: string;\n\n constructor(config: OTLPExporterConfig) {\n this.url = config.url;\n this.headers = Object.assign({}, defaultHeaders, config.headers);\n }\n\n export(items: any[], resultCallback: (result: ExportResult) => void): void {\n this._export(items)\n .then(() => {\n resultCallback({ code: ExportResultCode.SUCCESS });\n })\n .catch((error) => {\n resultCallback({ code: ExportResultCode.FAILED, error });\n });\n }\n\n private _export(items: any[]): Promise<unknown> {\n return new Promise<void>((resolve, reject) => {\n try {\n this.send(items, resolve, reject);\n } catch (error) {\n reject(error);\n }\n });\n }\n\n send(\n items: any[],\n onSuccess: () => void,\n onError: (error: OTLPExporterError) => void,\n ): void {\n const decoder = new TextDecoder();\n const exportMessage = JsonTraceSerializer.serializeRequest(items);\n\n const body = decoder.decode(exportMessage);\n const params: RequestInit = {\n method: 'POST',\n headers: this.headers,\n body,\n };\n\n fetch(this.url, params)\n .then((response) => {\n if (response.ok) {\n onSuccess();\n } else {\n onError(\n new OTLPExporterError(\n `Exporter received a statusCode: ${response.status}`,\n ),\n );\n }\n })\n .catch((error) => {\n onError(\n new OTLPExporterError(\n `Exception during export: ${error.toString()}`,\n error.code,\n error.stack,\n ),\n );\n });\n }\n\n async shutdown(): Promise<void> {\n // No-op for edge environments\n }\n}\n","/**\n * Shared types for autotel-edge\n */\n\nimport type {\n Attributes,\n Context,\n Span,\n SpanOptions,\n TextMapPropagator,\n} from '@opentelemetry/api';\nimport type {\n ReadableSpan,\n Sampler,\n SpanExporter,\n SpanProcessor,\n} from '@opentelemetry/sdk-trace-base';\n\n// Re-export commonly used types\n\n\n/**\n * Extended SpanOptions with per-span sampler support\n */\nexport interface ExtendedSpanOptions extends SpanOptions {\n sampler?: Sampler;\n}\n\n/**\n * Trigger types for edge handlers\n * Can be a Request or any vendor-specific trigger type\n */\nexport type Trigger =\n | Request\n | DOConstructorTrigger\n | WorkflowTrigger\n | 'do-alarm'\n | unknown;\n\nexport interface DOConstructorTrigger {\n id: string;\n name?: string;\n}\n\nexport interface WorkflowTrigger {\n type: 'workflow';\n name: string;\n}\n\n/**\n * Config types\n */\nexport interface OTLPExporterConfig {\n url: string;\n headers?: Record<string, string>;\n}\n\nexport type ExporterConfig = OTLPExporterConfig | SpanExporter;\n\nexport interface ServiceConfig {\n name: string;\n namespace?: string;\n version?: string;\n}\n\nexport interface ParentRatioSamplingConfig {\n acceptRemote?: boolean;\n ratio: number;\n}\n\ntype HeadSamplerConf = Sampler | ParentRatioSamplingConfig;\n\nexport interface SamplingConfig<HS extends HeadSamplerConf = HeadSamplerConf> {\n headSampler?: HS;\n tailSampler?: TailSampleFn;\n}\n\nexport interface InstrumentationOptions {\n instrumentGlobalFetch?: boolean;\n instrumentGlobalCache?: boolean;\n /**\n * Disable instrumentation entirely (useful for local development)\n * When enabled, the handler is returned as-is without any instrumentation\n * @default false\n */\n disabled?: boolean;\n}\n\n/**\n * Utility types\n */\nexport type OrPromise<T> = T | Promise<T>;\n\n/**\n * Adapter event types\n */\nexport type FunnelStepStatus =\n | 'started'\n | 'completed'\n | 'abandoned'\n | 'failed'\n | (string & {});\n\nexport type OutcomeStatus =\n | 'success'\n | 'failure'\n | 'partial'\n | (string & {});\n\nexport interface EdgeEventBase {\n [key: string]: unknown;\n service: string;\n timestamp: number;\n attributes: Record<string, unknown>;\n traceId?: string;\n spanId?: string;\n correlationId?: string;\n name: string; // Normalized event name for easy access\n}\n\nexport interface EdgeTrackEvent extends EdgeEventBase {\n type: 'event';\n event: string;\n}\n\nexport interface EdgeFunnelStepEvent extends EdgeEventBase {\n type: 'funnel-step';\n funnel: string;\n status: FunnelStepStatus;\n}\n\nexport interface EdgeOutcomeEvent extends EdgeEventBase {\n type: 'outcome';\n operation: string;\n outcome: OutcomeStatus;\n}\n\nexport interface EdgeValueEvent extends EdgeEventBase {\n type: 'value';\n metric: string;\n value: number;\n}\n\nexport type EdgeEvent =\n | EdgeTrackEvent\n | EdgeFunnelStepEvent\n | EdgeOutcomeEvent\n | EdgeValueEvent;\n\nexport type EdgeSubscriber = (event: EdgeEvent) => OrPromise<void>;\n\nexport interface FetcherConfig {\n includeTraceContext?: boolean | ((request: Request) => boolean);\n}\n\nexport interface RouteServiceConfig {\n service: string;\n}\n\nexport interface PostProcessParams {\n /**\n * The request object that was passed to the fetch handler.\n */\n request: Request;\n /**\n * The generated response object.\n */\n response: Response;\n /**\n * A readable version of the span object that can be used to access the span's attributes and events.\n */\n readable: ReadableSpan;\n}\n\nexport interface FetchHandlerConfig {\n /**\n * Whether to enable context propagation for incoming requests to `fetch`.\n * This enables or disables distributed tracing from W3C Trace Context headers.\n * @default true\n */\n acceptTraceContext?: boolean | ((request: Request) => boolean);\n /**\n * Allows further customization of the generated span, based on the request/response data.\n */\n postProcess?: (span: Span, ctx: PostProcessParams) => void;\n /**\n * Route patterns to include for fetch handler instrumentation.\n * Supports glob patterns like '/api/**'.\n * If not set, all routes are included.\n */\n include?: string[];\n /**\n * Route patterns to exclude from fetch handler instrumentation.\n * Supports glob patterns like '/health' and '/_internal/**'.\n * Exclusions take precedence over inclusions.\n */\n exclude?: string[];\n /**\n * Route-specific service mapping.\n * The first matching pattern wins based on object iteration order.\n */\n routes?: Record<string, RouteServiceConfig>;\n}\n\nexport interface HandlerConfig {\n fetch?: FetchHandlerConfig;\n}\n\nexport interface DataSafetyConfig {\n /** Redact query parameters from URL attributes (default: false) */\n redactQueryParams?: boolean;\n /** Control D1 SQL statement capture: 'full' (default), 'obfuscated', or 'off' */\n captureDbStatement?: 'off' | 'obfuscated' | 'full';\n /** Only capture these email headers (lowercase). When set, other headers are excluded. */\n emailHeaderAllowlist?: string[];\n}\n\ninterface EdgeConfigBase {\n service: ServiceConfig;\n handlers?: HandlerConfig;\n fetch?: FetcherConfig;\n postProcessor?: PostProcessorFn;\n sampling?: SamplingConfig;\n propagator?: TextMapPropagator;\n instrumentation?: InstrumentationOptions;\n subscribers?: EdgeSubscriber[];\n /** Opt-in data safety controls for sensitive attribute capture */\n dataSafety?: DataSafetyConfig;\n}\n\ninterface EdgeConfigExporter extends EdgeConfigBase {\n exporter: ExporterConfig;\n}\n\ninterface EdgeConfigSpanProcessors extends EdgeConfigBase {\n spanProcessors: SpanProcessor | SpanProcessor[];\n}\n\nexport type EdgeConfig = EdgeConfigExporter | EdgeConfigSpanProcessors;\n\nexport function isSpanProcessorConfig(\n config: EdgeConfig,\n): config is EdgeConfigSpanProcessors {\n return !!(config as EdgeConfigSpanProcessors).spanProcessors;\n}\n\nexport interface ResolvedEdgeConfig extends EdgeConfigBase {\n handlers: Required<HandlerConfig>;\n fetch: Required<FetcherConfig>;\n postProcessor: PostProcessorFn;\n sampling: Required<SamplingConfig<Sampler>>;\n spanProcessors: SpanProcessor[];\n propagator: TextMapPropagator;\n instrumentation: InstrumentationOptions;\n subscribers: EdgeSubscriber[];\n}\n\n/**\n * Function types\n */\nexport type ResolveConfigFn<Env = any> = (\n env: Env,\n trigger: Trigger,\n) => EdgeConfig;\nexport type ConfigurationOption = EdgeConfig | ResolveConfigFn;\n\nexport type PostProcessorFn = (spans: ReadableSpan[]) => ReadableSpan[];\nexport type TailSampleFn = (traceInfo: LocalTrace) => boolean;\n\nexport interface LocalTrace {\n traceId: string;\n spans: ReadableSpan[];\n localRootSpan: ReadableSpan;\n}\n\n/**\n * Span processor with flush support\n */\nexport type TraceFlushableSpanProcessor = SpanProcessor & {\n forceFlush: (traceId?: string) => Promise<void>;\n};\n\n/**\n * Handler instrumentation\n */\nexport interface InitialSpanInfo {\n name: string;\n options: SpanOptions;\n context?: Context;\n}\n\nexport interface HandlerInstrumentation<T extends Trigger, R extends any> {\n getInitialSpanInfo: (trigger: T) => InitialSpanInfo;\n getAttributesFromResult?: (result: Awaited<R>) => Attributes;\n instrumentTrigger?: (trigger: T) => T;\n executionSucces?: (span: Span, trigger: T, result: Awaited<R>) => void;\n executionFailed?: (span: Span, trigger: T, error?: any) => void;\n}\n\n/**\n * Utility types\n */\n\nexport {type Attributes, type Context, type Span, type SpanOptions} from '@opentelemetry/api';\nexport {type ReadableSpan} from '@opentelemetry/sdk-trace-base';\n","/**\n * Span processor with flush and tail sampling support\n */\n\nimport type { Context } from '@opentelemetry/api';\nimport type {\n ReadableSpan,\n Span,\n SpanExporter,\n SpanProcessor,\n} from '@opentelemetry/sdk-trace-base';\nimport type { PostProcessorFn, TailSampleFn, LocalTrace } from '../types';\n\n/**\n * Span processor that supports flush by trace ID and tail sampling\n */\nexport class SpanProcessorWithFlush implements SpanProcessor {\n private exporter: SpanExporter;\n private postProcessor?: PostProcessorFn;\n private spans: Map<string, ReadableSpan[]> = new Map();\n\n constructor(exporter: SpanExporter, postProcessor?: PostProcessorFn) {\n this.exporter = exporter;\n this.postProcessor = postProcessor;\n }\n\n onStart(_span: Span, _parentContext: Context): void {\n // No-op for now\n }\n\n onEnd(span: ReadableSpan): void {\n const traceId = span.spanContext().traceId;\n\n if (!this.spans.has(traceId)) {\n this.spans.set(traceId, []);\n }\n\n this.spans.get(traceId)!.push(span);\n }\n\n /**\n * Force flush spans for a specific trace\n */\n async forceFlush(traceId?: string): Promise<void> {\n if (traceId) {\n const spans = this.spans.get(traceId);\n if (spans && spans.length > 0) {\n await this.exportSpans(spans);\n this.spans.delete(traceId);\n }\n } else {\n // Flush all traces\n const promises: Promise<void>[] = [];\n for (const [id, spans] of this.spans.entries()) {\n promises.push(this.exportSpans(spans));\n this.spans.delete(id);\n }\n await Promise.all(promises);\n }\n }\n\n async shutdown(): Promise<void> {\n await this.forceFlush();\n if (this.exporter) {\n await this.exporter.shutdown();\n }\n }\n\n /**\n * Export spans with post-processing\n * Errors are caught and logged but don't throw to prevent worker instability\n */\n private async exportSpans(spans: ReadableSpan[]): Promise<void> {\n if (spans.length === 0) return;\n if (!this.exporter) return; // No exporter configured (e.g., in tests)\n\n let processedSpans = spans;\n\n if (this.postProcessor) {\n try {\n processedSpans = this.postProcessor(spans);\n } catch (error) {\n // Post-processor errors should not prevent export\n console.error('[autotel-edge] Post-processor error:', error);\n // Continue with original spans\n processedSpans = spans;\n }\n }\n\n return new Promise((resolve) => {\n this.exporter.export(processedSpans, (result) => {\n if (result.code === 0) {\n // SUCCESS\n resolve();\n } else {\n // Log but don't reject - exporter failures shouldn't crash the worker\n console.error(\n '[autotel-edge] Exporter error:',\n result.error?.message || 'Unknown error',\n );\n resolve(); // Resolve instead of reject to prevent unhandled promise rejection\n }\n });\n });\n }\n}\n\n/**\n * Span processor that supports tail sampling decisions\n */\nexport class TailSamplingSpanProcessor implements SpanProcessor {\n private wrapped: SpanProcessorWithFlush;\n private tailSampler?: TailSampleFn;\n private traces: Map<string, LocalTrace> = new Map();\n\n constructor(\n exporter: SpanExporter,\n postProcessor?: PostProcessorFn,\n tailSampler?: TailSampleFn,\n ) {\n this.wrapped = new SpanProcessorWithFlush(exporter, postProcessor);\n this.tailSampler = tailSampler;\n }\n\n onStart(span: Span, parentContext: Context): void {\n this.wrapped.onStart(span, parentContext);\n }\n\n onEnd(span: ReadableSpan): void {\n const traceId = span.spanContext().traceId;\n const spanId = span.spanContext().spanId;\n const parentSpanId = 'parentSpanId' in span ? span.parentSpanId : undefined;\n\n // Initialize trace if not exists\n if (!this.traces.has(traceId)) {\n this.traces.set(traceId, {\n traceId,\n spans: [],\n localRootSpan: undefined as any, // Will be set when we identify the local root\n });\n }\n\n const trace = this.traces.get(traceId)!;\n\n // Determine if this span is a local root by checking if its parent is in buffered spans\n // A span is a local root if:\n // 1. It has no parentSpanId (definitive root)\n // 2. Its parentSpanId doesn't match any already-buffered span (remote parent = distributed trace entry)\n const hasLocalParent = parentSpanId &&\n trace.spans.some(s => s.spanContext().spanId === parentSpanId);\n\n // Set localRootSpan if this is the local root (no local parent found in buffer)\n if (!hasLocalParent) {\n trace.localRootSpan = span;\n }\n\n trace.spans.push(span); // Buffer the span AFTER checking parent relationships\n\n // Auto-flush decision: only auto-flush for normal traces (no parentSpanId at all)\n // For distributed traces (parentSpanId present), we rely on explicit forceFlush() from instrument.ts\n // This ensures we don't trigger before all spans have been buffered\n const isDefinitiveRoot = !parentSpanId;\n const shouldAutoFlush = isDefinitiveRoot && trace.localRootSpan &&\n trace.localRootSpan.spanContext().spanId === spanId;\n\n if (shouldAutoFlush) {\n if (this.tailSampler) {\n const shouldKeep = this.tailSampler(trace);\n\n if (shouldKeep) {\n // Export ALL buffered spans in the trace\n for (const bufferedSpan of trace.spans) {\n this.wrapped.onEnd(bufferedSpan);\n }\n // Force flush to actually export the spans\n void this.wrapped.forceFlush(traceId);\n }\n // If not keeping, just drop all spans (don't export)\n } else {\n // No tail sampler, export all buffered spans\n for (const bufferedSpan of trace.spans) {\n this.wrapped.onEnd(bufferedSpan);\n }\n // Force flush to actually export the spans\n void this.wrapped.forceFlush(traceId);\n }\n\n // Clean up trace after decision\n this.traces.delete(traceId);\n }\n // If not local root span, just buffer it - don't export yet\n }\n\n async forceFlush(traceId?: string): Promise<void> {\n if (traceId) {\n // Make tail sampling decision for this specific trace before flushing\n const trace = this.traces.get(traceId);\n if (trace) {\n // Ensure localRootSpan is set (fallback to first span if not)\n // This handles distributed traces where no span has undefined parentSpanId\n if (!trace.localRootSpan && trace.spans.length > 0) {\n trace.localRootSpan = trace.spans[0];\n }\n\n if (this.tailSampler) {\n const shouldKeep = this.tailSampler(trace);\n\n if (shouldKeep) {\n // Export ALL buffered spans in the trace\n for (const bufferedSpan of trace.spans) {\n this.wrapped.onEnd(bufferedSpan);\n }\n }\n } else {\n // No tail sampler, export all buffered spans\n for (const bufferedSpan of trace.spans) {\n this.wrapped.onEnd(bufferedSpan);\n }\n }\n\n // Clean up trace after decision\n this.traces.delete(traceId);\n }\n }\n return this.wrapped.forceFlush(traceId);\n }\n\n async shutdown(): Promise<void> {\n this.traces.clear();\n return this.wrapped.shutdown();\n }\n}\n","/**\n * Configuration system for autotel-edge\n */\n\nimport { W3CTraceContextPropagator } from '@opentelemetry/core';\nimport { ParentBasedSampler, AlwaysOnSampler } from '@opentelemetry/sdk-trace-base';\nimport { context as api_context, createContextKey, type Context } from '@opentelemetry/api';\nimport type {\n EdgeConfig,\n ResolvedEdgeConfig,\n ConfigurationOption,\n Trigger,\n ParentRatioSamplingConfig,\n} from '../types';\nimport { isSpanProcessorConfig } from '../types';\nimport { OTLPExporter } from './exporter';\nimport { TailSamplingSpanProcessor } from './spanprocessor';\n\n/**\n * Type for config initialization function\n */\nexport type Initialiser = (env: any, trigger: Trigger) => ResolvedEdgeConfig;\n\n/**\n * Context key for storing config (isolates config per-request)\n */\nconst CONFIG_KEY = createContextKey('autotel-edge-config');\n\n/**\n * Get the currently active config from context\n *\n * This reads the config from the active context, ensuring each request\n * has its own isolated config even when multiple requests are in-flight.\n */\nexport function getActiveConfig(): ResolvedEdgeConfig | null {\n const value = api_context.active().getValue(CONFIG_KEY) as\n | ResolvedEdgeConfig\n | null\n | undefined;\n return value ?? null;\n}\n\n/**\n * Set the active config in context\n *\n * Returns a new context with the config stored. This context should be\n * used with api_context.with() to ensure the config is isolated per-request.\n *\n * @example\n * ```typescript\n * const config = parseConfig({ service: { name: 'my-service' } });\n * const context = setConfig(config);\n *\n * api_context.with(context, () => {\n * // Config is available here via getActiveConfig()\n * });\n * ```\n */\nexport function setConfig(config: ResolvedEdgeConfig): Context {\n return api_context.active().setValue(CONFIG_KEY, config);\n}\n\n/**\n * Parse and validate configuration\n */\nexport function parseConfig(config: EdgeConfig): ResolvedEdgeConfig {\n // Parse head sampler\n const headSampler =\n config.sampling?.headSampler ??\n new ParentBasedSampler({\n root: new AlwaysOnSampler(),\n });\n\n const parsedHeadSampler =\n typeof headSampler === 'object' && 'ratio' in headSampler\n ? createParentRatioSampler(headSampler)\n : headSampler;\n\n // Parse tail sampler (default: keep sampled or error traces)\n const tailSampler =\n config.sampling?.tailSampler ??\n ((traceInfo) => {\n const localRootSpan = traceInfo.localRootSpan;\n const ctx = localRootSpan.spanContext();\n // Keep if sampled or if root span has error\n return (ctx.traceFlags & 1) === 1 || localRootSpan.status.code === 2; // SAMPLED flag | ERROR status\n });\n\n // Parse exporter - use TailSamplingSpanProcessor when tail sampler is present\n const spanProcessors = isSpanProcessorConfig(config)\n ? Array.isArray(config.spanProcessors)\n ? config.spanProcessors\n : [config.spanProcessors]\n : [\n // Use TailSamplingSpanProcessor to enable tail sampling\n new TailSamplingSpanProcessor(\n typeof config.exporter === 'object' && 'url' in config.exporter\n ? new OTLPExporter(config.exporter)\n : config.exporter,\n config.postProcessor,\n tailSampler, // Wire up the tail sampler!\n ),\n ];\n\n // Build resolved config\n const resolved: ResolvedEdgeConfig = {\n service: config.service,\n handlers: {\n fetch: config.handlers?.fetch ?? {},\n },\n fetch: {\n includeTraceContext: config.fetch?.includeTraceContext ?? true,\n },\n postProcessor: config.postProcessor ?? ((spans) => spans),\n sampling: {\n headSampler: parsedHeadSampler,\n tailSampler,\n },\n spanProcessors,\n propagator: config.propagator ?? new W3CTraceContextPropagator(),\n instrumentation: {\n instrumentGlobalFetch: config.instrumentation?.instrumentGlobalFetch ?? true,\n instrumentGlobalCache: config.instrumentation?.instrumentGlobalCache ?? false,\n disabled: config.instrumentation?.disabled ?? false,\n },\n subscribers: config.subscribers ?? [],\n dataSafety: config.dataSafety,\n };\n\n return resolved;\n}\n\n/**\n * Create a parent-based ratio sampler\n */\nfunction createParentRatioSampler(config: ParentRatioSamplingConfig) {\n const { ratio, acceptRemote = true } = config;\n\n // Simple ratio sampler\n const ratioSampler = {\n shouldSample: () => ({\n decision: Math.random() < ratio ? 1 : 0, // RECORD_AND_SAMPLED : NOT_RECORD\n attributes: {},\n }),\n toString: () => `ParentRatioSampler{ratio=${ratio}}`,\n };\n\n if (acceptRemote) {\n return new ParentBasedSampler({ root: ratioSampler as any });\n }\n\n return ratioSampler;\n}\n\n/**\n * Create a config initializer function\n */\nexport function createInitialiser(config: ConfigurationOption): Initialiser {\n if (typeof config === 'function') {\n return (env, trigger) => {\n const conf = parseConfig(config(env, trigger));\n return conf;\n };\n } else {\n const parsed = parseConfig(config);\n return () => parsed;\n }\n}\n"]}
package/dist/events.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { E as EdgeEvent, O as OrPromise, F as FunnelStepStatus, a as OutcomeStatus } from './types-CZFcIjJ4.js';
2
- export { b as EdgeFunnelStepEvent, c as EdgeOutcomeEvent, d as EdgeTrackEvent, e as EdgeValueEvent } from './types-CZFcIjJ4.js';
1
+ import { E as EdgeEvent, O as OrPromise, F as FunnelStepStatus, a as OutcomeStatus } from './types-CzykyUX6.js';
2
+ export { b as EdgeFunnelStepEvent, c as EdgeOutcomeEvent, d as EdgeTrackEvent, e as EdgeValueEvent } from './types-CzykyUX6.js';
3
3
  import '@opentelemetry/api';
4
4
  import '@opentelemetry/sdk-trace-base';
5
5
 
package/dist/events.js CHANGED
@@ -1,4 +1,4 @@
1
- import { getActiveConfig } from './chunk-D7K4WLCX.js';
1
+ import { getActiveConfig } from './chunk-UJGJ663T.js';
2
2
  import { trace } from '@opentelemetry/api';
3
3
 
4
4
  var DEFAULT_SERVICE_NAME = "edge-service";
@@ -54,6 +54,7 @@ interface ExecutionLogger {
54
54
  error(error: Error | string, fields?: Record<string, unknown>): void;
55
55
  getContext(): Record<string, unknown>;
56
56
  emitNow(overrides?: Record<string, unknown>): ExecutionLogSnapshot;
57
+ fork(label: string, fn: () => void | Promise<void>): void;
57
58
  }
58
59
  interface ExecutionLogSnapshot {
59
60
  timestamp: string;
package/dist/index.d.ts CHANGED
@@ -5,11 +5,11 @@ import { Resource } from '@opentelemetry/resources';
5
5
  import { ReadableSpan, TimedEvent, SpanProcessor, SpanExporter, TracerConfig, Sampler } from '@opentelemetry/sdk-trace-base';
6
6
  export { ReadableSpan } from '@opentelemetry/sdk-trace-base';
7
7
  import { OTLPExporterError } from '@opentelemetry/otlp-exporter-base';
8
- import { f as OTLPExporterConfig, g as Trigger, R as ResolvedEdgeConfig, C as ConfigurationOption, h as EdgeConfig } from './types-CZFcIjJ4.js';
9
- export { D as DOConstructorTrigger, i as DataSafetyConfig, j as EdgeSubscriber, k as ExporterConfig, H as HandlerInstrumentation, I as InitialSpanInfo, l as InstrumentationOptions, L as LocalTrace, P as PostProcessorFn, m as ResolveConfigFn, S as SamplingConfig, n as ServiceConfig, T as TailSampleFn, o as TraceFlushableSpanProcessor, W as WorkflowTrigger } from './types-CZFcIjJ4.js';
8
+ import { f as OTLPExporterConfig, g as Trigger, R as ResolvedEdgeConfig, C as ConfigurationOption, h as EdgeConfig } from './types-CzykyUX6.js';
9
+ export { D as DOConstructorTrigger, i as DataSafetyConfig, j as EdgeSubscriber, k as ExporterConfig, H as HandlerInstrumentation, I as InitialSpanInfo, l as InstrumentationOptions, L as LocalTrace, P as PostProcessorFn, m as ResolveConfigFn, S as SamplingConfig, n as ServiceConfig, T as TailSampleFn, o as TraceFlushableSpanProcessor, W as WorkflowTrigger } from './types-CzykyUX6.js';
10
10
  export { Buffer } from 'node:buffer';
11
- import { T as TraceContext } from './execution-logger-Cv6joA6T.js';
12
- export { E as ExecutionLogSnapshot, a as ExecutionLogger, b as ExecutionLoggerOptions, g as getExecutionLogger } from './execution-logger-Cv6joA6T.js';
11
+ import { T as TraceContext } from './execution-logger-DxsSbafj.js';
12
+ export { E as ExecutionLogSnapshot, a as ExecutionLogger, b as ExecutionLoggerOptions, g as getExecutionLogger } from './execution-logger-DxsSbafj.js';
13
13
  export { ParsedError, parseError } from './parse-error.js';
14
14
 
15
15
  /**
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- export { OTLPExporter, createInitialiser, getActiveConfig, parseConfig, setConfig } from './chunk-D7K4WLCX.js';
2
- import { setSpanName, createTraceContext } from './chunk-A2AH5BBJ.js';
3
- export { getExecutionLogger } from './chunk-A2AH5BBJ.js';
1
+ export { OTLPExporter, createInitialiser, getActiveConfig, parseConfig, setConfig } from './chunk-UJGJ663T.js';
2
+ import { setSpanName, createTraceContext } from './chunk-FCYVL2I7.js';
3
+ export { getExecutionLogger } from './chunk-FCYVL2I7.js';
4
4
  export { parseError } from './chunk-M7Z4P5MC.js';
5
5
  import { sanitizeAttributes, isAttributeValue, isTimeInput, hrTimeDuration } from '@opentelemetry/core';
6
6
  import { SEMATTRS_EXCEPTION_MESSAGE, SEMATTRS_EXCEPTION_TYPE, SEMATTRS_EXCEPTION_STACKTRACE } from '@opentelemetry/semantic-conventions';
package/dist/logger.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { E as ExecutionLogSnapshot, a as ExecutionLogger, b as ExecutionLoggerOptions, g as getExecutionLogger } from './execution-logger-Cv6joA6T.js';
1
+ export { E as ExecutionLogSnapshot, a as ExecutionLogger, b as ExecutionLoggerOptions, g as getExecutionLogger } from './execution-logger-DxsSbafj.js';
2
2
  import '@opentelemetry/api';
3
3
 
4
4
  /**
package/dist/logger.js CHANGED
@@ -1,4 +1,4 @@
1
- export { getExecutionLogger } from './chunk-A2AH5BBJ.js';
1
+ export { getExecutionLogger } from './chunk-FCYVL2I7.js';
2
2
  import { createContextKey, context, trace } from '@opentelemetry/api';
3
3
 
4
4
  // src/api/redact.ts
@@ -1,4 +1,4 @@
1
- import { T as TailSampleFn, L as LocalTrace } from './types-CZFcIjJ4.js';
1
+ import { T as TailSampleFn, L as LocalTrace } from './types-CzykyUX6.js';
2
2
  import '@opentelemetry/api';
3
3
  import '@opentelemetry/sdk-trace-base';
4
4
 
@@ -93,6 +93,9 @@ type EdgeSubscriber = (event: EdgeEvent) => OrPromise<void>;
93
93
  interface FetcherConfig {
94
94
  includeTraceContext?: boolean | ((request: Request) => boolean);
95
95
  }
96
+ interface RouteServiceConfig {
97
+ service: string;
98
+ }
96
99
  interface PostProcessParams {
97
100
  /**
98
101
  * The request object that was passed to the fetch handler.
@@ -118,6 +121,23 @@ interface FetchHandlerConfig {
118
121
  * Allows further customization of the generated span, based on the request/response data.
119
122
  */
120
123
  postProcess?: (span: Span, ctx: PostProcessParams) => void;
124
+ /**
125
+ * Route patterns to include for fetch handler instrumentation.
126
+ * Supports glob patterns like '/api/**'.
127
+ * If not set, all routes are included.
128
+ */
129
+ include?: string[];
130
+ /**
131
+ * Route patterns to exclude from fetch handler instrumentation.
132
+ * Supports glob patterns like '/health' and '/_internal/**'.
133
+ * Exclusions take precedence over inclusions.
134
+ */
135
+ exclude?: string[];
136
+ /**
137
+ * Route-specific service mapping.
138
+ * The first matching pattern wins based on object iteration order.
139
+ */
140
+ routes?: Record<string, RouteServiceConfig>;
121
141
  }
122
142
  interface HandlerConfig {
123
143
  fetch?: FetchHandlerConfig;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autotel-edge",
3
- "version": "3.16.3",
3
+ "version": "3.16.4",
4
4
  "description": "Vendor-agnostic OpenTelemetry for edge runtimes - foundation for Cloudflare, Vercel, Netlify, Deno",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -223,6 +223,31 @@ describe('Config System', () => {
223
223
 
224
224
  expect(parsed.fetch.includeTraceContext).toBe(customFn);
225
225
  });
226
+
227
+ it('should preserve fetch handler route filtering and service mappings', () => {
228
+ const config: EdgeConfig = {
229
+ service: { name: 'test-service' },
230
+ handlers: {
231
+ fetch: {
232
+ include: ['/api/**'],
233
+ exclude: ['/api/internal/**'],
234
+ routes: {
235
+ '/api/auth/**': { service: 'auth-service' },
236
+ '/api/**': { service: 'api-service' },
237
+ },
238
+ },
239
+ },
240
+ };
241
+
242
+ const parsed = parseConfig(config);
243
+
244
+ expect(parsed.handlers.fetch.include).toEqual(['/api/**']);
245
+ expect(parsed.handlers.fetch.exclude).toEqual(['/api/internal/**']);
246
+ expect(parsed.handlers.fetch.routes).toEqual({
247
+ '/api/auth/**': { service: 'auth-service' },
248
+ '/api/**': { service: 'api-service' },
249
+ });
250
+ });
226
251
  });
227
252
 
228
253
  describe('createInitialiser()', () => {
@@ -103,6 +103,22 @@ describe('getExecutionLogger', () => {
103
103
  });
104
104
  });
105
105
 
106
+ it('deep merges objects and concatenates arrays in set()', () => {
107
+ const ctx = createMockContext();
108
+ const log = getExecutionLogger(ctx);
109
+
110
+ log.set({ job: { id: 'j1', tags: ['initial'] } });
111
+ log.set({ job: { status: 'done', tags: ['final'] } });
112
+
113
+ expect(log.getContext()).toEqual({
114
+ job: {
115
+ id: 'j1',
116
+ status: 'done',
117
+ tags: ['initial', 'final'],
118
+ },
119
+ });
120
+ });
121
+
106
122
  it('emitNow records a manual event and returns a snapshot', async () => {
107
123
  const ctx = createMockContext();
108
124
  const onEmit = vi.fn(async () => {});
@@ -133,6 +149,39 @@ describe('getExecutionLogger', () => {
133
149
  expect(onEmit).toHaveBeenCalledWith(snapshot);
134
150
  });
135
151
 
152
+ it('warns and returns the first snapshot on duplicate emitNow()', () => {
153
+ const ctx = createMockContext();
154
+ const log = getExecutionLogger(ctx);
155
+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
156
+
157
+ log.set({ workflow: { id: 'wf-1' } });
158
+ const first = log.emitNow();
159
+ const second = log.emitNow();
160
+
161
+ expect(warnSpy).toHaveBeenCalledWith(
162
+ expect.stringContaining(
163
+ '[autotel-edge] log.emitNow() called after the execution event was emitted',
164
+ ),
165
+ );
166
+ expect(second).toBe(first);
167
+ });
168
+
169
+ it('drops writes after emitNow and warns', () => {
170
+ const ctx = createMockContext();
171
+ const log = getExecutionLogger(ctx);
172
+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
173
+
174
+ log.emitNow();
175
+ log.set({ dropped: true });
176
+
177
+ expect(log.getContext()).toEqual({});
178
+ expect(warnSpy).toHaveBeenCalledWith(
179
+ expect.stringContaining(
180
+ '[autotel-edge] log.set() called after the execution event was emitted',
181
+ ),
182
+ );
183
+ });
184
+
136
185
  it('resolves context from the active span when no ctx is passed', () => {
137
186
  const span = {
138
187
  spanContext: () => ({
@@ -168,4 +217,57 @@ describe('getExecutionLogger', () => {
168
217
  '[autotel-edge] getExecutionLogger() requires an active span or explicit TraceContext. Wrap your handler with trace() or pass ctx directly.',
169
218
  );
170
219
  });
220
+
221
+ it('fork runs in a child span and keeps parent context isolated', async () => {
222
+ const parent = createMockContext();
223
+ const log = getExecutionLogger(parent);
224
+
225
+ const childSpan = {
226
+ spanContext: () => ({
227
+ traceId: 'a'.repeat(32),
228
+ spanId: 'b'.repeat(16),
229
+ traceFlags: 1,
230
+ }),
231
+ setAttribute: vi.fn(),
232
+ setAttributes: vi.fn(),
233
+ setStatus: vi.fn(),
234
+ recordException: vi.fn(),
235
+ addEvent: vi.fn(),
236
+ addLink: vi.fn(),
237
+ addLinks: vi.fn(),
238
+ updateName: vi.fn(),
239
+ isRecording: vi.fn(() => true),
240
+ end: vi.fn(),
241
+ };
242
+
243
+ const tracer = {
244
+ startActiveSpan: (_name: string, cb: (span: typeof childSpan) => Promise<void>) =>
245
+ cb(childSpan),
246
+ };
247
+
248
+ vi.spyOn(otelTrace, 'getTracer').mockReturnValue(
249
+ tracer as unknown as ReturnType<typeof otelTrace.getTracer>,
250
+ );
251
+
252
+ log.fork('queue-retry', async () => {
253
+ const childLog = getExecutionLogger();
254
+ childLog.info('retrying', { attempt: 2 });
255
+ });
256
+
257
+ await new Promise((resolve) => setImmediate(resolve));
258
+ await new Promise((resolve) => setImmediate(resolve));
259
+
260
+ expect(childSpan.setAttributes).toHaveBeenCalledWith(
261
+ expect.objectContaining({
262
+ operation: 'queue-retry',
263
+ _parentCorrelationId: 'corr-id',
264
+ }),
265
+ );
266
+ expect(childSpan.addEvent).toHaveBeenCalledWith(
267
+ 'log.emit.manual',
268
+ expect.any(Object),
269
+ );
270
+ expect(parent.setAttributes).not.toHaveBeenCalled();
271
+ expect(childSpan.end).toHaveBeenCalledTimes(1);
272
+ });
171
273
  });
@@ -3,6 +3,54 @@ import type { AttributeValue } from '@opentelemetry/api';
3
3
  import type { TraceContext } from './functional';
4
4
  import { createTraceContext } from './core/trace-context';
5
5
 
6
+ const POST_EMIT_FORK_HINT =
7
+ "For intentional background work tied to this execution, use log.fork('label', fn) when available.";
8
+
9
+ function warnPostEmit(method: string, detail: string): void {
10
+ console.warn(
11
+ `[autotel-edge] ${method} called after the execution event was emitted - ${detail} This data will not appear in observability. ${POST_EMIT_FORK_HINT}`,
12
+ );
13
+ }
14
+
15
+ function mergeInto(
16
+ target: Record<string, unknown>,
17
+ source: Record<string, unknown>,
18
+ ): void {
19
+ for (const key in source) {
20
+ const sourceVal = source[key];
21
+ if (sourceVal === undefined) continue;
22
+ const targetVal = target[key];
23
+ if (
24
+ sourceVal !== null &&
25
+ typeof sourceVal === 'object' &&
26
+ !Array.isArray(sourceVal) &&
27
+ targetVal !== null &&
28
+ typeof targetVal === 'object' &&
29
+ !Array.isArray(targetVal)
30
+ ) {
31
+ mergeInto(
32
+ targetVal as Record<string, unknown>,
33
+ sourceVal as Record<string, unknown>,
34
+ );
35
+ } else if (Array.isArray(targetVal) && Array.isArray(sourceVal)) {
36
+ target[key] = [...targetVal, ...sourceVal];
37
+ } else {
38
+ target[key] = sourceVal;
39
+ }
40
+ }
41
+ }
42
+
43
+ function generateCorrelationId(): string {
44
+ if (
45
+ typeof globalThis.crypto !== 'undefined' &&
46
+ typeof globalThis.crypto.randomUUID === 'function'
47
+ ) {
48
+ return globalThis.crypto.randomUUID();
49
+ }
50
+
51
+ return `exec-${Date.now()}-${Math.random().toString(16).slice(2)}`;
52
+ }
53
+
6
54
  export interface ExecutionLogger {
7
55
  set(fields: Record<string, unknown>): void;
8
56
  info(message: string, fields?: Record<string, unknown>): void;
@@ -10,6 +58,7 @@ export interface ExecutionLogger {
10
58
  error(error: Error | string, fields?: Record<string, unknown>): void;
11
59
  getContext(): Record<string, unknown>;
12
60
  emitNow(overrides?: Record<string, unknown>): ExecutionLogSnapshot;
61
+ fork(label: string, fn: () => void | Promise<void>): void;
13
62
  }
14
63
 
15
64
  export interface ExecutionLogSnapshot {
@@ -162,6 +211,8 @@ export function getExecutionLogger(
162
211
  ): ExecutionLogger {
163
212
  const activeContext = resolveContext(ctx);
164
213
  let contextState: Record<string, unknown> = {};
214
+ let emitted = false;
215
+ let lastSnapshot: ExecutionLogSnapshot | null = null;
165
216
 
166
217
  const addLogEvent = (
167
218
  level: 'info' | 'warn' | 'error',
@@ -175,39 +226,54 @@ export function getExecutionLogger(
175
226
  });
176
227
  };
177
228
 
229
+ const sealCheck = (method: string, keys: string[]): void => {
230
+ if (emitted) {
231
+ warnPostEmit(
232
+ method,
233
+ `Keys dropped: ${keys.length ? keys.join(', ') : '(empty)'}.`,
234
+ );
235
+ }
236
+ };
237
+
178
238
  return {
179
239
  set(fields: Record<string, unknown>) {
180
- contextState = {
181
- ...contextState,
182
- ...fields,
183
- };
240
+ sealCheck('log.set()', Object.keys(fields));
241
+ if (emitted) return;
242
+ mergeInto(contextState, fields);
184
243
  activeContext.setAttributes(flattenToAttributes(fields));
185
244
  },
186
245
 
187
246
  info(message: string, fields?: Record<string, unknown>) {
247
+ const keys = fields
248
+ ? ['message', ...Object.keys(fields).filter((k) => k !== 'requestLogs')]
249
+ : ['message'];
250
+ sealCheck('log.info()', keys);
251
+ if (emitted) return;
188
252
  addLogEvent('info', message, fields);
189
253
  if (fields) {
190
- contextState = {
191
- ...contextState,
192
- ...fields,
193
- };
254
+ mergeInto(contextState, fields);
194
255
  activeContext.setAttributes(flattenToAttributes(fields));
195
256
  }
196
257
  },
197
258
 
198
259
  warn(message: string, fields?: Record<string, unknown>) {
260
+ const keys = fields
261
+ ? ['message', ...Object.keys(fields).filter((k) => k !== 'requestLogs')]
262
+ : ['message'];
263
+ sealCheck('log.warn()', keys);
264
+ if (emitted) return;
199
265
  addLogEvent('warn', message, fields);
200
266
  activeContext.setAttribute('autotel.log.level', 'warn');
201
267
  if (fields) {
202
- contextState = {
203
- ...contextState,
204
- ...fields,
205
- };
268
+ mergeInto(contextState, fields);
206
269
  activeContext.setAttributes(flattenToAttributes(fields));
207
270
  }
208
271
  },
209
272
 
210
273
  error(error: Error | string, fields?: Record<string, unknown>) {
274
+ const keys = fields ? [...Object.keys(fields), 'error'] : ['error'];
275
+ sealCheck('log.error()', keys);
276
+ if (emitted) return;
211
277
  const err = typeof error === 'string' ? new Error(error) : error;
212
278
 
213
279
  activeContext.recordException(err);
@@ -219,10 +285,7 @@ export function getExecutionLogger(
219
285
  addLogEvent('error', err.message, fields);
220
286
 
221
287
  if (fields) {
222
- contextState = {
223
- ...contextState,
224
- ...fields,
225
- };
288
+ mergeInto(contextState, fields);
226
289
  activeContext.setAttributes(flattenToAttributes(fields));
227
290
  }
228
291
 
@@ -234,6 +297,11 @@ export function getExecutionLogger(
234
297
  },
235
298
 
236
299
  emitNow(overrides?: Record<string, unknown>): ExecutionLogSnapshot {
300
+ if (emitted) {
301
+ warnPostEmit('log.emitNow()', 'Ignoring duplicate emit.');
302
+ return lastSnapshot as ExecutionLogSnapshot;
303
+ }
304
+
237
305
  const mergedContext = {
238
306
  ...contextState,
239
307
  ...(overrides ?? {}),
@@ -259,7 +327,50 @@ export function getExecutionLogger(
259
327
  });
260
328
  }
261
329
 
330
+ emitted = true;
331
+ lastSnapshot = snapshot;
262
332
  return snapshot;
263
333
  },
334
+
335
+ fork(label: string, fn: () => void | Promise<void>): void {
336
+ const parentCorrelationId = activeContext.correlationId;
337
+ if (
338
+ typeof parentCorrelationId !== 'string' ||
339
+ parentCorrelationId.length === 0
340
+ ) {
341
+ throw new Error(
342
+ '[autotel-edge] log.fork() requires the parent logger to have a correlationId. ' +
343
+ 'Ensure execution context was created by autotel trace instrumentation.',
344
+ );
345
+ }
346
+
347
+ const tracer = otelTrace.getTracer('autotel-edge.execution-logger');
348
+ void tracer.startActiveSpan(`execution.fork:${label}`, (childSpan) => {
349
+ const childContext: TraceContext = {
350
+ ...createTraceContext(childSpan),
351
+ correlationId: generateCorrelationId(),
352
+ };
353
+
354
+ const childLog = getExecutionLogger(childContext);
355
+ childLog.set({
356
+ operation: label,
357
+ _parentCorrelationId: parentCorrelationId,
358
+ });
359
+
360
+ return Promise.resolve()
361
+ .then(() => fn())
362
+ .then(() => {
363
+ childLog.emitNow();
364
+ })
365
+ .catch((err: unknown) => {
366
+ const error = err instanceof Error ? err : new Error(String(err));
367
+ childLog.error(error);
368
+ childLog.emitNow();
369
+ })
370
+ .finally(() => {
371
+ childSpan.end();
372
+ });
373
+ });
374
+ },
264
375
  };
265
376
  }
package/src/types.ts CHANGED
@@ -153,6 +153,10 @@ export interface FetcherConfig {
153
153
  includeTraceContext?: boolean | ((request: Request) => boolean);
154
154
  }
155
155
 
156
+ export interface RouteServiceConfig {
157
+ service: string;
158
+ }
159
+
156
160
  export interface PostProcessParams {
157
161
  /**
158
162
  * The request object that was passed to the fetch handler.
@@ -179,6 +183,23 @@ export interface FetchHandlerConfig {
179
183
  * Allows further customization of the generated span, based on the request/response data.
180
184
  */
181
185
  postProcess?: (span: Span, ctx: PostProcessParams) => void;
186
+ /**
187
+ * Route patterns to include for fetch handler instrumentation.
188
+ * Supports glob patterns like '/api/**'.
189
+ * If not set, all routes are included.
190
+ */
191
+ include?: string[];
192
+ /**
193
+ * Route patterns to exclude from fetch handler instrumentation.
194
+ * Supports glob patterns like '/health' and '/_internal/**'.
195
+ * Exclusions take precedence over inclusions.
196
+ */
197
+ exclude?: string[];
198
+ /**
199
+ * Route-specific service mapping.
200
+ * The first matching pattern wins based on object iteration order.
201
+ */
202
+ routes?: Record<string, RouteServiceConfig>;
182
203
  }
183
204
 
184
205
  export interface HandlerConfig {
@@ -281,4 +302,4 @@ export interface HandlerInstrumentation<T extends Trigger, R extends any> {
281
302
  */
282
303
 
283
304
  export {type Attributes, type Context, type Span, type SpanOptions} from '@opentelemetry/api';
284
- export {type ReadableSpan} from '@opentelemetry/sdk-trace-base';
305
+ export {type ReadableSpan} from '@opentelemetry/sdk-trace-base';
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/core/trace-context.ts","../src/execution-logger.ts"],"names":["otelTrace"],"mappings":";;;;;AAgBA,IAAM,WAAA,uBAAkB,OAAA,EAAsB;AAsDvC,SAAS,mBAAmB,IAAA,EAA0B;AAC3D,EAAA,MAAM,WAAA,GAAc,KAAK,WAAA,EAAY;AACrC,EAAA,OAAO;AAAA,IACL,SAAS,WAAA,CAAY,OAAA;AAAA,IACrB,QAAQ,WAAA,CAAY,MAAA;AAAA,IACpB,aAAA,EAAe,WAAA,CAAY,OAAA,CAAQ,KAAA,CAAM,GAAG,EAAE,CAAA;AAAA,IAC9C,eAAA,EAAiB,WAAA,CAAY,GAAA,C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A,OAAO,EAAE,GAAG,YAAA,EAAa;AAAA,IAC3B,CAAA;AAAA,IAEA,QAAQ,SAAA,EAA2D;AACjE,MAAA,MAAM,aAAA,GAAgB;AAAA,QACpB,GAAG,YAAA;AAAA,QACH,GAAI,aAAa;AAAC,OACpB;AACA,MAAA,MAAM,SAAA,GAAY,oBAAoB,aAAa,CAAA;AACnD,MAAA,aAAA,CAAc,cAAc,SAAS,CAAA;AAErC,MAAA,MAAM,QAAA,GAAiC;AAAA,QACrC,SAAA,EAAA,iBAAW,IAAI,IAAA,EAAK,EAAE,WAAA,EAAY;AAAA,QAClC,SAAS,aAAA,CAAc,OAAA;AAAA,QACvB,QAAQ,aAAA,CAAc,MAAA;AAAA,QACtB,eAAe,aAAA,CAAc,aAAA;AAAA,QAC7B,OAAA,EAAS;AAAA,OACX;AAEA,MAAA,aAAA,CAAc,SAAS,iBAAA,EAAmB;AAAA,QACxC,GAAG;AAAA,OACJ,CAAA;AAED,MAAA,IAAI,SAAS,MAAA,EAAQ;AACnB,QAAA,OAAA,CAAQ,OAAA,CAAQ,QAAQ,MAAA,CAAO,QAAQ,CAAC,CAAA,CAAE,KAAA,CAAM,CAAC,KAAA,KAAU;AACzD,UAAA,OAAA,CAAQ,IAAA,CAAK,kDAAkD,KAAK,CAAA;AAAA,QACtE,CAAC,CAAA;AAAA,MACH;AAEA,MAAA,OAAO,QAAA;AAAA,IACT;AAAA,GACF;AACF","file":"chunk-A2AH5BBJ.js","sourcesContent":["/**\n * Trace context types and utilities\n */\n\nimport type {\n AttributeValue,\n Link,\n Span,\n SpanStatusCode,\n TimeInput,\n} from '@opentelemetry/api';\n\n/**\n * WeakMap to store span names for active spans.\n * Enables retrieving span names for correlation helpers.\n */\nconst spanNameMap = new WeakMap<Span, string>();\n\n/**\n * Base trace context containing trace identifiers\n */\nexport interface TraceContextBase {\n traceId: string;\n spanId: string;\n correlationId: string;\n 'code.function'?: string;\n}\n\n/**\n * Span methods available on trace context\n */\nexport interface SpanMethods {\n /** Set a single attribute on the span */\n setAttribute(key: string, value: AttributeValue): void;\n /** Set multiple attributes on the span */\n setAttributes(attrs: Record<string, AttributeValue>): void;\n /** Set the status of the span */\n setStatus(status: { code: SpanStatusCode; message?: string }): void;\n /** Record an exception on the span */\n recordException(exception: Error, time?: TimeInput): void;\n /** Add an event to the span (for logging milestones/checkpoints) */\n addEvent(\n name: string,\n attributesOrStartTime?: Record<string, AttributeValue> | TimeInput,\n startTime?: TimeInput,\n ): void;\n /** Add a link to another span */\n addLink(link: Link): void;\n /** Add multiple links to other spans */\n addLinks(links: Link[]): void;\n /** Update the span name dynamically */\n updateName(name: string): void;\n /** Check if the span is recording */\n isRecording(): boolean;\n}\n\n/**\n * Complete trace context that merges base context and span methods\n *\n * This is the ctx parameter passed to factory functions in trace().\n * It provides access to trace IDs and span manipulation methods.\n */\nexport type TraceContext = TraceContextBase & SpanMethods;\n\n/**\n * Create a TraceContext from an OpenTelemetry Span\n *\n * This utility extracts trace context information from a span\n * and provides span manipulation methods in a consistent format.\n */\nexport function createTraceContext(span: Span): TraceContext {\n const spanContext = span.spanContext();\n return {\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n correlationId: spanContext.traceId.slice(0, 16),\n 'code.function': spanNameMap.get(span),\n setAttribute: span.setAttribute.bind(span),\n setAttributes: span.setAttributes.bind(span),\n setStatus: span.setStatus.bind(span),\n recordException: span.recordException.bind(span),\n addEvent: span.addEvent.bind(span),\n addLink: span.addLink.bind(span),\n addLinks: span.addLinks.bind(span),\n updateName: span.updateName.bind(span),\n isRecording: span.isRecording.bind(span),\n };\n}\n\n/**\n * Store the span name for later retrieval via trace context helpers.\n */\nexport function setSpanName(span: Span, name: string): void {\n spanNameMap.set(span, name);\n}\n","import { trace as otelTrace, SpanStatusCode } from '@opentelemetry/api';\nimport type { AttributeValue } from '@opentelemetry/api';\nimport type { TraceContext } from './functional';\nimport { createTraceContext } from './core/trace-context';\n\nexport interface ExecutionLogger {\n set(fields: Record<string, unknown>): void;\n info(message: string, fields?: Record<string, unknown>): void;\n warn(message: string, fields?: Record<string, unknown>): void;\n error(error: Error | string, fields?: Record<string, unknown>): void;\n getContext(): Record<string, unknown>;\n emitNow(overrides?: Record<string, unknown>): ExecutionLogSnapshot;\n}\n\nexport interface ExecutionLogSnapshot {\n timestamp: string;\n traceId: string;\n spanId: string;\n correlationId: string;\n context: Record<string, unknown>;\n}\n\nexport interface ExecutionLoggerOptions {\n onEmit?: (snapshot: ExecutionLogSnapshot) => void | Promise<void>;\n}\n\nfunction toAttributeValue(value: unknown): AttributeValue | undefined {\n if (\n typeof value === 'string' ||\n typeof value === 'number' ||\n typeof value === 'boolean'\n ) {\n return value;\n }\n\n if (Array.isArray(value)) {\n if (\n value.every((item) => typeof item === 'string') ||\n value.every((item) => typeof item === 'number') ||\n value.every((item) => typeof item === 'boolean')\n ) {\n return value as AttributeValue;\n }\n\n try {\n return JSON.stringify(value);\n } catch {\n return '<serialization-failed>';\n }\n }\n\n if (value instanceof Date) {\n return value.toISOString();\n }\n\n if (value instanceof Error) {\n return value.message;\n }\n\n return undefined;\n}\n\nfunction flattenToAttributes(\n fields: Record<string, unknown>,\n prefix = '',\n): Record<string, AttributeValue> {\n const out: Record<string, AttributeValue> = {};\n const seen = new WeakSet<object>();\n\n function flatten(obj: Record<string, unknown>, currentPrefix: string): void {\n for (const [key, value] of Object.entries(obj)) {\n if (value == null) continue;\n\n const nextKey = currentPrefix ? `${currentPrefix}.${key}` : key;\n const attr = toAttributeValue(value);\n\n if (attr !== undefined) {\n out[nextKey] = attr;\n continue;\n }\n\n if (typeof value === 'object' && value.constructor === Object) {\n if (seen.has(value)) {\n out[nextKey] = '<circular-reference>';\n continue;\n }\n\n seen.add(value);\n flatten(value as Record<string, unknown>, nextKey);\n continue;\n }\n\n try {\n out[nextKey] = JSON.stringify(value);\n } catch {\n out[nextKey] = '<serialization-failed>';\n }\n }\n }\n\n flatten(fields, prefix);\n return out;\n}\n\nfunction getErrorAttributes(error: Error): Record<string, AttributeValue> {\n const attributes: Record<string, AttributeValue> = {\n 'error.type': error.name || 'Error',\n 'error.message': error.message,\n };\n\n if (error.stack) {\n attributes['error.stack'] = error.stack;\n }\n\n const structured = error as Error & {\n why?: string;\n fix?: string;\n link?: string;\n code?: string | number;\n status?: number;\n details?: Record<string, unknown>;\n };\n\n if (structured.why) attributes['error.why'] = structured.why;\n if (structured.fix) attributes['error.fix'] = structured.fix;\n if (structured.link) attributes['error.link'] = structured.link;\n if (structured.code !== undefined) {\n attributes['error.code'] =\n typeof structured.code === 'string'\n ? structured.code\n : String(structured.code);\n }\n if (structured.status !== undefined) {\n attributes['error.status'] = structured.status;\n }\n if (structured.details) {\n Object.assign(\n attributes,\n flattenToAttributes(structured.details, 'error.details'),\n );\n }\n\n return attributes;\n}\n\nfunction resolveContext(ctx?: TraceContext): TraceContext {\n if (ctx) return ctx;\n\n const span = otelTrace.getActiveSpan();\n if (!span) {\n throw new Error(\n '[autotel-edge] getExecutionLogger() requires an active span or explicit TraceContext. Wrap your handler with trace() or pass ctx directly.',\n );\n }\n\n return createTraceContext(span);\n}\n\nexport function getExecutionLogger(\n ctx?: TraceContext,\n options?: ExecutionLoggerOptions,\n): ExecutionLogger {\n const activeContext = resolveContext(ctx);\n let contextState: Record<string, unknown> = {};\n\n const addLogEvent = (\n level: 'info' | 'warn' | 'error',\n message: string,\n fields?: Record<string, unknown>,\n ) => {\n const attrs = fields ? flattenToAttributes(fields) : undefined;\n activeContext.addEvent(`log.${level}`, {\n message,\n ...attrs,\n });\n };\n\n return {\n set(fields: Record<string, unknown>) {\n contextState = {\n ...contextState,\n ...fields,\n };\n activeContext.setAttributes(flattenToAttributes(fields));\n },\n\n info(message: string, fields?: Record<string, unknown>) {\n addLogEvent('info', message, fields);\n if (fields) {\n contextState = {\n ...contextState,\n ...fields,\n };\n activeContext.setAttributes(flattenToAttributes(fields));\n }\n },\n\n warn(message: string, fields?: Record<string, unknown>) {\n addLogEvent('warn', message, fields);\n activeContext.setAttribute('autotel.log.level', 'warn');\n if (fields) {\n contextState = {\n ...contextState,\n ...fields,\n };\n activeContext.setAttributes(flattenToAttributes(fields));\n }\n },\n\n error(error: Error | string, fields?: Record<string, unknown>) {\n const err = typeof error === 'string' ? new Error(error) : error;\n\n activeContext.recordException(err);\n activeContext.setStatus({\n code: SpanStatusCode.ERROR,\n message: err.message,\n });\n activeContext.setAttributes(getErrorAttributes(err));\n addLogEvent('error', err.message, fields);\n\n if (fields) {\n contextState = {\n ...contextState,\n ...fields,\n };\n activeContext.setAttributes(flattenToAttributes(fields));\n }\n\n activeContext.setAttribute('autotel.log.level', 'error');\n },\n\n getContext() {\n return { ...contextState };\n },\n\n emitNow(overrides?: Record<string, unknown>): ExecutionLogSnapshot {\n const mergedContext = {\n ...contextState,\n ...(overrides ?? {}),\n };\n const flattened = flattenToAttributes(mergedContext);\n activeContext.setAttributes(flattened);\n\n const snapshot: ExecutionLogSnapshot = {\n timestamp: new Date().toISOString(),\n traceId: activeContext.traceId,\n spanId: activeContext.spanId,\n correlationId: activeContext.correlationId,\n context: mergedContext,\n };\n\n activeContext.addEvent('log.emit.manual', {\n ...flattened,\n });\n\n if (options?.onEmit) {\n Promise.resolve(options.onEmit(snapshot)).catch((error) => {\n console.warn('[autotel-edge] execution logger onEmit failed:', error);\n });\n }\n\n return snapshot;\n },\n };\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/core/exporter.ts","../src/types.ts","../src/core/spanprocessor.ts","../src/core/config.ts"],"names":["api_context"],"mappings":";;;;;;;AAiBA,IAAM,eAAA,GAAkB,QAAA;AAExB,IAAM,cAAA,GAAyC;AAAA,EAC7C,MAAA,EAAQ,kBAAA;AAAA,EACR,cAAA,EAAgB,kBAAA;AAAA,EAChB,YAAA,EAAc,iBAAiB,eAAe,CAAA;AAChD,CAAA;AAKO,IAAM,eAAN,MAA2C;AAAA,EACxC,OAAA;AAAA,EACA,GAAA;AAAA,EAER,YAAY,MAAA,EAA4B;AACtC,IAAA,IAA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AAA,MACnB,QAAA,EAAU,IAAA,CAAK,MAAA,EAAO,GAAI,QAAQ,CAAA,GAAI,CAAA;AAAA;AAAA,MACtC,YAAY;AAAC,KACf,CAAA;AAAA,IACA,QAAA,EAAU,MAAM,CAAA,yBAAA,EAA4B,KAAK,CAAA,CAAA;AAAA,GACnD;AAEA,EAAA,IAAI,YAAA,EAAc;AAChB,IAAA,OAAO,IAAI,kBAAA,CAAmB,EAAE,IAAA,EAAM,cAAqB,CAAA;AAAA,EAC7D;AAEA,EAAA,OAAO,YAAA;AACT;AAKO,SAAS,kBAAkB,MAAA,EAA0C;AAC1E,EAAA,IAAI,OAAO,WAAW,UAAA,EAAY;AAChC,IAAA,OAAO,CAAC,KAAK,OAAA,KAAY;AACvB,MAAA,MAAM,IAAA,GAAO,WAAA,CAAY,MAAA,CAAO,GAAA,EAAK,OAAO,CAAC,CAAA;AAC7C,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,EACF,CAAA,MAAO;AACL,IAAA,MAAM,MAAA,GAAS,YAAY,MAAM,CAAA;AACjC,IAAA,OAAO,MAAM,MAAA;AAAA,EACf;AACF","file":"chunk-D7K4WLCX.js","sourcesContent":["/**\n * Lightweight OTLP exporter for edge environments\n * Ported and adapted from @microlabs/\n *\n * This exporter is much smaller than the standard @opentelemetry/exporter-trace-otlp-http\n * because it uses fetch() directly instead of Node.js http/https modules.\n */\n\nimport type { ExportResult } from '@opentelemetry/core';\nimport { ExportResultCode } from '@opentelemetry/core';\nimport { OTLPExporterError } from '@opentelemetry/otlp-exporter-base';\nimport { JsonTraceSerializer } from '@opentelemetry/otlp-transformer';\nimport type { SpanExporter } from '@opentelemetry/sdk-trace-base';\nimport type { OTLPExporterConfig } from '../types';\n\n// Version is injected at build time via tsup define\n// This avoids runtime filesystem access which isn't available in edge environments\nconst PACKAGE_VERSION = process.env.AUTOTEL_EDGE_VERSION || '0.1.1';\n\nconst defaultHeaders: Record<string, string> = {\n accept: 'application/json',\n 'content-type': 'application/json',\n 'user-agent': `autotel-edge v${PACKAGE_VERSION}`,\n};\n\n/**\n * Minimal OTLP exporter using fetch()\n */\nexport class OTLPExporter implements SpanExporter {\n private headers: Record<string, string>;\n private url: string;\n\n constructor(config: OTLPExporterConfig) {\n this.url = config.url;\n this.headers = Object.assign({}, defaultHeaders, config.headers);\n }\n\n export(items: any[], resultCallback: (result: ExportResult) => void): void {\n this._export(items)\n .then(() => {\n resultCallback({ code: ExportResultCode.SUCCESS });\n })\n .catch((error) => {\n resultCallback({ code: ExportResultCode.FAILED, error });\n });\n }\n\n private _export(items: any[]): Promise<unknown> {\n return new Promise<void>((resolve, reject) => {\n try {\n this.send(items, resolve, reject);\n } catch (error) {\n reject(error);\n }\n });\n }\n\n send(\n items: any[],\n onSuccess: () => void,\n onError: (error: OTLPExporterError) => void,\n ): void {\n const decoder = new TextDecoder();\n const exportMessage = JsonTraceSerializer.serializeRequest(items);\n\n const body = decoder.decode(exportMessage);\n const params: RequestInit = {\n method: 'POST',\n headers: this.headers,\n body,\n };\n\n fetch(this.url, params)\n .then((response) => {\n if (response.ok) {\n onSuccess();\n } else {\n onError(\n new OTLPExporterError(\n `Exporter received a statusCode: ${response.status}`,\n ),\n );\n }\n })\n .catch((error) => {\n onError(\n new OTLPExporterError(\n `Exception during export: ${error.toString()}`,\n error.code,\n error.stack,\n ),\n );\n });\n }\n\n async shutdown(): Promise<void> {\n // No-op for edge environments\n }\n}\n","/**\n * Shared types for autotel-edge\n */\n\nimport type {\n Attributes,\n Context,\n Span,\n SpanOptions,\n TextMapPropagator,\n} from '@opentelemetry/api';\nimport type {\n ReadableSpan,\n Sampler,\n SpanExporter,\n SpanProcessor,\n} from '@opentelemetry/sdk-trace-base';\n\n// Re-export commonly used types\n\n\n/**\n * Extended SpanOptions with per-span sampler support\n */\nexport interface ExtendedSpanOptions extends SpanOptions {\n sampler?: Sampler;\n}\n\n/**\n * Trigger types for edge handlers\n * Can be a Request or any vendor-specific trigger type\n */\nexport type Trigger =\n | Request\n | DOConstructorTrigger\n | WorkflowTrigger\n | 'do-alarm'\n | unknown;\n\nexport interface DOConstructorTrigger {\n id: string;\n name?: string;\n}\n\nexport interface WorkflowTrigger {\n type: 'workflow';\n name: string;\n}\n\n/**\n * Config types\n */\nexport interface OTLPExporterConfig {\n url: string;\n headers?: Record<string, string>;\n}\n\nexport type ExporterConfig = OTLPExporterConfig | SpanExporter;\n\nexport interface ServiceConfig {\n name: string;\n namespace?: string;\n version?: string;\n}\n\nexport interface ParentRatioSamplingConfig {\n acceptRemote?: boolean;\n ratio: number;\n}\n\ntype HeadSamplerConf = Sampler | ParentRatioSamplingConfig;\n\nexport interface SamplingConfig<HS extends HeadSamplerConf = HeadSamplerConf> {\n headSampler?: HS;\n tailSampler?: TailSampleFn;\n}\n\nexport interface InstrumentationOptions {\n instrumentGlobalFetch?: boolean;\n instrumentGlobalCache?: boolean;\n /**\n * Disable instrumentation entirely (useful for local development)\n * When enabled, the handler is returned as-is without any instrumentation\n * @default false\n */\n disabled?: boolean;\n}\n\n/**\n * Utility types\n */\nexport type OrPromise<T> = T | Promise<T>;\n\n/**\n * Adapter event types\n */\nexport type FunnelStepStatus =\n | 'started'\n | 'completed'\n | 'abandoned'\n | 'failed'\n | (string & {});\n\nexport type OutcomeStatus =\n | 'success'\n | 'failure'\n | 'partial'\n | (string & {});\n\nexport interface EdgeEventBase {\n [key: string]: unknown;\n service: string;\n timestamp: number;\n attributes: Record<string, unknown>;\n traceId?: string;\n spanId?: string;\n correlationId?: string;\n name: string; // Normalized event name for easy access\n}\n\nexport interface EdgeTrackEvent extends EdgeEventBase {\n type: 'event';\n event: string;\n}\n\nexport interface EdgeFunnelStepEvent extends EdgeEventBase {\n type: 'funnel-step';\n funnel: string;\n status: FunnelStepStatus;\n}\n\nexport interface EdgeOutcomeEvent extends EdgeEventBase {\n type: 'outcome';\n operation: string;\n outcome: OutcomeStatus;\n}\n\nexport interface EdgeValueEvent extends EdgeEventBase {\n type: 'value';\n metric: string;\n value: number;\n}\n\nexport type EdgeEvent =\n | EdgeTrackEvent\n | EdgeFunnelStepEvent\n | EdgeOutcomeEvent\n | EdgeValueEvent;\n\nexport type EdgeSubscriber = (event: EdgeEvent) => OrPromise<void>;\n\nexport interface FetcherConfig {\n includeTraceContext?: boolean | ((request: Request) => boolean);\n}\n\nexport interface PostProcessParams {\n /**\n * The request object that was passed to the fetch handler.\n */\n request: Request;\n /**\n * The generated response object.\n */\n response: Response;\n /**\n * A readable version of the span object that can be used to access the span's attributes and events.\n */\n readable: ReadableSpan;\n}\n\nexport interface FetchHandlerConfig {\n /**\n * Whether to enable context propagation for incoming requests to `fetch`.\n * This enables or disables distributed tracing from W3C Trace Context headers.\n * @default true\n */\n acceptTraceContext?: boolean | ((request: Request) => boolean);\n /**\n * Allows further customization of the generated span, based on the request/response data.\n */\n postProcess?: (span: Span, ctx: PostProcessParams) => void;\n}\n\nexport interface HandlerConfig {\n fetch?: FetchHandlerConfig;\n}\n\nexport interface DataSafetyConfig {\n /** Redact query parameters from URL attributes (default: false) */\n redactQueryParams?: boolean;\n /** Control D1 SQL statement capture: 'full' (default), 'obfuscated', or 'off' */\n captureDbStatement?: 'off' | 'obfuscated' | 'full';\n /** Only capture these email headers (lowercase). When set, other headers are excluded. */\n emailHeaderAllowlist?: string[];\n}\n\ninterface EdgeConfigBase {\n service: ServiceConfig;\n handlers?: HandlerConfig;\n fetch?: FetcherConfig;\n postProcessor?: PostProcessorFn;\n sampling?: SamplingConfig;\n propagator?: TextMapPropagator;\n instrumentation?: InstrumentationOptions;\n subscribers?: EdgeSubscriber[];\n /** Opt-in data safety controls for sensitive attribute capture */\n dataSafety?: DataSafetyConfig;\n}\n\ninterface EdgeConfigExporter extends EdgeConfigBase {\n exporter: ExporterConfig;\n}\n\ninterface EdgeConfigSpanProcessors extends EdgeConfigBase {\n spanProcessors: SpanProcessor | SpanProcessor[];\n}\n\nexport type EdgeConfig = EdgeConfigExporter | EdgeConfigSpanProcessors;\n\nexport function isSpanProcessorConfig(\n config: EdgeConfig,\n): config is EdgeConfigSpanProcessors {\n return !!(config as EdgeConfigSpanProcessors).spanProcessors;\n}\n\nexport interface ResolvedEdgeConfig extends EdgeConfigBase {\n handlers: Required<HandlerConfig>;\n fetch: Required<FetcherConfig>;\n postProcessor: PostProcessorFn;\n sampling: Required<SamplingConfig<Sampler>>;\n spanProcessors: SpanProcessor[];\n propagator: TextMapPropagator;\n instrumentation: InstrumentationOptions;\n subscribers: EdgeSubscriber[];\n}\n\n/**\n * Function types\n */\nexport type ResolveConfigFn<Env = any> = (\n env: Env,\n trigger: Trigger,\n) => EdgeConfig;\nexport type ConfigurationOption = EdgeConfig | ResolveConfigFn;\n\nexport type PostProcessorFn = (spans: ReadableSpan[]) => ReadableSpan[];\nexport type TailSampleFn = (traceInfo: LocalTrace) => boolean;\n\nexport interface LocalTrace {\n traceId: string;\n spans: ReadableSpan[];\n localRootSpan: ReadableSpan;\n}\n\n/**\n * Span processor with flush support\n */\nexport type TraceFlushableSpanProcessor = SpanProcessor & {\n forceFlush: (traceId?: string) => Promise<void>;\n};\n\n/**\n * Handler instrumentation\n */\nexport interface InitialSpanInfo {\n name: string;\n options: SpanOptions;\n context?: Context;\n}\n\nexport interface HandlerInstrumentation<T extends Trigger, R extends any> {\n getInitialSpanInfo: (trigger: T) => InitialSpanInfo;\n getAttributesFromResult?: (result: Awaited<R>) => Attributes;\n instrumentTrigger?: (trigger: T) => T;\n executionSucces?: (span: Span, trigger: T, result: Awaited<R>) => void;\n executionFailed?: (span: Span, trigger: T, error?: any) => void;\n}\n\n/**\n * Utility types\n */\n\nexport {type Attributes, type Context, type Span, type SpanOptions} from '@opentelemetry/api';\nexport {type ReadableSpan} from '@opentelemetry/sdk-trace-base';","/**\n * Span processor with flush and tail sampling support\n */\n\nimport type { Context } from '@opentelemetry/api';\nimport type {\n ReadableSpan,\n Span,\n SpanExporter,\n SpanProcessor,\n} from '@opentelemetry/sdk-trace-base';\nimport type { PostProcessorFn, TailSampleFn, LocalTrace } from '../types';\n\n/**\n * Span processor that supports flush by trace ID and tail sampling\n */\nexport class SpanProcessorWithFlush implements SpanProcessor {\n private exporter: SpanExporter;\n private postProcessor?: PostProcessorFn;\n private spans: Map<string, ReadableSpan[]> = new Map();\n\n constructor(exporter: SpanExporter, postProcessor?: PostProcessorFn) {\n this.exporter = exporter;\n this.postProcessor = postProcessor;\n }\n\n onStart(_span: Span, _parentContext: Context): void {\n // No-op for now\n }\n\n onEnd(span: ReadableSpan): void {\n const traceId = span.spanContext().traceId;\n\n if (!this.spans.has(traceId)) {\n this.spans.set(traceId, []);\n }\n\n this.spans.get(traceId)!.push(span);\n }\n\n /**\n * Force flush spans for a specific trace\n */\n async forceFlush(traceId?: string): Promise<void> {\n if (traceId) {\n const spans = this.spans.get(traceId);\n if (spans && spans.length > 0) {\n await this.exportSpans(spans);\n this.spans.delete(traceId);\n }\n } else {\n // Flush all traces\n const promises: Promise<void>[] = [];\n for (const [id, spans] of this.spans.entries()) {\n promises.push(this.exportSpans(spans));\n this.spans.delete(id);\n }\n await Promise.all(promises);\n }\n }\n\n async shutdown(): Promise<void> {\n await this.forceFlush();\n if (this.exporter) {\n await this.exporter.shutdown();\n }\n }\n\n /**\n * Export spans with post-processing\n * Errors are caught and logged but don't throw to prevent worker instability\n */\n private async exportSpans(spans: ReadableSpan[]): Promise<void> {\n if (spans.length === 0) return;\n if (!this.exporter) return; // No exporter configured (e.g., in tests)\n\n let processedSpans = spans;\n\n if (this.postProcessor) {\n try {\n processedSpans = this.postProcessor(spans);\n } catch (error) {\n // Post-processor errors should not prevent export\n console.error('[autotel-edge] Post-processor error:', error);\n // Continue with original spans\n processedSpans = spans;\n }\n }\n\n return new Promise((resolve) => {\n this.exporter.export(processedSpans, (result) => {\n if (result.code === 0) {\n // SUCCESS\n resolve();\n } else {\n // Log but don't reject - exporter failures shouldn't crash the worker\n console.error(\n '[autotel-edge] Exporter error:',\n result.error?.message || 'Unknown error',\n );\n resolve(); // Resolve instead of reject to prevent unhandled promise rejection\n }\n });\n });\n }\n}\n\n/**\n * Span processor that supports tail sampling decisions\n */\nexport class TailSamplingSpanProcessor implements SpanProcessor {\n private wrapped: SpanProcessorWithFlush;\n private tailSampler?: TailSampleFn;\n private traces: Map<string, LocalTrace> = new Map();\n\n constructor(\n exporter: SpanExporter,\n postProcessor?: PostProcessorFn,\n tailSampler?: TailSampleFn,\n ) {\n this.wrapped = new SpanProcessorWithFlush(exporter, postProcessor);\n this.tailSampler = tailSampler;\n }\n\n onStart(span: Span, parentContext: Context): void {\n this.wrapped.onStart(span, parentContext);\n }\n\n onEnd(span: ReadableSpan): void {\n const traceId = span.spanContext().traceId;\n const spanId = span.spanContext().spanId;\n const parentSpanId = 'parentSpanId' in span ? span.parentSpanId : undefined;\n\n // Initialize trace if not exists\n if (!this.traces.has(traceId)) {\n this.traces.set(traceId, {\n traceId,\n spans: [],\n localRootSpan: undefined as any, // Will be set when we identify the local root\n });\n }\n\n const trace = this.traces.get(traceId)!;\n\n // Determine if this span is a local root by checking if its parent is in buffered spans\n // A span is a local root if:\n // 1. It has no parentSpanId (definitive root)\n // 2. Its parentSpanId doesn't match any already-buffered span (remote parent = distributed trace entry)\n const hasLocalParent = parentSpanId &&\n trace.spans.some(s => s.spanContext().spanId === parentSpanId);\n\n // Set localRootSpan if this is the local root (no local parent found in buffer)\n if (!hasLocalParent) {\n trace.localRootSpan = span;\n }\n\n trace.spans.push(span); // Buffer the span AFTER checking parent relationships\n\n // Auto-flush decision: only auto-flush for normal traces (no parentSpanId at all)\n // For distributed traces (parentSpanId present), we rely on explicit forceFlush() from instrument.ts\n // This ensures we don't trigger before all spans have been buffered\n const isDefinitiveRoot = !parentSpanId;\n const shouldAutoFlush = isDefinitiveRoot && trace.localRootSpan &&\n trace.localRootSpan.spanContext().spanId === spanId;\n\n if (shouldAutoFlush) {\n if (this.tailSampler) {\n const shouldKeep = this.tailSampler(trace);\n\n if (shouldKeep) {\n // Export ALL buffered spans in the trace\n for (const bufferedSpan of trace.spans) {\n this.wrapped.onEnd(bufferedSpan);\n }\n // Force flush to actually export the spans\n void this.wrapped.forceFlush(traceId);\n }\n // If not keeping, just drop all spans (don't export)\n } else {\n // No tail sampler, export all buffered spans\n for (const bufferedSpan of trace.spans) {\n this.wrapped.onEnd(bufferedSpan);\n }\n // Force flush to actually export the spans\n void this.wrapped.forceFlush(traceId);\n }\n\n // Clean up trace after decision\n this.traces.delete(traceId);\n }\n // If not local root span, just buffer it - don't export yet\n }\n\n async forceFlush(traceId?: string): Promise<void> {\n if (traceId) {\n // Make tail sampling decision for this specific trace before flushing\n const trace = this.traces.get(traceId);\n if (trace) {\n // Ensure localRootSpan is set (fallback to first span if not)\n // This handles distributed traces where no span has undefined parentSpanId\n if (!trace.localRootSpan && trace.spans.length > 0) {\n trace.localRootSpan = trace.spans[0];\n }\n\n if (this.tailSampler) {\n const shouldKeep = this.tailSampler(trace);\n\n if (shouldKeep) {\n // Export ALL buffered spans in the trace\n for (const bufferedSpan of trace.spans) {\n this.wrapped.onEnd(bufferedSpan);\n }\n }\n } else {\n // No tail sampler, export all buffered spans\n for (const bufferedSpan of trace.spans) {\n this.wrapped.onEnd(bufferedSpan);\n }\n }\n\n // Clean up trace after decision\n this.traces.delete(traceId);\n }\n }\n return this.wrapped.forceFlush(traceId);\n }\n\n async shutdown(): Promise<void> {\n this.traces.clear();\n return this.wrapped.shutdown();\n }\n}\n","/**\n * Configuration system for autotel-edge\n */\n\nimport { W3CTraceContextPropagator } from '@opentelemetry/core';\nimport { ParentBasedSampler, AlwaysOnSampler } from '@opentelemetry/sdk-trace-base';\nimport { context as api_context, createContextKey, type Context } from '@opentelemetry/api';\nimport type {\n EdgeConfig,\n ResolvedEdgeConfig,\n ConfigurationOption,\n Trigger,\n ParentRatioSamplingConfig,\n} from '../types';\nimport { isSpanProcessorConfig } from '../types';\nimport { OTLPExporter } from './exporter';\nimport { TailSamplingSpanProcessor } from './spanprocessor';\n\n/**\n * Type for config initialization function\n */\nexport type Initialiser = (env: any, trigger: Trigger) => ResolvedEdgeConfig;\n\n/**\n * Context key for storing config (isolates config per-request)\n */\nconst CONFIG_KEY = createContextKey('autotel-edge-config');\n\n/**\n * Get the currently active config from context\n *\n * This reads the config from the active context, ensuring each request\n * has its own isolated config even when multiple requests are in-flight.\n */\nexport function getActiveConfig(): ResolvedEdgeConfig | null {\n const value = api_context.active().getValue(CONFIG_KEY) as\n | ResolvedEdgeConfig\n | null\n | undefined;\n return value ?? null;\n}\n\n/**\n * Set the active config in context\n *\n * Returns a new context with the config stored. This context should be\n * used with api_context.with() to ensure the config is isolated per-request.\n *\n * @example\n * ```typescript\n * const config = parseConfig({ service: { name: 'my-service' } });\n * const context = setConfig(config);\n *\n * api_context.with(context, () => {\n * // Config is available here via getActiveConfig()\n * });\n * ```\n */\nexport function setConfig(config: ResolvedEdgeConfig): Context {\n return api_context.active().setValue(CONFIG_KEY, config);\n}\n\n/**\n * Parse and validate configuration\n */\nexport function parseConfig(config: EdgeConfig): ResolvedEdgeConfig {\n // Parse head sampler\n const headSampler =\n config.sampling?.headSampler ??\n new ParentBasedSampler({\n root: new AlwaysOnSampler(),\n });\n\n const parsedHeadSampler =\n typeof headSampler === 'object' && 'ratio' in headSampler\n ? createParentRatioSampler(headSampler)\n : headSampler;\n\n // Parse tail sampler (default: keep sampled or error traces)\n const tailSampler =\n config.sampling?.tailSampler ??\n ((traceInfo) => {\n const localRootSpan = traceInfo.localRootSpan;\n const ctx = localRootSpan.spanContext();\n // Keep if sampled or if root span has error\n return (ctx.traceFlags & 1) === 1 || localRootSpan.status.code === 2; // SAMPLED flag | ERROR status\n });\n\n // Parse exporter - use TailSamplingSpanProcessor when tail sampler is present\n const spanProcessors = isSpanProcessorConfig(config)\n ? Array.isArray(config.spanProcessors)\n ? config.spanProcessors\n : [config.spanProcessors]\n : [\n // Use TailSamplingSpanProcessor to enable tail sampling\n new TailSamplingSpanProcessor(\n typeof config.exporter === 'object' && 'url' in config.exporter\n ? new OTLPExporter(config.exporter)\n : config.exporter,\n config.postProcessor,\n tailSampler, // Wire up the tail sampler!\n ),\n ];\n\n // Build resolved config\n const resolved: ResolvedEdgeConfig = {\n service: config.service,\n handlers: {\n fetch: config.handlers?.fetch ?? {},\n },\n fetch: {\n includeTraceContext: config.fetch?.includeTraceContext ?? true,\n },\n postProcessor: config.postProcessor ?? ((spans) => spans),\n sampling: {\n headSampler: parsedHeadSampler,\n tailSampler,\n },\n spanProcessors,\n propagator: config.propagator ?? new W3CTraceContextPropagator(),\n instrumentation: {\n instrumentGlobalFetch: config.instrumentation?.instrumentGlobalFetch ?? true,\n instrumentGlobalCache: config.instrumentation?.instrumentGlobalCache ?? false,\n disabled: config.instrumentation?.disabled ?? false,\n },\n subscribers: config.subscribers ?? [],\n dataSafety: config.dataSafety,\n };\n\n return resolved;\n}\n\n/**\n * Create a parent-based ratio sampler\n */\nfunction createParentRatioSampler(config: ParentRatioSamplingConfig) {\n const { ratio, acceptRemote = true } = config;\n\n // Simple ratio sampler\n const ratioSampler = {\n shouldSample: () => ({\n decision: Math.random() < ratio ? 1 : 0, // RECORD_AND_SAMPLED : NOT_RECORD\n attributes: {},\n }),\n toString: () => `ParentRatioSampler{ratio=${ratio}}`,\n };\n\n if (acceptRemote) {\n return new ParentBasedSampler({ root: ratioSampler as any });\n }\n\n return ratioSampler;\n}\n\n/**\n * Create a config initializer function\n */\nexport function createInitialiser(config: ConfigurationOption): Initialiser {\n if (typeof config === 'function') {\n return (env, trigger) => {\n const conf = parseConfig(config(env, trigger));\n return conf;\n };\n } else {\n const parsed = parseConfig(config);\n return () => parsed;\n }\n}\n"]}