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 sampling rate #28

Merged
merged 14 commits into from
Apr 20, 2020
Merged

Conversation

samullen
Copy link
Contributor

@samullen samullen commented Apr 8, 2020

telemetry_metrics recently added :reporter_options (telemetry_metrics#52) initiated from a request to add extra metrics options.

This PR allows developers to pass [sample_rate: <rate>] (where "" is a value between 0.0 and 1.0) to :reporter_options to limit how many measurements to capture.

Copy link
Collaborator

@arkgil arkgil left a comment

Choose a reason for hiding this comment

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

Hey, thanks for the PR! ⚡️

Apart from specific comments there is one missing piece here, meaning pushing the sampling rate down to the formatter. Using a sampling rate without reporting it upstream may lead to invalid data, e.g. a counter with sampling rate of 0.1 bumped up 100 times would show up as 10 (on average) in the system we report to.

Fortunately, both stock StatsD and Datadog support providing the sampling rate. See https://docs.datadoghq.com/developers/dogstatsd/datagram_shell?tab=metrics and https://github.com/statsd/statsd/blob/master/docs/metric_types.md#sampling.

mix.lock Outdated
@@ -1,21 +1,21 @@
%{
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "1.3.2", "b840562ea3d67795ffbb5bd88940b1bed0ed9fa32834915125ea7d02e35888a5", [:mix], [], "hexpm"},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm", "6c32a70ed5d452c6650916555b1f96c79af5fc4bf286997f8b15f213de786f73"},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you revert the dependency update change? This is unrelated to the PR, we can do it in another one 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍


@spec sample(Metrics.t()) :: Metrics.measurement() | nil
defp sample(metric) do
rate = Keyword.get(metric.reporter_options, :sample_rate, 1.0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
rate = Keyword.get(metric.reporter_options, :sample_rate, 1.0)
rate = Keyword.get(metric.reporter_options, :sampling_rate, 1.0)

Can we use that name, or is sample_rate the correct wording?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed. I wondered about that, but went with Bryan's comment from beam-telemetry/telemetry_metrics#33 since he was the last to say anything.

@samullen
Copy link
Contributor Author

samullen commented Apr 8, 2020

I've updated both the standard and datadog formatters to include sampling rate. Not terribly happy with the duplicate use of Keyword.get(reporter_options, :sampling_rate, 1.0), but ¯_(ツ)_/¯

]
)

In this example, we are captureing 100% of the measurements for the counter,
Copy link

Choose a reason for hiding this comment

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

captureing -> capturing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thaks. Speling is hard.

Copy link
Collaborator

@arkgil arkgil left a comment

Choose a reason for hiding this comment

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

Thank you, this is great! 🎉

@arkgil
Copy link
Collaborator

arkgil commented Apr 12, 2020

Ah, @samullen, it is failing on OTP 19, because :rand.uniform_real is not available in that release. Could you please switch to :rand.uniform (unless there is a reason why we can't use it)?

@samullen
Copy link
Contributor Author

For whatever the reason, I thought :real.uniform/0 returned an integer. I've updated the code and tests to reflect the switch to it.

@arkgil arkgil changed the title Add support for sample rates Add support for sampling rate Apr 20, 2020
@arkgil arkgil merged commit e1d6580 into beam-telemetry:master Apr 20, 2020
@arkgil
Copy link
Collaborator

arkgil commented Apr 20, 2020

Thanks! 🎉

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

Successfully merging this pull request may close these issues.

3 participants