import{getClientIp as v}from"../../utils/get-client-ip.js";import{PRODUCT_NAME as d}from"../../../../config/product-gates.js";import{toAttribute as e,getAttributesStringValue as a}from"./otlp.js";import{otelEventHandlers as p}from"./otel-event-handlers.js";import{EventType as _}from"./types.js";const g=process.env.OTEL_TRACES_URL||"https://otel.cloud.redocly.com/v1/traces";async function L(t){const o=await t.req.json(),r=y(t),s={resourceSpans:o.resourceSpans.map(n=>{const c=a(n.resource.attributes.find(u=>u.key==="redocly.session.id"));return c?{...n,resource:{...n.resource,attributes:[...n.resource.attributes,...m(r,c)]},scopeSpans:n.scopeSpans.map(u=>({...u,spans:u.spans.map(i=>({...i,attributes:i.name.startsWith("event.")?A(i,r,c):i.attributes}))}))}:n})};return await fetch(g,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(s)}),t.newResponse(null,200,{})}function y(t){const o=t.get("auth")?.claims?.id||t.get("auth")?.claims?.sub,r=v(t.req.raw)||t.req.raw.context.remoteAddr?.hostname,s=t.req.raw.headers.get("user-agent"),n=t.req.raw.headers.get("accept-language"),c=n?.split(",")[0];return{userId:o,clientIp:r,userAgent:s||void 0,acceptLanguage:n||void 0,locale:c}}function m(t,o){const r=[e("redocly.organization.id",process.env.ORGANIZATION_ID),e("redocly.organization.slug",process.env.ORGANIZATION_SLUG),e("redocly.project.id",process.env.PROJECT_ID),e("redocly.project.slug",process.env.PROJECT_SLUG),e("redocly.product.type",d.toLowerCase()),e("redocly.client.ip",t.clientIp),e("redocly.client.userAgent",t.userAgent),e("redocly.client.acceptLanguage",t.acceptLanguage),e("redocly.client.locale",t.locale)];return t.userId?r.push(e("redocly.user.id",t.userId)):r.push(e("redocly.anonymous.id",l(o))),r.filter(s=>s!==void 0)}function f(t,o,r,s){const n=I(o.userId,r),c=new Date(Math.floor(Number(s.startTimeUnixNano)/1e6)).toISOString();return[e("cloudevents.event_spec_version","1.0"),e("cloudevents.event_object","event"),e("cloudevents.event_id",`evt_${a(t.get("redocly.event_id"))}`),e("cloudevents.event_origin","realm-ui"),e("cloudevents.event_source",n.uri),e("cloudevents.event_source_details.id",n.id),e("cloudevents.event_source_details.object",n.object),e("cloudevents.event_source_details.uri",n.uri),e("cloudevents.event_data_content_type","application/json; charset=utf-8"),e("cloudevents.event_organization_id",process.env.ORGANIZATION_ID),e("cloudevents.event_organization_slug",process.env.ORGANIZATION_SLUG),e("cloudevents.event_project_id",process.env.PROJECT_ID),e("cloudevents.event_project_slug",process.env.PROJECT_SLUG),e("cloudevents.event_product_type",d.toLowerCase()),e("cloudevents.event_session_id",r),e("cloudevents.event_time",c),e("cloudevents.event_client.ip",o.clientIp),e("cloudevents.event_client.user_agent",o.userAgent),e("cloudevents.event_client.accept_language",o.acceptLanguage),e("cloudevents.event_client.locale",o.locale)]}function A(t,o,r){const s=new Map;t.attributes.forEach(u=>{s.set(u.key,u)});const n=a(s.get("redocly.event_type"));if(!Object.values(_).includes(n))throw new Error(`Unknown event type: ${n}. Please ensure it is added to the handlers.`);const c=p[n];if(!c)throw new Error(`Unknown event type: ${n}`);return[e("cloudevents.event_type",n),...f(s,o,r,t),...c(s)].filter(u=>u!==void 0)}function I(t,o){return t?{id:t,object:"user",uri:`${process.env.MAIN_API_URL}/users/${t}`}:{id:l(o),object:"anonymous",uri:`${process.env.MAIN_API_URL}/anonymous/${l(o)}`}}function l(t){return t.replace("ses_","ann_")}export{L as otelTracesHandler,A as toCloudEventAttributes,I as toSource};
