aetos 0.1.1

A Rust proc macro library for generating Prometheus metrics rendering code from annotated structs
Documentation
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
use aetos::core::PrometheusMetric;
use aetos::{Label, define_histogram, metrics};

#[test]
fn test_scalar_counter() {
    #[metrics]
    struct TestMetrics {
        #[counter(help = "Total requests")]
        requests: u64,
    }

    let m = TestMetrics { requests: 42 };
    let output = m.to_string();

    assert!(output.contains("# HELP requests Total requests\n"));
    assert!(output.contains("# TYPE requests counter\n"));
    assert!(output.contains("requests 42\n"));
}

#[test]
fn test_scalar_gauge() {
    #[metrics]
    struct TestMetrics {
        #[gauge(help = "Current temperature")]
        temperature: f64,
    }

    let m = TestMetrics { temperature: 23.5 };
    let output = m.to_string();

    assert!(output.contains("# HELP temperature Current temperature\n"));
    assert!(output.contains("# TYPE temperature gauge\n"));
    assert!(output.contains("temperature 23.5\n"));
}

#[test]
fn test_with_prefix() {
    #[metrics(prefix = "myapp")]
    struct TestMetrics {
        #[counter(help = "Test counter")]
        count: u64,
    }

    let m = TestMetrics { count: 100 };
    let output = m.to_string();

    assert!(output.contains("# HELP myapp_count Test counter\n"));
    assert!(output.contains("# TYPE myapp_count counter\n"));
    assert!(output.contains("myapp_count 100\n"));
}

#[test]
fn test_name_override() {
    #[metrics]
    struct TestMetrics {
        #[counter(help = "Custom name", name = "custom_metric_name")]
        field: u64,
    }

    let m = TestMetrics { field: 77 };
    let output = m.to_string();

    assert!(output.contains("# HELP custom_metric_name Custom name\n"));
    assert!(output.contains("# TYPE custom_metric_name counter\n"));
    assert!(output.contains("custom_metric_name 77\n"));
}

