@@ -370,6 +370,170 @@ describe('addDerivedDoubleGauge', () => {
370370 } ) ;
371371} ) ;
372372
373+ describe ( 'addInt64Cumulative' , ( ) => {
374+ let registry : MetricRegistry ;
375+ const realHrtimeFn = process . hrtime ;
376+ const realNowFn = Date . now ;
377+ const mockedTime : Timestamp = { seconds : 1450000100 , nanos : 1e7 } ;
378+
379+ beforeEach ( ( ) => {
380+ registry = new MetricRegistry ( ) ;
381+
382+ process . hrtime = ( ) => [ 100 , 1e7 ] ;
383+ Date . now = ( ) => 1450000000000 ;
384+ // Force the clock to recalibrate the time offset with the mocked time
385+ TEST_ONLY . setHrtimeReference ( ) ;
386+ } ) ;
387+
388+ afterEach ( ( ) => {
389+ process . hrtime = realHrtimeFn ;
390+ Date . now = realNowFn ;
391+ // Reset the hrtime reference so that it uses a real clock again.
392+ TEST_ONLY . resetHrtimeFunctionCache ( ) ;
393+ } ) ;
394+
395+ it ( 'should return a metric' , ( ) => {
396+ const int64Gauge = registry . addInt64Cumulative ( METRIC_NAME , METRIC_OPTIONS ) ;
397+ const pointEntry = int64Gauge . getOrCreateTimeSeries ( LABEL_VALUES_200 ) ;
398+ pointEntry . inc ( ) ;
399+
400+ const metrics = registry . getMetricProducer ( ) . getMetrics ( ) ;
401+ assert . strictEqual ( metrics . length , 1 ) ;
402+ const [ { descriptor, timeseries} ] = metrics ;
403+ assert . deepStrictEqual ( descriptor , {
404+ name : METRIC_NAME ,
405+ description : METRIC_DESCRIPTION ,
406+ labelKeys : LABEL_KEYS ,
407+ unit : UNIT ,
408+ type : MetricDescriptorType . CUMULATIVE_INT64
409+ } ) ;
410+ assert . strictEqual ( timeseries . length , 1 ) ;
411+ const [ { points} ] = timeseries ;
412+ const [ point ] = points ;
413+ assert . equal ( point . value , 1 ) ;
414+ assert . deepStrictEqual (
415+ point . timestamp ,
416+ { seconds : mockedTime . seconds , nanos : mockedTime . nanos } ) ;
417+ } ) ;
418+
419+ it ( 'should return a metric without options' , ( ) => {
420+ const int64Gauge = registry . addInt64Cumulative ( METRIC_NAME ) ;
421+ const pointEntry = int64Gauge . getDefaultTimeSeries ( ) ;
422+ pointEntry . inc ( 100 ) ;
423+
424+ const metrics = registry . getMetricProducer ( ) . getMetrics ( ) ;
425+ assert . strictEqual ( metrics . length , 1 ) ;
426+ const [ { descriptor, timeseries} ] = metrics ;
427+ assert . deepStrictEqual ( descriptor , {
428+ name : METRIC_NAME ,
429+ description : '' ,
430+ labelKeys : [ ] ,
431+ unit : UNIT ,
432+ type : MetricDescriptorType . CUMULATIVE_INT64
433+ } ) ;
434+ assert . strictEqual ( timeseries . length , 1 ) ;
435+ const [ { points} ] = timeseries ;
436+ const [ point ] = points ;
437+ assert . equal ( point . value , 100 ) ;
438+ assert . deepStrictEqual (
439+ point . timestamp ,
440+ { seconds : mockedTime . seconds , nanos : mockedTime . nanos } ) ;
441+ } ) ;
442+
443+ it ( 'should throw an error when the duplicate keys in labelKeys and constantLabels' ,
444+ ( ) => {
445+ const constantLabels = new Map ( ) ;
446+ constantLabels . set ( { key : 'k1' } , { value : 'v1' } ) ;
447+ const labelKeys = [ { key : 'k1' , description : 'desc' } ] ;
448+ assert . throws ( ( ) => {
449+ registry . addInt64Cumulative ( METRIC_NAME , { constantLabels, labelKeys} ) ;
450+ } , / ^ E r r o r : T h e k e y s f r o m L a b e l K e y s s h o u l d n o t b e p r e s e n t i n c o n s t a n t L a b e l s o r L a b e l K e y s s h o u l d n o t c o n t a i n s d u p l i c a t e k e y s $ / ) ;
451+ } ) ;
452+ } ) ;
453+
454+ describe ( 'addDoubleCumulative' , ( ) => {
455+ let registry : MetricRegistry ;
456+ const realHrtimeFn = process . hrtime ;
457+ const realNowFn = Date . now ;
458+ const mockedTime : Timestamp = { seconds : 1450000100 , nanos : 1e7 } ;
459+
460+ beforeEach ( ( ) => {
461+ registry = new MetricRegistry ( ) ;
462+
463+ process . hrtime = ( ) => [ 100 , 1e7 ] ;
464+ Date . now = ( ) => 1450000000000 ;
465+ // Force the clock to recalibrate the time offset with the mocked time
466+ TEST_ONLY . setHrtimeReference ( ) ;
467+ } ) ;
468+
469+ afterEach ( ( ) => {
470+ process . hrtime = realHrtimeFn ;
471+ Date . now = realNowFn ;
472+ // Reset the hrtime reference so that it uses a real clock again.
473+ TEST_ONLY . resetHrtimeFunctionCache ( ) ;
474+ } ) ;
475+
476+ it ( 'should return a metric' , ( ) => {
477+ const int64Gauge =
478+ registry . addDoubleCumulative ( METRIC_NAME , METRIC_OPTIONS ) ;
479+ const pointEntry = int64Gauge . getOrCreateTimeSeries ( LABEL_VALUES_200 ) ;
480+ pointEntry . inc ( 1.1 ) ;
481+
482+ const metrics = registry . getMetricProducer ( ) . getMetrics ( ) ;
483+ assert . strictEqual ( metrics . length , 1 ) ;
484+ const [ { descriptor, timeseries} ] = metrics ;
485+ assert . deepStrictEqual ( descriptor , {
486+ name : METRIC_NAME ,
487+ description : METRIC_DESCRIPTION ,
488+ labelKeys : LABEL_KEYS ,
489+ unit : UNIT ,
490+ type : MetricDescriptorType . CUMULATIVE_DOUBLE
491+ } ) ;
492+ assert . strictEqual ( timeseries . length , 1 ) ;
493+ const [ { points} ] = timeseries ;
494+ const [ point ] = points ;
495+ assert . equal ( point . value , 1.1 ) ;
496+ assert . deepStrictEqual (
497+ point . timestamp ,
498+ { seconds : mockedTime . seconds , nanos : mockedTime . nanos } ) ;
499+ } ) ;
500+
501+ it ( 'should return a metric without options' , ( ) => {
502+ const int64Gauge = registry . addDoubleCumulative ( METRIC_NAME ) ;
503+ const pointEntry = int64Gauge . getDefaultTimeSeries ( ) ;
504+ pointEntry . inc ( ) ;
505+ pointEntry . inc ( 100.12 ) ;
506+
507+ const metrics = registry . getMetricProducer ( ) . getMetrics ( ) ;
508+ assert . strictEqual ( metrics . length , 1 ) ;
509+ const [ { descriptor, timeseries} ] = metrics ;
510+ assert . deepStrictEqual ( descriptor , {
511+ name : METRIC_NAME ,
512+ description : '' ,
513+ labelKeys : [ ] ,
514+ unit : UNIT ,
515+ type : MetricDescriptorType . CUMULATIVE_DOUBLE
516+ } ) ;
517+ assert . strictEqual ( timeseries . length , 1 ) ;
518+ const [ { points} ] = timeseries ;
519+ const [ point ] = points ;
520+ assert . equal ( point . value , 101.12 ) ;
521+ assert . deepStrictEqual (
522+ point . timestamp ,
523+ { seconds : mockedTime . seconds , nanos : mockedTime . nanos } ) ;
524+ } ) ;
525+
526+ it ( 'should throw an error when the duplicate keys in labelKeys and constantLabels' ,
527+ ( ) => {
528+ const constantLabels = new Map ( ) ;
529+ constantLabels . set ( { key : 'k1' } , { value : 'v1' } ) ;
530+ const labelKeys = [ { key : 'k1' , description : 'desc' } ] ;
531+ assert . throws ( ( ) => {
532+ registry . addDoubleCumulative ( METRIC_NAME , { constantLabels, labelKeys} ) ;
533+ } , / ^ E r r o r : T h e k e y s f r o m L a b e l K e y s s h o u l d n o t b e p r e s e n t i n c o n s t a n t L a b e l s o r L a b e l K e y s s h o u l d n o t c o n t a i n s d u p l i c a t e k e y s $ / ) ;
534+ } ) ;
535+ } ) ;
536+
373537describe ( 'Add multiple gauges' , ( ) => {
374538 let registry : MetricRegistry ;
375539 const realHrtimeFn = process . hrtime ;
@@ -406,9 +570,13 @@ describe('Add multiple gauges', () => {
406570 size : ( ) => arr . length ,
407571 } ) ;
408572
573+ const int64Cumulative =
574+ registry . addInt64Cumulative ( 'metric-name4' , METRIC_OPTIONS ) ;
575+ int64Cumulative . getOrCreateTimeSeries ( LABEL_VALUES_200 ) . inc ( ) ;
576+
409577 const metrics = registry . getMetricProducer ( ) . getMetrics ( ) ;
410- assert . strictEqual ( metrics . length , 3 ) ;
411- const [ { descriptor : descriptor1 , timeseries : timeseries1 } , { descriptor : descriptor2 , timeseries : timeseries2 } , { descriptor : descriptor3 , timeseries : timeseries3 } ] = metrics ;
578+ assert . strictEqual ( metrics . length , 4 ) ;
579+ const [ { descriptor : descriptor1 , timeseries : timeseries1 } , { descriptor : descriptor2 , timeseries : timeseries2 } , { descriptor : descriptor3 , timeseries : timeseries3 } , { descriptor : descriptor4 , timeseries : timeseries4 } ] = metrics ;
412580 assert . deepStrictEqual ( descriptor1 , {
413581 name : 'metric-name1' ,
414582 description : METRIC_DESCRIPTION ,
@@ -430,6 +598,13 @@ describe('Add multiple gauges', () => {
430598 unit : UNIT ,
431599 type : MetricDescriptorType . GAUGE_INT64
432600 } ) ;
601+ assert . deepStrictEqual ( descriptor4 , {
602+ name : 'metric-name4' ,
603+ description : METRIC_DESCRIPTION ,
604+ labelKeys : LABEL_KEYS ,
605+ unit : UNIT ,
606+ type : MetricDescriptorType . CUMULATIVE_INT64
607+ } ) ;
433608 assert . strictEqual ( timeseries1 . length , 1 ) ;
434609 assert . strictEqual ( timeseries1 [ 0 ] . points . length , 1 ) ;
435610 assert . equal ( timeseries1 [ 0 ] . points [ 0 ] . value , 100 ) ;
@@ -446,5 +621,8 @@ describe('Add multiple gauges', () => {
446621 timeseries1 [ 0 ] . points [ 0 ] . timestamp , timeseries2 [ 0 ] . points [ 0 ] . timestamp ) ;
447622 assert . deepStrictEqual (
448623 timeseries2 [ 0 ] . points [ 0 ] . timestamp , timeseries3 [ 0 ] . points [ 0 ] . timestamp ) ;
624+ assert . strictEqual ( timeseries4 . length , 1 ) ;
625+ assert . strictEqual ( timeseries4 [ 0 ] . points . length , 1 ) ;
626+ assert . equal ( timeseries4 [ 0 ] . points [ 0 ] . value , 1 ) ;
449627 } ) ;
450628} ) ;
0 commit comments