-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathmod.rs
More file actions
95 lines (87 loc) · 2.94 KB
/
Copy pathmod.rs
File metadata and controls
95 lines (87 loc) · 2.94 KB
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
#[cfg(feature = "api")]
pub mod api;
#[cfg(feature = "api")]
pub mod l2;
#[cfg(any(feature = "api", feature = "metrics"))]
pub mod metrics_blocks;
#[cfg(any(feature = "api", feature = "metrics"))]
pub mod metrics_process;
#[cfg(any(feature = "api", feature = "transactions"))]
pub mod metrics_transactions;
#[cfg(feature = "api")]
pub mod profiling;
#[cfg(feature = "api")]
pub mod rpc;
/// A macro to conditionally enable metrics-related code.
///
/// This macro wraps the provided code block with a `#[cfg(feature = "metrics")]` attribute.
/// The enclosed code will only be compiled and executed if the `metrics` feature is enabled in the
/// `Cargo.toml` file.
///
/// ## Usage
///
/// If the `metrics` feature is enabled, the code inside the macro will be executed. Otherwise,
/// it will be excluded from the compilation. This is useful for enabling/disabling metrics collection
/// code without cluttering the source with manual feature conditionals.
///
/// ## In `Cargo.toml`
/// The `metrics` feature has to be set in the Cargo.toml of the crate we desire to take metrics from.
///
/// To enable the `metrics` feature, add the following to your `Cargo.toml`:
/// ```toml
/// ethrex-metrics = { path = "./metrics", default-features = false }
/// [features]
/// metrics = ["ethrex-metrics/transactions"]
/// ```
///
/// In this way, when the `metrics` feature is enabled for that crate, the macro is triggered and the metrics_api is also used.
///
/// Example In Code:
/// ```sh
/// use ethrex_metrics::metrics;
// #[cfg(feature = "metrics")]
// use ethrex_metrics::metrics_transactions::{METRICS_TX};
///
/// metrics!(METRICS_TX.inc());
/// ```
///
/// If you build without the `metrics` feature, the code inside `metrics!` will not be compiled, nor will the Prometheus crate.
#[macro_export]
macro_rules! metrics {
($($code:tt)*) => {
#[cfg(feature = "metrics")]
{
$($code)*
}
};
}
#[derive(Debug, thiserror::Error)]
pub enum MetricsApiError {
#[error("{0}")]
TcpError(#[from] std::io::Error),
}
#[derive(Debug, thiserror::Error)]
pub enum MetricsError {
#[error("MetricsL2Error: {0}")]
PrometheusErr(String),
#[error("MetricsL2Error {0}")]
TryInto(#[from] std::num::TryFromIntError),
#[error("MetricsL2Error {0}")]
FromUtf8Error(#[from] std::string::FromUtf8Error),
}
#[cfg(feature = "api")]
/// Returns all metrics currently registered in Prometheus' default registry.
///
/// Both profiling and RPC metrics register with this default registry, and the
/// metrics API surfaces them by calling this helper.
pub fn gather_default_metrics() -> Result<String, MetricsError> {
use prometheus::{Encoder, TextEncoder};
let encoder = TextEncoder::new();
let metric_families = prometheus::gather();
let mut buffer = Vec::new();
encoder
.encode(&metric_families, &mut buffer)
.map_err(|e| MetricsError::PrometheusErr(e.to_string()))?;
let res = String::from_utf8(buffer)?;
Ok(res)
}