File tree Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,14 @@ with g.track_inprogress():
113
113
pass
114
114
```
115
115
116
+ A Gauge can also take it's value from a callback:
117
+
118
+ ``` python
119
+ d = Gauge(' data_objects' , ' Number of objects' )
120
+ my_dict = {}
121
+ d.set_function(lambda : len (my_dict))
122
+ ```
123
+
116
124
### Summary
117
125
118
126
Summaries track the size and number of events.
Original file line number Diff line number Diff line change 8
8
import os
9
9
import time
10
10
import threading
11
+ import types
11
12
try :
12
13
from BaseHTTPServer import BaseHTTPRequestHandler
13
14
from BaseHTTPServer import HTTPServer
@@ -279,6 +280,17 @@ def wrapped(*args, **kwargs):
279
280
280
281
return InprogressTracker (self )
281
282
283
+ def set_function (self , f ):
284
+ '''Call the provided function to return the Gauge value.
285
+
286
+ The function must return a float, and may be called from
287
+ multiple threads.
288
+ All other methods of the Gauge become NOOPs.
289
+ '''
290
+ def samples (self ):
291
+ return (('' , {}, float (f ())), )
292
+ self ._samples = types .MethodType (samples , self )
293
+
282
294
def _samples (self ):
283
295
with self ._lock :
284
296
return (('' , {}, self ._value ), )
Original file line number Diff line number Diff line change @@ -87,6 +87,15 @@ def test_block_decorator(self):
87
87
self .assertEqual (1 , self .registry .get_sample_value ('g' ))
88
88
self .assertEqual (0 , self .registry .get_sample_value ('g' ))
89
89
90
+ def test_gauge_function (self ):
91
+ x = {}
92
+ self .gauge .set_function (lambda : len (x ))
93
+ self .assertEqual (0 , self .registry .get_sample_value ('g' ))
94
+ self .gauge .inc ()
95
+ self .assertEqual (0 , self .registry .get_sample_value ('g' ))
96
+ x ['a' ] = None
97
+ self .assertEqual (1 , self .registry .get_sample_value ('g' ))
98
+
90
99
91
100
class TestSummary (unittest .TestCase ):
92
101
def setUp (self ):
You can’t perform that action at this time.
0 commit comments