Commit 3ef865e
authored
Allow to add labels inside a context manager (#730)
* Allow to add labels inside a context manager
This way labels that depend on the result of the measured operation can
be added more conveniently, e.g. the status code of an http request:
from prometheus_client import Histogram
from requests import get
teapot = Histogram('teapot', 'A teapot', ['status'])
with teapot.time() as metric:
response = get('https://httpbin.org/status/418')
metric.labels(status=response.status_code)
Signed-off-by: Andreas Zeidler <andreas.zeidler@zeit.de>
* Also allow to add deferred labels for 'gauge' and 'summary' metrics
For this to work the 'observability' check needs to be deferred as
well, in case a label is added inside the context manager thereby
making the metric observable.
Signed-off-by: Andreas Zeidler <andreas.zeidler@zeit.de>
* Pass metric instance and callback name to `Timer`
This should make the code slightly more readable.
Signed-off-by: Andreas Zeidler <andreas.zeidler@zeit.de>
* Remove redundant check for observability
The callbacks are already taking care of this anyway.
Signed-off-by: Andreas Zeidler <andreas.zeidler@zeit.de>1 parent 7c44be2 commit 3ef865e
3 files changed
Lines changed: 47 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
57 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
58 | 59 | | |
59 | 60 | | |
60 | | - | |
| 61 | + | |
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
| 65 | + | |
64 | 66 | | |
65 | 67 | | |
66 | 68 | | |
67 | 69 | | |
68 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
69 | 75 | | |
70 | 76 | | |
71 | 77 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
402 | 402 | | |
403 | 403 | | |
404 | 404 | | |
405 | | - | |
406 | | - | |
| 405 | + | |
407 | 406 | | |
408 | 407 | | |
409 | 408 | | |
| |||
481 | 480 | | |
482 | 481 | | |
483 | 482 | | |
484 | | - | |
485 | | - | |
| 483 | + | |
486 | 484 | | |
487 | 485 | | |
488 | 486 | | |
| |||
606 | 604 | | |
607 | 605 | | |
608 | 606 | | |
609 | | - | |
| 607 | + | |
610 | 608 | | |
611 | 609 | | |
612 | 610 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
222 | 222 | | |
223 | 223 | | |
224 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
225 | 232 | | |
226 | 233 | | |
227 | 234 | | |
228 | 235 | | |
229 | 236 | | |
230 | 237 | | |
231 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
232 | 244 | | |
233 | 245 | | |
234 | 246 | | |
| |||
318 | 330 | | |
319 | 331 | | |
320 | 332 | | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
321 | 340 | | |
322 | 341 | | |
323 | 342 | | |
324 | | - | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
325 | 348 | | |
326 | 349 | | |
327 | 350 | | |
| |||
435 | 458 | | |
436 | 459 | | |
437 | 460 | | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
438 | 470 | | |
439 | 471 | | |
440 | 472 | | |
| |||
0 commit comments