#[test]
fn test_single_label_with_explicit_name() {
    #[metrics]
    struct TestMetrics {
        #[counter(help = "Events by type", label = "event_type")]
        events: Vec<(String, u64)>,
    }

    let m = TestMetrics {
        events: vec![("stake".to_string(), 10), ("unstake".to_string(), 5)],
    };
    let output = m.to_string();

    assert!(output.contains("# HELP events Events by type\n"));
    assert!(output.contains("# TYPE events counter\n"));
    assert!(output.contains(r#"events{event_type="stake"} 10"#));
    assert!(output.contains(r#"events{event_type="unstake"} 5"#));
}

#[test]
fn test_single_label_derived_name() {
    #[derive(Debug)]
    enum HttpMethod {
        Get,
        Post,
    }

    impl std::fmt::Display for HttpMethod {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "{:?}", self)
        }
    }

    #[metrics]
    struct TestMetrics {
        #[counter(help = "Requests by method", label = "http_method")]
        requests: Vec<(HttpMethod, u64)>,
    }

    let m = TestMetrics {
        requests: vec![(HttpMethod::Get, 100), (HttpMethod::Post, 50)],
    };
    let output = m.to_string();

    assert!(output.contains("# HELP requests Requests by method\n"));
    assert!(output.contains("# TYPE requests counter\n"));
    assert!(output.contains(r#"requests{http_method="Get"} 100"#));
    assert!(output.contains(r#"requests{http_method="Post"} 50"#));
}

#[test]
fn test_multi_label() {
    #[derive(Label)]
    struct RequestLabels {
        method: String,
        status: u32,
    }

    #[metrics]
    struct TestMetrics {
        #[counter(help = "HTTP requests")]
        http_requests: Vec<(RequestLabels, u64)>,
    }

    let m = TestMetrics {
        http_requests: vec![
            (
                RequestLabels {
                    method: "GET".to_string(),
                    status: 200,
                },
                150,
            ),
            (
                RequestLabels {
                    method: "POST".to_string(),
                    status: 404,
                },
                3,
            ),
        ],
    };
    let output = m.to_string();

    assert!(output.contains("# HELP http_requests HTTP requests\n"));
    assert!(output.contains("# TYPE http_requests counter\n"));
    assert!(output.contains(r#"http_requests{method="GET",status="200"} 150"#));
    assert!(output.contains(r#"http_requests{method="POST",status="404"} 3"#));
}

#[test]
fn test_mixed_fields() {
    #[derive(Label)]
    struct Labels {
        region: String,
    }

    #[metrics(prefix = "app")]
    struct TestMetrics {
        #[counter(help = "Total count")]
        total: u64,

        #[gauge(help = "Current value")]
        current: f64,

        #[counter(help = "Events by type", label = "type")]
        events: Vec<(String, u64)>,

        #[counter(help = "Requests by region")]
        requests: Vec<(Labels, u64)>,
    }

    let m = TestMetrics {
        total: 1000,
        current: 42.5,
        events: vec![("error".to_string(), 5)],
        requests: vec![(
            Labels {
                region: "us-east".to_string(),
            },
            200,
        )],
    };

    let output = m.to_string();

    assert!(output.contains("# HELP app_total Total count\n"));
    assert!(output.contains("# TYPE app_total counter\n"));
    assert!(output.contains("app_total 1000\n"));

    assert!(output.contains("# HELP app_current Current value\n"));
    assert!(output.contains("# TYPE app_current gauge\n"));
    assert!(output.contains("app_current 42.5\n"));

    assert!(output.contains("# HELP app_events Events by type\n"));
    assert!(output.contains(r#"app_events{type="error"} 5"#));

    assert!(output.contains("# HELP app_requests Requests by region\n"));
    assert!(output.contains(r#"app_requests{region="us-east"} 200"#));
}

#[test]
#[cfg(not(feature = "no-escaping"))]
fn test_label_value_escaping_in_metrics() {
    #[metrics]
    struct TestMetrics {
        #[counter(help = "Test", label = "value")]
        test: Vec<(String, u64)>,
    }

    let m = TestMetrics {
        test: vec![(r#"has"quotes"#.to_string(), 1)],
    };

    let output = m.to_string();
    assert!(output.contains(r#"test{value="has\"quotes"} 1"#));
}

#[test]
fn test_prometheus_metric_trait() {
    #[metrics]
    struct TestMetrics {
        #[counter(help = "Test")]
        count: u64,
    }

    let m = TestMetrics { count: 42 };

    fn accepts_prometheus_metric<T: PrometheusMetric>(_: &T) {}
    accepts_prometheus_metric(&m);
}

#[test]
fn test_borrowed_str_labels() {
    #[metrics]
    struct TestMetrics<'a> {
        #[counter(help = "Requests by endpoint", label = "endpoint")]
        requests_by_endpoint: Vec<(&'a str, u64)>,
    }

    let m = TestMetrics {
        requests_by_endpoint: vec![("/api/users", 100), ("/api/posts", 50), ("/health", 25)],
    };

    let output = m.to_string();

    assert!(output.contains("# HELP requests_by_endpoint Requests by endpoint\n"));
    assert!(output.contains("# TYPE requests_by_endpoint counter\n"));
    assert!(output.contains(r#"requests_by_endpoint{endpoint="/api/users"} 100"#));
    assert!(output.contains(r#"requests_by_endpoint{endpoint="/api/posts"} 50"#));
    assert!(output.contains(r#"requests_by_endpoint{endpoint="/health"} 25"#));
}

#[test]
fn test_hashmap_single_label() {
    use std::collections::HashMap;

    #[metrics]
    struct TestMetrics {
        #[counter(help = "Requests by method", label = "method")]
        requests: HashMap<String, u64>,
    }

    let mut requests = HashMap::new();
    requests.insert("GET".to_string(), 100);
    requests.insert("POST".to_string(), 50);

    let m = TestMetrics { requests };
    let output = m.to_string();

    assert!(output.contains("# HELP requests Requests by method\n"));
    assert!(output.contains("# TYPE requests counter\n"));
    assert!(output.contains(r#"requests{method="GET"} 100"#));
    assert!(output.contains(r#"requests{method="POST"} 50"#));
}

#[test]
fn test_hashmap_multi_label() {
    use std::collections::HashMap;

    #[derive(Label, Clone, PartialEq, Eq, Hash)]
    struct HttpLabels {
        method: String,
        status: u16,
    }

    #[metrics]
    struct TestMetrics {
        #[counter(help = "HTTP requests")]
        http_requests: HashMap<HttpLabels, u64>,
    }

    let mut http_requests = HashMap::new();
    http_requests.insert(
        HttpLabels {
            method: "GET".to_string(),
            status: 200,
        },
        150,
    );
    http_requests.insert(
        HttpLabels {
            method: "POST".to_string(),
            status: 404,
        },
        3,
    );

    let m = TestMetrics { http_requests };
    let output = m.to_string();

    assert!(output.contains("# HELP http_requests HTTP requests\n"));
    assert!(output.contains("# TYPE http_requests counter\n"));
    assert!(output.contains(r#"http_requests{method="GET",status="200"} 150"#));
    assert!(output.contains(r#"http_requests{method="POST",status="404"} 3"#));
}

#[test]
fn test_btreemap_single_label() {
    use std::collections::BTreeMap;

    #[metrics]
    struct TestMetrics {
        #[counter(help = "Events by type", label = "event_type")]
        events: BTreeMap<String, u64>,
    }

    let mut events = BTreeMap::new();
    events.insert("stake".to_string(), 10);
    events.insert("unstake".to_string(), 5);

    let m = TestMetrics { events };
    let output = m.to_string();

    assert!(output.contains("# HELP events Events by type\n"));
    assert!(output.contains("# TYPE events counter\n"));
    assert!(output.contains(r#"events{event_type="stake"} 10"#));
    assert!(output.contains(r#"events{event_type="unstake"} 5"#));
}

#[test]
fn test_btreemap_multi_label() {
    use std::collections::BTreeMap;

    #[derive(Label, Clone, PartialEq, Eq, PartialOrd, Ord)]
    struct MetricLabels {
        region: String,
        zone: String,
    }

    #[metrics]
    struct TestMetrics {
        #[counter(help = "Requests by region and zone")]
        requests: BTreeMap<MetricLabels, u64>,
    }

    let mut requests = BTreeMap::new();
    requests.insert(
        MetricLabels {
            region: "us-east".to_string(),
            zone: "1a".to_string(),
        },
        200,
    );
    requests.insert(
        MetricLabels {
            region: "us-west".to_string(),
            zone: "2b".to_string(),
        },
        150,
    );

    let m = TestMetrics { requests };
    let output = m.to_string();

    assert!(output.contains("# HELP requests Requests by region and zone\n"));
    assert!(output.contains("# TYPE requests counter\n"));
    assert!(output.contains(r#"requests{region="us-east",zone="1a"} 200"#));
    assert!(output.contains(r#"requests{region="us-west",zone="2b"} 150"#));
}

#[test]
fn test_histogram_with_labels() {
    #[derive(Label, Hash, Eq, PartialEq, Clone, Debug)]
    struct RequestLabel {
        method: &'static str,
        status: u16,
    }

    define_histogram!(RequestLatency<RequestLabel> = [0.1, 0.5, 1.0]);

    #[metrics]
    struct TestMetrics {
        #[histogram(help = "Request latency in seconds")]
        latency: RequestLatency,
    }

    let mut m = TestMetrics {
        latency: RequestLatency::default(),
    };

    m.latency.observe(
        RequestLabel {
            method: "GET",
            status: 200,
        },
        0.25,
    );
    m.latency.observe(
        RequestLabel {
            method: "GET",
            status: 200,
        },
        0.75,
    );
    m.latency.observe(
        RequestLabel {
            method: "POST",
            status: 201,
        },
        0.15,
    );

    let output = m.to_string();

    assert!(output.contains("# HELP latency Request latency in seconds\n"));
    assert!(output.contains("# TYPE latency histogram\n"));

    // GET 200 observations (0.25 and 0.75)
    assert!(output.contains(r#"latency_bucket{method="GET",status="200",le="0.100"} 0"#));
    assert!(output.contains(r#"latency_bucket{method="GET",status="200",le="0.500"} 1"#));
    assert!(output.contains(r#"latency_bucket{method="GET",status="200",le="1.000"} 2"#));
    assert!(output.contains(r#"latency_bucket{method="GET",status="200",le="+Inf"} 2"#));
    assert!(output.contains(r#"latency_sum{method="GET",status="200"} 1"#));
    assert!(output.contains(r#"latency_count{method="GET",status="200"} 2"#));

    // POST 201 observation (0.15)
    assert!(output.contains(r#"latency_bucket{method="POST",status="201",le="0.100"} 0"#));
    assert!(output.contains(r#"latency_bucket{method="POST",status="201",le="0.500"} 1"#));
    assert!(output.contains(r#"latency_bucket{method="POST",status="201",le="1.000"} 1"#));
    assert!(output.contains(r#"latency_bucket{method="POST",status="201",le="+Inf"} 1"#));
    assert!(output.contains(r#"latency_sum{method="POST",status="201"} 0.15"#));
    assert!(output.contains(r#"latency_count{method="POST",status="201"} 1"#));
}

#[test]
fn test_histogram_unlabeled() {
    define_histogram!(ResponseTime<()> = [0.05, 0.1, 0.5]);

    #[metrics]
    struct TestMetrics {
        #[histogram(help = "Response time distribution")]
        response_time: ResponseTime,
    }

    let mut m = TestMetrics {
        response_time: ResponseTime::default(),
    };

    m.response_time.observe((), 0.03);
    m.response_time.observe((), 0.08);
    m.response_time.observe((), 0.45);

    let output = m.to_string();

    assert!(output.contains("# HELP response_time Response time distribution\n"));
    assert!(output.contains("# TYPE response_time histogram\n"));

    // Check bucket counts (cumulative)
    assert!(output.contains(r#"response_time_bucket{le="0.050"} 1"#));
    assert!(output.contains(r#"response_time_bucket{le="0.100"} 2"#));
    assert!(output.contains(r#"response_time_bucket{le="0.500"} 3"#));
    assert!(output.contains(r#"response_time_bucket{le="+Inf"} 3"#));

    // Check sum and count
    assert!(output.contains(r#"response_time_sum{} 0.56"#));
    assert!(output.contains(r#"response_time_count{} 3"#));
}