@@ -24,18 +24,20 @@ class Gauge extends Metric {
24
24
* @returns {void }
25
25
*/
26
26
set ( labels , value ) {
27
- if ( ! isObject ( labels ) ) {
28
- return set . call ( this , null ) ( labels , value ) ;
29
- }
30
- return set . call ( this , labels ) ( value ) ;
27
+ value = getValueArg ( labels , value ) ;
28
+ labels = getLabelArg ( labels ) ;
29
+ set ( this , labels , value ) ;
31
30
}
32
31
33
32
/**
34
33
* Reset gauge
35
34
* @returns {void }
36
35
*/
37
36
reset ( ) {
38
- return reset . call ( this ) ;
37
+ this . hashMap = { } ;
38
+ if ( this . labelNames . length === 0 ) {
39
+ setValue ( this . hashMap , 0 , { } ) ;
40
+ }
39
41
}
40
42
41
43
/**
@@ -45,7 +47,10 @@ class Gauge extends Metric {
45
47
* @returns {void }
46
48
*/
47
49
inc ( labels , value ) {
48
- inc . call ( this , labels ) ( value ) ;
50
+ value = getValueArg ( labels , value ) ;
51
+ labels = getLabelArg ( labels ) ;
52
+ if ( value === undefined ) value = 1 ;
53
+ set ( this , labels , this . _getValue ( labels ) + value ) ;
49
54
}
50
55
51
56
/**
@@ -55,7 +60,10 @@ class Gauge extends Metric {
55
60
* @returns {void }
56
61
*/
57
62
dec ( labels , value ) {
58
- dec . call ( this , labels ) ( value ) ;
63
+ value = getValueArg ( labels , value ) ;
64
+ labels = getLabelArg ( labels ) ;
65
+ if ( value === undefined ) value = 1 ;
66
+ set ( this , labels , this . _getValue ( labels ) - value ) ;
59
67
}
60
68
61
69
/**
@@ -64,7 +72,12 @@ class Gauge extends Metric {
64
72
* @returns {void }
65
73
*/
66
74
setToCurrentTime ( labels ) {
67
- return setToCurrentTime . call ( this , labels ) ( ) ;
75
+ const now = Date . now ( ) / 1000 ;
76
+ if ( labels === undefined ) {
77
+ this . set ( now ) ;
78
+ } else {
79
+ this . set ( labels , now ) ;
80
+ }
68
81
}
69
82
70
83
/**
@@ -78,7 +91,11 @@ class Gauge extends Metric {
78
91
* });
79
92
*/
80
93
startTimer ( labels ) {
81
- return startTimer . call ( this , labels ) ( ) ;
94
+ const start = process . hrtime ( ) ;
95
+ return endLabels => {
96
+ const delta = process . hrtime ( start ) ;
97
+ this . set ( Object . assign ( { } , labels , endLabels ) , delta [ 0 ] + delta [ 1 ] / 1e9 ) ;
98
+ } ;
82
99
}
83
100
84
101
async get ( ) {
@@ -104,11 +121,11 @@ class Gauge extends Metric {
104
121
const labels = getLabels ( this . labelNames , arguments ) ;
105
122
validateLabel ( this . labelNames , labels ) ;
106
123
return {
107
- inc : inc . call ( this , labels ) ,
108
- dec : dec . call ( this , labels ) ,
109
- set : set . call ( this , labels ) ,
110
- setToCurrentTime : setToCurrentTime . call ( this , labels ) ,
111
- startTimer : startTimer . call ( this , labels ) ,
124
+ inc : this . inc . bind ( this , labels ) ,
125
+ dec : this . dec . bind ( this , labels ) ,
126
+ set : this . set . bind ( this , labels ) ,
127
+ setToCurrentTime : this . setToCurrentTime . bind ( this , labels ) ,
128
+ startTimer : this . startTimer . bind ( this , labels ) ,
112
129
} ;
113
130
}
114
131
@@ -119,78 +136,21 @@ class Gauge extends Metric {
119
136
}
120
137
}
121
138
122
- function setToCurrentTime ( labels ) {
123
- return ( ) => {
124
- const now = Date . now ( ) / 1000 ;
125
- if ( labels === undefined ) {
126
- this . set ( now ) ;
127
- } else {
128
- this . set ( labels , now ) ;
129
- }
130
- } ;
131
- }
132
-
133
- function startTimer ( startLabels ) {
134
- return ( ) => {
135
- const start = process . hrtime ( ) ;
136
- return endLabels => {
137
- const delta = process . hrtime ( start ) ;
138
- this . set (
139
- Object . assign ( { } , startLabels , endLabels ) ,
140
- delta [ 0 ] + delta [ 1 ] / 1e9 ,
141
- ) ;
142
- } ;
143
- } ;
144
- }
145
-
146
- function dec ( labels ) {
147
- return value => {
148
- const data = convertLabelsAndValues ( labels , value ) ;
149
- if ( data . value === undefined ) data . value = 1 ;
150
- this . set ( data . labels , this . _getValue ( data . labels ) - data . value ) ;
151
- } ;
152
- }
153
-
154
- function inc ( labels ) {
155
- return value => {
156
- const data = convertLabelsAndValues ( labels , value ) ;
157
- if ( data . value === undefined ) data . value = 1 ;
158
- this . set ( data . labels , this . _getValue ( data . labels ) + data . value ) ;
159
- } ;
160
- }
161
-
162
- function set ( labels ) {
163
- return value => {
164
- if ( typeof value !== 'number' ) {
165
- throw new TypeError ( `Value is not a valid number: ${ util . format ( value ) } ` ) ;
166
- }
167
-
168
- labels = labels || { } ;
139
+ function set ( gauge , labels , value ) {
140
+ if ( typeof value !== 'number' ) {
141
+ throw new TypeError ( `Value is not a valid number: ${ util . format ( value ) } ` ) ;
142
+ }
169
143
170
- validateLabel ( this . labelNames , labels ) ;
171
- this . hashMap = setValue ( this . hashMap , value , labels ) ;
172
- } ;
144
+ validateLabel ( gauge . labelNames , labels ) ;
145
+ setValue ( gauge . hashMap , value , labels ) ;
173
146
}
174
147
175
- function reset ( ) {
176
- this . hashMap = { } ;
177
-
178
- if ( this . labelNames . length === 0 ) {
179
- this . hashMap = setValue ( { } , 0 , { } ) ;
180
- }
148
+ function getLabelArg ( labels ) {
149
+ return isObject ( labels ) ? labels : { } ;
181
150
}
182
151
183
- function convertLabelsAndValues ( labels , value ) {
184
- if ( ! isObject ( labels ) ) {
185
- return {
186
- value : labels ,
187
- labels : { } ,
188
- } ;
189
- }
190
- return {
191
- labels,
192
- value,
193
- } ;
152
+ function getValueArg ( labels , value ) {
153
+ return isObject ( labels ) ? value : labels ;
194
154
}
195
155
196
156
module . exports = Gauge ;
0 commit comments