eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 1.64 kB
JavaScript
import{AGENT_SPAN_NAMES}from"#tracing/agent-span-contract.js";import{getInstrumentationRuntime}from"#instrumentation/runtime.js";import{SpanKind,SpanStatusCode,context,propagation,trace}from"#compiled/@opentelemetry/api/index.js";import{markAgentTraceContext}from"#tracing/agent-trace-context.js";import{withErrorContent}from"#tracing/error-content-context.js";import{agentSpanNamingAttributes}from"#tracing/agent-span-naming.js";const headersGetter={get(e,t){return e.get(t)??void 0},keys(e){return[...e.keys()]}};async function traceChannelRequest(e,t){if(getInstrumentationRuntime()?.otelSettings?.traceChannelRequests!==!0)return await t(void 0);let{request:n,routeKey:r}=e,i=propagation.extract(context.active(),n.headers,headersGetter),a=AGENT_SPAN_NAMES.channelRequest,o=trace.getTracer(`eve.channel`).startSpan(a,{attributes:{...baseAttributes(n,r),...a===`agent.channel.request`?agentSpanNamingAttributes(a):void 0},kind:SpanKind.SERVER},i),s=markAgentTraceContext(withErrorContent(trace.setSpan(i,o),!1));try{let e=await context.with(s,()=>t(o));return o.setAttribute(`http.response.status_code`,e.status),e.status>=500&&o.setStatus({code:SpanStatusCode.ERROR}),e}catch(e){throw o.setStatus({code:SpanStatusCode.ERROR}),e}finally{o.end()}}function baseAttributes(e,t){let n={"http.request.method":e.method,"http.route":routeTemplate(t)},r=parseRequestUrl(e.url);return r!==void 0&&(n[`url.scheme`]=r.protocol.replace(/:$/,``),n[`server.address`]=r.hostname),n}function routeTemplate(e){let t=e.indexOf(` `);return t===-1?e:e.slice(t+1)}function parseRequestUrl(e){try{return new URL(e)}catch{return}}export{traceChannelRequest};