UNPKG

17.2 kBJavaScriptView Raw
1/**
2* statful-client-javascript 2.0.2
3* Copyright 2017 Statful <https://www.statful.com/>
4*/
5
6!function(a){'use strict';function b(a){this.debugEnabled=a||!1}b.prototype.info=function(){if(this.debugEnabled){var a=Array.prototype.slice.call(arguments);console.info.apply(console,a)}},b.prototype.debug=function(){if(this.debugEnabled){var a=Array.prototype.slice.call(arguments);console.debug.apply(console,a)}},b.prototype.error=function(){if(this.debugEnabled){var a=Array.prototype.slice.call(arguments);console.error.apply(console,a)}},a.StatfulLogger=b}(window),function(a){'use strict';function b(a){a=a||{};var b,c=this;this.config={},this.constants={aggregationList:['avg','count','sum','first','last','p90','p95','min','max'],aggregationFrequencyList:[10,30,60,120,180,300]},Object.keys(a).forEach(function(b){c.config[b]=a[b]}),this.listQueues=[],b=new StatfulLogger(c.config.debug),this.sendRequest=function(c,d){var e=[this.config.apiAddress,c],f=e.join('/');b.debug('Request: '+f,d);var g=new XMLHttpRequest;g.open('POST',f,!0),g.timeout=a.timeout,g.setRequestHeader('Content-type','application/json'),g.send(d),g.onreadystatechange=function(){200==g.status||201==g.status?b.debug('Successfully send metric'):b.debug('Error send metric',f,g.status)}},this.registerQueue=function(a,c,d){if(d=d||this.config.flushInterval,'string'==typeof a&&'number'==typeof d){var e=this;return this.listQueues[a]={data:[],endpoint:c},this.listQueues[a].timer=setInterval(function(){var c=e.listQueues[a];c.data.length>0&&(e.config.dryrun?b.debug('Dryrun data',c.endpoint,c.data):e.sendRequest(c.endpoint,JSON.stringify(c.data)),c.data=[])},d),!0}return!1},this.unregisterQueue=function(a){this.listQueues[a]&&(clearInterval(this.listQueues[a].timer),this.listQueues[a]=void 0)},this.addItemToQueue=function(a,c){var d=(c.sampleRate||this.config.sampleRate||100)/100;return this.listQueues[a]&&Math.random()<=d?(this.listQueues[a].data.push(c),!0):(b.debug('Metric was discarded due to sample rate.'),!1)},this.setAggregations=function(a,b,c){function d(a,b,c){return a&&c.indexOf(a)===b}var e=b;return e=e.concat(c).filter(d),e=a?e.concat(a).filter(d):e||[],this.filterAggregations(e)},this.setTags=function(a,b,c,d){var e={};return Object.keys(b).forEach(function(a){e[a]=b[a]}),Object.keys(c).forEach(function(a){e[a]=c[a]}),Object.keys(a).forEach(function(b){e[b]=a[b]}),!e.app&&d&&(e.app=d),e},this.setAggregationFrequency=function(a,b,c){var d=b;return c&&(d=c),a&&(d=a),this.filterAggregationFrequency(d)},this.filterAggregations=function(a){var b=this.constants.aggregationList;return a=a||[],a.filter(function(a){return-1!==b.indexOf(a)})},this.filterAggregationFrequency=function(a){var b=this.constants.aggregationFrequencyList,c=10;return b.indexOf(a)>-1&&(c=a),c}}a.StatfulUtil=b}(window),function(a){'use strict';var b,c={dryrun:!1,debug:!1,app:void 0,namespace:'web',tags:{},aggregations:[],aggregationFrequency:10,timer:{tags:{unit:'ms'},aggregations:['avg','p90','count']},counter:{tags:{},aggregations:['sum','count']},gauge:{tags:{},aggregations:['last']},timeout:2e3,flushInterval:1e4,sampleRate:100},d={config:{apiAddress:'https://beacon.statful.com'},endpoints:{metrics:'beacon/metrics'},perf:a.performance,initialize:function(a){var d=this;d.mergeConfigs=function(a){'object'==typeof a&&null!==a||(a={}),Object.keys(c).forEach(function(a){d.config[a]=c[a]}),Object.keys(a).forEach(function(b){d.config[b]=a[b]})},d.metricsData=function(a,b,c,e,f,g,h,i){return{name:a,type:b,value:c,tags:d.util.setTags(e||{},d.config.tags,d.config[b].tags,d.config.app),aggregations:d.util.setAggregations(f,d.config.aggregations,d.config[b].aggregations),aggregationFrequency:d.util.setAggregationFrequency(g,d.config.aggregationFrequency,d.config[b].aggregationFrequency),namespace:h||d.config.namespace,sampleRate:i||d.config.sampleRate}},this.mergeConfigs(a),b=new StatfulLogger(d.config.debug),d.util=new StatfulUtil({apiAddress:this.config.apiAddress,debug:this.config.debug,dryrun:this.config.dryrun,flushInterval:this.config.flushInterval,timeout:this.config.timeout}),d.util.registerQueue('metrics',this.endpoints.metrics,this.config.flushInterval)},measureTimeUserTiming:function(a){var c,e=d.perf.getEntriesByName(a).filter(function(a){return'measure'===a.entryType});return e.length>0?c=e[e.length-1].duration:b.debug('Measure '+a+' not found'),c},clearMarks:function(a){try{a?a.forEach(function(a){a&&d.perf.clearMarks(a)}):d.perf.clearMarks()}catch(a){b.error(a)}},clearResources:function(){try{d.perf.clearResourceTimings()}catch(a){b.error(a)}},clearMeasures:function(a){try{a?a.forEach(function(a){d.perf.clearMeasures(a)}):d.perf.clearMeasures()}catch(a){b.error(a)}},registerMark:function(a){try{b.debug('Register Mark',a),a?d.perf.mark(a):b.error('Undefined resource name to register as a mark')}catch(a){b.error(a)}},registerMeasure:function(a,c,e){try{if(b.debug('Register Measure',a,c,e),a){var f={clearMarks:!1,clearMeasures:!1};e=e||{},Object.keys(e).forEach(function(a){f[a]=e[a]}),f.endMark||(this.registerMark(a),f.endMark=a),d.perf.measure(a,f.startMark,f.endMark);var g=this.measureTimeUserTiming(a);g?this.util.addItemToQueue('metrics',new this.metricsData(c,'timer',g,f.tags,f.aggregations,f.aggregationFrequency,f.namespace,f.sampleRate)):b.error('Failed to get measure time to register as timer value'),f.clearMarks&&this.clearMarks([f.startMark,f.endMark]),f.clearMeasures&&this.clearMeasures([a])}else b.error('Undefined resource name to register as a measure')}catch(a){b.error(a)}},timer:function(a,c,d){try{if(b.debug('Register Timer',a,c,d),a&&c>=0){d=d||{};var e=new this.metricsData(a,'timer',c,d.tags,d.agg,d.aggFreq,d.namespace,d.sampleRate);this.util.addItemToQueue('metrics',e)}else b.error('Undefined metric name or invalid value to register as a timer')}catch(a){b.error(a)}},counter:function(a,c,d){try{if(b.debug('Register Counter',a,d),c=c||1,a){d=d||{};var e=new this.metricsData(a,'counter',c,d.tags,d.agg,d.aggFreq,d.namespace,d.sampleRate);this.util.addItemToQueue('metrics',e)}else b.error('Undefined metric name to register as a counter')}catch(a){b.error(a)}},gauge:function(a,c,d){try{if(b.debug('Register Gauge',a,c,d),a&&c){d=d||{};var e=new this.metricsData(a,'gauge',c,d.tags,d.agg,d.aggFreq,d.namespace,d.sampleRate);this.util.addItemToQueue('metrics',e)}else b.error('Undefined metric name/value to register as a gauge')}catch(a){b.error(a)}}};a.statful=d}(window),function(a){'use strict';void 0===a&&(a={}),void 0===a.performance&&(a.performance={}),a._perfRefForUserTimingPolyfill=a.performance,a.performance.userTimingJsNow=!1,a.performance.userTimingJsNowPrefixed=!1,a.performance.userTimingJsUserTiming=!1,a.performance.userTimingJsUserTimingPrefixed=!1,a.performance.userTimingJsPerformanceTimeline=!1,a.performance.userTimingJsPerformanceTimelinePrefixed=!1;var b,c,d=[],e=[],f=null;if('function'!=typeof a.performance.now){for(a.performance.userTimingJsNow=!0,e=['webkitNow','msNow','mozNow'],b=0;b<e.length;b++)if('function'==typeof a.performance[e[b]]){a.performance.now=a.performance[e[b]],a.performance.userTimingJsNowPrefixed=!0;break}var g=+new Date;a.performance.timing&&a.performance.timing.navigationStart?g=a.performance.timing.navigationStart:'undefined'!=typeof process&&'function'==typeof process.hrtime&&(g=process.hrtime(),a.performance.now=function(){var a=process.hrtime(g);return 1e3*a[0]+1e-6*a[1]}),'function'!=typeof a.performance.now&&(Date.now?a.performance.now=function(){return Date.now()-g}:a.performance.now=function(){return+new Date-g})}var h=function(){},i=function(){},j=[],k=!1,l=!1;if('function'!=typeof a.performance.getEntries||'function'!=typeof a.performance.mark){for('function'==typeof a.performance.getEntries&&'function'!=typeof a.performance.mark&&(l=!0),a.performance.userTimingJsPerformanceTimeline=!0,d=['webkit','moz'],e=['getEntries','getEntriesByName','getEntriesByType'],b=0;b<e.length;b++)for(c=0;c<d.length;c++)f=d[c]+e[b].substr(0,1).toUpperCase()+e[b].substr(1),'function'==typeof a.performance[f]&&(a.performance[e[b]]=a.performance[f],a.performance.userTimingJsPerformanceTimelinePrefixed=!0);h=function(a){j.push(a),'measure'===a.entryType&&(k=!0)};var m=function(){k&&(j.sort(function(a,b){return a.startTime-b.startTime}),k=!1)};if(i=function(a,c){for(b=0;b<j.length;)j[b].entryType===a&&(void 0===c||j[b].name===c)?j.splice(b,1):b++},'function'!=typeof a.performance.getEntries||l){var n=a.performance.getEntries;a.performance.getEntries=function(){m();var b=j.slice(0);return l&&n&&(Array.prototype.push.apply(b,n.call(a.performance)),b.sort(function(a,b){return a.startTime-b.startTime})),b}}if('function'!=typeof a.performance.getEntriesByType||l){var o=a.performance.getEntriesByType;a.performance.getEntriesByType=function(c){if(void 0===c||'mark'!==c&&'measure'!==c)return l&&o?o.call(a.performance,c):[];'measure'===c&&m();var d=[];for(b=0;b<j.length;b++)j[b].entryType===c&&d.push(j[b]);return d}}if('function'!=typeof a.performance.getEntriesByName||l){var p=a.performance.getEntriesByName;a.performance.getEntriesByName=function(c,d){if(d&&'mark'!==d&&'measure'!==d)return l&&p?p.call(a.performance,c,d):[];void 0!==d&&'measure'===d&&m();var e=[];for(b=0;b<j.length;b++)void 0!==d&&j[b].entryType!==d||j[b].name===c&&e.push(j[b]);return l&&p&&(Array.prototype.push.apply(e,p.call(a.performance,c,d)),e.sort(function(a,b){return a.startTime-b.startTime})),e}}}if('function'!=typeof a.performance.mark){for(a.performance.userTimingJsUserTiming=!0,d=['webkit','moz','ms'],e=['mark','measure','clearMarks','clearMeasures'],b=0;b<e.length;b++)for(c=0;c<d.length;c++)f=d[c]+e[b].substr(0,1).toUpperCase()+e[b].substr(1),'function'==typeof a.performance[f]&&(a.performance[e[b]]=a.performance[f],a.performance.userTimingJsUserTimingPrefixed=!0);var q={};'function'!=typeof a.performance.mark&&(a.performance.mark=function(b){var c=a.performance.now();if(void 0===b)throw new SyntaxError('Mark name must be specified');if(a.performance.timing&&b in a.performance.timing)throw new SyntaxError('Mark name is not allowed');q[b]||(q[b]=[]),q[b].push(c),h({entryType:'mark',name:b,startTime:c,duration:0})}),'function'!=typeof a.performance.clearMarks&&(a.performance.clearMarks=function(a){a?q[a]=[]:q={},i('mark',a)}),'function'!=typeof a.performance.measure&&(a.performance.measure=function(b,c,d){var e=a.performance.now();if(void 0===b)throw new SyntaxError('Measure must be specified');if(!c)return void h({entryType:'measure',name:b,startTime:0,duration:e});var f=0;if(a.performance.timing&&c in a.performance.timing){if('navigationStart'!==c&&0===a.performance.timing[c])throw new Error(c+' has a timing of 0');f=a.performance.timing[c]-a.performance.timing.navigationStart}else{if(!(c in q))throw new Error(c+' mark not found');f=q[c][q[c].length-1]}var g=e;if(d)if(g=0,a.performance.timing&&d in a.performance.timing){if('navigationStart'!==d&&0===a.performance.timing[d])throw new Error(d+' has a timing of 0');g=a.performance.timing[d]-a.performance.timing.navigationStart}else{if(!(d in q))throw new Error(d+' mark not found');g=q[d][q[d].length-1]}h({entryType:'measure',name:b,startTime:f,duration:g-f})}),'function'!=typeof a.performance.clearMeasures&&(a.performance.clearMeasures=function(a){i('measure',a)})}'function'==typeof define&&define.amd?define([],function(){return a.performance}):'undefined'!=typeof module&&void 0!==module.exports&&(module.exports=a.performance)}('undefined'!=typeof window?window:void 0),Object.getPrototypeOf||(Object.getPrototypeOf=function(a){if(a!==Object(a))throw TypeError('Object.getPrototypeOf called on non-object');return a.__proto__||a.constructor.prototype||Object.prototype}),'function'!=typeof Object.getOwnPropertyNames&&(Object.getOwnPropertyNames=function(a){if(a!==Object(a))throw TypeError('Object.getOwnPropertyNames called on non-object');var b,c=[];for(b in a)Object.prototype.hasOwnProperty.call(a,b)&&c.push(b);return c}),'function'!=typeof Object.create&&(Object.create=function(a,b){function c(){}if('object'!=typeof a)throw TypeError();c.prototype=a;var d=new c;if(a&&(d.constructor=c),void 0!==b){if(b!==Object(b))throw TypeError();Object.defineProperties(d,b)}return d}),function(){if(!Object.defineProperty||!function(){try{return Object.defineProperty({},'x',{}),!0}catch(a){return!1}}()){var a=Object.defineProperty;Object.defineProperty=function(b,c,d){if(a)try{return a(b,c,d)}catch(a){}if(b!==Object(b))throw TypeError('Object.defineProperty called on non-object');return Object.prototype.__defineGetter__&&'get'in d&&Object.prototype.__defineGetter__.call(b,c,d.get),Object.prototype.__defineSetter__&&'set'in d&&Object.prototype.__defineSetter__.call(b,c,d.set),'value'in d&&(b[c]=d.value),b}}}(),'function'!=typeof Object.defineProperties&&(Object.defineProperties=function(a,b){if(a!==Object(a))throw TypeError('Object.defineProperties called on non-object');var c;for(c in b)Object.prototype.hasOwnProperty.call(b,c)&&Object.defineProperty(a,c,b[c]);return a}),Object.keys||(Object.keys=function(a){if(a!==Object(a))throw TypeError('Object.keys called on non-object');var b,c=[];for(b in a)Object.prototype.hasOwnProperty.call(a,b)&&c.push(b);return c}),Function.prototype.bind||(Function.prototype.bind=function(a){if('function'!=typeof this)throw TypeError('Bind must be called on a function');var b=Array.prototype.slice.call(arguments,1),c=this,d=function(){},e=function(){return c.apply(this instanceof d?this:a,b.concat(Array.prototype.slice.call(arguments)))};return this.prototype&&(d.prototype=this.prototype),e.prototype=new d,e}),Array.isArray=Array.isArray||function(a){return Boolean(a&&'[object Array]'===Object.prototype.toString.call(Object(a)))},Array.prototype.indexOf||(Array.prototype.indexOf=function(a){if(void 0===this||null===this)throw TypeError();var b=Object(this),c=b.length>>>0;if(0===c)return-1;var d=0;if(arguments.length>0&&(d=Number(arguments[1]),isNaN(d)?d=0:0!==d&&d!==1/0&&d!==-1/0&&(d=(d>0||-1)*Math.floor(Math.abs(d)))),d>=c)return-1;for(var e=d>=0?d:Math.max(c-Math.abs(d),0);e<c;e++)if(e in b&&b[e]===a)return e;return-1}),Array.prototype.lastIndexOf||(Array.prototype.lastIndexOf=function(a){if(void 0===this||null===this)throw TypeError();var b=Object(this),c=b.length>>>0;if(0===c)return-1;var d=c;arguments.length>1&&(d=Number(arguments[1]),d!==d?d=0:0!==d&&d!==1/0&&d!==-1/0&&(d=(d>0||-1)*Math.floor(Math.abs(d))));for(var e=d>=0?Math.min(d,c-1):c-Math.abs(d);e>=0;e--)if(e in b&&b[e]===a)return e;return-1}),Array.prototype.every||(Array.prototype.every=function(a){if(void 0===this||null===this)throw TypeError();var b=Object(this),c=b.length>>>0;if('function'!=typeof a)throw TypeError();var d,e=arguments[1];for(d=0;d<c;d++)if(d in b&&!a.call(e,b[d],d,b))return!1;return!0}),Array.prototype.some||(Array.prototype.some=function(a){if(void 0===this||null===this)throw TypeError();var b=Object(this),c=b.length>>>0;if('function'!=typeof a)throw TypeError();var d,e=arguments[1];for(d=0;d<c;d++)if(d in b&&a.call(e,b[d],d,b))return!0;return!1}),Array.prototype.forEach||(Array.prototype.forEach=function(a){if(void 0===this||null===this)throw TypeError();var b=Object(this),c=b.length>>>0;if('function'!=typeof a)throw TypeError();var d,e=arguments[1];for(d=0;d<c;d++)d in b&&a.call(e,b[d],d,b)}),Array.prototype.map||(Array.prototype.map=function(a){if(void 0===this||null===this)throw TypeError();var b=Object(this),c=b.length>>>0;if('function'!=typeof a)throw TypeError();var d=[];d.length=c;var e,f=arguments[1];for(e=0;e<c;e++)e in b&&(d[e]=a.call(f,b[e],e,b));return d}),Array.prototype.filter||(Array.prototype.filter=function(a){if(void 0===this||null===this)throw TypeError();var b=Object(this),c=b.length>>>0;if('function'!=typeof a)throw TypeError();var d,e=[],f=arguments[1];for(d=0;d<c;d++)if(d in b){var g=b[d];a.call(f,g,d,b)&&e.push(g)}return e}),Array.prototype.reduce||(Array.prototype.reduce=function(a){if(void 0===this||null===this)throw TypeError();var b=Object(this),c=b.length>>>0;if('function'!=typeof a)throw TypeError();if(0===c&&1===arguments.length)throw TypeError();var d,e=0;if(arguments.length>=2)d=arguments[1];else for(;;){if(e in b){d=b[e++];break}if(++e>=c)throw TypeError()}for(;e<c;)e in b&&(d=a.call(void 0,d,b[e],e,b)),e++;return d}),Array.prototype.reduceRight||(Array.prototype.reduceRight=function(a){if(void 0===this||null===this)throw TypeError();var b=Object(this),c=b.length>>>0;if('function'!=typeof a)throw TypeError();if(0===c&&1===arguments.length)throw TypeError();var d,e=c-1;if(arguments.length>=2)d=arguments[1];else for(;;){if(e in this){d=this[e--];break}if(--e<0)throw TypeError()}for(;e>=0;)e in b&&(d=a.call(void 0,d,b[e],e,b)),e--;return d}),String.prototype.trim||(String.prototype.trim=function(){return String(this).replace(/^\s+/,'').replace(/\s+$/,'')}),Date.now||(Date.now=function(){return Number(new Date)}),Date.prototype.toISOString||(Date.prototype.toISOString=function(){function a(a){return('00'+a).slice(-2)}return this.getUTCFullYear()+'-'+a(this.getUTCMonth()+1)+'-'+a(this.getUTCDate())+'T'+a(this.getUTCHours())+':'+a(this.getUTCMinutes())+':'+a(this.getUTCSeconds())+'.'+function(a){return('000'+a).slice(-3)}(this.getUTCMilliseconds())+'Z'});
\No newline at end of file