Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a timed context manager to statsd. #65

Merged
merged 2 commits into from
Jun 30, 2015

Conversation

clokep
Copy link
Contributor

@clokep clokep commented Jun 30, 2015

This enables a context manager in statsd so you can easily time a set of code without saving the start time, etc. manually:

    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 #25

Please let me know if you want anything changed, etc. I'd love to see this in the next release!

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
@yannmh
Copy link
Member

yannmh commented Jun 30, 2015

Great feature @clokep, I love it. Thanks!

I think we could make statsd.timed acting as a decorator and a context manager, c.f.
http://stackoverflow.com/questions/9213600/function-acting-as-both-decorator-and-context-manager-in-python

What do you think ?

@yannmh yannmh self-assigned this Jun 30, 2015
@clokep
Copy link
Contributor Author

clokep commented Jun 30, 2015

@yannmh Sure, seems reasonable -- I'll just need to add a __call__ method to my decorator. I'll try to get to that "soon".

@clokep clokep force-pushed the context-manager branch from ab9a46c to ec2ab07 Compare June 30, 2015 19:15
@clokep
Copy link
Contributor Author

clokep commented Jun 30, 2015

@yannmh Please let me know if this works for you. I wasn't sure if you like to squash things or not.

@yannmh
Copy link
Member

yannmh commented Jun 30, 2015

Thanks a lot @clokep, it works great. No need to squash, both commits lead to legit code.
Let's merge it so we can release it tomorrow with the v0.7.0 🚀 .

yannmh added a commit that referenced this pull request Jun 30, 2015
Add a timed context manager to statsd.
@yannmh yannmh merged commit 282395e into DataDog:master Jun 30, 2015
@clokep
Copy link
Contributor Author

clokep commented Jun 30, 2015

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.

@RustyRaven621
Copy link

do I need to include pass at the end of my code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Context managed statsd.timed
3 participants