@@ -11,19 +11,16 @@ class PrometheusMetric {
1111 if ( this . options . labels === undefined ) {
1212 this . options . labels = { names : [ ] } ;
1313 }
14- this . staticLabels = this . options . prometheus . staticLabels ;
15- if ( this . staticLabels !== undefined ) {
16- if ( Object . keys ( this . staticLabels ) . length > 0 ) {
17- Object . keys ( this . staticLabels ) . forEach ( ( name ) => {
18- this . options . labels . names . unshift ( name ) ;
19- this . staticLabels [ name ] = this . _normalize ( this . staticLabels [ name ] ) ;
20- } ) ;
21- }
22- }
14+ this . staticLabels = this . options . prometheus . staticLabels || { } ;
15+
16+ // Add staticLabel names to list of known label names.
17+ Object . keys ( this . staticLabels ) . forEach ( ( labelName ) => {
18+ this . options . labels . names . unshift ( labelName ) ;
19+ } ) ;
20+ // Normalize all the label names.
21+ this . options . labels . names = this . options . labels . names . map ( this . _normalize )
22+
2323 this . options . prometheus . name = this . _normalize ( this . options . prometheus . name ) ;
24- this . options . prometheus . labelNames = this . options . prometheus . labelNames || [ ] ;
25- this . options . prometheus . labelNames = this . options . prometheus . labelNames
26- . map ( this . _normalize ) ;
2724 this . metric = new this . client [ this . options . type ] ( {
2825 name : this . options . prometheus . name ,
2926 help : this . options . prometheus . help ,
@@ -33,61 +30,75 @@ class PrometheusMetric {
3330 } ) ;
3431 }
3532
36- _handleStaticLabels ( labels ) {
37- const updatedLabels = [ ...labels ] ;
38- if ( this . staticLabels !== undefined ) {
39- Object . keys ( this . staticLabels ) . forEach ( ( name ) => {
40- if ( updatedLabels . indexOf ( this . staticLabels [ name ] ) === - 1 ) {
41- updatedLabels . unshift ( this . staticLabels [ name ] ) ;
42- }
43- } ) ;
44- }
45- return updatedLabels ;
46- }
47-
33+ /**
34+ * Normalizes a prometheus string. Should be used for label
35+ * and metric names, but is not needed for label values.
36+ *
37+ * @param {string } str
38+ * @return {string }
39+ */
4840 _normalize ( str ) {
4941 return String ( str ) . replace ( / \W / g, '_' ) // replace non-alphanumerics
5042 . replace ( / _ + / g, '_' ) // dedupe underscores
5143 . replace ( / ( ^ _ + | _ + $ ) / g, '' ) ; // trim leading and trailing underscores
5244 }
5345
54- increment ( amount , labels ) {
55- const updatedLabels = this . _handleStaticLabels ( labels ) . map ( this . _normalize ) ;
56- this . metric . labels . apply ( this . metric , updatedLabels ) . inc ( amount ) ;
46+ /**
47+ * Gets label values array for this metric
48+ * including both static and dynamic labels merged together.
49+ *
50+ * @param {Array } labelValues
51+ * @return {Array }
52+ */
53+ _getLabelValues ( labelValues ) {
54+ // make a clone of labelValues.
55+ const updatedLabelValues = [ ...labelValues ] ;
56+ // Add staticLabel values to updatedLabelValues if they aren't already listed.
57+ Object . keys ( this . staticLabels ) . forEach ( ( labelName ) => {
58+ if ( updatedLabelValues . indexOf ( this . staticLabels [ labelName ] ) === - 1 ) {
59+ updatedLabelValues . unshift ( this . staticLabels [ labelName ] ) ;
60+ }
61+ } ) ;
62+ return updatedLabelValues ;
63+ }
64+
65+ increment ( amount , labelValues ) {
66+ const updatedLabelValues = this . _getLabelValues ( labelValues ) ;
67+ this . metric . labels . apply ( this . metric , updatedLabelValues ) . inc ( amount ) ;
5768 }
5869
59- decrement ( amount , labels ) {
60- const updatedLabels = this . _handleStaticLabels ( labels ) . map ( this . _normalize ) ;
61- this . metric . labels . apply ( this . metric , updatedLabels ) . dec ( amount ) ;
70+ decrement ( amount , labelValues ) {
71+ const updatedLabelValues = this . _getLabelValues ( labelValues ) ;
72+ this . metric . labels . apply ( this . metric , updatedLabelValues ) . dec ( amount ) ;
6273 }
6374
64- observe ( value , labels ) {
65- const updatedLabels = this . _handleStaticLabels ( labels ) . map ( this . _normalize ) ;
66- this . metric . labels . apply ( this . metric , updatedLabels ) . observe ( value ) ;
75+ observe ( value , labelValues ) {
76+ const updatedLabelValues = this . _getLabelValues ( labelValues ) ;
77+ this . metric . labels . apply ( this . metric , updatedLabelValues ) . observe ( value ) ;
6778 }
6879
69- gauge ( amount , labels ) {
70- const updatedLabels = this . _handleStaticLabels ( labels ) . map ( this . _normalize ) ;
80+ gauge ( amount , labelValues ) {
81+ const updatedLabelValues = this . _getLabelValues ( labelValues ) ;
7182 if ( amount < 0 ) {
72- this . metric . labels . apply ( this . metric , updatedLabels ) . dec ( Math . abs ( amount ) ) ;
83+ this . metric . labels . apply ( this . metric , updatedLabelValues ) . dec ( Math . abs ( amount ) ) ;
7384 } else {
74- this . metric . labels . apply ( this . metric , updatedLabels ) . inc ( amount ) ;
85+ this . metric . labels . apply ( this . metric , updatedLabelValues ) . inc ( amount ) ;
7586 }
7687 }
7788
78- set ( value , labels ) {
79- const updatedLabels = this . _handleStaticLabels ( labels ) . map ( this . _normalize ) ;
80- this . metric . labels . apply ( this . metric , updatedLabels ) . set ( value ) ;
89+ set ( value , labelValues ) {
90+ const updatedLabelValues = this . _getLabelValues ( labelValues ) ;
91+ this . metric . labels . apply ( this . metric , updatedLabelValues ) . set ( value ) ;
8192 }
8293
83- timing ( value , labels ) {
84- const updatedLabels = this . _handleStaticLabels ( labels ) . map ( this . _normalize ) ;
85- this . observe ( value , updatedLabels ) ;
94+ timing ( value , labelValues ) {
95+ const updatedLabelValues = this . _getLabelValues ( labelValues ) ;
96+ this . observe ( value , updatedLabelValues ) ;
8697 }
8798
88- endTiming ( startTime , labels ) {
89- const updatedLabels = this . _handleStaticLabels ( labels ) . map ( this . _normalize ) ;
90- this . timing ( Date . now ( ) - startTime , updatedLabels ) ;
99+ endTiming ( startTime , labelValues ) {
100+ const updatedLabelValues = this . _getLabelValues ( labelValues ) ;
101+ this . timing ( Date . now ( ) - startTime , updatedLabelValues ) ;
91102 }
92103}
93104
0 commit comments