@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.8 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("@opentelemetry/api"),require("@opentelemetry/sdk-trace-base"),require("@opentelemetry/exporter-trace-otlp-http"),require("@opentelemetry/resources"),require("@opentelemetry/semantic-conventions")):"function"==typeof define&&define.amd?define(["@opentelemetry/api","@opentelemetry/sdk-trace-base","@opentelemetry/exporter-trace-otlp-http","@opentelemetry/resources","@opentelemetry/semantic-conventions"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).TomoClientTelemetry=t(e.opentelemetryApi,e.opentelemetrySdkTraceBase,e.opentelemetryExporterTraceOtlpHttp,e.opentelemetryResources,e.opentelemetrySemanticConventions)}(this,function(e,t,r,o,n){"use strict";let i,s=null;function a(){return s}function c(){return i}function u(e,t){if(e)for(const[r,o]of Object.entries(t))e.setAttribute(r,o)}function p(){if(globalThis.__patchedFetch)return;const t=a(),r=globalThis.fetch,o=t.collectorUrl;globalThis.fetch=async(t,n)=>{const i="string"==typeof t?t:t.url;if(i.includes(o))try{return await r(t,n)}catch(e){throw console.error(e),e}const s=n?.method||"object"==typeof t&&t.method||"GET",{body:a,type:p}=function(e){if(e&&e.body){if("string"==typeof e.body){let t="string";try{JSON.parse(e.body),t="json"}catch{}return{body:e.body.length>2048?e.body.slice(0,2048)+"...[truncated]":e.body,type:t}}return e.body instanceof FormData?{body:"[FormData body]",type:"formdata"}:e.body instanceof URLSearchParams?{body:e.body.toString(),type:"urlencoded"}:e.body instanceof ArrayBuffer?{body:"[ArrayBuffer body]",type:"arraybuffer"}:e.body instanceof Blob?{body:`[Blob: ${e.body.type}]`,type:"blob"}:{body:"[unhandled body type]",type:typeof e.body}}return{body:void 0,type:void 0}}(n);let d;return"GET"===s.toUpperCase()&&(d=function(e){let t="string"==typeof e?e:e&&e.url?e.url:void 0;if(t)try{const e=new URL(t,window.location.origin);return e.search?e.search:void 0}catch{return}}(t)),async function(t,r,o){const n=c();if(!n)return await o(null);const i=r&&r.parentContext?r.parentContext:e.context.active(),s=n.startSpan(t,r,i);try{return await e.context.with(e.trace.setSpan(i,s),async()=>await o(s))}catch(t){throw s.setStatus({code:e.SpanStatusCode.ERROR}),t}finally{s.end()}}("tomo.http.request",{kind:e.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!==p?{httpRequestBodyType:p}:{},...void 0!==d?{httpQueryParams:d}:{}}},async o=>{console.log("Running with span",o);try{const i=await r(t,n);return function(t,r){t&&r&&"number"==typeof r.status&&(u(t,{httpStatusCode:r.status}),t.setStatus({code:r.status>=400?e.SpanStatusCode.ERROR:e.SpanStatusCode.OK}))}(o,i),await async function(e,t){if(!e||!t)return t;let r="",o="";const n=t.headers.get("content-type")||"";let i=t.clone();try{if(n.includes("application/json")){const e=await i.json();r=JSON.stringify(e),o="json"}else n.startsWith("text/")&&(r=await i.text(),o="text")}catch(e){i=t.clone();try{r=await i.text(),o="text"}catch{r="[unreadable response body]",o="unknown"}}u(e,{httpResponse:r,httpResponseType:o})}(o,i),i}catch(t){throw u(o,{httpError:t&&t.message?t.message:String(t),httpStatusCode:t&&t.status?t.status:500}),o.setStatus({code:e.SpanStatusCode.ERROR}),t}})},globalThis.__patchedFetch=!0}return 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(e){s=e}(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}}),p=[new t.BatchSpanProcessor(u)];if(s.debug){const e=new t.ConsoleSpanExporter;p.push(new t.BatchSpanProcessor(e))}const d=new t.BasicTracerProvider({resource:c,spanProcessors:p});e.trace.setGlobalTracerProvider(d),i=e.trace.getTracer(s.serviceName)}(),this.tracer=c(),this._initializeNetworkTracing()}getTracer(){return this.tracer}_initializeNetworkTracing(){p()}}});
//# sourceMappingURL=index.umd.js.map