Add support for distribution context manager and decorator #581
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
DISTRIBUTION
metric.Description of the Change
Introduces new
DistributedContextManagerDecorator
which 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.distribution
instead ofstatsd.timing
.Thinking ahead, we do it by re-defining the
self.timing_func
used, 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 flake8
andtox -e py36
locally. Also experimented with the context manager after installing the changes locally usingpip install -e <path_to_cloned_project_dir>
, following yourDEVELOPMENT
guide. Usedjimf/statsd-logger
for 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-incompatible
label attached.do-not-merge/
label attached.kind/
andseverity/
labels attached at least.