11'use strict' ;
22
3+ const Joi = require ( 'joi' ) ;
34const { Writable } = require ( 'readable-stream' ) ;
45const {
56 createCounter,
@@ -14,7 +15,7 @@ const {
1415 overrideConfig,
1516 constructorOptions,
1617} = require ( '../schemas' ) ;
17- const Joi = require ( 'joi' ) ;
18+
1819const overrides = new Map ( ) ;
1920const metricTypes = {
2021 HISTOGRAM : 'histogram' ,
@@ -29,29 +30,26 @@ module.exports = class PrometheusMetricsConsumer extends Writable {
2930 this . options = Joi . attempt (
3031 options ,
3132 constructorOptions ,
32- 'Invalid `options` object given to new FinnPodiumMetrics.'
33+ 'Invalid `options` object given to new PrometheusMetricsConsumer.' ,
3334 ) ;
3435
35- const client = this . options . client ;
36+ const { client } = this . options ;
3637 this . Counter = client . Counter ;
3738 this . Gauge = client . Gauge ;
3839 this . Histogram = client . Histogram ;
3940 this . Summary = client . Summary ;
4041 this . bucketFunction = client . exponentialBuckets ;
41- this . register = client . register ;
42+ this . registry = new client . Registry ( ) ;
4243 }
4344
4445 get [ Symbol . toStringTag ] ( ) {
4546 return 'PrometheusMetricsConsumer' ;
4647 }
4748
4849 labels ( metric ) {
49- const labelNames = [ ] ;
50- const labelValues = [ ] ;
51- for ( const [ key , val ] of Object . entries ( metric . meta || { } ) ) {
52- labelNames . push ( key ) ;
53- labelValues . push ( val ) ;
54- }
50+ const labelNames = Object . keys ( metric . meta || { } ) ;
51+ const labelValues = Object . values ( metric . meta || { } ) ;
52+
5553 return { labelNames, labelValues } ;
5654 }
5755
@@ -63,11 +61,14 @@ module.exports = class PrometheusMetricsConsumer extends Writable {
6361 const { name } = metric ;
6462 const { labelNames, labelValues } = this . labels ( metric ) ;
6563
66- if ( ! this . register . getSingleMetric ( name ) ) {
67- createCounter ( this . Counter , metric , { labels : labelNames } ) ;
64+ if ( ! this . registry . getSingleMetric ( name ) ) {
65+ createCounter ( this . Counter , metric , {
66+ labels : labelNames ,
67+ registry : this . registry ,
68+ } ) ;
6869 }
6970
70- this . register
71+ this . registry
7172 . getSingleMetric ( name )
7273 . labels ( ...labelValues )
7374 . inc ( 1 ) ;
@@ -77,11 +78,14 @@ module.exports = class PrometheusMetricsConsumer extends Writable {
7778 const { name } = metric ;
7879 const { labelNames, labelValues } = this . labels ( metric ) ;
7980
80- if ( ! this . register . getSingleMetric ( name ) ) {
81- createGauge ( this . Gauge , metric , { labels : labelNames } ) ;
81+ if ( ! this . registry . getSingleMetric ( name ) ) {
82+ createGauge ( this . Gauge , metric , {
83+ labels : labelNames ,
84+ registry : this . registry ,
85+ } ) ;
8286 }
8387
84- this . register
88+ this . registry
8589 . getSingleMetric ( name )
8690 . labels ( ...labelValues )
8791 . set ( metric . value ) ;
@@ -91,14 +95,15 @@ module.exports = class PrometheusMetricsConsumer extends Writable {
9195 const { name, time } = metric ;
9296 const { labelNames, labelValues } = this . labels ( metric ) ;
9397
94- if ( ! this . register . getSingleMetric ( name ) ) {
98+ if ( ! this . registry . getSingleMetric ( name ) ) {
9599 createHistogram ( this . Histogram , this . bucketFunction , metric , {
96100 ...this . options ,
97101 ...this . bucketsForHistogram ( name ) ,
98102 labels : labelNames ,
103+ registry : this . registry ,
99104 } ) ;
100105 }
101- this . register
106+ this . registry
102107 . getSingleMetric ( name )
103108 . labels ( ...labelValues )
104109 . observe ( time ) ;
@@ -108,12 +113,13 @@ module.exports = class PrometheusMetricsConsumer extends Writable {
108113 const { name, time } = metric ;
109114 const { labelNames, labelValues } = this . labels ( metric ) ;
110115
111- if ( ! this . register . getSingleMetric ( name ) ) {
116+ if ( ! this . registry . getSingleMetric ( name ) ) {
112117 createSummary ( this . Summary , metric , {
113118 labels : labelNames ,
119+ registry : this . registry ,
114120 } ) ;
115121 }
116- this . register
122+ this . registry
117123 . getSingleMetric ( name )
118124 . labels ( ...labelValues )
119125 . observe ( time ) ;
@@ -124,13 +130,13 @@ module.exports = class PrometheusMetricsConsumer extends Writable {
124130 Joi . attempt (
125131 name ,
126132 metricName ,
127- 'Invalid `name` given to prometheusMetricsConsumer.override.'
133+ 'Invalid `name` given to prometheusMetricsConsumer.override.' ,
128134 ) ,
129135 Joi . attempt (
130136 config ,
131137 overrideConfig ,
132- 'Invalid `config` object given to prometheusMetricsConsumer.override.'
133- )
138+ 'Invalid `config` object given to prometheusMetricsConsumer.override.' ,
139+ ) ,
134140 ) ;
135141 }
136142
@@ -153,13 +159,14 @@ module.exports = class PrometheusMetricsConsumer extends Writable {
153159 _write ( metric , enc , cb ) {
154160 if ( this . typeFor ( metric ) === metricTypes . HISTOGRAM ) {
155161 if ( ! isHistogram ( metric ) ) {
156- return cb (
162+ cb (
157163 new Error (
158164 `Attempted to plot metric "${
159165 metric . name
160- } " on a histogram, however metric was missing \`time\` value`
161- )
166+ } " on a histogram, however metric was missing \`time\` value`,
167+ ) ,
162168 ) ;
169+ return ;
163170 }
164171 this . histogram ( metric ) ;
165172 } else if ( this . typeFor ( metric ) === metricTypes . GAUGE ) {
@@ -171,4 +178,12 @@ module.exports = class PrometheusMetricsConsumer extends Writable {
171178 }
172179 cb ( ) ;
173180 }
181+
182+ metrics ( ) {
183+ return this . registry . metrics ( ) ;
184+ }
185+
186+ contentType ( ) {
187+ return this . registry . contentType ;
188+ }
174189} ;
0 commit comments