From 429966fce69449dea461e0983b193767a3baba9c Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Fri, 23 Oct 2020 18:58:03 +0200 Subject: [PATCH 01/14] feat(metrics): Use buffering asynchronous sink --- Cargo.lock | 18 ++++++++++++++---- relay-common/Cargo.toml | 2 +- relay-common/src/metrics.rs | 27 +++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7e4173fd5b2..fcfb430f083 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,7 +9,7 @@ dependencies = [ "actix_derive", "bitflags", "bytes 0.4.12", - "crossbeam-channel", + "crossbeam-channel 0.3.9", "failure", "fnv", "futures 0.1.29", @@ -390,11 +390,11 @@ dependencies = [ [[package]] name = "cadence" -version = "0.17.1" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773c648ebe292d8fcdac6a0d332edd2a54bb3110890751c7a05e6b76659c7df0" +checksum = "6281d1200ac3293fd08be899c9a0c17b83cda0672221fcbe1fefc886a555e35e" dependencies = [ - "crossbeam-channel", + "crossbeam-channel 0.4.4", ] [[package]] @@ -630,6 +630,16 @@ dependencies = [ "crossbeam-utils 0.6.6", ] +[[package]] +name = "crossbeam-channel" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b153fe7cbef478c567df0f972e02e6d736db11affe43dfc9c56a9374d1adfb87" +dependencies = [ + "crossbeam-utils 0.7.2", + "maybe-uninit", +] + [[package]] name = "crossbeam-deque" version = "0.7.3" diff --git a/relay-common/Cargo.toml b/relay-common/Cargo.toml index 731a9e81a15..09f96b43a62 100644 --- a/relay-common/Cargo.toml +++ b/relay-common/Cargo.toml @@ -11,7 +11,7 @@ publish = false [dependencies] backoff = "0.1.6" -cadence = "0.17.1" +cadence = "0.22.0" chrono = "0.4.11" failure = "0.1.8" globset = "0.4.5" diff --git a/relay-common/src/metrics.rs b/relay-common/src/metrics.rs index 1fe8a389e52..e8dab195b48 100644 --- a/relay-common/src/metrics.rs +++ b/relay-common/src/metrics.rs @@ -60,14 +60,19 @@ //! [Metric Types]: https://github.com/statsd/statsd/blob/master/docs/metric_types.md use std::collections::BTreeMap; -use std::net::ToSocketAddrs; +use std::net::{ToSocketAddrs, UdpSocket}; use std::ops::{Deref, DerefMut}; use std::sync::Arc; -use cadence::{Metric, MetricBuilder, StatsdClient}; +use cadence::{BufferedUdpMetricSink, Metric, MetricBuilder, QueuingMetricSink, StatsdClient}; use lazy_static::lazy_static; use parking_lot::RwLock; +use crate::LogError; + +/// Maximum number of metric events that can be queued before we start dropping them +const METRICS_MAX_QUEUE_SIZE: usize = 100_000; + /// Client configuration object to store globally. #[derive(Debug)] pub struct MetricsClient { @@ -102,7 +107,14 @@ impl MetricsClient { metric = metric.with_tag(k, v); } - metric.send(); + match metric.try_send() { + Ok(_) => (), + Err(error) => log::error!( + "Error sending a metric: {}, maximum capacity: {}", + LogError(&error), + METRICS_MAX_QUEUE_SIZE + ), + }; } } @@ -145,7 +157,14 @@ pub fn configure_statsd( if !addrs.is_empty() { log::info!("reporting metrics to statsd at {}", addrs[0]); } - let statsd_client = StatsdClient::from_udp_host(prefix, &addrs[..]).unwrap(); + + let socket = UdpSocket::bind(&addrs[..]).unwrap(); + socket.set_nonblocking(true).unwrap(); + + let udp_sink = BufferedUdpMetricSink::from(host, socket).unwrap(); + let queuing_sink = QueuingMetricSink::with_capacity(udp_sink, METRICS_MAX_QUEUE_SIZE); + + let statsd_client = StatsdClient::from_sink(prefix, queuing_sink); set_client(MetricsClient { statsd_client, default_tags, From 7464415c74840eeaa4e3f136ad466213c7938b72 Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Sun, 25 Oct 2020 00:31:14 +0200 Subject: [PATCH 02/14] Add config option --- relay-common/src/metrics.rs | 16 ++++++++++++---- relay-config/src/config.rs | 9 +++++++++ relay/src/setup.rs | 7 ++++++- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/relay-common/src/metrics.rs b/relay-common/src/metrics.rs index e8dab195b48..87a01c7400c 100644 --- a/relay-common/src/metrics.rs +++ b/relay-common/src/metrics.rs @@ -64,7 +64,9 @@ use std::net::{ToSocketAddrs, UdpSocket}; use std::ops::{Deref, DerefMut}; use std::sync::Arc; -use cadence::{BufferedUdpMetricSink, Metric, MetricBuilder, QueuingMetricSink, StatsdClient}; +use cadence::{ + BufferedUdpMetricSink, Metric, MetricBuilder, QueuingMetricSink, StatsdClient, UdpMetricSink, +}; use lazy_static::lazy_static; use parking_lot::RwLock; @@ -152,6 +154,7 @@ pub fn configure_statsd( prefix: &str, host: A, default_tags: BTreeMap, + buffering: bool, ) { let addrs: Vec<_> = host.to_socket_addrs().unwrap().collect(); if !addrs.is_empty() { @@ -161,10 +164,15 @@ pub fn configure_statsd( let socket = UdpSocket::bind(&addrs[..]).unwrap(); socket.set_nonblocking(true).unwrap(); - let udp_sink = BufferedUdpMetricSink::from(host, socket).unwrap(); - let queuing_sink = QueuingMetricSink::with_capacity(udp_sink, METRICS_MAX_QUEUE_SIZE); + let statsd_client = if buffering { + let udp_sink = BufferedUdpMetricSink::from(host, socket).unwrap(); + let queuing_sink = QueuingMetricSink::with_capacity(udp_sink, METRICS_MAX_QUEUE_SIZE); + StatsdClient::from_sink(prefix, queuing_sink) + } else { + let simple_sink = UdpMetricSink::from(host, socket).unwrap(); + StatsdClient::from_sink(prefix, simple_sink) + }; - let statsd_client = StatsdClient::from_sink(prefix, queuing_sink); set_client(MetricsClient { statsd_client, default_tags, diff --git a/relay-config/src/config.rs b/relay-config/src/config.rs index 9fed4972b9a..dabe6c98eca 100644 --- a/relay-config/src/config.rs +++ b/relay-config/src/config.rs @@ -424,6 +424,9 @@ struct Metrics { default_tags: BTreeMap, /// A tag name to report the hostname to, for each metric. Defaults to not sending such a tag. hostname_tag: Option, + /// If set to true, emitted metrics will be buffered to optimize performance. + /// Defaults to true. + buffering: bool, } impl Default for Metrics { @@ -433,6 +436,7 @@ impl Default for Metrics { prefix: "sentry.relay".into(), default_tags: BTreeMap::new(), hostname_tag: None, + buffering: true, } } } @@ -1247,6 +1251,11 @@ impl Config { self.values.metrics.hostname_tag.as_deref() } + /// Returns true if metrics buffering is enabled, false otherwise + pub fn metrics_buffering(&self) -> bool { + self.values.metrics.buffering + } + /// Returns the default timeout for all upstream HTTP requests. pub fn http_timeout(&self) -> Duration { Duration::from_secs(self.values.http.timeout.into()) diff --git a/relay/src/setup.rs b/relay/src/setup.rs index 878a2f22963..2fb50a64140 100644 --- a/relay/src/setup.rs +++ b/relay/src/setup.rs @@ -214,7 +214,12 @@ pub fn init_metrics(config: &Config) -> Result<(), Error> { default_tags.insert(hostname_tag.to_owned(), hostname); } } - metrics::configure_statsd(config.metrics_prefix(), &addrs[..], default_tags); + metrics::configure_statsd( + config.metrics_prefix(), + &addrs[..], + default_tags, + config.metrics_buffering(), + ); Ok(()) } From 2cf8426555ea48b8b9b1a74b48009ad76596920f Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Sun, 25 Oct 2020 00:33:43 +0200 Subject: [PATCH 03/14] Add a comment --- relay-common/src/metrics.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/relay-common/src/metrics.rs b/relay-common/src/metrics.rs index 87a01c7400c..6f8b73df193 100644 --- a/relay-common/src/metrics.rs +++ b/relay-common/src/metrics.rs @@ -172,6 +172,10 @@ pub fn configure_statsd( let simple_sink = UdpMetricSink::from(host, socket).unwrap(); StatsdClient::from_sink(prefix, simple_sink) }; + log::debug!( + "metrics buffering is {}", + if buffering { "enabled" } else { "disabled" } + ); set_client(MetricsClient { statsd_client, From feb29eee2ff99954c236a376884eb6f153f79872 Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Sun, 25 Oct 2020 01:23:51 +0200 Subject: [PATCH 04/14] WIP sampling --- Cargo.lock | 1 + relay-common/Cargo.toml | 1 + relay-common/src/metrics.rs | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index fcfb430f083..90d4d61038b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2901,6 +2901,7 @@ dependencies = [ "log", "lru", "parking_lot 0.10.2", + "rand 0.7.3", "regex", "schemars", "sentry-types", diff --git a/relay-common/Cargo.toml b/relay-common/Cargo.toml index 09f96b43a62..8299892e62c 100644 --- a/relay-common/Cargo.toml +++ b/relay-common/Cargo.toml @@ -20,6 +20,7 @@ lazycell = "1.2.1" log = { version = "0.4.8", features = ["serde"] } lru = "0.4.0" parking_lot = "0.10.0" +rand = "0.7.3" regex = "1.3.9" sentry-types = "0.20.0" schemars = { version = "0.8.0-alpha-4", features = ["uuid", "chrono"], optional = true } diff --git a/relay-common/src/metrics.rs b/relay-common/src/metrics.rs index 6f8b73df193..da87bfd091e 100644 --- a/relay-common/src/metrics.rs +++ b/relay-common/src/metrics.rs @@ -69,6 +69,7 @@ use cadence::{ }; use lazy_static::lazy_static; use parking_lot::RwLock; +use rand::distributions::{Distribution, Uniform}; use crate::LogError; @@ -105,6 +106,25 @@ impl MetricsClient { where T: Metric + From, { + // Decide if we want to sample this metric + let sample_rate: f32 = 0.5; + + let to_send = if sample_rate <= 0.0 { + println!("zero"); + false + } else if sample_rate >= 1.0 { + println!("one!!!"); + true + } else { + let mut rng = rand::thread_rng(); + let between = Uniform::new(0.0, 1.0); + between.sample(&mut rng) <= sample_rate + }; + + if !to_send { + return; + } + for (k, v) in &self.default_tags { metric = metric.with_tag(k, v); } From 77129c2ea9f1ead527bec8bad2d65e8231d32749 Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Mon, 26 Oct 2020 19:53:19 +0100 Subject: [PATCH 05/14] Move sampling to a separate functiion --- relay-common/src/metrics.rs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/relay-common/src/metrics.rs b/relay-common/src/metrics.rs index da87bfd091e..c88997c65fa 100644 --- a/relay-common/src/metrics.rs +++ b/relay-common/src/metrics.rs @@ -106,22 +106,7 @@ impl MetricsClient { where T: Metric + From, { - // Decide if we want to sample this metric - let sample_rate: f32 = 0.5; - - let to_send = if sample_rate <= 0.0 { - println!("zero"); - false - } else if sample_rate >= 1.0 { - println!("one!!!"); - true - } else { - let mut rng = rand::thread_rng(); - let between = Uniform::new(0.0, 1.0); - between.sample(&mut rng) <= sample_rate - }; - - if !to_send { + if !self._should_send() { return; } @@ -138,6 +123,23 @@ impl MetricsClient { ), }; } + + fn _should_send(&self) -> bool { + // Decide if we want to sample this metric + let sample_rate: f32 = 0.5; + + if sample_rate <= 0.0 { + println!("zero"); + false + } else if sample_rate >= 1.0 { + println!("one!!!"); + true + } else { + let mut rng = rand::thread_rng(); + let between = Uniform::new(0.0, 1.0); + between.sample(&mut rng) <= sample_rate + } + } } lazy_static! { From 11e50e0438b1b9ac6c66a4c04ef829b294acfe55 Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Mon, 26 Oct 2020 21:49:55 +0100 Subject: [PATCH 06/14] Move sample rate to config --- relay-common/src/metrics.rs | 32 ++++++++++++++++++++++++-------- relay-config/src/config.rs | 10 +++++++++- relay/src/setup.rs | 1 + 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/relay-common/src/metrics.rs b/relay-common/src/metrics.rs index c88997c65fa..9c6a68bfd1b 100644 --- a/relay-common/src/metrics.rs +++ b/relay-common/src/metrics.rs @@ -83,6 +83,8 @@ pub struct MetricsClient { pub statsd_client: StatsdClient, /// Default tags to apply to every metric pub default_tags: BTreeMap, + /// Global sample rate + pub sample_rate: f32, } impl Deref for MetricsClient { @@ -125,19 +127,14 @@ impl MetricsClient { } fn _should_send(&self) -> bool { - // Decide if we want to sample this metric - let sample_rate: f32 = 0.5; - - if sample_rate <= 0.0 { - println!("zero"); + if self.sample_rate <= 0.0 { false - } else if sample_rate >= 1.0 { - println!("one!!!"); + } else if self.sample_rate >= 1.0 { true } else { let mut rng = rand::thread_rng(); let between = Uniform::new(0.0, 1.0); - between.sample(&mut rng) <= sample_rate + between.sample(&mut rng) <= self.sample_rate } } } @@ -177,12 +174,30 @@ pub fn configure_statsd( host: A, default_tags: BTreeMap, buffering: bool, + sample_rate: f32, ) { let addrs: Vec<_> = host.to_socket_addrs().unwrap().collect(); if !addrs.is_empty() { log::info!("reporting metrics to statsd at {}", addrs[0]); } + let normalized_sample_rate = if sample_rate >= 1.0 { + 1.0 + } else if sample_rate <= 0.0 { + 0.0 + } else { + sample_rate + }; + log::debug!( + "metrics sample rate is set to {}{}", + normalized_sample_rate, + if normalized_sample_rate == 0.0 { + ", no metrics will be reported" + } else { + "" + } + ); + let socket = UdpSocket::bind(&addrs[..]).unwrap(); socket.set_nonblocking(true).unwrap(); @@ -202,6 +217,7 @@ pub fn configure_statsd( set_client(MetricsClient { statsd_client, default_tags, + sample_rate, }); } diff --git a/relay-config/src/config.rs b/relay-config/src/config.rs index dabe6c98eca..30bdd7f5e62 100644 --- a/relay-config/src/config.rs +++ b/relay-config/src/config.rs @@ -427,6 +427,8 @@ struct Metrics { /// If set to true, emitted metrics will be buffered to optimize performance. /// Defaults to true. buffering: bool, + /// Global sample rate for all emitted metrics. + sample_rate: f32, } impl Default for Metrics { @@ -437,6 +439,7 @@ impl Default for Metrics { default_tags: BTreeMap::new(), hostname_tag: None, buffering: true, + sample_rate: 1.0, } } } @@ -1251,11 +1254,16 @@ impl Config { self.values.metrics.hostname_tag.as_deref() } - /// Returns true if metrics buffering is enabled, false otherwise + /// Returns true if metrics buffering is enabled, false otherwise. pub fn metrics_buffering(&self) -> bool { self.values.metrics.buffering } + /// Returns the global sample rate for all metrics. + pub fn metrics_sample_rate(&self) -> f32 { + self.values.metrics.sample_rate + } + /// Returns the default timeout for all upstream HTTP requests. pub fn http_timeout(&self) -> Duration { Duration::from_secs(self.values.http.timeout.into()) diff --git a/relay/src/setup.rs b/relay/src/setup.rs index 2fb50a64140..2e8b743c27a 100644 --- a/relay/src/setup.rs +++ b/relay/src/setup.rs @@ -219,6 +219,7 @@ pub fn init_metrics(config: &Config) -> Result<(), Error> { &addrs[..], default_tags, config.metrics_buffering(), + config.metrics_sample_rate(), ); Ok(()) From f0e34dd009e2271283d61d2ab14c35a578a33e4b Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Tue, 27 Oct 2020 10:53:19 +0100 Subject: [PATCH 07/14] Use normalized rate --- relay-common/src/metrics.rs | 7 ++++--- relay-config/src/config.rs | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/relay-common/src/metrics.rs b/relay-common/src/metrics.rs index 9c6a68bfd1b..94f7ccb5bb2 100644 --- a/relay-common/src/metrics.rs +++ b/relay-common/src/metrics.rs @@ -133,8 +133,8 @@ impl MetricsClient { true } else { let mut rng = rand::thread_rng(); - let between = Uniform::new(0.0, 1.0); - between.sample(&mut rng) <= self.sample_rate + RNG_UNIFORM_DISTRIBUTION + .with(|uniform_dist| uniform_dist.sample(&mut rng) <= self.sample_rate) } } } @@ -145,6 +145,7 @@ lazy_static! { thread_local! { static CURRENT_CLIENT: Option> = METRICS_CLIENT.read().clone(); + static RNG_UNIFORM_DISTRIBUTION: Uniform = Uniform::new(0.0, 1.0); } /// Internal prelude for the macro @@ -217,7 +218,7 @@ pub fn configure_statsd( set_client(MetricsClient { statsd_client, default_tags, - sample_rate, + normalized_sample_rate, }); } diff --git a/relay-config/src/config.rs b/relay-config/src/config.rs index 30bdd7f5e62..1fbd93e00e0 100644 --- a/relay-config/src/config.rs +++ b/relay-config/src/config.rs @@ -427,7 +427,8 @@ struct Metrics { /// If set to true, emitted metrics will be buffered to optimize performance. /// Defaults to true. buffering: bool, - /// Global sample rate for all emitted metrics. + /// Global sample rate for all emitted metrics. Should be between 0.0 and 1.0. + /// For example, the value of 0.3 means that only 30% of the emitted metrics will be sent. sample_rate: f32, } From af6085f2f9f64d1d2231ae53d6cefe22c4f9dce5 Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Tue, 27 Oct 2020 10:58:53 +0100 Subject: [PATCH 08/14] Clarify uniform dist --- relay-common/src/metrics.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/relay-common/src/metrics.rs b/relay-common/src/metrics.rs index 94f7ccb5bb2..918de66de56 100644 --- a/relay-common/src/metrics.rs +++ b/relay-common/src/metrics.rs @@ -132,6 +132,10 @@ impl MetricsClient { } else if self.sample_rate >= 1.0 { true } else { + // Using thread local RNG and uniform distribution here because Rng::gen_range is + // "optimized for the case that only a single sample is made from the given range". + // See https://docs.rs/rand/0.7.3/rand/distributions/uniform/struct.Uniform.html for more + // details. let mut rng = rand::thread_rng(); RNG_UNIFORM_DISTRIBUTION .with(|uniform_dist| uniform_dist.sample(&mut rng) <= self.sample_rate) From c5d05d8ee64c9a28b64528a6112032a844f703b0 Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Tue, 27 Oct 2020 11:00:42 +0100 Subject: [PATCH 09/14] Fix constructor --- Makefile | 2 +- relay-common/src/metrics.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 223100a02cf..43533b3cb3e 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ clean: # Builds build: setup-git - @cargo +stable build --all-features + cargo +stable build --all-features .PHONY: build release: setup-git diff --git a/relay-common/src/metrics.rs b/relay-common/src/metrics.rs index 918de66de56..25eb410df73 100644 --- a/relay-common/src/metrics.rs +++ b/relay-common/src/metrics.rs @@ -222,7 +222,7 @@ pub fn configure_statsd( set_client(MetricsClient { statsd_client, default_tags, - normalized_sample_rate, + sample_rate: normalized_sample_rate, }); } From 08d3852a6229f3c77fac3a63448ec01ac53a73c8 Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Tue, 27 Oct 2020 12:51:35 +0100 Subject: [PATCH 10/14] Update socket options --- relay-common/src/metrics.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/relay-common/src/metrics.rs b/relay-common/src/metrics.rs index 25eb410df73..8c5a7c89a4d 100644 --- a/relay-common/src/metrics.rs +++ b/relay-common/src/metrics.rs @@ -24,7 +24,7 @@ //! # use std::collections::BTreeMap; //! use relay_common::metrics; //! -//! metrics::configure_statsd("myprefix", "localhost:8125", BTreeMap::new()); +//! metrics::configure_statsd("myprefix", "localhost:8125", BTreeMap::new(), true, 1.0); //! ``` //! //! ## Macro Usage @@ -203,7 +203,7 @@ pub fn configure_statsd( } ); - let socket = UdpSocket::bind(&addrs[..]).unwrap(); + let socket = UdpSocket::bind("0.0.0.0:0").unwrap(); socket.set_nonblocking(true).unwrap(); let statsd_client = if buffering { From b0b0edc13951656358907ba03de3d2d39f3a9c3a Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Tue, 27 Oct 2020 18:41:12 +0100 Subject: [PATCH 11/14] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e37e1916823..71778d1ddaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ **Features**: - Rename upstream retries histogram metric and add upstream requests duration metric. ([#816](https://github.com/getsentry/relay/pull/816)) +- Add metrics buffering and sampling. ([#821](https://github.com/getsentry/relay/pull/821)) **Internal**: From 99cd298d190c9c010b9bfef61c026c719a96dc64 Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Tue, 27 Oct 2020 18:47:56 +0100 Subject: [PATCH 12/14] More idiomatic --- relay-common/src/metrics.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/relay-common/src/metrics.rs b/relay-common/src/metrics.rs index 8c5a7c89a4d..9e058203916 100644 --- a/relay-common/src/metrics.rs +++ b/relay-common/src/metrics.rs @@ -116,13 +116,12 @@ impl MetricsClient { metric = metric.with_tag(k, v); } - match metric.try_send() { - Ok(_) => (), - Err(error) => log::error!( + if let Err(error) = metric.try_send() { + log::error!( "Error sending a metric: {}, maximum capacity: {}", LogError(&error), METRICS_MAX_QUEUE_SIZE - ), + ); }; } From 04e1ece87a54c5fcdb003413f4db4bc7792f1340 Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Wed, 28 Oct 2020 12:21:07 +0100 Subject: [PATCH 13/14] Update CHANGELOG again --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71778d1ddaf..22c5f41146d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ **Features**: - Rename upstream retries histogram metric and add upstream requests duration metric. ([#816](https://github.com/getsentry/relay/pull/816)) -- Add metrics buffering and sampling. ([#821](https://github.com/getsentry/relay/pull/821)) +- Add options for metrics buffering (`metrics.buffering`) and sampling (`metrics.sample_rate`). ([#821](https://github.com/getsentry/relay/pull/821)) **Internal**: From fd40ce2be6e4fd269c02c928083c95bace5e22a6 Mon Sep 17 00:00:00 2001 From: Anton Ovchinnikov Date: Wed, 28 Oct 2020 12:24:52 +0100 Subject: [PATCH 14/14] Nicer normalization --- relay-common/src/metrics.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/relay-common/src/metrics.rs b/relay-common/src/metrics.rs index 9e058203916..25d8773d514 100644 --- a/relay-common/src/metrics.rs +++ b/relay-common/src/metrics.rs @@ -185,17 +185,12 @@ pub fn configure_statsd( log::info!("reporting metrics to statsd at {}", addrs[0]); } - let normalized_sample_rate = if sample_rate >= 1.0 { - 1.0 - } else if sample_rate <= 0.0 { - 0.0 - } else { - sample_rate - }; + // Normalize sample_rate + let sample_rate = sample_rate.max(0.).min(1.); log::debug!( "metrics sample rate is set to {}{}", - normalized_sample_rate, - if normalized_sample_rate == 0.0 { + sample_rate, + if sample_rate == 0.0 { ", no metrics will be reported" } else { "" @@ -221,7 +216,7 @@ pub fn configure_statsd( set_client(MetricsClient { statsd_client, default_tags, - sample_rate: normalized_sample_rate, + sample_rate, }); }