@@ -26,6 +26,7 @@ const LABEL_KEYS: LabelKey[] = [{key: 'code', description: 'desc'}];
2626const LABEL_KEYS_WITH_NULL : LabelKey [ ] =
2727 [ { key : 'code' , description : 'desc' } , null ] ;
2828const LABEL_VALUES_200 : LabelValue [ ] = [ { value : '200' } ] ;
29+ const LABEL_VALUES_400 : LabelValue [ ] = [ { value : '400' } ] ;
2930
3031describe ( 'addInt64Gauge' , ( ) => {
3132 const oldProcessHrtime = process . hrtime ;
@@ -174,12 +175,12 @@ describe('addDoubleGauge', () => {
174175 } , / ^ E r r o r : l a b e l K e y e l e m e n t s s h o u l d n o t b e a N U L L $ / ) ;
175176 } ) ;
176177 it ( 'should return a metric' , ( ) => {
177- const int64Gauge = registry . addDoubleGauge (
178+ const doubleGauge = registry . addDoubleGauge (
178179 METRIC_NAME , METRIC_DESCRIPTION , UNIT , LABEL_KEYS ) ;
179- const pointEntry = int64Gauge . getOrCreateTimeSeries ( LABEL_VALUES_200 ) ;
180+ const pointEntry = doubleGauge . getOrCreateTimeSeries ( LABEL_VALUES_200 ) ;
180181 pointEntry . add ( 5.5 ) ;
181182
182- const pointEntry1 = int64Gauge . getOrCreateTimeSeries ( LABEL_VALUES_200 ) ;
183+ const pointEntry1 = doubleGauge . getOrCreateTimeSeries ( LABEL_VALUES_200 ) ;
183184 pointEntry1 . set ( 0.7 ) ;
184185
185186 const metrics = registry . getMetricProducer ( ) . getMetrics ( ) ;
@@ -272,6 +273,39 @@ describe('addDerivedInt64Gauge', () => {
272273 METRIC_NAME , METRIC_DESCRIPTION , UNIT , LABEL_KEYS_WITH_NULL ) ;
273274 } , / ^ E r r o r : l a b e l K e y e l e m e n t s s h o u l d n o t b e a N U L L $ / ) ;
274275 } ) ;
276+ it ( 'should return a metric' , ( ) => {
277+ const map = new Map ( ) ;
278+ map . set ( 'key' , 'value' ) ;
279+ const derivedInt64Gauge = registry . addDerivedInt64Gauge (
280+ METRIC_NAME , METRIC_DESCRIPTION , UNIT , LABEL_KEYS ) ;
281+ derivedInt64Gauge . createTimeSeries ( LABEL_VALUES_200 , map ) ;
282+ map . set ( 'key1' , 'value1' ) ;
283+
284+ const metrics = registry . getMetricProducer ( ) . getMetrics ( ) ;
285+ assert . strictEqual ( metrics . length , 1 ) ;
286+ const [ { descriptor, timeseries} ] = metrics ;
287+ assert . deepStrictEqual ( descriptor , {
288+ name : METRIC_NAME ,
289+ description : METRIC_DESCRIPTION ,
290+ 'labelKeys' : LABEL_KEYS ,
291+ unit : UNIT ,
292+ type : MetricDescriptorType . GAUGE_INT64
293+ } ) ;
294+ assert . strictEqual ( timeseries . length , 1 ) ;
295+ const [ { points} ] = timeseries ;
296+ const [ point ] = points ;
297+ assert . equal ( point . value , 2 ) ;
298+ assert . deepStrictEqual ( point . timestamp , { seconds : 1000 , nanos : 1e7 } ) ;
299+ } ) ;
300+
301+ it ( 'should throw an error when the register same metric' , ( ) => {
302+ registry . addDerivedInt64Gauge (
303+ METRIC_NAME , METRIC_DESCRIPTION , UNIT , LABEL_KEYS ) ;
304+ assert . throws ( ( ) => {
305+ registry . addDerivedInt64Gauge (
306+ METRIC_NAME , METRIC_DESCRIPTION , UNIT , LABEL_KEYS ) ;
307+ } , / ^ E r r o r : A m e t r i c w i t h t h e n a m e m e t r i c - n a m e h a s a l r e a d y b e e n r e g i s t e r e d .$ / ) ;
308+ } ) ;
275309} ) ;
276310
277311describe ( 'addDerivedDoubleGauge' , ( ) => {
@@ -339,4 +373,104 @@ describe('addDerivedDoubleGauge', () => {
339373 METRIC_NAME , METRIC_DESCRIPTION , UNIT , LABEL_KEYS_WITH_NULL ) ;
340374 } , / ^ E r r o r : l a b e l K e y e l e m e n t s s h o u l d n o t b e a N U L L $ / ) ;
341375 } ) ;
376+ it ( 'should return a metric' , ( ) => {
377+ class QueueManager {
378+ getValue ( ) : number {
379+ return 0.7 ;
380+ }
381+ }
382+ const derivedDoubleGauge = registry . addDerivedDoubleGauge (
383+ METRIC_NAME , METRIC_DESCRIPTION , UNIT , LABEL_KEYS ) ;
384+ derivedDoubleGauge . createTimeSeries ( LABEL_VALUES_200 , new QueueManager ( ) ) ;
385+
386+ const metrics = registry . getMetricProducer ( ) . getMetrics ( ) ;
387+ assert . strictEqual ( metrics . length , 1 ) ;
388+ const [ { descriptor, timeseries} ] = metrics ;
389+ assert . deepStrictEqual ( descriptor , {
390+ name : METRIC_NAME ,
391+ description : METRIC_DESCRIPTION ,
392+ 'labelKeys' : LABEL_KEYS ,
393+ unit : UNIT ,
394+ type : MetricDescriptorType . GAUGE_DOUBLE
395+ } ) ;
396+ assert . strictEqual ( timeseries . length , 1 ) ;
397+ const [ { points} ] = timeseries ;
398+ const [ point ] = points ;
399+ assert . equal ( point . value , 0.7 ) ;
400+ assert . deepStrictEqual ( point . timestamp , { seconds : 1000 , nanos : 1e7 } ) ;
401+ } ) ;
402+
403+ it ( 'should throw an error when the register same metric' , ( ) => {
404+ registry . addDerivedDoubleGauge (
405+ METRIC_NAME , METRIC_DESCRIPTION , UNIT , LABEL_KEYS ) ;
406+ assert . throws ( ( ) => {
407+ registry . addDerivedDoubleGauge (
408+ METRIC_NAME , METRIC_DESCRIPTION , UNIT , LABEL_KEYS ) ;
409+ } , / ^ E r r o r : A m e t r i c w i t h t h e n a m e m e t r i c - n a m e h a s a l r e a d y b e e n r e g i s t e r e d .$ / ) ;
410+ } ) ;
411+ } ) ;
412+
413+ describe ( 'Add multiple gauges' , ( ) => {
414+ const oldProcessHrtime = process . hrtime ;
415+ let registry : MetricRegistry ;
416+
417+ before ( ( ) => {
418+ registry = new MetricRegistry ( ) ;
419+ process . hrtime = ( ) => [ 1000 , 1e7 ] ;
420+ } ) ;
421+
422+ after ( ( ) => {
423+ process . hrtime = oldProcessHrtime ;
424+ } ) ;
425+
426+ it ( 'should return metrics' , ( ) => {
427+ const int64Gauge = registry . addInt64Gauge (
428+ 'metric-name1' , METRIC_DESCRIPTION , UNIT , LABEL_KEYS ) ;
429+ int64Gauge . getOrCreateTimeSeries ( LABEL_VALUES_200 ) . add ( 100 ) ;
430+
431+ const doubleGauge = registry . addDoubleGauge (
432+ 'metric-name2' , METRIC_DESCRIPTION , UNIT , LABEL_KEYS ) ;
433+ doubleGauge . getOrCreateTimeSeries ( LABEL_VALUES_200 ) . add ( 5.5 ) ;
434+
435+ const arr = new Array ( 5 ) . fill ( 'test' ) ;
436+ const derivedInt64Gauge = registry . addDerivedInt64Gauge (
437+ 'metric-name3' , METRIC_DESCRIPTION , UNIT , LABEL_KEYS ) ;
438+ derivedInt64Gauge . createTimeSeries ( LABEL_VALUES_400 , {
439+ size : ( ) => arr . length ,
440+ } ) ;
441+
442+ const metrics = registry . getMetricProducer ( ) . getMetrics ( ) ;
443+ assert . strictEqual ( metrics . length , 3 ) ;
444+ const [ { descriptor : descriptor1 , timeseries : timeseries1 } , { descriptor : descriptor2 , timeseries : timeseries2 } , { descriptor : descriptor3 , timeseries : timeseries3 } ] = metrics ;
445+ assert . deepStrictEqual ( descriptor1 , {
446+ name : 'metric-name1' ,
447+ description : METRIC_DESCRIPTION ,
448+ 'labelKeys' : LABEL_KEYS ,
449+ unit : UNIT ,
450+ type : MetricDescriptorType . GAUGE_INT64
451+ } ) ;
452+ assert . deepStrictEqual ( descriptor2 , {
453+ name : 'metric-name2' ,
454+ description : METRIC_DESCRIPTION ,
455+ 'labelKeys' : LABEL_KEYS ,
456+ unit : UNIT ,
457+ type : MetricDescriptorType . GAUGE_DOUBLE
458+ } ) ;
459+ assert . deepStrictEqual ( descriptor3 , {
460+ name : 'metric-name3' ,
461+ description : METRIC_DESCRIPTION ,
462+ 'labelKeys' : LABEL_KEYS ,
463+ unit : UNIT ,
464+ type : MetricDescriptorType . GAUGE_INT64
465+ } ) ;
466+ assert . strictEqual ( timeseries1 . length , 1 ) ;
467+ assert . strictEqual ( timeseries1 [ 0 ] . points . length , 1 ) ;
468+ assert . equal ( timeseries1 [ 0 ] . points [ 0 ] . value , 100 ) ;
469+ assert . strictEqual ( timeseries2 . length , 1 ) ;
470+ assert . strictEqual ( timeseries2 [ 0 ] . points . length , 1 ) ;
471+ assert . equal ( timeseries2 [ 0 ] . points [ 0 ] . value , 5.5 ) ;
472+ assert . strictEqual ( timeseries3 . length , 1 ) ;
473+ assert . strictEqual ( timeseries3 [ 0 ] . points . length , 1 ) ;
474+ assert . equal ( timeseries3 [ 0 ] . points [ 0 ] . value , 5 ) ;
475+ } ) ;
342476} ) ;
0 commit comments