import{ulid as c}from"ulid";import{trace as p}from"@opentelemetry/api";import{CompositePropagator as m,W3CBaggagePropagator as d,W3CTraceContextPropagator as l}from"@opentelemetry/core";import{Resource as E}from"@opentelemetry/resources";import{BatchSpanProcessor as T,NodeTracerProvider as f}from"@opentelemetry/sdk-trace-node";import{registerInstrumentations as u}from"@opentelemetry/instrumentation";import{OTLPTraceExporter as R}from"@opentelemetry/exporter-trace-otlp-http";import{HttpInstrumentation as _}from"@opentelemetry/instrumentation-http";import{ATTR_SERVICE_NAME as v,ATTR_SERVICE_VERSION as C}from"@opentelemetry/semantic-conventions";import{PACKAGE_NAME as g}from"../../../config/product-gates.js";import{PORTAL_VERSION as O}from"../../../server/version.js";const L=process.env.OTEL_TRACES_URL||"https://otel.cloud.redocly.com/v1/traces";class w{init(){if(!this.disabled())try{const e=new f({resource:new E({[v]:"realm-cli",[C]:`${g}@${O}`})});e.addSpanProcessor(new T(new R({url:L,headers:{}}),{scheduledDelayMillis:100})),e.register({propagator:new m({propagators:[new d,new l]})}),u({instrumentations:[new _]})}catch(e){process.env.REDOCLY_ENV!=="production"&&console.error("Error initializing OTEL:",e)}}send(e,n){try{if(this.disabled())return;const r=new Date,s=c(),t=p.getTracer("CliTelemetry","0.0.2").startSpan(`event.${e}`,{attributes:{"cloudevents.event_client.id":s,"cloudevents.event_client.type":e},startTime:r});Object.entries(n).forEach(([i,o])=>{const a=i.replace(/([A-Z])/g,"_$1").toLowerCase();o!==void 0&&t.setAttribute(`cloudevents.event_data.${a}`,o)}),t.end(r)}catch(r){process.env.REDOCLY_ENV!=="production"&&console.error("Event reporting failed:",r)}}disabled(){return process.env.REDOCLY_ENV!=="production"||process.env.REDOCLY_TELEMETRY==="off"}}const x=new w;export{w as OtelServerTelemetry,x as otelTelemetry};
