UNPKG

@get-tomo/tomo-client-telemetry-sdk

Version:

Drop-in SDK to initialize and instrument web applications. It collects network traces and sends them to the specified collector URL. It follows the OpenTelemetry specification.

3 lines (2 loc) 4.09 kB
import{trace as t,context as e,SpanStatusCode as r,SpanKind as o}from"@opentelemetry/api";import{BatchSpanProcessor as n,ConsoleSpanExporter as i,BasicTracerProvider as s}from"@opentelemetry/sdk-trace-base";import{OTLPTraceExporter as a}from"@opentelemetry/exporter-trace-otlp-http";import{resourceFromAttributes as c}from"@opentelemetry/resources";import{SemanticResourceAttributes as d}from"@opentelemetry/semantic-conventions";let u,y=null;function l(){return y}function p(){return u}function h(t,e){if(t)for(const[r,o]of Object.entries(e))t.setAttribute(r,o)}function f(){if(globalThis.__patchedFetch)return;const n=l(),i=globalThis.fetch,s=n.collectorUrl;globalThis.fetch=async(n,a)=>{const c="string"==typeof n?n:n.url;if(c.includes(s))try{return await i(n,a)}catch(t){throw console.error(t),t}const d=a?.method||"object"==typeof n&&n.method||"GET",{body:u,type:y}=function(t){if(t&&t.body){if("string"==typeof t.body){let e="string";try{JSON.parse(t.body),e="json"}catch{}return{body:t.body.length>2048?t.body.slice(0,2048)+"...[truncated]":t.body,type:e}}return t.body instanceof FormData?{body:"[FormData body]",type:"formdata"}:t.body instanceof URLSearchParams?{body:t.body.toString(),type:"urlencoded"}:t.body instanceof ArrayBuffer?{body:"[ArrayBuffer body]",type:"arraybuffer"}:t.body instanceof Blob?{body:`[Blob: ${t.body.type}]`,type:"blob"}:{body:"[unhandled body type]",type:typeof t.body}}return{body:void 0,type:void 0}}(a);let l;return"GET"===d.toUpperCase()&&(l=function(t){let e="string"==typeof t?t:t&&t.url?t.url:void 0;if(e)try{const t=new URL(e,window.location.origin);return t.search?t.search:void 0}catch{return}}(n)),async function(o,n,i){const s=p();if(!s)return await i(null);const a=n&&n.parentContext?n.parentContext:e.active(),c=s.startSpan(o,n,a);try{return await e.with(t.setSpan(a,c),async()=>await i(c))}catch(t){throw c.setStatus({code:r.ERROR}),t}finally{c.end()}}("tomo.http.request",{kind:o.CLIENT,attributes:{httpMethod:d,httpUrl:c,httpHost:new URL(c,window.location.origin).host,httpPath:new URL(c,window.location.origin).pathname,...void 0!==u?{httpRequestBody:u}:{},...void 0!==y?{httpRequestBodyType:y}:{},...void 0!==l?{httpQueryParams:l}:{}}},async t=>{console.log("Running with span",t);try{const e=await i(n,a);return function(t,e){t&&e&&"number"==typeof e.status&&(h(t,{httpStatusCode:e.status}),t.setStatus({code:e.status>=400?r.ERROR:r.OK}))}(t,e),await async function(t,e){if(!t||!e)return e;let r="",o="";const n=e.headers.get("content-type")||"";let i=e.clone();try{if(n.includes("application/json")){const t=await i.json();r=JSON.stringify(t),o="json"}else n.startsWith("text/")&&(r=await i.text(),o="text")}catch(t){i=e.clone();try{r=await i.text(),o="text"}catch{r="[unreadable response body]",o="unknown"}}h(t,{httpResponse:r,httpResponseType:o})}(t,e),e}catch(e){throw h(t,{httpError:e&&e.message?e.message:String(e),httpStatusCode:e&&e.status?e.status:500}),t.setStatus({code:r.ERROR}),e}})},globalThis.__patchedFetch=!0}class b{constructor(e){if(!e.apiKey)throw new Error("apiKey required");if(!e.serviceName)throw new Error("serviceName required");if(!e.serviceVersion)throw new Error("serviceVersion required");if(!e.collectorUrl)throw new Error("collectorUrl required");void 0===e.debug&&(e.debug=!1),function(t){y=t}(e),function(){const e=l(),r=c({[d.SERVICE_NAME]:e.serviceName,[d.SERVICE_VERSION]:e.serviceVersion,[d.TELEMETRY_SDK_LANGUAGE]:"javascript",[d.TELEMETRY_SDK_NAME]:"tomo-client-telemetry-sdk",[d.TELEMETRY_SDK_VERSION]:"0.1.0",[d.DEPLOYMENT_ENVIRONMENT]:"browser","user.agent":navigator.userAgent,"screen.resolution":`${screen.width}x${screen.height}`,"viewport.size":`${window.innerWidth}x${window.innerHeight}`}),o=new a({url:e.collectorUrl,headers:{"x-api-key":e.apiKey}}),y=[new n(o)];if(e.debug){const t=new i;y.push(new n(t))}const p=new s({resource:r,spanProcessors:y});t.setGlobalTracerProvider(p),u=t.getTracer(e.serviceName)}(),this.tracer=p(),this._initializeNetworkTracing()}getTracer(){return this.tracer}_initializeNetworkTracing(){f()}}export{b as default}; //# sourceMappingURL=index.esm.js.map