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.27 kB
"use strict";var t=require("@opentelemetry/api"),e=require("@opentelemetry/sdk-trace-base"),r=require("@opentelemetry/exporter-trace-otlp-http"),o=require("@opentelemetry/resources"),n=require("@opentelemetry/semantic-conventions");let i,s=null;function a(){return s}function c(){return i}function u(t,e){if(t)for(const[r,o]of Object.entries(e))t.setAttribute(r,o)}function d(){if(globalThis.__patchedFetch)return;const e=a(),r=globalThis.fetch,o=e.collectorUrl;globalThis.fetch=async(e,n)=>{const i="string"==typeof e?e:e.url;if(i.includes(o))try{return await r(e,n)}catch(t){throw console.error(t),t}const s=n?.method||"object"==typeof e&&e.method||"GET",{body:a,type:d}=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}}(n);let p;return"GET"===s.toUpperCase()&&(p=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}}(e)),async function(e,r,o){const n=c();if(!n)return await o(null);const i=r&&r.parentContext?r.parentContext:t.context.active(),s=n.startSpan(e,r,i);try{return await t.context.with(t.trace.setSpan(i,s),async()=>await o(s))}catch(e){throw s.setStatus({code:t.SpanStatusCode.ERROR}),e}finally{s.end()}}("tomo.http.request",{kind:t.SpanKind.CLIENT,attributes:{httpMethod:s,httpUrl:i,httpHost:new URL(i,window.location.origin).host,httpPath:new URL(i,window.location.origin).pathname,...void 0!==a?{httpRequestBody:a}:{},...void 0!==d?{httpRequestBodyType:d}:{},...void 0!==p?{httpQueryParams:p}:{}}},async o=>{console.log("Running with span",o);try{const i=await r(e,n);return function(e,r){e&&r&&"number"==typeof r.status&&(u(e,{httpStatusCode:r.status}),e.setStatus({code:r.status>=400?t.SpanStatusCode.ERROR:t.SpanStatusCode.OK}))}(o,i),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"}}u(t,{httpResponse:r,httpResponseType:o})}(o,i),i}catch(e){throw u(o,{httpError:e&&e.message?e.message:String(e),httpStatusCode:e&&e.status?e.status:500}),o.setStatus({code:t.SpanStatusCode.ERROR}),e}})},globalThis.__patchedFetch=!0}module.exports=class{constructor(u){if(!u.apiKey)throw new Error("apiKey required");if(!u.serviceName)throw new Error("serviceName required");if(!u.serviceVersion)throw new Error("serviceVersion required");if(!u.collectorUrl)throw new Error("collectorUrl required");void 0===u.debug&&(u.debug=!1),function(t){s=t}(u),function(){const s=a(),c=o.resourceFromAttributes({[n.SemanticResourceAttributes.SERVICE_NAME]:s.serviceName,[n.SemanticResourceAttributes.SERVICE_VERSION]:s.serviceVersion,[n.SemanticResourceAttributes.TELEMETRY_SDK_LANGUAGE]:"javascript",[n.SemanticResourceAttributes.TELEMETRY_SDK_NAME]:"tomo-client-telemetry-sdk",[n.SemanticResourceAttributes.TELEMETRY_SDK_VERSION]:"0.1.0",[n.SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]:"browser","user.agent":navigator.userAgent,"screen.resolution":`${screen.width}x${screen.height}`,"viewport.size":`${window.innerWidth}x${window.innerHeight}`}),u=new r.OTLPTraceExporter({url:s.collectorUrl,headers:{"x-api-key":s.apiKey}}),d=[new e.BatchSpanProcessor(u)];if(s.debug){const t=new e.ConsoleSpanExporter;d.push(new e.BatchSpanProcessor(t))}const p=new e.BasicTracerProvider({resource:c,spanProcessors:d});t.trace.setGlobalTracerProvider(p),i=t.trace.getTracer(s.serviceName)}(),this.tracer=c(),this._initializeNetworkTracing()}getTracer(){return this.tracer}_initializeNetworkTracing(){d()}}; //# sourceMappingURL=index.js.map