Add support for distribution context manager and decorator#581
Merged
hush-hush merged 2 commits intoDataDog:masterfrom Jul 22, 2020
Merged
Add support for distribution context manager and decorator#581hush-hush merged 2 commits intoDataDog:masterfrom
hush-hush merged 2 commits intoDataDog:masterfrom
Conversation
d8d3ca1 to
3e0d3c7
Compare
Contributor
Author
|
Is it possible to get a review here @hush-hush? 🙏 |
hush-hush
requested changes
Jul 20, 2020
Member
hush-hush
left a comment
There was a problem hiding this comment.
On tiny detail but looks good otherwise !
3e0d3c7 to
8cd8f3e
Compare
A decorator or context manager that will measure the distribution of a
function's/context's run time using custom metric DISTRIBUTION.
E.g. of usage follows:
@statsd.distributed('user.query.time', sample_rate=0.5)
def get_user(user_id):
# Do what you need to ...
pass
# Is equivalent to ...
with statsd.distributed('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.distribution('user.query.time', time.time() - start)
Closes DataDog#563
8cd8f3e to
da80e82
Compare
hush-hush
approved these changes
Jul 21, 2020
Member
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
Member
|
Thanks @dnlserrano for the PR ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Takes a stab at #563.
Introduces the ability to use a context manager or decorator for the custom
DISTRIBUTIONmetric.Description of the Change
Introduces new
DistributedContextManagerDecoratorwhich allows one to use a new context manager or decorator (e.g.,statsd.distributed). It does the same asTimedContextManagerDecorator(it is a subclass of it afterall), but changes the timing metric to usestatsd.distributioninstead ofstatsd.timing.Thinking ahead, we do it by re-defining the
self.timing_funcused, which may later be passed in as an option tostatsd.timer, as discussed here.Alternate Designs
We could have gone with the global switch from the get go, but decided to be more conservative from the start following @hush-hush's comment.
Possible Drawbacks
Not sure if we can afford doing this change at the level of the general
context.py. I'm not aware if this is used for Statsd-compatible metrics only or if it allows for changes regarding custom metrics, which is the case forDISTRIBUTION, as far as I'm aware. Appreciate any inputs there.Verification Process
Ran test with both
tox -e flake8andtox -e py36locally. Also experimented with the context manager after installing the changes locally usingpip install -e <path_to_cloned_project_dir>, following yourDEVELOPMENTguide. Usedjimf/statsd-loggerfor quick-and-dirty experiment.Additional Notes
Python noob here, so please bear with. 🐻
Release Notes
Should be backwards compatible since it only introduces new behaviour, it does not break previous usage. Please double-check.
Review checklist (to be filled by reviewers)
changelog/label attached. If applicable it should have thebackward-incompatiblelabel attached.do-not-merge/label attached.kind/andseverity/labels attached at least.