UNPKG

autotel

Version:
262 lines (259 loc) 7.88 kB
'use strict'; var chunkUTZR7P7E_cjs = require('./chunk-UTZR7P7E.cjs'); require('./chunk-GML3FBOT.cjs'); require('./chunk-D5LMF53P.cjs'); require('./chunk-JSNUWSBH.cjs'); require('./chunk-HZ3FYBJG.cjs'); require('./chunk-563EL6O6.cjs'); require('./chunk-OC6X2VIN.cjs'); require('./chunk-CEAQK2QY.cjs'); require('./chunk-ZNMBW67B.cjs'); require('./chunk-IOYFAFHJ.cjs'); require('./chunk-CMNGGTQL.cjs'); require('./chunk-CU6IDACR.cjs'); require('./chunk-6S5RUKU3.cjs'); require('./chunk-YS6C2YJE.cjs'); require('./chunk-VH77IPJN.cjs'); require('./chunk-FU6R566Y.cjs'); require('./chunk-ESLWRGAG.cjs'); require('./chunk-YREV3LGG.cjs'); require('./chunk-JEQ2X3Z6.cjs'); var api = require('@opentelemetry/api'); var InMemoryTraceContextStore = class { constructor(options = {}) { this.options = options; const cleanupMs = options.cleanupIntervalMs ?? 6e4; if (cleanupMs > 0) { this.cleanupInterval = setInterval(() => this.cleanup(), cleanupMs); if (this.cleanupInterval.unref) { this.cleanupInterval.unref(); } } } options; store = /* @__PURE__ */ new Map(); cleanupInterval = null; async save(key, context) { this.store.set(key, context); } async load(key) { const context = this.store.get(key); if (!context) { return null; } if (context.ttlMs) { const age = Date.now() - context.parkedAt; if (age > context.ttlMs) { this.store.delete(key); return null; } } return context; } async delete(key) { this.store.delete(key); } /** * Get number of stored contexts (for testing) */ get size() { return this.store.size; } /** * Clear all stored contexts (for testing) */ clear() { this.store.clear(); } /** * Stop the cleanup interval */ destroy() { if (this.cleanupInterval) { clearInterval(this.cleanupInterval); this.cleanupInterval = null; } } cleanup() { const now = Date.now(); for (const [key, context] of this.store.entries()) { if (context.ttlMs) { const age = now - context.parkedAt; if (age > context.ttlMs) { this.store.delete(key); } } } } }; function createParkingLot(config) { const { store, defaultTTLMs = 24 * 60 * 60 * 1e3, // 24 hours keyPrefix = "parkingLot:", autoDeleteOnRetrieve = true, onMiss } = config; function getCurrentSpanContext() { const activeSpan = api.trace.getActiveSpan(); if (!activeSpan) { return null; } return activeSpan.spanContext(); } function prefixKey(key) { return `${keyPrefix}${key}`; } const parkingLot = { async park(correlationKey, metadata) { const spanContext = getCurrentSpanContext(); const fullKey = prefixKey(correlationKey); const storedContext = { traceId: spanContext?.traceId ?? "", spanId: spanContext?.spanId ?? "", traceFlags: spanContext?.traceFlags ?? 0, parkedAt: Date.now(), ttlMs: defaultTTLMs, metadata }; await store.save(fullKey, storedContext); const activeSpan = api.trace.getActiveSpan(); if (activeSpan) { activeSpan.addEvent("trace_context_parked", { "parking_lot.correlation_key": correlationKey, "parking_lot.ttl_ms": defaultTTLMs, ...metadata && Object.fromEntries( Object.entries(metadata).map(([k, v]) => [ `parking_lot.metadata.${k}`, v ]) ) }); } return correlationKey; }, async retrieve(correlationKey) { const fullKey = prefixKey(correlationKey); const storedContext = await store.load(fullKey); if (!storedContext) { onMiss?.(correlationKey); return null; } if (autoDeleteOnRetrieve) { await store.delete(fullKey); } return storedContext; }, traceCallback(callbackConfig) { return (fnFactory) => { return chunkUTZR7P7E_cjs.trace( { name: callbackConfig.name, spanKind: api.SpanKind.SERVER }, (baseCtx) => { return async (...args) => { const correlationKey = callbackConfig.correlationKeyFrom(args); const parkedContext = await parkingLot.retrieve(correlationKey); const elapsedMs = parkedContext ? Date.now() - parkedContext.parkedAt : null; baseCtx.setAttribute( "parking_lot.correlation_key", correlationKey ); if (parkedContext) { baseCtx.setAttribute("parking_lot.elapsed_ms", elapsedMs); baseCtx.setAttribute( "parking_lot.original_trace_id", parkedContext.traceId ); baseCtx.setAttribute( "parking_lot.original_span_id", parkedContext.spanId ); if (parkedContext.metadata) { for (const [key, value] of Object.entries( parkedContext.metadata )) { baseCtx.setAttribute(`parking_lot.metadata.${key}`, value); } } const link = parkingLot.createLink(parkedContext); baseCtx.addLinks([link]); baseCtx.addEvent("parked_context_retrieved", { "parking_lot.correlation_key": correlationKey, "parking_lot.elapsed_ms": elapsedMs, "parking_lot.original_trace_id": parkedContext.traceId }); } else { baseCtx.setAttribute("parking_lot.context_found", false); if (callbackConfig.requireParkedContext) { const error = new Error( `Required parked context not found for key: ${correlationKey}` ); baseCtx.recordException(error); throw error; } } if (callbackConfig.attributes) { for (const [key, value] of Object.entries( callbackConfig.attributes )) { baseCtx.setAttribute(key, value); } } const callbackCtx = { ...baseCtx, parkedContext, elapsedMs, correlationKey }; const userFn = fnFactory(callbackCtx); return userFn(...args); }; } ); }; }, createLink(storedContext) { return { context: { traceId: storedContext.traceId, spanId: storedContext.spanId, traceFlags: storedContext.traceFlags, isRemote: true }, attributes: { "link.type": "parking_lot", "parking_lot.parked_at": storedContext.parkedAt, ...storedContext.metadata && { "parking_lot.has_metadata": true } } }; }, async exists(correlationKey) { const fullKey = prefixKey(correlationKey); const context = await store.load(fullKey); return context !== null; } }; return parkingLot; } function createCorrelationKey(...parts) { return parts.map(String).join(":"); } function toSpanContext(storedContext) { return { traceId: storedContext.traceId, spanId: storedContext.spanId, traceFlags: storedContext.traceFlags, isRemote: true }; } exports.InMemoryTraceContextStore = InMemoryTraceContextStore; exports.createCorrelationKey = createCorrelationKey; exports.createParkingLot = createParkingLot; exports.toSpanContext = toSpanContext; //# sourceMappingURL=webhook.cjs.map //# sourceMappingURL=webhook.cjs.map