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