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

ref(statsd): Use statsdproxy to pre-aggregate metrics in-memory #2425

Merged
merged 6 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

- Add quotas to global config. ([#3086](https://github.com/getsentry/relay/pull/3086))
- Adds support for dynamic metric bucket encoding. ([#3137](https://github.com/getsentry/relay/pull/3137))
- Use statsdproxy to pre-aggregate metrics. ([#2425](https://github.com/getsentry/relay/pull/2425))

## 24.2.0

Expand Down
35 changes: 35 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions relay-config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ struct Metrics {
///
/// Defaults to `true`.
buffering: bool,
/// Emitted metrics will be aggregated to optimize bandwidth.
///
/// Defaults to `true`.
aggregation: bool,
/// Global sample rate for all emitted metrics between `0.0` and `1.0`.
///
/// For example, a value of `0.3` means that only 30% of the emitted metrics will be sent.
Expand All @@ -512,6 +516,7 @@ impl Default for Metrics {
default_tags: BTreeMap::new(),
hostname_tag: None,
buffering: true,
aggregation: true,
sample_rate: 1.0,
}
}
Expand Down Expand Up @@ -1823,6 +1828,11 @@ impl Config {
self.values.metrics.buffering
}

/// Returns true if metrics aggregation is enabled, false otherwise.
pub fn metrics_aggregation(&self) -> bool {
self.values.metrics.aggregation
}

/// Returns the global sample rate for all metrics.
pub fn metrics_sample_rate(&self) -> f32 {
self.values.metrics.sample_rate
Expand Down
1 change: 1 addition & 0 deletions relay-statsd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ crossbeam-channel = "0.5.6"
parking_lot = { workspace = true }
rand = { workspace = true }
relay-log = { path = "../relay-log" }
statsdproxy = { version = "0.1.2", features = ["cadence-adapter"], default-features = false }

[features]
default = []
Expand Down
24 changes: 22 additions & 2 deletions relay-statsd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! ```no_run
//! # use std::collections::BTreeMap;
//!
//! relay_statsd::init("myprefix", "localhost:8125", BTreeMap::new(), true, 1.0);
//! relay_statsd::init("myprefix", "localhost:8125", BTreeMap::new(), true, true, 1.0);
//! ```
//!
//! ## Macro Usage
Expand Down Expand Up @@ -66,6 +66,8 @@ use cadence::{
};
use parking_lot::RwLock;
use rand::distributions::{Distribution, Uniform};
use statsdproxy::cadence::StatsdProxyMetricSink;
use statsdproxy::config::AggregateMetricsConfig;

/// Maximum number of metric events that can be queued before we start dropping them
const METRICS_MAX_QUEUE_SIZE: usize = 100_000;
Expand Down Expand Up @@ -226,6 +228,7 @@ pub fn init<A: ToSocketAddrs>(
host: A,
default_tags: BTreeMap<String, String>,
buffering: bool,
aggregating: bool,
sample_rate: f32,
) {
let addrs: Vec<_> = host.to_socket_addrs().unwrap().collect();
Expand All @@ -247,7 +250,24 @@ pub fn init<A: ToSocketAddrs>(
let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
socket.set_nonblocking(true).unwrap();

let statsd_client = if buffering {
let statsd_client = if aggregating {
let host = host.to_socket_addrs().unwrap().next().unwrap();
let statsdproxy_sink = StatsdProxyMetricSink::new(move || {
Dav1dde marked this conversation as resolved.
Show resolved Hide resolved
let next_step = statsdproxy::middleware::upstream::Upstream::new(host)
.expect("failed to create statsdproxy metric sink");
statsdproxy::middleware::aggregate::AggregateMetrics::new(
AggregateMetricsConfig {
aggregate_gauges: true,
aggregate_counters: true,
flush_interval: 1,
flush_offset: 0,
max_map_size: None,
Comment on lines +262 to +264
Copy link
Member

Choose a reason for hiding this comment

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

Should these be configurable?

Copy link
Member Author

Choose a reason for hiding this comment

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

they should probably not have been options to begin with tbh

},
next_step,
)
});
StatsdClient::from_sink(prefix, statsdproxy_sink)
} else 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)
Expand Down
1 change: 1 addition & 0 deletions relay/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub fn init_metrics(config: &Config) -> Result<()> {
&addrs[..],
default_tags,
config.metrics_buffering(),
config.metrics_aggregation(),
Copy link
Member

Choose a reason for hiding this comment

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

nit: With this number of arguments, it might be nice to pass a StatsdConfig object instead. Not a blocker though.

Copy link
Member

Choose a reason for hiding this comment

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

Plan is to get rid of the options all together: #2425 (comment)

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

OK!

config.metrics_sample_rate(),
);

Expand Down
Loading