penumbra_sdk_dex/component/
metrics.rs

1//! Crate-specific metrics functionality.
2//!
3//! This module re-exports the contents of the `metrics` crate.  This is
4//! effectively a way to monkey-patch the functions in this module into the
5//! `metrics` crate, at least from the point of view of the other code in this
6//! crate.
7//!
8//! Code in this crate that wants to use metrics should `use crate::metrics;`,
9//! so that this module shadows the `metrics` crate.
10//!
11//! This trick is probably good to avoid in general, because it could be
12//! confusing, but in this limited case, it seems like a clean option.
13
14pub use metrics::*;
15
16/// Registers all metrics used by this crate.
17pub fn register_metrics() {
18    describe_histogram!(
19        DEX_ARB_DURATION,
20        Unit::Seconds,
21        "The time spent computing arbitrage during endblock phase"
22    );
23    describe_histogram!(
24        DEX_BATCH_DURATION,
25        Unit::Seconds,
26        "The time spent executing batches within the DEX"
27    );
28    describe_histogram!(
29        DEX_PATH_SEARCH_DURATION,
30        Unit::Seconds,
31        "The time spent searching for paths while executing trades within the DEX"
32    );
33    describe_counter!(
34        DEX_PATH_SEARCH_RELAX_PATH_DURATION,
35        Unit::Seconds,
36        "The time spent relaxing a path while routing trades within the DEX"
37    );
38    describe_histogram!(
39        DEX_ROUTE_FILL_DURATION,
40        Unit::Seconds,
41        "The time spent filling routes while executing trades within the DEX"
42    );
43    describe_histogram!(
44        DEX_RPC_SIMULATE_TRADE_DURATION,
45        Unit::Seconds,
46        "The time spent processing a SimulateTrade RPC request"
47    );
48}
49
50// We configure buckets for the DEX routing times manually, in order to ensure
51// Prometheus metrics are structured as a Histogram, rather than as a Summary.
52// These values may need to be updated over time.
53// These values are logarithmically spaced from 5us to 67ms.
54const GENERIC_DEX_BUCKETS: &[f64; 16] = &[
55    0.0005,
56    0.000792,
57    0.001256,
58    0.001991,
59    0.003155,
60    0.005,
61    0.00648985018,
62    0.00842363108,
63    0.01093362074,
64    0.01419151211,
65    0.01842015749,
66    0.02390881249,
67    0.03103292223,
68    0.0402798032,
69    0.05228197763,
70    0.06786044041,
71];
72
73pub const DEX_PATH_SEARCH_DURATION: &str = "penumbra_dex_path_search_duration_seconds";
74pub const DEX_PATH_SEARCH_RELAX_PATH_DURATION: &str =
75    "penumbra_dex_path_search_relax_path_duration_seconds";
76pub const DEX_ROUTE_FILL_DURATION: &str = "penumbra_dex_route_fill_duration_seconds";
77pub const DEX_ARB_DURATION: &str = "penumbra_dex_arb_duration_seconds";
78pub const DEX_BATCH_DURATION: &str = "penumbra_dex_batch_duration_seconds";
79pub const DEX_RPC_SIMULATE_TRADE_DURATION: &str =
80    "penumbra_dex_rpc_simulate_trade_duration_seconds";
81
82/// An extension trait providing DEX-related interfaces for [`PrometheusBuilder`].
83///
84/// [builder]: metrics_exporter_prometheus::PrometheusBuilder
85pub trait PrometheusBuilderExt
86where
87    Self: Sized,
88{
89    /// Configure buckets for histogram metrics.
90    fn set_buckets_for_dex_metrics(self) -> Result<Self, metrics_exporter_prometheus::BuildError>;
91}
92
93impl PrometheusBuilderExt for metrics_exporter_prometheus::PrometheusBuilder {
94    fn set_buckets_for_dex_metrics(self) -> Result<Self, metrics_exporter_prometheus::BuildError> {
95        use metrics_exporter_prometheus::Matcher::Full;
96        self.set_buckets_for_metric(
97            Full(DEX_PATH_SEARCH_DURATION.to_owned()),
98            GENERIC_DEX_BUCKETS,
99        )?
100        .set_buckets_for_metric(
101            Full(DEX_PATH_SEARCH_RELAX_PATH_DURATION.to_owned()),
102            GENERIC_DEX_BUCKETS,
103        )?
104        .set_buckets_for_metric(
105            Full(DEX_ROUTE_FILL_DURATION.to_owned()),
106            GENERIC_DEX_BUCKETS,
107        )?
108        .set_buckets_for_metric(Full(DEX_ARB_DURATION.to_owned()), GENERIC_DEX_BUCKETS)?
109        .set_buckets_for_metric(Full(DEX_BATCH_DURATION.to_owned()), GENERIC_DEX_BUCKETS)?
110        .set_buckets_for_metric(
111            Full(DEX_RPC_SIMULATE_TRADE_DURATION.to_owned()),
112            GENERIC_DEX_BUCKETS,
113        )
114    }
115}