Skip to content

Add a timed context manager to statsd.#65

Merged
yannmh merged 2 commits intoDataDog:masterfrom
percipient:context-manager
Jun 30, 2015
Merged

Add a timed context manager to statsd.#65
yannmh merged 2 commits intoDataDog:masterfrom
percipient:context-manager

Conversation

@clokep
Copy link
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown

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 pushed 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
Copy Markdown
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.

@c-warwick
Copy link
Copy Markdown

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