hyperlane_plugin_metrics/struct.rs
1use super::*;
2
3/// Lock-free monotonically increasing 64-bit counter metric.
4#[derive(Debug, Default)]
5pub struct Counter {
6 /// Atomic counter value.
7 pub(crate) value: AtomicU64,
8}
9
10/// Lock-free signed 64-bit gauge metric that may increment or decrement.
11#[derive(Debug, Default)]
12pub struct Gauge {
13 /// Atomic gauge value.
14 pub(crate) value: AtomicI64,
15}
16
17/// Lock-free fixed-bucket latency/size histogram metric.
18#[derive(Debug)]
19pub struct Histogram {
20 /// Sorted finite upper bucket bounds.
21 pub(crate) bounds: Box<[f64]>,
22 /// Pre-formatted bound text slices matching `bounds` for fast Prometheus rendering.
23 pub(crate) bound_labels: Box<[String]>,
24 /// Per-bucket observation counts (`bounds.len() + 1` including `+Inf`).
25 pub(crate) bucket_counts: Box<[AtomicU64]>,
26 /// Total count of all observations.
27 pub(crate) total_count: AtomicU64,
28 /// IEEE-754 `f64::to_bits` cumulative sum of observed values.
29 pub(crate) sum_bits: AtomicU64,
30}
31
32/// Registered custom counter entry with prebuilt Prometheus header text.
33pub(crate) struct RegisteredCounter {
34 /// Pre-formatted `# HELP ... # TYPE ... counter\n<name> ` prefix.
35 pub(crate) prom_prefix: String,
36 /// Lock-free atomic counter handle.
37 pub(crate) metric: Arc<Counter>,
38}
39
40/// Registered custom gauge entry with prebuilt Prometheus header text.
41pub(crate) struct RegisteredGauge {
42 /// Pre-formatted `# HELP ... # TYPE ... gauge\n<name> ` prefix.
43 pub(crate) prom_prefix: String,
44 /// Lock-free atomic gauge handle.
45 pub(crate) metric: Arc<Gauge>,
46}
47
48/// Registered custom histogram entry with pre-formatted Prometheus line prefixes.
49pub(crate) struct RegisteredHistogram {
50 /// Pre-formatted `# HELP ... # TYPE ... histogram\n` header.
51 pub(crate) prom_header: String,
52 /// Pre-formatted `<name>_bucket{le="<bound>"} ` prefix for each finite bucket.
53 pub(crate) prom_bucket_prefixes: Box<[String]>,
54 /// Pre-formatted `<name>_bucket{le="+Inf"} ` prefix.
55 pub(crate) prom_inf_prefix: String,
56 /// Pre-formatted `<name>_sum ` prefix.
57 pub(crate) prom_sum_prefix: String,
58 /// Pre-formatted `<name>_count ` prefix.
59 pub(crate) prom_count_prefix: String,
60 /// Lock-free atomic histogram handle.
61 pub(crate) metric: Arc<Histogram>,
62}
63
64/// Configuration for [`MetricsPlugin`] and [`MetricsRequestHook`].
65#[derive(Clone, Data, Debug, Eq, PartialEq)]
66pub struct MetricsConfig {
67 /// HTTP path serving Prometheus text format 0.0.4 (default `"/metrics"`).
68 pub(crate) endpoint: String,
69 /// Metric name prefix namespace (default `"hyperlane"`).
70 pub(crate) namespace: String,
71 /// Optional bearer token required to scrape the metrics endpoint.
72 #[get(type(clone))]
73 pub(crate) bearer_token: Option<String>,
74}
75
76/// Shared interior state of [`MetricsRegistry`].
77pub(crate) struct MetricsRegistryInner {
78 /// Precomputed byte capacity estimate covering all built-in Prometheus lines.
79 pub(crate) builtin_est_bytes: usize,
80 /// Pre-formatted `# HELP` + `# TYPE` header for built-in HTTP request counter.
81 pub(crate) prom_req_header: String,
82 /// Pre-formatted line prefixes for each of the 6 status class counters.
83 pub(crate) prom_req_lines: [String; 6],
84 /// Pre-formatted `# HELP` + `# TYPE` + metric prefix for in-flight gauge.
85 pub(crate) prom_in_flight_prefix: String,
86 /// Pre-formatted `# HELP` + `# TYPE` + metric prefix for response bytes counter.
87 pub(crate) prom_resp_bytes_prefix: String,
88 /// Pre-formatted `# HELP` + `# TYPE` header for built-in latency histogram.
89 pub(crate) prom_dur_header: String,
90 /// Pre-formatted bucket line prefixes for each built-in latency bucket.
91 pub(crate) prom_dur_bucket_prefixes: Box<[String]>,
92 /// Pre-formatted `+Inf` bucket line prefix for built-in latency histogram.
93 pub(crate) prom_dur_inf_prefix: String,
94 /// Pre-formatted `_sum ` prefix for built-in latency histogram.
95 pub(crate) prom_dur_sum_prefix: String,
96 /// Pre-formatted `_count ` prefix for built-in latency histogram.
97 pub(crate) prom_dur_count_prefix: String,
98 /// Lock-free HTTP request counters partitioned by status class (`[1xx, 2xx, 3xx, 4xx, 5xx, other]`).
99 pub(crate) status_class_counts: [Counter; 6],
100 /// Lock-free gauge tracking active in-flight HTTP requests.
101 pub(crate) in_flight: Gauge,
102 /// Lock-free counter tracking total response payload bytes emitted.
103 pub(crate) response_bytes_total: Counter,
104 /// Lock-free HTTP request latency histogram in seconds.
105 pub(crate) duration_seconds: Histogram,
106 /// Lock-free count of registered custom counters (skips read lock when zero).
107 pub(crate) custom_counter_count: AtomicUsize,
108 /// Lock-free count of registered custom gauges (skips read lock when zero).
109 pub(crate) custom_gauge_count: AtomicUsize,
110 /// Lock-free count of registered custom histograms (skips read lock when zero).
111 pub(crate) custom_histogram_count: AtomicUsize,
112 /// Lock-free estimated output bytes for all registered custom metrics (eliminates reallocations).
113 pub(crate) custom_est_bytes: AtomicUsize,
114 /// Custom user-registered counters.
115 pub(crate) custom_counters: RwLock<HashMapXxHash3_64<String, RegisteredCounter>>,
116 /// Custom user-registered gauges.
117 pub(crate) custom_gauges: RwLock<HashMapXxHash3_64<String, RegisteredGauge>>,
118 /// Custom user-registered histograms.
119 pub(crate) custom_histograms: RwLock<HashMapXxHash3_64<String, RegisteredHistogram>>,
120}
121
122/// Lock-free HTTP and custom application metrics registry with Prometheus 0.0.4 exposition.
123#[derive(Clone)]
124pub struct MetricsRegistry {
125 /// Shared registry state.
126 pub(crate) inner: Arc<MetricsRegistryInner>,
127}
128
129/// Process-wide [`MetricsPlugin`] singleton storing [`MetricsConfig`] and [`MetricsRegistry`].
130#[derive(Clone)]
131pub struct MetricsPlugin {
132 /// Endpoint and scrape-auth configuration.
133 pub(crate) config: MetricsConfig,
134 /// Shared lock-free metrics registry.
135 pub(crate) registry: MetricsRegistry,
136}
137
138/// Opt-in request middleware incrementing in-flight gauges and serving `/metrics` scrapes.
139#[derive(Clone, Copy, Debug, Default)]
140#[hyperlane::request_middleware(-115)]
141pub struct MetricsRequestHook;
142
143/// Opt-in response middleware recording status class, response bytes, and request duration.
144#[derive(Clone, Copy, Debug, Default)]
145#[hyperlane::response_middleware(95)]
146pub struct MetricsResponseHook;