UNPKG

3.77 kBJavaScriptView Raw
1!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("node-fetch")):"function"==typeof define&&define.amd?define(["node-fetch"],e):(n="undefined"!=typeof globalThis?globalThis:n||self).stagnant=e(n.fetch)}(this,(function(n){"use strict";function e(n){return n&&"object"==typeof n&&"default"in n?n:{default:n}}var t=e(n);function a(n={}){n={onevent:function(){},onerror:function(){},onsuccess:function(){},onflush:function(){},console:{log(){},error(){},warn(){}},generateId:function(){return Math.random().toString(15).slice(2,8)},...n};const{generateId:e}=n;async function t(e){await n.onevent(e),e.error?await n.onerror(e):await n.onsuccess(e)}function a({parentId:n,id:e,startTime:t,endTime:a,name:r,data:o,error:c}){return{parentId:n,id:e,startTime:t,endTime:a,name:r,data:o,error:c}}function r(o){function c({sync:c},...i){const{data:s,callback:d,name:l}=function(...n){const e=n.findIndex((n=>"function"==typeof n));let t=n[e],a=e>0?n.slice(0,e):n,[r,o]=a;return"string"!=typeof r&&(o=r,t&&(r=o.name||t.toString().replace(/\s/g,"").replace(/(.)*=>/,""))),null==o&&(o={}),{name:r,data:o,callback:t}}(...i),{event:u,childP:f}=d?function({parentEvent:n,name:t,data:o,sync:c}){const i=a({parentId:n.id,id:e(),name:t,startTime:null,endTime:null,data:{...n.data||{},...o},error:null,sync:n.sync||c});return{childP:r(i),event:i}}({parentEvent:o,name:l,data:s,sync:c}):{};return d&&c?function({callback:e,name:a,event:r,childP:o}){try{r.startTime=Date.now();const c=e(o);return r.endTime=Date.now(),null!=c&&"then"in c&&n.console.warn(a,"A call to trace.sync was made but the response was async. This is likely a mistake and should be corrected."),c}catch(n){throw r.endTime=Date.now(),r.error=n,n}finally{t(r).catch((e=>n.console.error("Failed to dispatch event",e)))}}({data:s,callback:d,childP:f,name:l,event:u}):d&&!c?async function({event:e,childP:a,callback:r}){if(o.sync)throw new Error("Cannot use an async trace within a synchronous trace");try{e.startTime=Date.now();const o=await r(a);return e.endTime=Date.now(),o}catch(n){throw e.endTime=Date.now(),e.error=n,n}finally{t(e).catch((e=>n.console.error("Failed to dispatch event",e)))}}({data:s,callback:d,childP:f,name:l,event:u}):function({data:n}){return o.data={...o.data,...n},null}({data:s})}async function i(...n){return await c({sync:!1},...n)}return i.sync=function(...n){return c({sync:!0},...n)},i}let o=function(){const r=a({parentId:null,id:e(),startTime:Date.now(),endTime:Date.now(),error:null,data:{}});return r.flush=async function(){return delete r.flush,r.endTime=Date.now(),await t(r),await n.onflush(r),r},r}(),c=r(o);return c.flush=o.flush,c.config=n,c}function r(n,...e){const t=e.find((n=>"function"==typeof n));return n?n(...e):t?t(((...n)=>r(null,...n))):null}return a.call=r,a.ensure=function(n){return n||((...n)=>r(null,...n))},function({name:n="root",dataset:e="default",writeKey:r=process.env.HONEYCOMB_WRITE_KEY,traceId:o=Math.random().toString(15).slice(2,8),parentId:c,data:i={},config:s={}}={}){const d=a({async onevent(a){const s=a.parentId?a.name:n,l=JSON.stringify({...a.data,...i,name:s,error:a.error?a.error.message:void 0,"error.stack":a.error?a.error.stack:void 0,"trace.trace_id":"trace-"+o,"trace.span_id":"span-"+a.id,"trace.parent_id":a.parentId?"span-"+a.parentId:c,service_name:"stagnant",duration_ms:a.endTime-a.startTime});try{const n=await t.default(`https://api.honeycomb.io/1/events/${e}`,{method:"post",headers:{"X-Honeycomb-Team":r,"X-Honeycomb-Event-Time":a.startTime,"Content-Type":"application/json"},body:l});d.config.console.log(s,n.status,l),a.parentId||d.config.console.log("flushed",n.status)}catch(n){d.config.console.error(n)}},...s});return d}}));
2//# sourceMappingURL=stagnant-honeycomb.browser.min.js.map