@lunora/runtime 1.0.0-alpha.280 → 1.0.0-alpha.282
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/LICENSE.md +201 -224
- package/README.md +3 -3
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.mjs +1 -1
- package/dist/packem_shared/{analyticsEngineSink-9QgJpNLs.mjs → analyticsEngineSink-CPMYRnjL.mjs} +1 -1
- package/dist/packem_shared/{composeWorker-ka0CpeBE.mjs → composeWorker-CpqLEos0.mjs} +16 -16
- package/dist/packem_shared/enrichSink-8_qnPWNH.mjs +1 -0
- package/dist/packem_shared/observability-sinks-BA76CH4h.mjs +6 -0
- package/package.json +3 -3
- package/dist/packem_shared/enrichSink-D4Q4jLT5.mjs +0 -1
- package/dist/packem_shared/observability-sinks-CTGNb3dU.mjs +0 -6
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{redactArgs as b,redactCredentials as y}from"@lunora/observability";import{S as O,n as S,t as x,e as E}from"./observability-sinks-BA76CH4h.mjs";const d=t=>{try{t()}catch{}},m=(t,o)=>{const r={};for(const n of O)t[n]!==void 0&&(r[n]=t[n]);return t.flush&&(r.flush=n=>{d(()=>t.flush?.(n))}),t.onLog&&(r.onLog=(n,c)=>{const e=o.log===void 0?n:o.log(n);e!==void 0&&d(()=>t.onLog?.(e,c))}),t.onMetric&&(r.onMetric=(n,c)=>{const e=o.metric===void 0?n:o.metric(n);e!==void 0&&d(()=>t.onMetric?.(e,c))}),t.onRpc&&(r.onRpc=(n,c)=>{const e=o.rpc===void 0?n:o.rpc(n);e!==void 0&&d(()=>t.onRpc?.(e,c))}),t.onSpan&&(r.onSpan=(n,c)=>{const e=o.span===void 0?n:o.span(n);e!==void 0&&d(()=>t.onSpan?.(e,c))}),r},g=5;let l=0;const j=t=>{if(l>=g)return;l+=1;const o=l===g?" Further enricher failures are silenced until the isolate restarts.":"";try{console.error(`[lunora:observability] enrichSink's enricher threw; delivering the event without its extra attributes.${o}`,t instanceof Error?t.name:typeof t)}catch{}},R=(t,o,r)=>{if(t instanceof Error)return r==="full"?x(t,o):E(t);const n=Reflect.get(t,"toJSON");if(typeof n=="function"){const e=n.call(t);return e!==t?o(e):Object.fromEntries(Object.entries(t).map(([i,s])=>[i,o(s)]))}if(Array.isArray(t))return t.map(e=>o(e));const c=Object.getPrototypeOf(t);return c!==Object.prototype&&c!==null?t:Object.fromEntries(Object.entries(t).map(([e,i])=>[e,o(i)]))},u=t=>({ancestors:new WeakSet,copies:new WeakMap,errors:t}),a=(t,o)=>{if(typeof t=="symbol"||typeof t=="function"||typeof t=="bigint")return String(t);if(typeof t!="object"||t===null)return t;if(!o.ancestors.has(t)){if(o.copies.has(t))return o.copies.get(t);o.ancestors.add(t);try{const r=R(t,n=>a(n,o),o.errors);return o.copies.set(t,r),r}finally{o.ancestors.delete(t)}}},p=(t,o,r)=>{try{const n=t(a(o,u("full")),r),c=n===void 0?void 0:Object.fromEntries(Object.entries(n).filter(([,f])=>f!==void 0)),e=r==="log"?"full":"headline",i=c===void 0?void 0:a(c,u(e)),s=r==="log"?b(i):y(i);return S(s,void 0,e)}catch(n){j(n);return}},M=(t,o)=>m(t,{log:r=>{const n=p(o,r,"log");return n===void 0?r:{...r,fields:{...n,...r.fields}}},metric:r=>{const n=p(o,r,"metric");return n===void 0?r:{...r,attributes:{...n,...r.attributes}}},rpc:r=>{const n=p(o,r,"rpc");return n===void 0?r:{...r,attributes:{...n,...r.attributes}}},span:r=>{const n=p(o,r,"span");return n===void 0?r:{...r,attributes:{...n,...r.attributes}}}}),h=(t,o)=>{const r=t.split("*");if(r.length===1)return o===t;const n=r[0]??"",c=r.at(-1)??"";if(o.length<n.length+c.length||!o.startsWith(n)||!o.endsWith(c))return!1;const e=o.length-c.length;let i=n.length;for(const s of r.slice(1,-1)){const f=o.indexOf(s,i);if(f===-1||f+s.length>e)return!1;i=f+s.length}return i<=e},F=(t,o)=>o.exclude?.some(r=>h(r,t))?!1:o.include===void 0||o.include.length===0||o.include.some(r=>h(r,t)),I=(t,o)=>{const r=n=>F(n.functionPath,o)?n:void 0;return m(t,{log:r,metric:r,rpc:r,span:r})};export{M as enrichSink,I as filterSink,h as globMatch};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import{LunoraError as lt}from"@lunora/errors";import{redactCredentials as ft,redactArgs as M}from"@lunora/observability";import{stableStringify as pt}from"@lunora/wire";import{e as l,L as h,c as W,o as G,f as S,O as Y,g as mt,h as q,w as ht,i as yt,j as gt,m as bt}from"./otlp-resource-BBR8xs6m.mjs";import{t as kt}from"./error-detail-C46mdOTW.mjs";const Z=t=>{const r=String(t.name),o=String(t.message);return o===""?r:`${r}: ${o}`},Q=t=>{if(typeof t=="string")return t;if(t instanceof Error)return Z(t);try{return JSON.stringify(t)??String(t)}catch{return String(t)}},St=4,It=10,v=3584,Et=4,J=512,Tt=["all","root","short","none","minimal"],tt=8,Ot=t=>typeof t=="object"&&t!==null&&!Array.isArray(t),rt=t=>{if(!Ot(t))return!1;const r=Object.getPrototypeOf(t);return r===Object.prototype||r===null},j=(t,r=0,o=new WeakSet)=>t instanceof Error?!0:typeof t!="object"||t===null||r>=tt||o.has(t)?!1:(o.add(t),Array.isArray(t)?t.some(s=>j(s,r+1,o)):rt(t)&&Object.values(t).some(s=>j(s,r+1,o))),ot=t=>{if(typeof t=="bigint")return String(t);if(typeof t!="object"||t===null)return t;try{return JSON.stringify(t),t}catch{return}},At=(t,r,o)=>{if(t!==void 0)return r==="none"||r==="minimal"||r==="root"&&o>0?"":r==="short"?t.split(`
|
|
2
|
+
`).slice(0,Et+1).join(`
|
|
3
|
+
`):t},st=(t,r,o,s)=>({...s===void 0?{}:{child:s},errors:t,mask:o,memo:new WeakMap,mode:r,path:new Set}),et=(t,r,o)=>{if(!r.path.has(t)){if(r.memo.has(t))return r.memo.get(t);r.path.add(t);try{const s=o();return r.memo.set(t,s),s}finally{r.path.delete(t)}}},nt=(t,r,o)=>{Object.defineProperty(t,r,{configurable:!0,enumerable:!0,value:o,writable:!0})},x=(t,r,o)=>r.path.has(t)||o>St?{message:t.message,name:t.name}:et(t,r,()=>{const s=n=>n instanceof Error?x(n,r,o+1):(r.child??(u=>w(u,r,0,o+1)))(n),e=r.mode==="minimal",i=n=>n.length>J?`${n.slice(0,J)}…`:n,a=n=>e&&!(n instanceof Error)?i(Q(n)):s(n),p={message:e?i(t.message):t.message,name:t.name};if(!e)for(const[n,u]of Object.entries(t))n!=="message"&&n!=="name"&&nt(p,n,s(u));const y=At(t.stack,r.mode,o);return y!==void 0&&(p.stack=y),t.cause!==void 0&&(p.cause=a(t.cause)),t instanceof AggregateError&&Array.isArray(t.errors)&&(p.errors=t.errors.slice(0,It).map(n=>a(n))),r.mask(p)}),w=(t,r,o,s)=>t instanceof Error?r.errors==="full"?x(t,r,s):Z(t):typeof t!="object"||t===null||o>=tt||!(Array.isArray(t)||rt(t))?ot(t):et(t,r,()=>Array.isArray(t)?t.map(e=>w(e,r,o+1,s)):Object.fromEntries(Object.entries(t).map(([e,i])=>[e,w(i,r,o+1,s)]))),Pt=t=>{try{return JSON.stringify(t).length}catch{return Number.POSITIVE_INFINITY}},U=t=>t,rr=(t,r=ot)=>{let o;for(const s of Tt)if(o=x(t,st("full",s,U,r),0),Pt(o)<=v)break;return o},Rt=(t,r,o)=>{const s=c=>JSON.stringify(w(t,st(r,c,o),0,0)),e=c=>c===void 0||c.length<=v,i=s("all");if(r==="headline"||e(i))return i;const a=s("minimal");if(!e(a))return a;for(const c of["root","short","none"]){const p=s(c);if(e(p))return p}return a},_=(t,r="headline",o=U)=>{if(typeof t=="boolean"||typeof t=="number"||typeof t=="string")return t;try{return!j(t)||r==="headline"&&t instanceof Error?Q(t):Rt(t,r,o)??String(t)}catch{return String(t)}},or=(t,r,o="headline",s=U)=>{if(t===void 0&&r===void 0)return;const e={};if(t!==void 0)for(const[i,a]of Object.entries(t))nt(e,i,_(a,o,s));return Object.keys(e).length===0?void 0:e},wt=512,_t=200,Lt=t=>{const r=t.maxItems??wt,o=t.maxDelayMs??_t;let s=[],e,i,a;const c=()=>{e!==void 0&&(clearTimeout(e),e=void 0)},p=async()=>{c();const n=s;s=[];const u=a;i=void 0,a=void 0;try{n.length>0&&await t.export(n)}catch{}finally{u?.()}},y=n=>{i===void 0&&(i=new Promise(u=>{a=u}),e=setTimeout(()=>{p()},o)),n?.(i)};return{add:(n,u)=>{for(s.push(n);s.length>r;)s.shift();y(u),s.length>=r&&p()},flush:async n=>{if(s.length===0){c();return}const u=p();return n?.(u),u},get size(){return s.length}}},Nt=1,Mt=0,jt=new Set(["cloudflare.ray_id","error.type","http.request.method","http.response.status_code","http.route","lunora.error_status","lunora.fanout.failed","lunora.fanout.shards","lunora.fanout.table","lunora.function_path","lunora.ok","lunora.shard_key","server.address","server.port","url.path","url.scheme","user_agent.original"]),xt={errorType:h.errorType,functionPath:h.functionPath,rayId:h.rayId,shardKey:h.shardKey,userId:h.userId},Ut=t=>{const r=kt(t);return r===void 0?[]:[l("lunora.error.internal",JSON.stringify(ft(r)))]},at=(t,r,o,s)=>[l("exception.type",t),l("exception.message",r),...o===void 0?[]:[l("exception.stacktrace",o)],...s===void 0?[]:Ut(s)],Dt=(t,r)=>{const o=[l(h.functionPath,t.functionPath),l(h.ok,t.ok)];if(t.method!==void 0&&o.push(l("http.request.method",t.method)),t.path!==void 0&&o.push(l("url.path",t.path)),o.push(l("http.route",t.functionPath)),t.scheme!==void 0&&o.push(l("url.scheme",t.scheme)),t.host!==void 0&&o.push(l("server.address",t.host)),t.port!==void 0&&o.push(l("server.port",t.port)),t.userAgent!==void 0&&o.push(l("user_agent.original",t.userAgent)),t.shardKey!==void 0&&o.push(l(h.shardKey,t.shardKey)),t.rayId!==void 0&&o.push(l(h.rayId,t.rayId)),o.push(l("http.response.status_code",t.error?.status??200)),t.error&&o.push(l(h.errorType,t.error.code),l("lunora.error_status",t.error.status)),t.fanOut&&o.push(l("lunora.fanout.table",t.fanOut.table),l("lunora.fanout.shards",t.fanOut.shards),l("lunora.fanout.failed",t.fanOut.failed)),t.attributes!==void 0){const e=new Set([...jt,...o.map(a=>a.key)]),i=W(Object.fromEntries(Object.entries(t.attributes).map(([a,c])=>[a,_(c)])));o.push(...i.filter(a=>!e.has(a.key)))}const s={attributes:o,endTimeUnixNano:S(r),kind:Y.server,name:t.functionPath,...t.parentSpanId===void 0?{}:{parentSpanId:t.parentSpanId},spanId:t.spanId??G(8),startTimeUnixNano:S(r-t.durationMs),status:t.ok?{code:1}:{code:2,message:t.error?.message??""},traceId:t.traceId??G(16)};return t.traceFlags!==void 0&&(s.flags=t.traceFlags),t.error&&(s.events=[{attributes:at(t.error.code,t.error.message,t.error.stack,t.error.internal),name:"exception",timeUnixNano:S(r)}]),s},D=(t,r)=>{const o=new Map,s=new Set(Object.keys(t).map(e=>xt[e]));for(const[e,i]of Object.entries(r??{}))s.has(e)||o.set(e,l(e,_(i)));return o.set(h.functionPath,l(h.functionPath,t.functionPath)),t.shardKey!==void 0&&o.set(h.shardKey,l(h.shardKey,t.shardKey)),t.userId!==void 0&&o.set(h.userId,l(h.userId,t.userId)),t.rayId!==void 0&&o.set(h.rayId,l(h.rayId,t.rayId)),t.errorType!==void 0&&o.set(h.errorType,l(h.errorType,t.errorType)),[...o.values()]},Ft=t=>{const r={attributes:D({errorType:t.error?.type,functionPath:t.functionPath,rayId:t.rayId,shardKey:t.shardKey,userId:t.userId},t.attributes),endTimeUnixNano:S(t.startTs+t.durationMs),flags:t.sampled===!1?Mt:Nt,kind:Y[t.kind??"internal"],name:t.name,parentSpanId:t.parentSpanId,spanId:t.spanId,startTimeUnixNano:S(t.startTs),status:t.ok?{code:1}:{code:2,message:t.error?.message??""},traceId:t.traceId},o=s=>W(Object.fromEntries(Object.entries(s??{}).map(([e,i])=>[e,_(i)])));return t.events!==void 0&&t.events.length>0&&(r.events=t.events.map(s=>({attributes:o(s.attributes),name:s.name,timeUnixNano:S(s.ts)}))),t.links!==void 0&&t.links.length>0&&(r.links=t.links.map(s=>({attributes:o(s.attributes),spanId:s.spanId,traceId:s.traceId}))),r},Kt=t=>{const r=S(t.ts),o=D({functionPath:t.functionPath,shardKey:t.shardKey},t.attributes),s=t.traceId===void 0?void 0:[{asDouble:t.value,timeUnixNano:r,traceId:t.traceId}],e={asDouble:t.value,attributes:o,...s===void 0?{}:{exemplars:s},timeUnixNano:r};return t.kind==="gauge"?{gauge:{dataPoints:[e]},name:t.name}:t.kind==="histogram"?{histogram:{aggregationTemporality:1,dataPoints:[{attributes:o,bucketCounts:["1"],count:"1",...s===void 0?{}:{exemplars:s},explicitBounds:[],max:t.value,min:t.value,sum:t.value,timeUnixNano:r}]},name:t.name}:{name:t.name,sum:{aggregationTemporality:1,dataPoints:[e],isMonotonic:!0}}},$t=t=>{const r={attributes:D({functionPath:t.functionPath,rayId:t.rayId,shardKey:t.shardKey,userId:t.userId},t.fields),body:{stringValue:t.message},severityNumber:mt[t.level],severityText:t.level.toUpperCase(),timeUnixNano:S(t.ts)};if(t.traceId!==void 0&&(r.traceId=t.traceId),t.spanId!==void 0&&(r.spanId=t.spanId),t.error!==void 0){const o=at(t.error.name,t.error.message,t.error.stack,t.error.internal),s=new Set(o.map(e=>e.key));r.attributes=[...r.attributes.filter(e=>!s.has(e.key)),...o]}return t.eventName!==void 0&&(r.eventName=t.eventName,r.attributes=[...r.attributes.filter(o=>o.key!=="event.name"),l("event.name",t.eventName)]),r},Ht=1024,V=5;let N=0;const Ct=(t,r)=>{if(N>=V)return;N+=1;let o;try{o=new URL(t).host}catch{o="<unparseable endpoint>"}const s=N===V?" Further OTLP rejections are silenced until the isolate restarts.":"";console.error(`[lunora:otlp] collector ${o} rejected the export with HTTP ${String(r)}; the batch is DROPPED (there is no retry).${s}`)},Bt=async t=>{const r=new Blob([t]).stream().pipeThrough(new CompressionStream("gzip"));return new Response(r).arrayBuffer()},it=async(t,r,o,s)=>{try{const e=JSON.stringify(r),{byteLength:i}=new TextEncoder().encode(e),a=(i<Ht?fetch(t,{body:e,headers:o,method:"POST"}):Bt(e).then(c=>fetch(t,{body:c,headers:{...o,"content-encoding":"gzip"},method:"POST"}))).then(c=>{c.ok||Ct(t,c.status)},()=>{});s?.(a),await a}catch{}},Gt=(t,r,o,s)=>{it(t,r,o,s?.waitUntil).catch(()=>{})},P=(t,r)=>r===!0&&t.ok,Jt=t=>t.kind==="metric"?void 0:t.event.traceId,Vt=t=>{const r=Map.groupBy(t,s=>Jt(s)),o=r.get(void 0)??[];return r.delete(void 0),{byTrace:r,untraced:o}},X=5,Xt=(t,r,o)=>{if(r===void 0)return t;const{byTrace:s,untraced:e}=Vt(t),i=[...e];let a=0,c;for(const[p,y]of s){let n;try{n=r({logs:y.filter(u=>u.kind==="log").map(u=>u.event),rpc:y.filter(u=>u.kind==="rpc").map(u=>u.event),spans:y.filter(u=>u.kind==="span").map(u=>u.event),traceId:p})}catch(u){a+=1,a===1&&(c=u),n=!0}n&&i.push(...y)}return a>0&&o(c,a),i},O=(t,r)=>{let o;try{o=r===void 0?t:r(t)}catch{o=void 0}return o??void 0},zt=t=>{const r=M(t.message);if(t.stack===void 0)return{...t,message:r};const o=`: ${t.message}`,s=t.stack.indexOf(o),i=t.message!==""&&s!==-1&&!t.stack.slice(0,s).includes(`
|
|
4
|
+
`)?t.stack.slice(s+o.length):t.stack.split(`
|
|
5
|
+
`).filter(a=>a.trimStart().startsWith("at ")).map(a=>`
|
|
6
|
+
${a}`).join("");return{...t,message:r,stack:`${t.name}: ${r}${i}`}},Wt=t=>({...t,...t.error===void 0?{}:{error:zt(t.error)},...t.fields===void 0?{}:{fields:M(t.fields)},message:M(t.message)}),z=(t,r,o)=>{if(t.kind==="rpc"){const e=O(t.event,r?.rpc);return e===void 0?void 0:{bucket:"spans",encoded:Dt(e,t.endMs)}}if(t.kind==="span"){const e=O(t.event,r?.span);return e===void 0?void 0:{bucket:"spans",encoded:Ft(e)}}if(t.kind==="log"){const e=O(o?Wt(t.event):t.event,r?.log);return e===void 0?void 0:{bucket:"logs",encoded:$t(e)}}const s=O(t.event,r?.metric);return s===void 0?void 0:{bucket:"metrics",encoded:Kt(s)}},Yt=["fuseCloudflareTraces","instrumentDatabase","metricHistory","traceFetch"],sr=(t={})=>{const{onlyErrors:r}=t;return{onLog:o=>{o.level==="error"||o.level==="fatal"?console.error("[lunora:log]",o.functionPath,o.message):console.log("[lunora:log]",o.functionPath,o.message)},onMetric:o=>{console.log("[lunora:metric]",`${o.name}=${String(o.value)}`,o.kind,o.functionPath)},onRpc:o=>{P(o,r)||(o.ok?console.log("[lunora:rpc]",o):console.error("[lunora:rpc]",o))},onSpan:o=>{const s=o.ok?"ok":`error ${o.error?.type??""}`.trim();console.log("[lunora:span]",o.name,`${String(o.durationMs)}ms`,s,o.functionPath)}}},er=t=>{const{headers:r,onlyErrors:o,transform:s,transformLog:e,url:i}=t,a=q({"content-type":"application/json"},r),c=(p,y)=>{try{const n=fetch(i,{body:JSON.stringify(p),headers:a,method:"POST"}).catch(()=>{});y?.waitUntil&&y.waitUntil(n)}catch{}};return{onLog:(p,y)=>{const n=O(p,e);n!==void 0&&c(n,y)},onRpc:(p,y)=>{if(P(p,o))return;const n=O(p,s);n!==void 0&&c(n,y)}}},nr=t=>{const{dataset:r,onlyErrors:o}=t;return{onRpc:s=>{if(!P(s,o))try{r.writeDataPoint({blobs:[s.functionPath,s.ok?"ok":"error",s.shardKey??"",s.error?.code??"",s.fanOut?.table??""],doubles:[s.durationMs,s.ok?0:1,s.fanOut?.shards??0,s.fanOut?.failed??0],indexes:[s.functionPath]})}catch{}}}},ar=t=>{const{pipeline:r,serializeFields:o}=t;return{onLog:(s,e)=>{try{const i={functionPath:s.functionPath,level:s.level,message:s.message,ts:s.ts};s.fields&&(i.fields=o===!0?JSON.stringify(s.fields):s.fields);for(const c of["shardKey","userId","traceId","spanId"])s[c]!==void 0&&(i[c]=s[c]);const a=r.send([i]).catch(()=>{});e?.waitUntil&&e.waitUntil(a)}catch{}}}},ir=t=>{const{batch:r,deploymentEnvironment:o,detectResources:s,endpoint:e,headers:i,onlyErrors:a,postProcessor:c,resourceAttributes:p,serviceNamespace:y,serviceVersion:n,tailSampler:u,token:ct}=t,F=t.serviceName??"lunora",K=t.redactLogs??!0;if(r!==!1&&r?.maxItems!==void 0&&(!Number.isInteger(r.maxItems)||r.maxItems<1))throw new lt("ENV_INVALID",`otlpSink: \`batch.maxItems\` must be a positive integer, received ${String(r.maxItems)}.`);const $={...n===void 0?{}:{"service.version":n},...y===void 0?{}:{"service.namespace":y},...o===void 0?{}:{"deployment.environment":o},...p},H=new WeakMap,k=f=>{if(s!==!0||f?.resourceAttributes===void 0)return $;const d=H.get(f);if(d!==void 0)return d;const m=bt(f.resourceAttributes(),$);return H.set(f,m),m};let I=e;for(;I.endsWith("/");)I=I.slice(0,-1);const C={logs:{url:`${I}/v1/logs`,wrap:gt},metrics:{url:`${I}/v1/metrics`,wrap:yt},spans:{url:`${I}/v1/traces`,wrap:ht}},B=q({"content-type":"application/json"},i,ct);let L=0;const dt=(f,d)=>{if(L>=X)return;L+=1;const m=L===X?" Further tailSampler failures from this sink are silenced until the isolate restarts.":"";console.error(`[lunora:otlp] tailSampler threw for ${String(d)} trace(s) in this flush window; keeping them (fail-open), so the sampling policy did NOT apply.${m}`,f)},ut=async f=>{const d=Xt(f,u,dt),m=new Map;for(const g of d){const b=z(g,c,K);if(b===void 0)continue;const R=pt(g.resource);let T=m.get(R);T===void 0&&(T={logs:[],metrics:[],resource:g.resource,spans:[]},m.set(R,T)),T[b.bucket].push(b.encoded)}const E=[];for(const[,g]of m)for(const b of["spans","logs","metrics"])if(g[b].length>0){const{url:R,wrap:T}=C[b];E.push(it(R,T(g[b],"@lunora/runtime",F,g.resource),B))}await Promise.all(E)};if(r===!1){const f=(d,m)=>{const E=z(d,c,K);if(E!==void 0){const{url:g,wrap:b}=C[E.bucket];Gt(g,b(E.encoded,"@lunora/runtime",F,d.resource),B,m)}};return{onLog:(d,m)=>{f({event:d,kind:"log",resource:k(m)},m)},onMetric:(d,m)=>{f({event:d,kind:"metric",resource:k(m)},m)},onRpc:(d,m)=>{P(d,a)||f({endMs:Date.now(),event:d,kind:"rpc",resource:k(m)},m)},onSpan:(d,m)=>{f({event:d,kind:"span",resource:k(m)},m)}}}const A=Lt({export:ut,...r?.maxDelayMs===void 0?{}:{maxDelayMs:r.maxDelayMs},...r?.maxItems===void 0?{}:{maxItems:r.maxItems}});return{flush:f=>{A.flush(f?.waitUntil).catch(()=>{})},onLog:(f,d)=>{A.add({event:f,kind:"log",resource:k(d)},d?.waitUntil)},onMetric:(f,d)=>{A.add({event:f,kind:"metric",resource:k(d)},d?.waitUntil)},onRpc:(f,d)=>{P(f,a)||A.add({endMs:Date.now(),event:f,kind:"rpc",resource:k(d)},d?.waitUntil)},onSpan:(f,d)=>{A.add({event:f,kind:"span",resource:k(d)},d?.waitUntil)}}},cr=(...t)=>{const r=(s,e)=>{for(const i of t){const a=i[s];if(a)try{a.apply(i,e)}catch{}}},o={};for(const s of t)for(const e of Yt)o[e]===void 0&&s[e]!==void 0&&(o[e]=s[e]);return{...o,flush:s=>{r("flush",[s])},onLog:(s,e)=>{r("onLog",[s,e])},onMetric:(s,e)=>{r("onMetric",[s,e])},onRpc:(s,e)=>{r("onRpc",[s,e])},onSpan:(s,e)=>{r("onSpan",[s,e])}}};export{Yt as S,nr as a,sr as b,cr as c,Z as e,or as n,ir as o,ar as p,rr as t,er as w};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/runtime",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.282",
|
|
4
4
|
"description": "Lunora Worker runtime: the RPC router, shard resolver, and query coordinator",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
],
|
|
15
15
|
"homepage": "https://lunora.sh",
|
|
16
16
|
"bugs": "https://github.com/anolilab/lunora/issues",
|
|
17
|
-
"license": "
|
|
17
|
+
"license": "Apache-2.0",
|
|
18
18
|
"author": {
|
|
19
19
|
"name": "Daniel Bannert",
|
|
20
20
|
"email": "d.bannert@anolilab.de"
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@lunora/bindings": "1.0.0-alpha.151",
|
|
50
50
|
"@lunora/errors": ">=1.0.0-alpha.51 <2.0.0-0",
|
|
51
|
-
"@lunora/observability": "^1.0.0-alpha.
|
|
51
|
+
"@lunora/observability": "^1.0.0-alpha.203",
|
|
52
52
|
"@lunora/platform": "1.0.0-alpha.80",
|
|
53
53
|
"@lunora/wire": "1.0.0-alpha.6"
|
|
54
54
|
},
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{redactArgs as b,redactCredentials as y}from"@lunora/observability";import{S,n as O,t as x}from"./observability-sinks-CTGNb3dU.mjs";const d=t=>{try{t()}catch{}},m=(t,o)=>{const r={};for(const n of S)t[n]!==void 0&&(r[n]=t[n]);return t.flush&&(r.flush=n=>{d(()=>t.flush?.(n))}),t.onLog&&(r.onLog=(n,c)=>{const e=o.log===void 0?n:o.log(n);e!==void 0&&d(()=>t.onLog?.(e,c))}),t.onMetric&&(r.onMetric=(n,c)=>{const e=o.metric===void 0?n:o.metric(n);e!==void 0&&d(()=>t.onMetric?.(e,c))}),t.onRpc&&(r.onRpc=(n,c)=>{const e=o.rpc===void 0?n:o.rpc(n);e!==void 0&&d(()=>t.onRpc?.(e,c))}),t.onSpan&&(r.onSpan=(n,c)=>{const e=o.span===void 0?n:o.span(n);e!==void 0&&d(()=>t.onSpan?.(e,c))}),r},g=5;let l=0;const E=t=>{if(l>=g)return;l+=1;const o=l===g?" Further enricher failures are silenced until the isolate restarts.":"";try{console.error(`[lunora:observability] enrichSink's enricher threw; delivering the event without its extra attributes.${o}`,t instanceof Error?t.name:typeof t)}catch{}},j=(t,o,r)=>{if(t instanceof Error)return r==="full"?x(t,o):String(t);const n=Reflect.get(t,"toJSON");if(typeof n=="function"){const e=n.call(t);return e!==t?o(e):Object.fromEntries(Object.entries(t).map(([i,s])=>[i,o(s)]))}if(Array.isArray(t))return t.map(e=>o(e));const c=Object.getPrototypeOf(t);return c!==Object.prototype&&c!==null?t:Object.fromEntries(Object.entries(t).map(([e,i])=>[e,o(i)]))},u=t=>({ancestors:new WeakSet,copies:new WeakMap,errors:t}),a=(t,o)=>{if(typeof t=="symbol"||typeof t=="function"||typeof t=="bigint")return String(t);if(typeof t!="object"||t===null)return t;if(!o.ancestors.has(t)){if(o.copies.has(t))return o.copies.get(t);o.ancestors.add(t);try{const r=j(t,n=>a(n,o),o.errors);return o.copies.set(t,r),r}finally{o.ancestors.delete(t)}}},p=(t,o,r)=>{try{const n=t(a(o,u("full")),r),c=n===void 0?void 0:Object.fromEntries(Object.entries(n).filter(([,f])=>f!==void 0)),e=r==="log"?"full":"headline",i=c===void 0?void 0:a(c,u(e)),s=r==="log"?b(i):y(i);return O(s,void 0,e)}catch(n){E(n);return}},A=(t,o)=>m(t,{log:r=>{const n=p(o,r,"log");return n===void 0?r:{...r,fields:{...n,...r.fields}}},metric:r=>{const n=p(o,r,"metric");return n===void 0?r:{...r,attributes:{...n,...r.attributes}}},rpc:r=>{const n=p(o,r,"rpc");return n===void 0?r:{...r,attributes:{...n,...r.attributes}}},span:r=>{const n=p(o,r,"span");return n===void 0?r:{...r,attributes:{...n,...r.attributes}}}}),h=(t,o)=>{const r=t.split("*");if(r.length===1)return o===t;const n=r[0]??"",c=r.at(-1)??"";if(o.length<n.length+c.length||!o.startsWith(n)||!o.endsWith(c))return!1;const e=o.length-c.length;let i=n.length;for(const s of r.slice(1,-1)){const f=o.indexOf(s,i);if(f===-1||f+s.length>e)return!1;i=f+s.length}return i<=e},R=(t,o)=>o.exclude?.some(r=>h(r,t))?!1:o.include===void 0||o.include.length===0||o.include.some(r=>h(r,t)),M=(t,o)=>{const r=n=>R(n.functionPath,o)?n:void 0;return m(t,{log:r,metric:r,rpc:r,span:r})};export{A as enrichSink,M as filterSink,h as globMatch};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import{LunoraError as ut}from"@lunora/errors";import{redactCredentials as lt,redactArgs as M}from"@lunora/observability";import{stableStringify as ft}from"@lunora/wire";import{e as c,L as y,c as Y,o as G,f as S,O as q,g as pt,h as Z,w as mt,i as ht,j as yt,m as gt}from"./otlp-resource-BBR8xs6m.mjs";import{t as bt}from"./error-detail-C46mdOTW.mjs";const kt=t=>{if(typeof t=="string")return t;if(t instanceof Error)return String(t);try{return JSON.stringify(t)??String(t)}catch{return String(t)}},St=4,It=10,Q=3584,Et=4,V=512,v=["all","root","short","none","minimal"],tt=8,Tt=t=>typeof t=="object"&&t!==null&&!Array.isArray(t),rt=t=>{if(!Tt(t))return!1;const r=Object.getPrototypeOf(t);return r===Object.prototype||r===null},x=(t,r=0,o=new WeakSet)=>t instanceof Error?!0:typeof t!="object"||t===null||r>=tt||o.has(t)?!1:(o.add(t),Array.isArray(t)?t.some(s=>x(s,r+1,o)):rt(t)&&Object.values(t).some(s=>x(s,r+1,o))),ot=t=>{if(typeof t=="bigint")return String(t);if(typeof t!="object"||t===null)return t;try{return JSON.stringify(t),t}catch{return}},Ot=(t,r,o)=>{if(t!==void 0)return r==="none"||r==="minimal"||r==="root"&&o>0?"":r==="short"?t.split(`
|
|
2
|
-
`).slice(0,Et+1).join(`
|
|
3
|
-
`):t},j=(t,r,o,s)=>({...s===void 0?{}:{child:s},errors:t,mask:o,memo:new WeakMap,mode:r,path:new Set}),st=(t,r,o)=>{if(!r.path.has(t)){if(r.memo.has(t))return r.memo.get(t);r.path.add(t);try{const s=o();return r.memo.set(t,s),s}finally{r.path.delete(t)}}},et=(t,r,o)=>{Object.defineProperty(t,r,{configurable:!0,enumerable:!0,value:o,writable:!0})},U=(t,r,o)=>r.path.has(t)||o>St?{message:t.message,name:t.name}:st(t,r,()=>{const s=u=>u instanceof Error?U(u,r,o+1):(r.child??(h=>P(h,r,0,o+1)))(u),e=r.mode==="minimal",n={message:e&&t.message.length>V?`${t.message.slice(0,V)}…`:t.message,name:t.name};if(!e)for(const[u,h]of Object.entries(t))u!=="message"&&u!=="name"&&et(n,u,s(h));const d=Ot(t.stack,r.mode,o);return d!==void 0&&(n.stack=d),t.cause!==void 0&&(n.cause=s(t.cause)),t instanceof AggregateError&&Array.isArray(t.errors)&&(n.errors=t.errors.slice(0,It).map(u=>s(u))),r.mask(n)}),P=(t,r,o,s)=>t instanceof Error?r.errors==="full"?U(t,r,s):String(t):typeof t!="object"||t===null||o>=tt||!(Array.isArray(t)||rt(t))?ot(t):st(t,r,()=>Array.isArray(t)?t.map(e=>P(e,r,o+1,s)):Object.fromEntries(Object.entries(t).map(([e,a])=>[e,P(a,r,o+1,s)]))),At=t=>{try{return JSON.stringify(t).length}catch{return Number.POSITIVE_INFINITY}},D=t=>t,tr=(t,r=ot)=>{let o;for(const s of v)if(o=U(t,j("full",s,D,r),0),At(o)<=Q)break;return o},Pt=(t,r,o)=>{if(r==="headline")return JSON.stringify(P(t,j("headline","none",o),0,0));let s;for(const e of v)if(s=JSON.stringify(P(t,j("full",e,o),0,0)),s===void 0||s.length<=Q)break;return s},_=(t,r="headline",o=D)=>{if(typeof t=="boolean"||typeof t=="number"||typeof t=="string")return t;try{return!x(t)||r==="headline"&&t instanceof Error?kt(t):Pt(t,r,o)??String(t)}catch{return String(t)}},rr=(t,r,o="headline",s=D)=>{if(t===void 0&&r===void 0)return;const e={};if(t!==void 0)for(const[a,n]of Object.entries(t))et(e,a,_(n,o,s));return Object.keys(e).length===0?void 0:e},Rt=512,wt=200,_t=t=>{const r=t.maxItems??Rt,o=t.maxDelayMs??wt;let s=[],e,a,n;const d=()=>{e!==void 0&&(clearTimeout(e),e=void 0)},u=async()=>{d();const p=s;s=[];const l=n;a=void 0,n=void 0;try{p.length>0&&await t.export(p)}catch{}finally{l?.()}},h=p=>{a===void 0&&(a=new Promise(l=>{n=l}),e=setTimeout(()=>{u()},o)),p?.(a)};return{add:(p,l)=>{for(s.push(p);s.length>r;)s.shift();h(l),s.length>=r&&u()},flush:async p=>{if(s.length===0){d();return}const l=u();return p?.(l),l},get size(){return s.length}}},Nt=1,Lt=0,Mt=new Set(["cloudflare.ray_id","error.type","http.request.method","http.response.status_code","http.route","lunora.error_status","lunora.fanout.failed","lunora.fanout.shards","lunora.fanout.table","lunora.function_path","lunora.ok","lunora.shard_key","server.address","server.port","url.path","url.scheme","user_agent.original"]),xt={errorType:y.errorType,functionPath:y.functionPath,rayId:y.rayId,shardKey:y.shardKey,userId:y.userId},jt=t=>{const r=bt(t);return r===void 0?[]:[c("lunora.error.internal",JSON.stringify(lt(r)))]},nt=(t,r,o,s)=>[c("exception.type",t),c("exception.message",r),...o===void 0?[]:[c("exception.stacktrace",o)],...s===void 0?[]:jt(s)],Ut=(t,r)=>{const o=[c(y.functionPath,t.functionPath),c(y.ok,t.ok)];if(t.method!==void 0&&o.push(c("http.request.method",t.method)),t.path!==void 0&&o.push(c("url.path",t.path)),o.push(c("http.route",t.functionPath)),t.scheme!==void 0&&o.push(c("url.scheme",t.scheme)),t.host!==void 0&&o.push(c("server.address",t.host)),t.port!==void 0&&o.push(c("server.port",t.port)),t.userAgent!==void 0&&o.push(c("user_agent.original",t.userAgent)),t.shardKey!==void 0&&o.push(c(y.shardKey,t.shardKey)),t.rayId!==void 0&&o.push(c(y.rayId,t.rayId)),o.push(c("http.response.status_code",t.error?.status??200)),t.error&&o.push(c(y.errorType,t.error.code),c("lunora.error_status",t.error.status)),t.fanOut&&o.push(c("lunora.fanout.table",t.fanOut.table),c("lunora.fanout.shards",t.fanOut.shards),c("lunora.fanout.failed",t.fanOut.failed)),t.attributes!==void 0){const e=new Set([...Mt,...o.map(n=>n.key)]),a=Y(Object.fromEntries(Object.entries(t.attributes).map(([n,d])=>[n,_(d)])));o.push(...a.filter(n=>!e.has(n.key)))}const s={attributes:o,endTimeUnixNano:S(r),kind:q.server,name:t.functionPath,...t.parentSpanId===void 0?{}:{parentSpanId:t.parentSpanId},spanId:t.spanId??G(8),startTimeUnixNano:S(r-t.durationMs),status:t.ok?{code:1}:{code:2,message:t.error?.message??""},traceId:t.traceId??G(16)};return t.traceFlags!==void 0&&(s.flags=t.traceFlags),t.error&&(s.events=[{attributes:nt(t.error.code,t.error.message,t.error.stack,t.error.internal),name:"exception",timeUnixNano:S(r)}]),s},F=(t,r)=>{const o=new Map,s=new Set(Object.keys(t).map(e=>xt[e]));for(const[e,a]of Object.entries(r??{}))s.has(e)||o.set(e,c(e,_(a)));return o.set(y.functionPath,c(y.functionPath,t.functionPath)),t.shardKey!==void 0&&o.set(y.shardKey,c(y.shardKey,t.shardKey)),t.userId!==void 0&&o.set(y.userId,c(y.userId,t.userId)),t.rayId!==void 0&&o.set(y.rayId,c(y.rayId,t.rayId)),t.errorType!==void 0&&o.set(y.errorType,c(y.errorType,t.errorType)),[...o.values()]},Dt=t=>{const r={attributes:F({errorType:t.error?.type,functionPath:t.functionPath,rayId:t.rayId,shardKey:t.shardKey,userId:t.userId},t.attributes),endTimeUnixNano:S(t.startTs+t.durationMs),flags:t.sampled===!1?Lt:Nt,kind:q[t.kind??"internal"],name:t.name,parentSpanId:t.parentSpanId,spanId:t.spanId,startTimeUnixNano:S(t.startTs),status:t.ok?{code:1}:{code:2,message:t.error?.message??""},traceId:t.traceId},o=s=>Y(Object.fromEntries(Object.entries(s??{}).map(([e,a])=>[e,_(a)])));return t.events!==void 0&&t.events.length>0&&(r.events=t.events.map(s=>({attributes:o(s.attributes),name:s.name,timeUnixNano:S(s.ts)}))),t.links!==void 0&&t.links.length>0&&(r.links=t.links.map(s=>({attributes:o(s.attributes),spanId:s.spanId,traceId:s.traceId}))),r},Ft=t=>{const r=S(t.ts),o=F({functionPath:t.functionPath,shardKey:t.shardKey},t.attributes),s=t.traceId===void 0?void 0:[{asDouble:t.value,timeUnixNano:r,traceId:t.traceId}],e={asDouble:t.value,attributes:o,...s===void 0?{}:{exemplars:s},timeUnixNano:r};return t.kind==="gauge"?{gauge:{dataPoints:[e]},name:t.name}:t.kind==="histogram"?{histogram:{aggregationTemporality:1,dataPoints:[{attributes:o,bucketCounts:["1"],count:"1",...s===void 0?{}:{exemplars:s},explicitBounds:[],max:t.value,min:t.value,sum:t.value,timeUnixNano:r}]},name:t.name}:{name:t.name,sum:{aggregationTemporality:1,dataPoints:[e],isMonotonic:!0}}},Kt=t=>{const r={attributes:F({functionPath:t.functionPath,rayId:t.rayId,shardKey:t.shardKey,userId:t.userId},t.fields),body:{stringValue:t.message},severityNumber:pt[t.level],severityText:t.level.toUpperCase(),timeUnixNano:S(t.ts)};if(t.traceId!==void 0&&(r.traceId=t.traceId),t.spanId!==void 0&&(r.spanId=t.spanId),t.error!==void 0){const o=nt(t.error.name,t.error.message,t.error.stack,t.error.internal),s=new Set(o.map(e=>e.key));r.attributes=[...r.attributes.filter(e=>!s.has(e.key)),...o]}return t.eventName!==void 0&&(r.eventName=t.eventName,r.attributes=[...r.attributes.filter(o=>o.key!=="event.name"),c("event.name",t.eventName)]),r},$t=1024,X=5;let L=0;const Ct=(t,r)=>{if(L>=X)return;L+=1;let o;try{o=new URL(t).host}catch{o="<unparseable endpoint>"}const s=L===X?" Further OTLP rejections are silenced until the isolate restarts.":"";console.error(`[lunora:otlp] collector ${o} rejected the export with HTTP ${String(r)}; the batch is DROPPED (there is no retry).${s}`)},Ht=async t=>{const r=new Blob([t]).stream().pipeThrough(new CompressionStream("gzip"));return new Response(r).arrayBuffer()},at=async(t,r,o,s)=>{try{const e=JSON.stringify(r),{byteLength:a}=new TextEncoder().encode(e),n=(a<$t?fetch(t,{body:e,headers:o,method:"POST"}):Ht(e).then(d=>fetch(t,{body:d,headers:{...o,"content-encoding":"gzip"},method:"POST"}))).then(d=>{d.ok||Ct(t,d.status)},()=>{});s?.(n),await n}catch{}},Bt=(t,r,o,s)=>{at(t,r,o,s?.waitUntil).catch(()=>{})},R=(t,r)=>r===!0&&t.ok,Jt=t=>t.kind==="metric"?void 0:t.event.traceId,Gt=t=>{const r=Map.groupBy(t,s=>Jt(s)),o=r.get(void 0)??[];return r.delete(void 0),{byTrace:r,untraced:o}},z=5,Vt=(t,r,o)=>{if(r===void 0)return t;const{byTrace:s,untraced:e}=Gt(t),a=[...e];let n=0,d;for(const[u,h]of s){let p;try{p=r({logs:h.filter(l=>l.kind==="log").map(l=>l.event),rpc:h.filter(l=>l.kind==="rpc").map(l=>l.event),spans:h.filter(l=>l.kind==="span").map(l=>l.event),traceId:u})}catch(l){n+=1,n===1&&(d=l),p=!0}p&&a.push(...h)}return n>0&&o(d,n),a},O=(t,r)=>{let o;try{o=r===void 0?t:r(t)}catch{o=void 0}return o??void 0},Xt=t=>{const r=M(t.message);if(t.stack===void 0)return{...t,message:r};const o=`: ${t.message}`,s=t.stack.indexOf(o),a=t.message!==""&&s!==-1&&!t.stack.slice(0,s).includes(`
|
|
4
|
-
`)?t.stack.slice(s+o.length):t.stack.split(`
|
|
5
|
-
`).filter(n=>n.trimStart().startsWith("at ")).map(n=>`
|
|
6
|
-
${n}`).join("");return{...t,message:r,stack:`${t.name}: ${r}${a}`}},zt=t=>({...t,...t.error===void 0?{}:{error:Xt(t.error)},...t.fields===void 0?{}:{fields:M(t.fields)},message:M(t.message)}),W=(t,r,o)=>{if(t.kind==="rpc"){const e=O(t.event,r?.rpc);return e===void 0?void 0:{bucket:"spans",encoded:Ut(e,t.endMs)}}if(t.kind==="span"){const e=O(t.event,r?.span);return e===void 0?void 0:{bucket:"spans",encoded:Dt(e)}}if(t.kind==="log"){const e=O(o?zt(t.event):t.event,r?.log);return e===void 0?void 0:{bucket:"logs",encoded:Kt(e)}}const s=O(t.event,r?.metric);return s===void 0?void 0:{bucket:"metrics",encoded:Ft(s)}},Wt=["fuseCloudflareTraces","instrumentDatabase","metricHistory","traceFetch"],or=(t={})=>{const{onlyErrors:r}=t;return{onLog:o=>{o.level==="error"||o.level==="fatal"?console.error("[lunora:log]",o.functionPath,o.message):console.log("[lunora:log]",o.functionPath,o.message)},onMetric:o=>{console.log("[lunora:metric]",`${o.name}=${String(o.value)}`,o.kind,o.functionPath)},onRpc:o=>{R(o,r)||(o.ok?console.log("[lunora:rpc]",o):console.error("[lunora:rpc]",o))},onSpan:o=>{const s=o.ok?"ok":`error ${o.error?.type??""}`.trim();console.log("[lunora:span]",o.name,`${String(o.durationMs)}ms`,s,o.functionPath)}}},sr=t=>{const{headers:r,onlyErrors:o,transform:s,transformLog:e,url:a}=t,n=Z({"content-type":"application/json"},r),d=(u,h)=>{try{const p=fetch(a,{body:JSON.stringify(u),headers:n,method:"POST"}).catch(()=>{});h?.waitUntil&&h.waitUntil(p)}catch{}};return{onLog:(u,h)=>{const p=O(u,e);p!==void 0&&d(p,h)},onRpc:(u,h)=>{if(R(u,o))return;const p=O(u,s);p!==void 0&&d(p,h)}}},er=t=>{const{dataset:r,onlyErrors:o}=t;return{onRpc:s=>{if(!R(s,o))try{r.writeDataPoint({blobs:[s.functionPath,s.ok?"ok":"error",s.shardKey??"",s.error?.code??"",s.fanOut?.table??""],doubles:[s.durationMs,s.ok?0:1,s.fanOut?.shards??0,s.fanOut?.failed??0],indexes:[s.functionPath]})}catch{}}}},nr=t=>{const{pipeline:r,serializeFields:o}=t;return{onLog:(s,e)=>{try{const a={functionPath:s.functionPath,level:s.level,message:s.message,ts:s.ts};s.fields&&(a.fields=o===!0?JSON.stringify(s.fields):s.fields);for(const d of["shardKey","userId","traceId","spanId"])s[d]!==void 0&&(a[d]=s[d]);const n=r.send([a]).catch(()=>{});e?.waitUntil&&e.waitUntil(n)}catch{}}}},ar=t=>{const{batch:r,deploymentEnvironment:o,detectResources:s,endpoint:e,headers:a,onlyErrors:n,postProcessor:d,resourceAttributes:u,serviceNamespace:h,serviceVersion:p,tailSampler:l,token:it}=t,K=t.serviceName??"lunora",$=t.redactLogs??!0;if(r!==!1&&r?.maxItems!==void 0&&(!Number.isInteger(r.maxItems)||r.maxItems<1))throw new ut("ENV_INVALID",`otlpSink: \`batch.maxItems\` must be a positive integer, received ${String(r.maxItems)}.`);const C={...p===void 0?{}:{"service.version":p},...h===void 0?{}:{"service.namespace":h},...o===void 0?{}:{"deployment.environment":o},...u},H=new WeakMap,k=f=>{if(s!==!0||f?.resourceAttributes===void 0)return C;const i=H.get(f);if(i!==void 0)return i;const m=gt(f.resourceAttributes(),C);return H.set(f,m),m};let I=e;for(;I.endsWith("/");)I=I.slice(0,-1);const B={logs:{url:`${I}/v1/logs`,wrap:yt},metrics:{url:`${I}/v1/metrics`,wrap:ht},spans:{url:`${I}/v1/traces`,wrap:mt}},J=Z({"content-type":"application/json"},a,it);let N=0;const ct=(f,i)=>{if(N>=z)return;N+=1;const m=N===z?" Further tailSampler failures from this sink are silenced until the isolate restarts.":"";console.error(`[lunora:otlp] tailSampler threw for ${String(i)} trace(s) in this flush window; keeping them (fail-open), so the sampling policy did NOT apply.${m}`,f)},dt=async f=>{const i=Vt(f,l,ct),m=new Map;for(const g of i){const b=W(g,d,$);if(b===void 0)continue;const w=ft(g.resource);let T=m.get(w);T===void 0&&(T={logs:[],metrics:[],resource:g.resource,spans:[]},m.set(w,T)),T[b.bucket].push(b.encoded)}const E=[];for(const[,g]of m)for(const b of["spans","logs","metrics"])if(g[b].length>0){const{url:w,wrap:T}=B[b];E.push(at(w,T(g[b],"@lunora/runtime",K,g.resource),J))}await Promise.all(E)};if(r===!1){const f=(i,m)=>{const E=W(i,d,$);if(E!==void 0){const{url:g,wrap:b}=B[E.bucket];Bt(g,b(E.encoded,"@lunora/runtime",K,i.resource),J,m)}};return{onLog:(i,m)=>{f({event:i,kind:"log",resource:k(m)},m)},onMetric:(i,m)=>{f({event:i,kind:"metric",resource:k(m)},m)},onRpc:(i,m)=>{R(i,n)||f({endMs:Date.now(),event:i,kind:"rpc",resource:k(m)},m)},onSpan:(i,m)=>{f({event:i,kind:"span",resource:k(m)},m)}}}const A=_t({export:dt,...r?.maxDelayMs===void 0?{}:{maxDelayMs:r.maxDelayMs},...r?.maxItems===void 0?{}:{maxItems:r.maxItems}});return{flush:f=>{A.flush(f?.waitUntil).catch(()=>{})},onLog:(f,i)=>{A.add({event:f,kind:"log",resource:k(i)},i?.waitUntil)},onMetric:(f,i)=>{A.add({event:f,kind:"metric",resource:k(i)},i?.waitUntil)},onRpc:(f,i)=>{R(f,n)||A.add({endMs:Date.now(),event:f,kind:"rpc",resource:k(i)},i?.waitUntil)},onSpan:(f,i)=>{A.add({event:f,kind:"span",resource:k(i)},i?.waitUntil)}}},ir=(...t)=>{const r=(s,e)=>{for(const a of t){const n=a[s];if(n)try{n.apply(a,e)}catch{}}},o={};for(const s of t)for(const e of Wt)o[e]===void 0&&s[e]!==void 0&&(o[e]=s[e]);return{...o,flush:s=>{r("flush",[s])},onLog:(s,e)=>{r("onLog",[s,e])},onMetric:(s,e)=>{r("onMetric",[s,e])},onRpc:(s,e)=>{r("onRpc",[s,e])},onSpan:(s,e)=>{r("onSpan",[s,e])}}};export{Wt as S,er as a,or as b,ir as c,rr as n,ar as o,nr as p,tr as t,sr as w};
|