@@ -57,10 +57,6 @@ export default class Statful {
5757 apiAddress : 'https://beacon.statful.com'
5858 } ;
5959
60- this . endpoints = {
61- metrics : 'beacon/metrics'
62- } ;
63-
6460 // Set default properties
6561 if ( typeof clientConfig !== 'object' || clientConfig === null ) {
6662 clientConfig = { } ;
@@ -73,16 +69,7 @@ export default class Statful {
7369 this . logger = new StatfulLogger ( this . config . debug ) ;
7470
7571 // Create Util
76- this . util = new StatfulUtil ( {
77- apiAddress : this . config . apiAddress ,
78- debug : this . config . debug ,
79- dryrun : this . config . dryrun ,
80- flushInterval : this . config . flushInterval ,
81- timeout : this . config . timeout
82- } ) ;
83-
84- //Register queue to send metrics
85- this . util . registerQueue ( 'metrics' , this . endpoints . metrics , this . config . flushInterval ) ;
72+ this . util = new StatfulUtil ( this . config ) ;
8673 }
8774
8875 /**
@@ -91,16 +78,14 @@ export default class Statful {
9178 * @returns {number }
9279 */
9380 static measureTimeUserTiming ( measureName = '' ) {
81+ const measure = window . performance . getEntriesByName ( measureName ) . filter ( ( entry ) => entry . entryType === 'measure' ) ;
9482 let time ;
95- let measure = window . performance . getEntriesByName ( measureName ) . filter ( ( entry ) => {
96- return entry . entryType === 'measure' ;
97- } ) ;
9883
9984 if ( measure . length > 0 ) {
10085 // Always use the most recent measure if more exist
10186 time = measure [ measure . length - 1 ] . duration ;
10287 } else {
103- this . logger . debug ( ' Measure ' + measureName + ' not found' ) ;
88+ this . logger . debug ( ` Measure ${ measureName } not found` ) ;
10489 }
10590
10691 return time ;
@@ -112,7 +97,7 @@ export default class Statful {
11297 */
11398 static clearMarks ( marks ) {
11499 try {
115- if ( marks ) {
100+ if ( Array . isArray ( marks ) ) {
116101 marks . forEach ( ( mark ) => {
117102 if ( mark ) {
118103 window . performance . clearMarks ( mark ) ;
@@ -143,9 +128,11 @@ export default class Statful {
143128 */
144129 static clearMeasures ( measures ) {
145130 try {
146- if ( measures ) {
131+ if ( Array . isArray ( measures ) ) {
147132 measures . forEach ( ( measure ) => {
148- window . performance . clearMeasures ( measure ) ;
133+ if ( measure ) {
134+ window . performance . clearMeasures ( measure ) ;
135+ }
149136 } ) ;
150137 } else {
151138 window . performance . clearMeasures ( ) ;
@@ -187,11 +174,7 @@ export default class Statful {
187174 clearMeasures : false
188175 } ;
189176
190- options = options || { } ;
191-
192- Object . keys ( options ) . forEach ( ( key ) => {
193- defaults [ key ] = options [ key ] ;
194- } ) ;
177+ Object . assign ( defaults , options ) ;
195178
196179 // Create endMark if none is set
197180 if ( ! defaults . endMark ) {
@@ -207,7 +190,7 @@ export default class Statful {
207190 if ( time ) {
208191 // Push metrics to queue
209192 let metricItem = new Metric ( metricName , 'timer' , time , defaults , this . config ) ;
210- this . util . addItemToQueue ( 'metrics' , metricItem ) ;
193+ this . util . addMetricToQueue ( metricItem ) ;
211194 } else {
212195 this . logger . error ( 'Failed to get measure time to register as timer value' ) ;
213196 }
@@ -234,19 +217,14 @@ export default class Statful {
234217 * @param {object } options - set of option (tags, agg, aggFreq, namespace)
235218 */
236219 static timer ( metricName , metricValue , options = { } ) {
237- try {
238- this . logger . debug ( 'Register Timer' , metricName , metricValue , options ) ;
239- if ( metricName && metricValue >= 0 ) {
240- options = options || { } ;
241-
242- // Push metrics to queue
243- let item = new Metric ( metricName , 'timer' , metricValue , options , this . config ) ;
244- this . util . addItemToQueue ( 'metrics' , item ) ;
245- } else {
246- this . logger . error ( 'Undefined metric name or invalid value to register as a timer' ) ;
247- }
248- } catch ( ex ) {
249- this . logger . error ( ex ) ;
220+ this . logger . debug ( 'Register Timer' , metricName , metricValue , options ) ;
221+ if ( ! isNaN ( metricValue ) && metricName ) {
222+ metricValue = Math . abs ( metricValue ) ;
223+ // Push metrics to queue
224+ let item = new Metric ( metricName , 'timer' , metricValue , options , this . config ) ;
225+ this . util . addMetricToQueue ( item ) ;
226+ } else {
227+ this . logger . error ( 'Undefined metric name or invalid value to register as a timer' ) ;
250228 }
251229 }
252230
@@ -256,43 +234,33 @@ export default class Statful {
256234 * @param {number } metricValue - count value to be sent
257235 * @param {object } options - set of option (tags, agg, aggFreq, namespace)
258236 */
259- static counter ( metricName , metricValue , options = { } ) {
260- try {
261- this . logger . debug ( 'Register Counter' , metricName , options ) ;
262- metricValue = metricValue || 1 ;
263-
264- if ( metricName ) {
265- options = options || { } ;
237+ static counter ( metricName , metricValue = 1 , options = { } ) {
238+ this . logger . debug ( 'Register Counter' , metricName , options ) ;
239+ if ( ! isNaN ( metricValue ) && metricName ) {
240+ metricValue = Math . abs ( parseInt ( metricValue , 10 ) ) ;
266241
267- // Push metrics to queue
268- let item = new Metric ( metricName , 'counter' , metricValue , options , this . config ) ;
269- this . util . addItemToQueue ( 'metrics' , item ) ;
270- } else {
271- this . logger . error ( 'Undefined metric name to register as a counter' ) ;
272- }
273- } catch ( ex ) {
274- this . logger . error ( ex ) ;
242+ // Push metrics to queue
243+ let item = new Metric ( metricName , 'counter' , metricValue , options , this . config ) ;
244+ this . util . addMetricToQueue ( item ) ;
245+ } else {
246+ this . logger . error ( 'Undefined metric name or invalid value to register as a counter' ) ;
275247 }
276248 }
277249
278250 /**
279251 * Register gauge
280- * @param {string } metricName metric name to be sent
281- * @param {number } metricValue gauge value to be sent
252+ * @param {string } metricName - metric name to be sent
253+ * @param {number } metricValue - gauge value to be sent
282254 * @param {object } options - set of option (tags, agg, aggFreq, namespace)
283255 */
284256 static gauge ( metricName , metricValue , options = { } ) {
285- try {
286- this . logger . debug ( 'Register Gauge' , metricName , metricValue , options ) ;
287- if ( metricName && metricValue ) {
288- // Push metrics to queue
289- let item = new Metric ( metricName , 'gauge' , metricValue , options , this . config ) ;
290- this . util . addItemToQueue ( 'metrics' , item ) ;
291- } else {
292- this . logger . error ( 'Undefined metric name/value to register as a gauge' ) ;
293- }
294- } catch ( ex ) {
295- this . logger . error ( ex ) ;
257+ this . logger . debug ( 'Register Gauge' , metricName , metricValue , options ) ;
258+ if ( ! isNaN ( metricValue ) && metricName ) {
259+ // Push metrics to queue
260+ let item = new Metric ( metricName , 'gauge' , metricValue , options , this . config ) ;
261+ this . util . addMetricToQueue ( item ) ;
262+ } else {
263+ this . logger . error ( 'Undefined metric name or invalid value to register as a gauge' ) ;
296264 }
297265 }
298266}
0 commit comments