2
2
3
3
import copy
4
4
import re
5
+ import time
6
+ from contextlib import contextmanager
5
7
from BaseHTTPServer import BaseHTTPRequestHandler
8
+ from functools import wraps
6
9
from threading import Lock
7
10
8
11
__all__ = ['Counter' , 'Gauge' , 'Summary' ]
@@ -165,6 +168,23 @@ def inc(self, amount=1):
165
168
with self ._lock :
166
169
self ._value += amount
167
170
171
+ @contextmanager
172
+ def trackBlockRaises (self ):
173
+ """Decorator to increment if a block raises an exception."""
174
+ try :
175
+ yield
176
+ except Exception , e :
177
+ self .inc ()
178
+ raise e
179
+
180
+ def trackFunctionRaises (self , f ):
181
+ """Decorator to increment if a function raises an exception."""
182
+ @wraps (f )
183
+ def wrapper (* args , ** kwargs ):
184
+ with self .trackBlockRaises ():
185
+ return f (* args , ** kwargs )
186
+ return wrapper
187
+
168
188
def _samples (self ):
169
189
with self ._lock :
170
190
return (('' , {}, self ._value ), )
@@ -187,10 +207,31 @@ def dec(self, amount=1):
187
207
self ._value -= amount
188
208
189
209
def set (self , value ):
190
- """Set gauge to the given amount ."""
210
+ """Set gauge to the given value ."""
191
211
with self ._lock :
192
212
self ._value = float (value )
193
213
214
+ def setToCurrentTime (self , value ):
215
+ """Set gauge to the current unixtime."""
216
+ self .set (time .time ())
217
+
218
+ @contextmanager
219
+ def trackBlockInprogress (self ):
220
+ """Decorator to track how many of a block are in progress."""
221
+ self .inc ()
222
+ try :
223
+ yield
224
+ finally :
225
+ self .dec ()
226
+
227
+ def trackFunctionInprogress (self , f ):
228
+ """Decorator to track how many of a function are in progress."""
229
+ @wraps (f )
230
+ def wrapper (* args , ** kwargs ):
231
+ with self .trackBlockInprogress ():
232
+ return f (* args , ** kwargs )
233
+ return wrapper
234
+
194
235
def _samples (self ):
195
236
with self ._lock :
196
237
return (('' , {}, self ._value ), )
@@ -209,6 +250,24 @@ def observe(self, amount):
209
250
self ._count += 1
210
251
self ._sum += amount
211
252
253
+ @contextmanager
254
+ def timeBlock (self ):
255
+ """Context manager to time how long a block of code takes in seconds."""
256
+ start = time .time ()
257
+ try :
258
+ yield
259
+ finally :
260
+ # Time can go backwards.
261
+ self .observe (max (time .time () - start , 0 ))
262
+
263
+ def timeFunction (self , f ):
264
+ """Decorator to time long a function takes in seconds."""
265
+ @wraps (f )
266
+ def wrapper (* args , ** kwargs ):
267
+ with self .timeBlock ():
268
+ return f (* args , ** kwargs )
269
+ return wrapper
270
+
212
271
def _samples (self ):
213
272
with self ._lock :
214
273
return (
@@ -257,7 +316,6 @@ def do_GET(self):
257
316
s = Summary ('ss' , 'A summary' , ['a' , 'b' ])
258
317
s .labels ('c' , 'd' ).observe (17 )
259
318
260
-
261
319
from BaseHTTPServer import HTTPServer
262
320
server_address = ('' , 8000 )
263
321
httpd = HTTPServer (server_address , MetricsHandler )
0 commit comments