Add a timed context manager to statsd.#65
Conversation
This enables a context manager in statsd so you can easily time
a set of code without saving the start time, etc. manually:
```python
with statsd.timed_context('user.query.time', sample_rate=0.5):
# Do what you need to ...
pass
# Is equivalent to ...
start = time.time()
try:
get_user(user_id)
finally:
statsd.timing('user.query.time', time.time() - start)
```
Two tests are also added for this code.
Fixes DataDog#25
|
Great feature @clokep, I love it. Thanks! I think we could make What do you think ? |
|
@yannmh Sure, seems reasonable -- I'll just need to add a |
|
@yannmh Please let me know if this works for you. I wasn't sure if you like to squash things or not. |
|
Thanks a lot @clokep, it works great. No need to squash, both commits lead to legit code. |
Add a timed context manager to statsd.
|
Thanks! I just realized there's a simpler way to do some of this code...same behavior, just reducing some code. Feel free to merge percipient@f1c8db2 if you'd like. |
|
do I need to include |
This enables a context manager in statsd so you can easily time a set of code without saving the start time, etc. manually:
Two tests are also added for this code.
Fixes #25
Please let me know if you want anything changed, etc. I'd love to see this in the next release!