relay_statsd/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
//! A high-level StatsD metric client built on cadence.
//!
//! ## Defining Metrics
//!
//! In order to use metrics, one needs to first define one of the metric traits on a custom enum.
//! The following types of metrics are available: `counter`, `timer`, `gauge`, `histogram`, and
//! `set`. For explanations on what that means see [Metric Types].
//!
//! The metric traits serve only to provide a type safe metric name. All metric types have exactly
//! the same form, they are different only to ensure that a metric can only be used for the type for
//! which it was defined, (e.g. a counter metric cannot be used as a timer metric). See the traits
//! for more detailed examples.
//!
//! ## Initializing the Client
//!
//! Metrics can be used without initializing a statsd client. In that case, invoking `with_client`
//! or the [`metric!`] macro will become a noop. Only when configured, metrics will actually be
//! collected.
//!
//! To initialize the client, either use [`set_client`] to pass a custom client, or use
//! [`init`] to create a default client with known arguments:
//!
//! ```no_run
//! # use std::collections::BTreeMap;
//!
//! relay_statsd::init("myprefix", "localhost:8125", BTreeMap::new(), 1.0, true);
//! ```
//!
//! ## Macro Usage
//!
//! The recommended way to record metrics is by using the [`metric!`] macro. See the trait docs
//! for more information on how to record each type of metric.
//!
//! ```
//! use relay_statsd::{metric, CounterMetric};
//!
//! struct MyCounter;
//!
//! impl CounterMetric for MyCounter {
//! fn name(&self) -> &'static str {
//! "counter"
//! }
//! }
//!
//! metric!(counter(MyCounter) += 1);
//! ```
//!
//! ## Manual Usage
//!
//! ```
//! use relay_statsd::prelude::*;
//!
//! relay_statsd::with_client(|client| {
//! client.count("mymetric", 1).ok();
//! });
//! ```
//!
//! [Metric Types]: https://github.com/statsd/statsd/blob/master/docs/metric_types.md
use std::collections::BTreeMap;
use std::net::ToSocketAddrs;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use cadence::{Metric, MetricBuilder, StatsdClient};
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;
/// Client configuration object to store globally.
#[derive(Debug)]
pub struct MetricsClient {
/// The raw statsd client.
pub statsd_client: StatsdClient,
/// Default tags to apply to every metric.
pub default_tags: BTreeMap<String, String>,
/// Global sample rate.
pub sample_rate: f32,
/// Receiver for external listeners.
///
/// Only available when the client was initialized with `init_basic`.
pub rx: Option<crossbeam_channel::Receiver<Vec<u8>>>,
}
impl Deref for MetricsClient {
type Target = StatsdClient;
fn deref(&self) -> &StatsdClient {
&self.statsd_client
}
}
impl DerefMut for MetricsClient {
fn deref_mut(&mut self) -> &mut StatsdClient {
&mut self.statsd_client
}
}
impl MetricsClient {
/// Send a metric with the default tags defined on this `MetricsClient`.
#[inline(always)]
pub fn send_metric<'a, T>(&'a self, mut metric: MetricBuilder<'a, '_, T>)
where
T: Metric + From<String>,
{
if !self._should_send() {
return;
}
for (k, v) in &self.default_tags {
metric = metric.with_tag(k, v);
}
if let Err(error) = metric.try_send() {
relay_log::error!(
error = &error as &dyn std::error::Error,
maximum_capacity = METRICS_MAX_QUEUE_SIZE,
"Error sending a metric",
);
}
}
fn _should_send(&self) -> bool {
if self.sample_rate <= 0.0 {
false
} 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)
}
}
}
static METRICS_CLIENT: RwLock<Option<Arc<MetricsClient>>> = RwLock::new(None);
thread_local! {
static CURRENT_CLIENT: std::cell::RefCell<Option<Arc<MetricsClient>>> = METRICS_CLIENT.read().clone().into();
static RNG_UNIFORM_DISTRIBUTION: Uniform<f32> = Uniform::new(0.0, 1.0);
}
/// Internal prelude for the macro
#[doc(hidden)]
pub mod _pred {
pub use cadence::prelude::*;
}
/// The metrics prelude that is necessary to use the client.
pub mod prelude {
pub use cadence::prelude::*;
}
/// Set a new statsd client.
pub fn set_client(client: MetricsClient) {
*METRICS_CLIENT.write() = Some(Arc::new(client));
CURRENT_CLIENT.with(|cell| cell.replace(METRICS_CLIENT.read().clone()));
}
/// Set a test client for the period of the called function (only affects the current thread).
// TODO: replace usages with `init_basic`
pub fn with_capturing_test_client(f: impl FnOnce()) -> Vec<String> {
let (rx, sink) = cadence::SpyMetricSink::new();
let test_client = MetricsClient {
statsd_client: StatsdClient::from_sink("", sink),
default_tags: Default::default(),
sample_rate: 1.0,
rx: None,
};
CURRENT_CLIENT.with(|cell| {
let old_client = cell.replace(Some(Arc::new(test_client)));
f();
cell.replace(old_client);
});
rx.iter().map(|x| String::from_utf8(x).unwrap()).collect()
}
// Setup a simple metrics listener.
//
// Returns `None` if the global metrics client has already been configured.
pub fn init_basic() -> Option<crossbeam_channel::Receiver<Vec<u8>>> {
CURRENT_CLIENT.with(|cell| {
if cell.borrow().is_none() {
// Setup basic observable metrics sink.
let (receiver, sink) = cadence::SpyMetricSink::new();
let test_client = MetricsClient {
statsd_client: StatsdClient::from_sink("", sink),
default_tags: Default::default(),
sample_rate: 1.0,
rx: Some(receiver.clone()),
};
cell.replace(Some(Arc::new(test_client)));
}
});
CURRENT_CLIENT.with(|cell| {
cell.borrow()
.as_deref()
.and_then(|client| match &client.rx {
Some(rx) => Some(rx.clone()),
None => {
relay_log::error!("Metrics client was already set up.");
None
}
})
})
}
/// Disable the client again.
pub fn disable() {
*METRICS_CLIENT.write() = None;
}
/// Tell the metrics system to report to statsd.
pub fn init<A: ToSocketAddrs>(
prefix: &str,
host: A,
default_tags: BTreeMap<String, String>,
sample_rate: f32,
aggregate: bool,
) {
let addrs: Vec<_> = host.to_socket_addrs().unwrap().collect();
if !addrs.is_empty() {
relay_log::info!("reporting metrics to statsd at {}", addrs[0]);
}
// Normalize sample_rate
let sample_rate = sample_rate.clamp(0., 1.);
relay_log::debug!(
"metrics sample rate is set to {sample_rate}{}",
if sample_rate == 0.0 {
", no metrics will be reported"
} else {
""
}
);
let statsd_client = if aggregate {
let statsdproxy_sink = StatsdProxyMetricSink::new(move || {
let upstream = statsdproxy::middleware::upstream::Upstream::new(addrs[0])
.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,
},
upstream,
)
});
StatsdClient::from_sink(prefix, statsdproxy_sink)
} else {
let statsdproxy_sink = StatsdProxyMetricSink::new(move || {
statsdproxy::middleware::upstream::Upstream::new(addrs[0])
.expect("failed to create statsdproxy metric sind")
});
StatsdClient::from_sink(prefix, statsdproxy_sink)
};
set_client(MetricsClient {
statsd_client,
default_tags,
sample_rate,
rx: None,
});
}
/// Invoke a callback with the current statsd client.
///
/// If statsd is not configured the callback is not invoked. For the most part
/// the [`metric!`] macro should be used instead.
#[inline(always)]
pub fn with_client<F, R>(f: F) -> R
where
F: FnOnce(&MetricsClient) -> R,
R: Default,
{
CURRENT_CLIENT.with(|client| {
if let Some(client) = client.borrow().as_deref() {
f(client)
} else {
R::default()
}
})
}
/// A metric for capturing timings.
///
/// Timings are a positive number of milliseconds between a start and end time. Examples include
/// time taken to render a web page or time taken for a database call to return.
///
/// ## Example
///
/// ```
/// use relay_statsd::{metric, TimerMetric};
///
/// enum MyTimer {
/// ProcessA,
/// ProcessB,
/// }
///
/// impl TimerMetric for MyTimer {
/// fn name(&self) -> &'static str {
/// match self {
/// Self::ProcessA => "process_a",
/// Self::ProcessB => "process_b",
/// }
/// }
/// }
///
/// # fn process_a() {}
///
/// // measure time by explicitly setting a std::timer::Duration
/// # use std::time::Instant;
/// let start_time = Instant::now();
/// process_a();
/// metric!(timer(MyTimer::ProcessA) = start_time.elapsed());
///
/// // provide tags to a timer
/// metric!(
/// timer(MyTimer::ProcessA) = start_time.elapsed(),
/// server = "server1",
/// host = "host1",
/// );
///
/// // measure time implicitly by enclosing a code block in a metric
/// metric!(timer(MyTimer::ProcessA), {
/// process_a();
/// });
///
/// // measure block and also provide tags
/// metric!(
/// timer(MyTimer::ProcessB),
/// server = "server1",
/// host = "host1",
/// {
/// process_a();
/// }
/// );
///
/// ```
pub trait TimerMetric {
/// Returns the timer metric name that will be sent to statsd.
fn name(&self) -> &'static str;
}
/// A metric for capturing counters.
///
/// Counters are simple values incremented or decremented by a client. The rates at which these
/// events occur or average values will be determined by the server receiving them. Examples of
/// counter uses include number of logins to a system or requests received.
///
/// ## Example
///
/// ```
/// use relay_statsd::{metric, CounterMetric};
///
/// enum MyCounter {
/// TotalRequests,
/// TotalBytes,
/// }
///
/// impl CounterMetric for MyCounter {
/// fn name(&self) -> &'static str {
/// match self {
/// Self::TotalRequests => "total_requests",
/// Self::TotalBytes => "total_bytes",
/// }
/// }
/// }
///
/// # let buffer = &[(), ()];
///
/// // add to the counter
/// metric!(counter(MyCounter::TotalRequests) += 1);
/// metric!(counter(MyCounter::TotalBytes) += buffer.len() as i64);
///
/// // add to the counter and provide tags
/// metric!(
/// counter(MyCounter::TotalRequests) += 1,
/// server = "s1",
/// host = "h1"
/// );
///
/// // subtract from the counter
/// metric!(counter(MyCounter::TotalRequests) -= 1);
///
/// // subtract from the counter and provide tags
/// metric!(
/// counter(MyCounter::TotalRequests) -= 1,
/// server = "s1",
/// host = "h1"
/// );
/// ```
pub trait CounterMetric {
/// Returns the counter metric name that will be sent to statsd.
fn name(&self) -> &'static str;
}
/// A metric for capturing histograms.
///
/// Histograms are values whose distribution is calculated by the server. The distribution
/// calculated for histograms is often similar to that of timers. Histograms can be thought of as a
/// more general (not limited to timing things) form of timers.
///
/// ## Example
///
/// ```
/// use relay_statsd::{metric, HistogramMetric};
///
/// struct QueueSize;
///
/// impl HistogramMetric for QueueSize {
/// fn name(&self) -> &'static str {
/// "queue_size"
/// }
/// }
///
/// # use std::collections::VecDeque;
/// let queue = VecDeque::new();
/// # let _hint: &VecDeque<()> = &queue;
///
/// // record a histogram value
/// metric!(histogram(QueueSize) = queue.len() as u64);
///
/// // record with tags
/// metric!(
/// histogram(QueueSize) = queue.len() as u64,
/// server = "server1",
/// host = "host1",
/// );
/// ```
pub trait HistogramMetric {
/// Returns the histogram metric name that will be sent to statsd.
fn name(&self) -> &'static str;
}
/// A metric for capturing sets.
///
/// Sets count the number of unique elements in a group. You can use them to, for example, count the
/// unique visitors to your site.
///
/// ## Example
///
/// ```
/// use relay_statsd::{metric, SetMetric};
///
/// enum MySet {
/// UniqueProjects,
/// UniqueUsers,
/// }
///
/// impl SetMetric for MySet {
/// fn name(&self) -> &'static str {
/// match self {
/// MySet::UniqueProjects => "unique_projects",
/// MySet::UniqueUsers => "unique_users",
/// }
/// }
/// }
///
/// # use std::collections::HashSet;
/// let users = HashSet::new();
/// # let _hint: &HashSet<()> = &users;
///
/// // use a set metric
/// metric!(set(MySet::UniqueUsers) = users.len() as i64);
///
/// // use a set metric with tags
/// metric!(
/// set(MySet::UniqueUsers) = users.len() as i64,
/// server = "server1",
/// host = "host1",
/// );
/// ```
pub trait SetMetric {
/// Returns the set metric name that will be sent to statsd.
fn name(&self) -> &'static str;
}
/// A metric for capturing gauges.
///
/// Gauge values are an instantaneous measurement of a value determined by the client. They do not
/// change unless changed by the client. Examples include things like load average or how many
/// connections are active.
///
/// ## Example
///
/// ```
/// use relay_statsd::{metric, GaugeMetric};
///
/// struct QueueSize;
///
/// impl GaugeMetric for QueueSize {
/// fn name(&self) -> &'static str {
/// "queue_size"
/// }
/// }
///
/// # use std::collections::VecDeque;
/// let queue = VecDeque::new();
/// # let _hint: &VecDeque<()> = &queue;
///
/// // a simple gauge value
/// metric!(gauge(QueueSize) = queue.len() as u64);
///
/// // a gauge with tags
/// metric!(
/// gauge(QueueSize) = queue.len() as u64,
/// server = "server1",
/// host = "host1"
/// );
/// ```
pub trait GaugeMetric {
/// Returns the gauge metric name that will be sent to statsd.
fn name(&self) -> &'static str;
}
/// Emits a metric.
///
/// See [crate-level documentation](self) for examples.
#[macro_export]
macro_rules! metric {
// counter increment
(counter($id:expr) += $value:expr $(, $k:ident = $v:expr)* $(,)?) => {
match $value {
value if value != 0 => {
$crate::with_client(|client| {
use $crate::_pred::*;
client.send_metric(
client.count_with_tags(&$crate::CounterMetric::name(&$id), value)
$(.with_tag(stringify!($k), $v))*
)
})
},
_ => {},
};
};
// counter decrement
(counter($id:expr) -= $value:expr $(, $k:ident = $v:expr)* $(,)?) => {
match $value {
value if value != 0 => {
$crate::with_client(|client| {
use $crate::_pred::*;
client.send_metric(
client.count_with_tags(&$crate::CounterMetric::name(&$id), -value)
$(.with_tag(stringify!($k), $v))*
)
})
},
_ => {},
};
};
// gauge set
(gauge($id:expr) = $value:expr $(, $k:ident = $v:expr)* $(,)?) => {
$crate::with_client(|client| {
use $crate::_pred::*;
client.send_metric(
client.gauge_with_tags(&$crate::GaugeMetric::name(&$id), $value)
$(.with_tag(stringify!($k), $v))*
)
})
};
// histogram
(histogram($id:expr) = $value:expr $(, $k:ident = $v:expr)* $(,)?) => {
$crate::with_client(|client| {
use $crate::_pred::*;
client.send_metric(
client.histogram_with_tags(&$crate::HistogramMetric::name(&$id), $value)
$(.with_tag(stringify!($k), $v))*
)
})
};
// sets (count unique occurrences of a value per time interval)
(set($id:expr) = $value:expr $(, $k:ident = $v:expr)* $(,)?) => {
$crate::with_client(|client| {
use $crate::_pred::*;
client.send_metric(
client.set_with_tags(&$crate::SetMetric::name(&$id), $value)
$(.with_tag(stringify!($k), $v))*
)
})
};
// timer value (duration)
(timer($id:expr) = $value:expr $(, $k:ident = $v:expr)* $(,)?) => {
$crate::with_client(|client| {
use $crate::_pred::*;
client.send_metric(
client.time_with_tags(&$crate::TimerMetric::name(&$id), $value)
$(.with_tag(stringify!($k), $v))*
)
})
};
// timed block
(timer($id:expr), $($k:ident = $v:expr,)* $block:block) => {{
let now = std::time::Instant::now();
let rv = {$block};
$crate::with_client(|client| {
use $crate::_pred::*;
client.send_metric(
client.time_with_tags(&$crate::TimerMetric::name(&$id), now.elapsed())
$(.with_tag(stringify!($k), $v))*
)
});
rv
}};
}
#[cfg(test)]
mod tests {
use cadence::{NopMetricSink, StatsdClient};
use crate::{set_client, with_capturing_test_client, with_client, GaugeMetric, MetricsClient};
enum TestGauges {
Foo,
Bar,
}
impl GaugeMetric for TestGauges {
fn name(&self) -> &'static str {
match self {
Self::Foo => "foo",
Self::Bar => "bar",
}
}
}
#[test]
fn test_capturing_client() {
let captures = with_capturing_test_client(|| {
metric!(
gauge(TestGauges::Foo) = 123,
server = "server1",
host = "host1"
);
metric!(
gauge(TestGauges::Bar) = 456,
server = "server2",
host = "host2"
);
});
assert_eq!(
captures,
[
"foo:123|g|#server:server1,host:host1",
"bar:456|g|#server:server2,host:host2"
]
)
}
#[test]
fn current_client_is_global_client() {
let client1 = with_client(|c| format!("{c:?}"));
set_client(MetricsClient {
statsd_client: StatsdClient::from_sink("", NopMetricSink),
default_tags: Default::default(),
sample_rate: 1.0,
rx: None,
});
let client2 = with_client(|c| format!("{c:?}"));
// After setting the global client,the current client must change:
assert_ne!(client1, client2);
}
}