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
//! # Aetos
//!
//! A Rust proc macro library for generating Prometheus metrics rendering code from annotated structs.
//!
//! ## Quick Start
//!
//! ```
//! use aetos::{metrics, Label};
//! use std::collections::HashMap;
//!
//! #[derive(Label)]
//! struct RequestLabels<'a> {
//! method: &'a str,
//! status: u32,
//! }
//!
//! #[metrics(prefix = "myapp")] // Optional prefix for all metrics
//! struct MyMetrics<'a> {
//! // Scalar metric, no labels
//! #[counter(name = "requests_total", help = "Total requests")] // name is optional
//! requests: u64,
//!
//! // HashMap metric, single label shorthand
//! #[counter(help = "Events by type", label = "event_type")]
//! events: HashMap<String, u64>,
//!
//! // Vec metric, multiple labels requires a Label type
//! #[counter(help = "HTTP requests by method and status")]
//! http_requests: Vec<(RequestLabels<'a>, u64)>,
//! }
//!
//! let metrics = MyMetrics {
//! requests: 1000,
//! events: HashMap::from([
//! ("add".to_string(), 10),
//! ("remove".to_string(), 5),
//! ]),
//! http_requests: vec![
//! (RequestLabels { method: "GET", status: 200 }, 150),
//! (RequestLabels { method: "POST", status: 404 }, 3),
//! ],
//! };
//!
//! println!("{}", metrics);
//! ```
//!
//! This outputs:
//! ```text
//! # HELP myapp_requests_total Total requests
//! # TYPE myapp_requests_total counter
//! myapp_requests_total 1000
//! # HELP myapp_events Events by type
//! # TYPE myapp_events counter
//! myapp_events{event_type="add"} 10
//! myapp_events{event_type="remove"} 5
//! # HELP myapp_http_requests HTTP requests by method and status
//! # TYPE myapp_http_requests counter
//! myapp_http_requests{method="GET",status="200"} 150
//! myapp_http_requests{method="POST",status="404"} 3
//! ```
//!
//! ## Collection Types
//!
//! Labeled metrics accept anything that implements `IntoIterator<&(K, V)>` or `IntoIterator<(&K, &V)>` (Vec, HashMap, BTreeMap, slices, etc.).
//!
//! - Single label: `K` implements `Display`
//! - Multiple labels: `K` implements `Label`
//!
//! ## Override Metric Names
//!
//! Use the `name` attribute to export a different metric name than the field name (see Quick Start example).
//!
//! ## Histograms
//!
//! Histograms track value distributions across predefined buckets:
//!
//! ```
//! use aetos::{define_histogram, metrics, Label};
//!
//! #[derive(Label, Hash, Eq, PartialEq, Clone, Debug)]
//! struct ResponseLabel {
//! endpoint: &'static str,
//! }
//!
//! // Labeled histogram
//! define_histogram!(Latency<ResponseLabel> = [0.1, 0.5, 1.0, 5.0]);
//!
//! // Unlabeled histogram
//! define_histogram!(QueueTime<()> = [0.01, 0.1, 1.0]);
//!
//! #[metrics]
//! struct Metrics {
//! #[histogram(help = "Response time by endpoint")]
//! response_time: Latency,
//!
//! #[histogram(help = "Queue wait time")]
//! queue_time: QueueTime,
//! }
//!
//! let mut m = Metrics {
//! response_time: Latency::default(),
//! queue_time: QueueTime::default(),
//! };
//!
//! m.response_time.observe(ResponseLabel { endpoint: "/api" }, 0.25);
//! m.queue_time.observe((), 0.05);
//!
//! println!("{}", m);
//! ```
//! If you don't want to manually specify buckets, you can use these functions to
//! generate them
//!
//! ```
//! use aetos::{define_histogram, linear_buckets};
//!
//! define_histogram!(RequestLatency<()> = linear_buckets::<10>(0.1, 0.1));
//! // Generates buckets: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
//! ```
//!
//! ```
//! use aetos::{define_histogram, exponential_buckets};
//!
//! define_histogram!(ResponseSize<()> = exponential_buckets::<8>(0.001, 2.0));
//! // Generates buckets: [0.001, 0.002, 0.004, 0.008, 0.016, 0.032, 0.064, 0.128]
//! ```
pub use ;
pub use aetos_core as core;
pub use ;
/// Defines a histogram type with compile-time validated bucket boundaries.
///
/// This macro creates a newtype wrapper around `Histogram<L, N>` with specific bucket
/// boundaries. The bucket values are validated at compile time to ensure they are in
/// strictly ascending order.
///
/// # Syntax
///
/// ```text
/// define_histogram!(HistogramName<LabelType> = [bucket1, bucket2, ...]);
/// ```
///
/// # Examples
///
/// Define a histogram with custom labels:
/// ```
/// use aetos::{define_histogram, Label};
///
/// #[derive(Label, Hash, Eq, PartialEq, Clone, Debug)]
/// struct HttpLabel {
/// method: &'static str,
/// status: u16,
/// }
///
/// define_histogram!(HttpLatency<HttpLabel> = [0.05, 0.1, 0.5, 1.0, 5.0]);
/// ```
///
/// Define an unlabeled histogram:
/// ```
/// use aetos::define_histogram;
///
/// define_histogram!(ResponseTime<()> = [0.1, 0.5, 1.0]);
/// ```
///
/// Invalid bucket ordering fails at compile time:
/// ```compile_fail
/// use aetos::define_histogram;
///
/// // This will fail because buckets are not in ascending order
/// define_histogram!(Bad<()> = [1.0, 0.5, 2.0]);
/// ```
///
/// Histogram labels come from the type parameter, not the `label` attribute:
/// ```compile_fail
/// use aetos::{define_histogram, metrics};
///
/// define_histogram!(ResponseTime<()> = [0.1, 0.5, 1.0]);
///
/// #[metrics]
/// struct Metrics {
/// // This will fail - histograms don't support 'label' attribute
/// #[histogram(help = "Response time", label = "endpoint")]
/// response_time: ResponseTime,
/// }
/// ```
}
};
}