@@ -24,8 +24,10 @@ class Histogram extends Metric {
24
24
this . type = 'histogram' ;
25
25
this . defaultLabels = { } ;
26
26
this . defaultExemplarLabelSet = { } ;
27
+ this . enableExemplars = false ;
27
28
28
29
if ( config . enableExemplars ) {
30
+ this . enableExemplars = true ;
29
31
this . observe = this . observeWithExemplar ;
30
32
} else {
31
33
this . observe = this . observeWithoutExemplar ;
@@ -143,6 +145,7 @@ class Histogram extends Metric {
143
145
/**
144
146
* Start a timer that could be used to logging durations
145
147
* @param {object } labels - Object with labels where key is the label key and value is label value. Can only be one level deep
148
+ * @param {object } exemplarLabels - Object with labels for exemplar where key is the label key and value is label value. Can only be one level deep
146
149
* @returns {function } - Function to invoke when you want to stop the timer and observe the duration in seconds
147
150
* @example
148
151
* var end = histogram.startTimer();
@@ -151,8 +154,10 @@ class Histogram extends Metric {
151
154
* console.log('Duration', duration);
152
155
* });
153
156
*/
154
- startTimer ( labels ) {
155
- return startTimer . call ( this , labels ) ( ) ;
157
+ startTimer ( labels , exemplarLabels ) {
158
+ return this . enableExemplars
159
+ ? startTimerWithExemplar . call ( this , labels , exemplarLabels ) ( )
160
+ : startTimer . call ( this , labels ) ( ) ;
156
161
}
157
162
158
163
labels ( ...args ) {
@@ -183,6 +188,26 @@ function startTimer(startLabels) {
183
188
} ;
184
189
}
185
190
191
+ function startTimerWithExemplar ( startLabels , startExemplarLabels ) {
192
+ return ( ) => {
193
+ const start = process . hrtime ( ) ;
194
+ return ( endLabels , endExemplarLabels ) => {
195
+ const delta = process . hrtime ( start ) ;
196
+ const value = delta [ 0 ] + delta [ 1 ] / 1e9 ;
197
+ this . observe ( {
198
+ labels : Object . assign ( { } , startLabels , endLabels ) ,
199
+ value,
200
+ exemplarLabels : Object . assign (
201
+ { } ,
202
+ startExemplarLabels ,
203
+ endExemplarLabels ,
204
+ ) ,
205
+ } ) ;
206
+ return value ;
207
+ } ;
208
+ } ;
209
+ }
210
+
186
211
function setValuePair ( labels , value , metricName , exemplar , sharedLabels = { } ) {
187
212
return {
188
213
labels,
0 commit comments