-
Notifications
You must be signed in to change notification settings - Fork 97
/
bucky.min.js
2 lines (2 loc) · 9 KB
/
bucky.min.js
1
2
/*! bucky 0.2.8 */
(function(){var a,b,c,d,e,f,g,h=[].slice;e="undefined"!=typeof module&&null!==module&&!("undefined"!=typeof window&&null!==window?window.module:void 0),e?(a=require("xmlhttprequest").XMLHttpRequest,g=function(){var a;return a=process.hrtime(),1e3*(a[0]+a[1]/1e9)}):g=function(){var a,b;return null!=(a=null!=(b=window.performance)&&"function"==typeof b.now?b.now():void 0)?a:+new Date},d=+new Date,c=function(){var a,b,c,d,e,f,g;for(a=arguments[0],d=2<=arguments.length?h.call(arguments,1):[],f=0,g=d.length;g>f;f++){c=d[f];for(b in c)e=c[b],a[b]=e}return a},f=function(){var a,b;return a=1<=arguments.length?h.call(arguments,0):[],null!=("undefined"!=typeof console&&null!==console&&null!=(b=console.log)?b.call:void 0)?console.log.apply(console,a):void 0},f.error=function(){var a,b;return a=1<=arguments.length?h.call(arguments,0):[],null!=("undefined"!=typeof console&&null!==console&&null!=(b=console.error)?b.call:void 0)?console.error.apply(console,a):void 0},b=function(){var b,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I;if(n={host:"/bucky",maxInterval:3e4,aggregationInterval:5e3,decimalPrecision:3,sendLatency:!1,sample:1,active:!0},B={},!e&&(b="function"==typeof document.querySelector?document.querySelector("[data-bucky-host],[data-bucky-page],[data-bucky-requests]"):void 0))for(B={host:b.getAttribute("data-bucky-host"),pagePerformanceKey:b.getAttribute("data-bucky-page"),requestsKey:b.getAttribute("data-bucky-requests")},G=["pagePerformanceKey","requestsKey"],E=0,F=G.length;F>E;E++)q=G[E],"true"===(null!=(H=B[q])?H.toString().toLowerCase():void 0)||""===B[q]?B[q]=!0:"false"===(null!=(I=B[q])?I.toString().toLowerCase():void 0)&&(B[q]=null);return v=c({},n,B),k={timer:"ms",gauge:"g",counter:"c"},i=v.active,(C=function(){return i=v.active&&Math.random()<v.sample})(),j=[],A=function(a){return c(v,a),("sample"in a||"active"in a)&&C(),v},x=function(a,b){return null==b&&(b=v.decimalPrecision),Math.round(a*Math.pow(10,b))/Math.pow(10,b)},w={},o=function(a,b,c){var d,e;if(i)return d=1,a in w&&("counter"===c?b+=w[a].value:(d=null!=(e=w[a].count)?e:d,d++,b=w[a].value+(b-w[a].value)/d)),w[a]={value:b,type:c,count:d},m()},z=null,u=null,p=function(){return clearTimeout(z),clearTimeout(u),u=null,z=null,y()},m=function(){return clearTimeout(z),z=setTimeout(p,v.aggregationInterval),null==u?u=setTimeout(p,v.maxInterval):void 0},t=function(b){var c,d,f,h,i,j,k,l,m,n;d=e||window.XMLHttpRequest&&(window.XMLHttpRequest.defake||"withCredentials"in new window.XMLHttpRequest),e?k=!0:(f=/^(https?:\/\/[^\/]+)/i.exec(v.host),f?(i=f[1],k=i===""+document.location.protocol+"//"+document.location.host?!0:!1):k=!0),l=g(),c="";for(h in b)m=b[h],c+=""+h+":"+m+"\n";return j=k||d||null==("undefined"!=typeof window&&null!==window?window.XDomainRequest:void 0)?new(null!=(n="undefined"!=typeof window&&null!==window?window.XMLHttpRequest:void 0)?n:a):new window.XDomainRequest,j.bucky={track:!1},j.open("POST",""+v.host+"/v1/send",!0),j.setRequestHeader("Content-Type","text/plain"),j.addEventListener("load",function(){return D(g()-l)},!1),j.send(c),j},y=function(){var a,b,c,d;if(!i)return void f("Would send bucky queue");a={};for(q in w)b=w[q],j.push({path:q,count:b.count,type:b.type,value:b.value}),null!=k[b.type]?(c=b.value,("gauge"===(d=b.type)||"timer"===d)&&(c=x(c)),a[q]=""+c+"|"+k[b.type],1!==b.count&&(a[q]+="@"+x(1/b.count,5))):f.error("Type "+b.type+" not understood by Bucky");return t(a),w={}},r=!1,D=function(a){return v.sendLatency&&!r?(o("bucky.latency",a,"timer"),r=!0,setTimeout(function(){return r=!1},3e5)):void 0},s=function(a){var b,c,e,k,l,m,n,r,t,u;null==a&&(a=""),b=function(b){return(null!=a?a.length:void 0)?a+"."+b:b},m=function(a,c,d){return null==d&&(d="gauge"),null==c||null==a?void f.error("Can't log "+a+":"+c):o(b(a),c,d)},t={TIMES:{},send:function(a,b){return m(a,b,"timer")},time:function(){var a,b,c,d,e;return e=arguments[0],a=arguments[1],c=arguments[2],b=4<=arguments.length?h.call(arguments,3):[],t.start(e),d=function(){return t.stop(e)},b.splice(0,0,d),a.apply(c,b)},timeSync:function(){var a,b,c,d,e;return d=arguments[0],a=arguments[1],c=arguments[2],b=4<=arguments.length?h.call(arguments,3):[],t.start(d),e=a.apply(c,b),t.stop(d),e},wrap:function(a,b){return null!=b?function(){var c;return c=1<=arguments.length?h.call(arguments,0):[],t.timeSync.apply(t,[a,b,this].concat(h.call(c)))}:function(b){return function(){var c;return c=1<=arguments.length?h.call(arguments,0):[],t.timeSync.apply(t,[a,b,this].concat(h.call(c)))}}},start:function(a){return t.TIMES[a]=g()},stop:function(a){var b;return null==t.TIMES[a]?void f.error("Timer "+a+" ended without having been started"):(b=g()-t.TIMES[a],t.TIMES[a]=void 0,t.send(a,b))},stopwatch:function(a,b){var c,d;return null!=b?d=function(){return+new Date}:(d=g,b=d()),c=b,{mark:function(c,e){var f;return null==e&&(e=0),f=d(),a&&(c=a+"."+c),t.send(c,f-b+e)},split:function(b,e){var f;return null==e&&(e=0),f=d(),a&&(b=a+"."+b),t.send(b,f-c+e),c=f}}},mark:function(a,b){var c;return null==b&&(b=+new Date),c=t.navigationStart(),t.send(a,b-c)},navigationStart:function(){var a,b,c;return null!=(a="undefined"!=typeof window&&null!==window&&null!=(b=window.performance)&&null!=(c=b.timing)?c.navigationStart:void 0)?a:d},responseEnd:function(){var a,b,c;return null!=(a="undefined"!=typeof window&&null!==window&&null!=(b=window.performance)&&null!=(c=b.timing)?c.responseEnd:void 0)?a:d},now:function(){return g()}},c=function(a,b){return null==b&&(b=1),m(a,b,"counter")},r=!1,n=function(a){var b,c,d,e,f,g=this;if(null==("undefined"!=typeof window&&null!==window&&null!=(d=window.performance)?d.timing:void 0))return!1;if(r)return!1;if(a&&a!==!0||(a=l.urlToKey(document.location.toString())+".page"),"uninitialized"===(e=document.readyState)||"loading"===e)return"function"==typeof window.addEventListener&&window.addEventListener("load",function(){return setTimeout(function(){return n.call(g,a)},500)},!1),!1;r=!0,b=window.performance.timing.navigationStart,f=window.performance.timing;for(q in f)c=f[q],"number"==typeof c&&t.send(""+a+"."+q,c-b);return!0},l={transforms:{mapping:{guid:/\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi,sha1:/\/[0-9a-f]{40}/gi,md5:/\/[0-9a-f]{32}/gi,id:/\/[0-9;_\-]+/g,email:/\/[^\/]+@[^\/]+/g,domain:[/\/[^\/]+\.[a-z]{2,3}\//gi,"/"]},enabled:["guid","sha1","md5","id","email","domain"],enable:function(a,b,c){return null==c&&(c=""),null!=b&&(this.mapping[a]=[b,c]),this.enabled.splice(0,0,a)},disable:function(a){var b,c,d;d=this.enabled;for(b in d)if(c=d[b],c===a)return void this.enabled.splice(b,1)}},sendReadyStateTimes:function(a,b){var c,d,e,f,g,h,i,j;if(null!=b){d={1:"sending",2:"headers",3:"waiting",4:"receiving"},e={},f=null;for(c in b)h=b[c],null!=f&&null!=d[c]&&(e[d[c]]=h-f),f=h;j=[];for(g in e)i=e[g],j.push(t.send(""+a+"."+g,i));return j}},urlToKey:function(a,b,c){var d,e,g,h,i,j,k,m,n,o;for(a=a.replace(/https?:\/\//i,""),h=/([^\/:]*)(?::\d+)?(\/[^\?#]*)?.*/i.exec(a),d=h[1],i=null!=(n=h[2])?n:"",o=l.transforms.enabled,k=0,m=o.length;m>k;k++)g=o[k],e=l.transforms.mapping[g],null!=e?"function"!=typeof e?(e instanceof RegExp&&(e=[e,""]),i=i.replace(e[0],e[1])):i=e(i,a,b,c):f.error("Bucky Error: Attempted to enable a mapping which is not defined: "+g);return i=decodeURIComponent(i),i=i.replace(/[^a-zA-Z0-9\-\.\/ ]+/g,"_"),j=d+i.replace(/[\/ ]/g,"."),j=j.replace(/(^\.)|(\.$)/g,""),j=j.replace(/\.com/,""),j=j.replace(/www\./,""),c&&(j=c+"."+j),b&&(j=j+"."+b.toLowerCase()),j=j.replace(/\.\./g,".")},getFullUrl:function(a,b){return null==b&&(b=document.location),/^\//.test(a)?b.hostname+a:/https?:\/\//i.test(a)?a:b.toString()+a},monitor:function(a){var b,d,e;return a&&a!==!0||(a=l.urlToKey(document.location.toString())+".requests"),d=this,b=function(b){var e,f,h,i,j,k,l,n;return l=b.type,n=b.url,f=b.event,i=b.request,h=b.readyStateTimes,j=b.startTime,null!=j?(e=g()-j,n=d.getFullUrl(n),k=d.urlToKey(n,l,a),m(k,e,"timer"),d.sendReadyStateTimes(k,h),null!=(null!=i?i.status:void 0)?(i.status>12e3?c(""+k+".0"):0!==i.status&&c(""+k+"."+i.status.toString().charAt(0)+"xx"),c(""+k+"."+i.status)):void 0):void 0},e=window.XMLHttpRequest,window.XMLHttpRequest=function(){var a,c,d,h,i,j;d=new e;try{h=null,c={},i=d.open,d.open=function(a,e,j){var k;try{c[0]=g(),d.addEventListener("readystatechange",function(){return c[d.readyState]=g()},!1),d.addEventListener("loadend",function(f){return null==d.bucky||d.bucky.track!==!1?b({type:a,url:e,event:f,startTime:h,readyStateTimes:c,request:d}):void 0},!1)}catch(l){k=l,f.error("Bucky error monitoring XHR open call",k)}return i.apply(d,arguments)},j=d.send,d.send=function(){return h=g(),j.apply(d,arguments)}}catch(k){a=k,f.error("Bucky error monitoring XHR",a)}return d}}},k=function(b){var c;return null==b&&(b=""),c=null!=a?a:"",c&&b&&(c+="."),b&&(c+=b),s(c)},e={send:m,count:c,timer:t,now:g,requests:l,sendPagePerformance:n,flush:p,setOptions:A,options:v,history:j,active:i};for(q in e)u=e[q],k[q]=u;return k},l=s(),v.pagePerformanceKey&&l.sendPagePerformance(v.pagePerformanceKey),v.requestsKey&&l.requests.monitor(v.requestsKey),l},"function"==typeof define&&define.amd?define(b):"object"==typeof exports?module.exports=b():window.Bucky=b()}).call(this);