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 support for distribution context manager and decorator #581

Merged
merged 2 commits into from
Jul 22, 2020

Conversation

dnlserrano
Copy link
Contributor

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 as TimedContextManagerDecorator (it is a subclass of it afterall), but changes the timing metric to use statsd.distribution instead of statsd.timing.

Thinking ahead, we do it by re-defining the self.timing_func used, which may later be passed in as an option to statsd.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 for DISTRIBUTION, as far as I'm aware. Appreciate any inputs there.

Verification Process

Ran test with both tox -e flake8 and tox -e py36 locally. Also experimented with the context manager after installing the changes locally using pip install -e <path_to_cloned_project_dir>, following your DEVELOPMENT guide. Used jimf/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)

  • Feature or bug fix MUST have appropriate tests (unit, integration, etc...)
  • PR title must be written as a CHANGELOG entry (see why)
  • Files changes must correspond to the primary purpose of the PR as described in the title (small unrelated changes should have their own PR)
  • PR must have one changelog/ label attached. If applicable it should have the backward-incompatible label attached.
  • PR should not have do-not-merge/ label attached.
  • If Applicable, issue must have kind/ and severity/ labels attached at least.

@dnlserrano
Copy link
Contributor Author

Is it possible to get a review here @hush-hush? 🙏

Copy link
Member

@hush-hush hush-hush left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On tiny detail but looks good otherwise !

datadog/dogstatsd/context.py Outdated Show resolved Hide resolved
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
@hush-hush
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@hush-hush hush-hush merged commit 190ca57 into DataDog:master Jul 22, 2020
@hush-hush
Copy link
Member

Thanks @dnlserrano for the PR !

@zippolyte zippolyte added the changelog/Added Added features results into a minor version bump label Aug 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog/Added Added features results into a minor version bump
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants