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 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository, or online at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Metrics for materialize systems.
//!
//! The idea here is that each subsystem keeps its metrics in a scoped-to-it struct, which gets
//! registered (once) to the server's (or a test's) prometheus registry.
//!
//! Instead of using prometheus's (very verbose) metrics definitions, we rely on type inference to
//! reduce the verbosity a little bit. A typical subsystem will look like the following:
//!
//! ```rust
//! # use mz_ore::metrics::{MetricsRegistry, IntCounter};
//! # use mz_ore::metric;
//! #[derive(Debug, Clone)] // Note that prometheus metrics can safely be cloned
//! struct Metrics {
//! pub bytes_sent: IntCounter,
//! }
//!
//! impl Metrics {
//! pub fn register_into(registry: &MetricsRegistry) -> Metrics {
//! Metrics {
//! bytes_sent: registry.register(metric!(
//! name: "mz_pg_sent_bytes",
//! help: "total number of bytes sent here",
//! )),
//! }
//! }
//! }
//! ```
use std::fmt;
use std::fmt::{Debug, Formatter};
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use std::time::{Duration, Instant};
use derivative::Derivative;
use pin_project::pin_project;
use prometheus::core::{
Atomic, AtomicF64, AtomicI64, AtomicU64, Collector, Desc, GenericCounter, GenericCounterVec,
GenericGauge, GenericGaugeVec,
};
use prometheus::proto::MetricFamily;
use prometheus::{HistogramOpts, Registry};
mod delete_on_drop;
pub use delete_on_drop::*;
pub use prometheus::Opts as PrometheusOpts;
/// Define a metric for use in materialize.
#[macro_export]
macro_rules! metric {
(
name: $name:expr,
help: $help:expr
$(, subsystem: $subsystem_name:expr)?
$(, const_labels: { $($cl_key:expr => $cl_value:expr ),* })?
$(, var_labels: [ $($vl_name:expr),* ])?
$(, buckets: $bk_name:expr)?
$(,)?
) => {{
let const_labels = (&[
$($(
($cl_key.to_string(), $cl_value.to_string()),
)*)?
]).into_iter().cloned().collect();
let var_labels = vec![
$(
$($vl_name.into(),)*
)?];
#[allow(unused_mut)]
let mut mk_opts = $crate::metrics::MakeCollectorOpts {
opts: $crate::metrics::PrometheusOpts::new($name, $help)
$(.subsystem( $subsystem_name ))?
.const_labels(const_labels)
.variable_labels(var_labels),
buckets: None,
};
// Set buckets if passed
$(mk_opts.buckets = Some($bk_name);)*
mk_opts
}}
}
/// Options for MakeCollector. This struct should be instantiated using the metric macro.
#[derive(Debug, Clone)]
pub struct MakeCollectorOpts {
/// Common Prometheus options
pub opts: PrometheusOpts,
/// Buckets to be used with Histogram and HistogramVec. Must be set to create Histogram types
/// and must not be set for other types.
pub buckets: Option<Vec<f64>>,
}
/// The materialize metrics registry.
#[derive(Clone, Derivative)]
#[derivative(Debug)]
pub struct MetricsRegistry {
inner: Registry,
#[derivative(Debug = "ignore")]
postprocessors: Arc<Mutex<Vec<Box<dyn FnMut(&mut Vec<MetricFamily>) + Send + Sync>>>>,
}
/// A wrapper for metrics to require delete on drop semantics
///
/// The wrapper behaves like regular metrics but only provides functions to create delete-on-drop
/// variants. This way, no metrics of this type can be leaked.
///
/// In situations where the delete-on-drop behavior is not desired or in legacy code, use the raw
/// variants of the metrics, as defined in [self::raw].
#[derive(Clone)]
pub struct DeleteOnDropWrapper<M> {
inner: M,
}
impl<M: MakeCollector + Debug> Debug for DeleteOnDropWrapper<M> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}
impl<M: Collector> Collector for DeleteOnDropWrapper<M> {
fn desc(&self) -> Vec<&Desc> {
self.inner.desc()
}
fn collect(&self) -> Vec<MetricFamily> {
self.inner.collect()
}
}
impl<M: MakeCollector> MakeCollector for DeleteOnDropWrapper<M> {
fn make_collector(opts: MakeCollectorOpts) -> Self {
DeleteOnDropWrapper {
inner: M::make_collector(opts),
}
}
}
impl<M: MetricVecExt> DeleteOnDropWrapper<M> {
/// Returns a metric that deletes its labels from this metrics vector when dropped.
pub fn get_delete_on_drop_metric<'a, L: PromLabelsExt<'a>>(
&self,
labels: L,
) -> DeleteOnDropMetric<'a, M, L> {
self.inner.get_delete_on_drop_metric(labels)
}
}
/// The unsigned integer version of [`Gauge`]. Provides better performance if
/// metric values are all unsigned integers.
pub type UIntGauge = GenericGauge<AtomicU64>;
/// Delete-on-drop shadow of Prometheus [prometheus::CounterVec].
pub type CounterVec = DeleteOnDropWrapper<prometheus::CounterVec>;
/// Delete-on-drop shadow of Prometheus [prometheus::Gauge].
pub type Gauge = DeleteOnDropWrapper<prometheus::Gauge>;
/// Delete-on-drop shadow of Prometheus [prometheus::CounterVec].
pub type GaugeVec = DeleteOnDropWrapper<prometheus::GaugeVec>;
/// Delete-on-drop shadow of Prometheus [prometheus::HistogramVec].
pub type HistogramVec = DeleteOnDropWrapper<prometheus::HistogramVec>;
/// Delete-on-drop shadow of Prometheus [prometheus::IntCounterVec].
pub type IntCounterVec = DeleteOnDropWrapper<prometheus::IntCounterVec>;
/// Delete-on-drop shadow of Prometheus [prometheus::IntGaugeVec].
pub type IntGaugeVec = DeleteOnDropWrapper<prometheus::IntGaugeVec>;
/// Delete-on-drop shadow of Prometheus [raw::UIntGaugeVec].
pub type UIntGaugeVec = DeleteOnDropWrapper<raw::UIntGaugeVec>;
pub use prometheus::{Counter, Histogram, IntCounter, IntGauge};
use crate::assert_none;
/// Access to non-delete-on-drop vector types
pub mod raw {
use prometheus::core::{AtomicU64, GenericGaugeVec};
/// The unsigned integer version of [`GaugeVec`].
/// Provides better performance if metric values are all unsigned integers.
pub type UIntGaugeVec = GenericGaugeVec<AtomicU64>;
pub use prometheus::{CounterVec, Gauge, GaugeVec, HistogramVec, IntCounterVec, IntGaugeVec};
}
impl MetricsRegistry {
/// Creates a new metrics registry.
pub fn new() -> Self {
MetricsRegistry {
inner: Registry::new(),
postprocessors: Arc::new(Mutex::new(vec![])),
}
}
/// Register a metric defined with the [`metric`] macro.
pub fn register<M>(&self, opts: MakeCollectorOpts) -> M
where
M: MakeCollector,
{
let collector = M::make_collector(opts);
self.inner.register(Box::new(collector.clone())).unwrap();
collector
}
/// Registers a gauge whose value is computed when observed.
pub fn register_computed_gauge<F, P>(
&self,
opts: MakeCollectorOpts,
f: F,
) -> ComputedGenericGauge<P>
where
F: Fn() -> P::T + Send + Sync + 'static,
P: Atomic + 'static,
{
let gauge = ComputedGenericGauge {
gauge: GenericGauge::make_collector(opts),
f: Arc::new(f),
};
self.inner.register(Box::new(gauge.clone())).unwrap();
gauge
}
/// Register a pre-defined prometheus collector.
pub fn register_collector<C: 'static + prometheus::core::Collector>(&self, collector: C) {
self.inner
.register(Box::new(collector))
.expect("registering pre-defined metrics collector");
}
/// Registers a metric postprocessor.
///
/// Postprocessors are invoked on every call to [`MetricsRegistry::gather`]
/// in the order that they are registered.
pub fn register_postprocessor<F>(&self, f: F)
where
F: FnMut(&mut Vec<MetricFamily>) + Send + Sync + 'static,
{
let mut postprocessors = self.postprocessors.lock().expect("lock poisoned");
postprocessors.push(Box::new(f));
}
/// Gather all the metrics from the metrics registry for reporting.
///
/// This function invokes the postprocessors on all gathered metrics (see
/// [`MetricsRegistry::register_postprocessor`]) in the order the
/// postprocessors were registered.
///
/// See also [`prometheus::Registry::gather`].
pub fn gather(&self) -> Vec<MetricFamily> {
let mut metrics = self.inner.gather();
let mut postprocessors = self.postprocessors.lock().expect("lock poisoned");
for postprocessor in &mut *postprocessors {
postprocessor(&mut metrics);
}
metrics
}
}
/// A wrapper for creating prometheus metrics more conveniently.
///
/// Together with the [`metric`] macro, this trait is mainly used by [`MetricsRegistry`] and should
/// not normally be used outside the metric registration flow.
pub trait MakeCollector: Collector + Clone + 'static {
/// Creates a new collector.
fn make_collector(opts: MakeCollectorOpts) -> Self;
}
impl<T> MakeCollector for GenericCounter<T>
where
T: Atomic + 'static,
{
fn make_collector(mk_opts: MakeCollectorOpts) -> Self {
assert_none!(mk_opts.buckets);
Self::with_opts(mk_opts.opts).expect("defining a counter")
}
}
impl<T> MakeCollector for GenericCounterVec<T>
where
T: Atomic + 'static,
{
fn make_collector(mk_opts: MakeCollectorOpts) -> Self {
assert_none!(mk_opts.buckets);
let labels: Vec<String> = mk_opts.opts.variable_labels.clone();
let label_refs: Vec<&str> = labels.iter().map(String::as_str).collect();
Self::new(mk_opts.opts, label_refs.as_slice()).expect("defining a counter vec")
}
}
impl<T> MakeCollector for GenericGauge<T>
where
T: Atomic + 'static,
{
fn make_collector(mk_opts: MakeCollectorOpts) -> Self {
assert_none!(mk_opts.buckets);
Self::with_opts(mk_opts.opts).expect("defining a gauge")
}
}
impl<T> MakeCollector for GenericGaugeVec<T>
where
T: Atomic + 'static,
{
fn make_collector(mk_opts: MakeCollectorOpts) -> Self {
assert_none!(mk_opts.buckets);
let labels = mk_opts.opts.variable_labels.clone();
let labels = &labels.iter().map(|x| x.as_str()).collect::<Vec<_>>();
Self::new(mk_opts.opts, labels).expect("defining a gauge vec")
}
}
impl MakeCollector for Histogram {
fn make_collector(mk_opts: MakeCollectorOpts) -> Self {
assert!(mk_opts.buckets.is_some());
Self::with_opts(HistogramOpts {
common_opts: mk_opts.opts,
buckets: mk_opts.buckets.unwrap(),
})
.expect("defining a histogram")
}
}
impl MakeCollector for raw::HistogramVec {
fn make_collector(mk_opts: MakeCollectorOpts) -> Self {
assert!(mk_opts.buckets.is_some());
let labels = mk_opts.opts.variable_labels.clone();
let labels = &labels.iter().map(|x| x.as_str()).collect::<Vec<_>>();
Self::new(
HistogramOpts {
common_opts: mk_opts.opts,
buckets: mk_opts.buckets.unwrap(),
},
labels,
)
.expect("defining a histogram vec")
}
}
/// A [`Gauge`] whose value is computed whenever it is observed.
pub struct ComputedGenericGauge<P>
where
P: Atomic,
{
gauge: GenericGauge<P>,
f: Arc<dyn Fn() -> P::T + Send + Sync>,
}
impl<P> fmt::Debug for ComputedGenericGauge<P>
where
P: Atomic + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("ComputedGenericGauge")
.field("gauge", &self.gauge)
.finish_non_exhaustive()
}
}
impl<P> Clone for ComputedGenericGauge<P>
where
P: Atomic,
{
fn clone(&self) -> ComputedGenericGauge<P> {
ComputedGenericGauge {
gauge: self.gauge.clone(),
f: Arc::clone(&self.f),
}
}
}
impl<T> Collector for ComputedGenericGauge<T>
where
T: Atomic,
{
fn desc(&self) -> Vec<&prometheus::core::Desc> {
self.gauge.desc()
}
fn collect(&self) -> Vec<MetricFamily> {
self.gauge.set((self.f)());
self.gauge.collect()
}
}
impl<P> ComputedGenericGauge<P>
where
P: Atomic,
{
/// Computes the current value of the gauge.
pub fn get(&self) -> P::T {
(self.f)()
}
}
/// A [`ComputedGenericGauge`] for 64-bit floating point numbers.
pub type ComputedGauge = ComputedGenericGauge<AtomicF64>;
/// A [`ComputedGenericGauge`] for 64-bit signed integers.
pub type ComputedIntGauge = ComputedGenericGauge<AtomicI64>;
/// A [`ComputedGenericGauge`] for 64-bit unsigned integers.
pub type ComputedUIntGauge = ComputedGenericGauge<AtomicU64>;
/// Exposes combinators that report metrics related to the execution of a [`Future`] to prometheus.
pub trait MetricsFutureExt<F> {
/// Records the number of seconds it takes a [`Future`] to complete according to "the clock on
/// the wall".
///
/// More specifically, it records the instant at which the `Future` was first polled, and the
/// instant at which the `Future` completes. Then reports the duration between those two
/// instances to the provided metric.
///
/// # Wall Time vs Execution Time
///
/// There is also [`MetricsFutureExt::exec_time`], which measures how long a [`Future`] spent
/// executing, instead of how long it took to complete. For example, a network request may have
/// a wall time of 1 second, meanwhile it's execution time may have only been 50ms. The 950ms
/// delta would be how long the [`Future`] waited for a response from the network.
///
/// # Uses
///
/// Recording the wall time can be useful for monitoring latency, for example the latency of a
/// SQL request.
///
/// Note: You must call either [`observe`] to record the execution time to a [`Histogram`] or
/// [`inc_by`] to record to a [`Counter`]. The following will not compile:
///
/// ```compile_fail
/// use mz_ore::metrics::MetricsFutureExt;
///
/// # let _ = async {
/// async { Ok(()) }
/// .wall_time()
/// .await;
/// # };
/// ```
///
/// [`observe`]: WallTimeFuture::observe
/// [`inc_by`]: WallTimeFuture::inc_by
fn wall_time(self) -> WallTimeFuture<F, UnspecifiedMetric>;
/// Records the total number of seconds for which a [`Future`] was executing.
///
/// More specifically, every time the `Future` is polled it records how long that individual
/// call took, and maintains a running sum until the `Future` completes. Then we report that
/// duration to the provided metric.
///
/// # Wall Time vs Execution Time
///
/// There is also [`MetricsFutureExt::wall_time`], which measures how long a [`Future`] took to
/// complete, instead of how long it spent executing. For example, a network request may have
/// a wall time of 1 second, meanwhile it's execution time may have only been 50ms. The 950ms
/// delta would be how long the [`Future`] waited for a response from the network.
///
/// # Uses
///
/// Recording execution time can be useful if you want to monitor [`Future`]s that could be
/// sensitive to CPU usage. For example, if you have a single logical control thread you'll
/// want to make sure that thread never spends too long running a single `Future`. Reporting
/// the execution time of `Future`s running on this thread can help ensure there is no
/// unexpected blocking.
///
/// Note: You must call either [`observe`] to record the execution time to a [`Histogram`] or
/// [`inc_by`] to record to a [`Counter`]. The following will not compile:
///
/// ```compile_fail
/// use mz_ore::metrics::MetricsFutureExt;
///
/// # let _ = async {
/// async { Ok(()) }
/// .exec_time()
/// .await;
/// # };
/// ```
///
/// [`observe`]: ExecTimeFuture::observe
/// [`inc_by`]: ExecTimeFuture::inc_by
fn exec_time(self) -> ExecTimeFuture<F, UnspecifiedMetric>;
}
impl<F: Future> MetricsFutureExt<F> for F {
fn wall_time(self) -> WallTimeFuture<F, UnspecifiedMetric> {
WallTimeFuture {
fut: self,
metric: UnspecifiedMetric(()),
start: None,
filter: None,
}
}
fn exec_time(self) -> ExecTimeFuture<F, UnspecifiedMetric> {
ExecTimeFuture {
fut: self,
metric: UnspecifiedMetric(()),
running_duration: Duration::from_millis(0),
filter: None,
}
}
}
/// Future returned by [`MetricsFutureExt::wall_time`].
#[must_use = "futures do nothing unless you `.await` or poll them"]
#[pin_project]
pub struct WallTimeFuture<F, Metric> {
/// The inner [`Future`] that we're recording the wall time for.
#[pin]
fut: F,
/// Prometheus metric that we'll report to.
metric: Metric,
/// [`Instant`] at which the [`Future`] was first polled.
start: Option<Instant>,
/// Optional filter that determines if we observe the wall time of this [`Future`].
filter: Option<Box<dyn FnMut(Duration) -> bool + Send + Sync>>,
}
impl<F: Debug, M: Debug> fmt::Debug for WallTimeFuture<F, M> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("WallTimeFuture")
.field("fut", &self.fut)
.field("metric", &self.metric)
.field("start", &self.start)
.field("filter", &self.filter.is_some())
.finish()
}
}
impl<F> WallTimeFuture<F, UnspecifiedMetric> {
/// Sets the recored metric to be a [`prometheus::Histogram`].
///
/// ```text
/// my_future
/// .wall_time()
/// .observe(metrics.slow_queries_hist.with_label_values(&["select"]))
/// ```
pub fn observe(
self,
histogram: prometheus::Histogram,
) -> WallTimeFuture<F, prometheus::Histogram> {
WallTimeFuture {
fut: self.fut,
metric: histogram,
start: self.start,
filter: self.filter,
}
}
/// Sets the recored metric to be a [`prometheus::Counter`].
///
/// ```text
/// my_future
/// .wall_time()
/// .inc_by(metrics.slow_queries.with_label_values(&["select"]))
/// ```
pub fn inc_by(self, counter: prometheus::Counter) -> WallTimeFuture<F, prometheus::Counter> {
WallTimeFuture {
fut: self.fut,
metric: counter,
start: self.start,
filter: self.filter,
}
}
/// Sets the recorded duration in a specific f64.
pub fn set_at(self, place: &mut f64) -> WallTimeFuture<F, &mut f64> {
WallTimeFuture {
fut: self.fut,
metric: place,
start: self.start,
filter: self.filter,
}
}
}
impl<F, M> WallTimeFuture<F, M> {
/// Specifies a filter which much return `true` for the wall time to be recorded.
///
/// This can be particularly useful if you have a high volume `Future` and you only want to
/// record ones that take a long time to complete.
pub fn with_filter(
mut self,
filter: impl FnMut(Duration) -> bool + Send + Sync + 'static,
) -> Self {
self.filter = Some(Box::new(filter));
self
}
}
impl<F: Future, M: DurationMetric> Future for WallTimeFuture<F, M> {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
if this.start.is_none() {
*this.start = Some(Instant::now());
}
let result = match this.fut.poll(cx) {
Poll::Ready(r) => r,
Poll::Pending => return Poll::Pending,
};
let duration = Instant::now().duration_since(this.start.expect("timer to be started"));
let pass = this
.filter
.as_mut()
.map(|filter| filter(duration))
.unwrap_or(true);
if pass {
this.metric.record(duration.as_secs_f64())
}
Poll::Ready(result)
}
}
/// Future returned by [`MetricsFutureExt::exec_time`].
#[must_use = "futures do nothing unless you `.await` or poll them"]
#[pin_project]
pub struct ExecTimeFuture<F, Metric> {
/// The inner [`Future`] that we're recording the wall time for.
#[pin]
fut: F,
/// Prometheus metric that we'll report to.
metric: Metric,
/// Total [`Duration`] for which this [`Future`] has been executing.
running_duration: Duration,
/// Optional filter that determines if we observe the execution time of this [`Future`].
filter: Option<Box<dyn FnMut(Duration) -> bool + Send + Sync>>,
}
impl<F: Debug, M: Debug> fmt::Debug for ExecTimeFuture<F, M> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("ExecTimeFuture")
.field("fut", &self.fut)
.field("metric", &self.metric)
.field("running_duration", &self.running_duration)
.field("filter", &self.filter.is_some())
.finish()
}
}
impl<F> ExecTimeFuture<F, UnspecifiedMetric> {
/// Sets the recored metric to be a [`prometheus::Histogram`].
///
/// ```text
/// my_future
/// .exec_time()
/// .observe(metrics.slow_queries_hist.with_label_values(&["select"]))
/// ```
pub fn observe(
self,
histogram: prometheus::Histogram,
) -> ExecTimeFuture<F, prometheus::Histogram> {
ExecTimeFuture {
fut: self.fut,
metric: histogram,
running_duration: self.running_duration,
filter: self.filter,
}
}
/// Sets the recored metric to be a [`prometheus::Counter`].
///
/// ```text
/// my_future
/// .exec_time()
/// .inc_by(metrics.slow_queries.with_label_values(&["select"]))
/// ```
pub fn inc_by(self, counter: prometheus::Counter) -> ExecTimeFuture<F, prometheus::Counter> {
ExecTimeFuture {
fut: self.fut,
metric: counter,
running_duration: self.running_duration,
filter: self.filter,
}
}
}
impl<F, M> ExecTimeFuture<F, M> {
/// Specifies a filter which much return `true` for the execution time to be recorded.
pub fn with_filter(
mut self,
filter: impl FnMut(Duration) -> bool + Send + Sync + 'static,
) -> Self {
self.filter = Some(Box::new(filter));
self
}
}
impl<F: Future, M: DurationMetric> Future for ExecTimeFuture<F, M> {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let start = Instant::now();
let result = this.fut.poll(cx);
let duration = Instant::now().duration_since(start);
*this.running_duration = this.running_duration.saturating_add(duration);
let result = match result {
Poll::Ready(result) => result,
Poll::Pending => return Poll::Pending,
};
let duration = *this.running_duration;
let pass = this
.filter
.as_mut()
.map(|filter| filter(duration))
.unwrap_or(true);
if pass {
this.metric.record(duration.as_secs_f64());
}
Poll::Ready(result)
}
}
/// A type level flag used to ensure callers specify the kind of metric to record for
/// [`MetricsFutureExt`].
///
/// For example, `WallTimeFuture<F, M>` only implements [`Future`] for `M` that implements
/// `DurationMetric` which [`UnspecifiedMetric`] does not. This forces users at build time to
/// call [`WallTimeFuture::observe`] or [`WallTimeFuture::inc_by`].
#[derive(Debug)]
pub struct UnspecifiedMetric(());
/// A trait makes recording a duration generic over different prometheus metrics. This allows us to
/// de-dupe the implemenation of [`Future`] for our wrapper Futures like [`WallTimeFuture`] and
/// [`ExecTimeFuture`] over different kinds of prometheus metrics.
trait DurationMetric {
fn record(&mut self, seconds: f64);
}
impl DurationMetric for prometheus::Histogram {
fn record(&mut self, seconds: f64) {
self.observe(seconds)
}
}
impl DurationMetric for prometheus::Counter {
fn record(&mut self, seconds: f64) {
self.inc_by(seconds)
}
}
// An implementation of `DurationMetric` that lets the user take the recorded
// value and use it elsewhere.
impl DurationMetric for &'_ mut f64 {
fn record(&mut self, seconds: f64) {
**self = seconds;
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use prometheus::{CounterVec, HistogramVec};
use crate::stats::histogram_seconds_buckets;
use super::{MetricsFutureExt, MetricsRegistry};
struct Metrics {
pub wall_time_hist: HistogramVec,
pub wall_time_cnt: CounterVec,
pub exec_time_hist: HistogramVec,
pub exec_time_cnt: CounterVec,
}
impl Metrics {
pub fn register_into(registry: &MetricsRegistry) -> Self {
Self {
wall_time_hist: registry.register(metric!(
name: "wall_time_hist",
help: "help",
var_labels: ["action"],
buckets: histogram_seconds_buckets(0.000_128, 8.0),
)),
wall_time_cnt: registry.register(metric!(
name: "wall_time_cnt",
help: "help",
var_labels: ["action"],
)),
exec_time_hist: registry.register(metric!(
name: "exec_time_hist",
help: "help",
var_labels: ["action"],
buckets: histogram_seconds_buckets(0.000_128, 8.0),
)),
exec_time_cnt: registry.register(metric!(
name: "exec_time_cnt",
help: "help",
var_labels: ["action"],
)),
}
}
}
#[crate::test]
#[cfg_attr(miri, ignore)] // unsupported operation: integer-to-pointer casts and `ptr::from_exposed_addr` are not supported with `-Zmiri-strict-provenance`
fn smoke_test_metrics_future_ext() {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.expect("failed to start runtime");
let registry = MetricsRegistry::new();
let metrics = Metrics::register_into(®istry);
// Record the walltime and execution time of an async sleep.
let async_sleep_future = async {
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
};
runtime.block_on(
async_sleep_future
.wall_time()
.observe(metrics.wall_time_hist.with_label_values(&["async_sleep_w"]))
.exec_time()
.observe(metrics.exec_time_hist.with_label_values(&["async_sleep_e"])),
);
let reports = registry.gather();
let exec_family = reports
.iter()
.find(|m| m.get_name() == "exec_time_hist")
.expect("metric not found");
let exec_metric = exec_family.get_metric();
assert_eq!(exec_metric.len(), 1);
assert_eq!(exec_metric[0].get_label()[0].get_value(), "async_sleep_e");
let exec_histogram = exec_metric[0].get_histogram();
assert_eq!(exec_histogram.get_sample_count(), 1);
// The 4th bucket is 1ms, which we should complete faster than, but is still much quicker
// than the 1 second we slept for.
assert_eq!(exec_histogram.get_bucket()[3].get_cumulative_count(), 1);
let wall_family = reports
.iter()
.find(|m| m.get_name() == "wall_time_hist")
.expect("metric not found");
let wall_metric = wall_family.get_metric();
assert_eq!(wall_metric.len(), 1);
assert_eq!(wall_metric[0].get_label()[0].get_value(), "async_sleep_w");
let wall_histogram = wall_metric[0].get_histogram();
assert_eq!(wall_histogram.get_sample_count(), 1);
// The 13th bucket is 512ms, which the wall time should be longer than, but is also much
// faster than the actual execution time of the async sleep.
assert_eq!(wall_histogram.get_bucket()[12].get_cumulative_count(), 0);
// Reset the registery to make collecting metrics easier.
let registry = MetricsRegistry::new();
let metrics = Metrics::register_into(®istry);
// Record the walltime and execution time of a thread sleep.
let thread_sleep_future = async {
std::thread::sleep(std::time::Duration::from_secs(1));
};
runtime.block_on(
thread_sleep_future
.wall_time()
.with_filter(|duration| duration < Duration::from_millis(10))
.inc_by(metrics.wall_time_cnt.with_label_values(&["thread_sleep_w"]))
.exec_time()
.inc_by(metrics.exec_time_cnt.with_label_values(&["thread_sleep_e"])),
);
let reports = registry.gather();
let exec_family = reports
.iter()
.find(|m| m.get_name() == "exec_time_cnt")
.expect("metric not found");
let exec_metric = exec_family.get_metric();
assert_eq!(exec_metric.len(), 1);
assert_eq!(exec_metric[0].get_label()[0].get_value(), "thread_sleep_e");
let exec_counter = exec_metric[0].get_counter();
// Since we're synchronously sleeping the execution time will be long.
assert!(exec_counter.get_value() >= 1.0);
let wall_family = reports
.iter()
.find(|m| m.get_name() == "wall_time_cnt")
.expect("metric not found");
let wall_metric = wall_family.get_metric();
assert_eq!(wall_metric.len(), 1);
let wall_counter = wall_metric[0].get_counter();
// We filtered wall time to < 10ms, so our wall time metric should be filtered out.
assert_eq!(wall_counter.get_value(), 0.0);
}
}