import{ulid as c}from"ulid";import{trace as p}from"@opentelemetry/api";import{CompositePropagator as d,W3CBaggagePropagator as l,W3CTraceContextPropagator as m}from"@opentelemetry/core";import{Resource as E}from"@opentelemetry/resources";import{OTLPTraceExporter as T}from"@opentelemetry/exporter-trace-otlp-http";import{ATTR_SERVICE_NAME as _,ATTR_SERVICE_VERSION as f}from"@opentelemetry/semantic-conventions";import{BatchSpanProcessor as v,NodeTracerProvider as R}from"@opentelemetry/sdk-trace-node";import{PACKAGE_NAME as y}from"../../config/product-gates.js";const O="http://localhost:4318/v1/traces";class g{init(){if(this.isTelemetryDisabled())return;const e=new R({resource:new E({[_]:"realm-server",[f]:`${y}@${process.env.REDOCLY_PORTAL_VERSION}`})});e.addSpanProcessor(new v(new T({url:O}),{scheduledDelayMillis:100})),e.register({propagator:new d({propagators:[new l,new m]})})}send(e,s){try{if(this.isTelemetryDisabled())return;const r=new Date,n=c(),o=p.getTracer("ServerTelemetry").startSpan(`event.${e}`,{attributes:{"redocly.event_id":n,"redocly.event_type":e,"redocly.project_id":process.env.PROJECT_ID,"redocly.organization_id":process.env.ORGANIZATION_ID},startTime:r});Object.entries(s).forEach(([a,t])=>{const i=a.replace(/([A-Z])/g,"_$1").toLowerCase();t!==void 0&&o.setAttribute(`redocly.event_data.${i}`,t)}),o.end(r)}catch(r){process.env.NODE_ENV!=="production"&&console.error("Event reporting failed:",r)}}isTelemetryDisabled(){return process.env.NODE_ENV!=="production"||process.env.REDOCLY_TELEMETRY==="off"}}const h=new g;export{g as OtelServerTelemetry,h as otelTelemetry};
