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
//! # Prometheus Metrics Macro
//!
//! This crate provides procedural macros for generating Prometheus metrics with automatic
//! registration. It uses syn-based AST parsing for robust and reliable
//! code generation.
//!
//! ## Features
//!
//! - Automatic metric registration with global registry
//! - Support for labeled and unlabeled metrics
//! - Type-safe label structs
//! - Compile-time metric validation
//!
//! ## Example
//!
//! ```rust,ignore
//! use metrics_macro::prometheus_metrics;
//! use prometheus_client::metrics::counter::Counter;
//! use prometheus_client::metrics::gauge::Gauge;
//!
//! prometheus_metrics! {
//! /// Total HTTP requests received
//! #[labels(method = String, status = u16)]
//! static http_requests_total: Counter;
//!
//! /// Current number of active connections
//! static active_connections: Gauge;
//! }
//! ```
extern crate prometheus_client;
use TokenStream;
use quote;
use parse_macro_input;
use generate_metric_code;
use ;
/// Define Prometheus metric families with automatic registration.
///
/// This macro generates:
/// - Static metric instances with proper types
/// - Label structs for metrics with labels
/// - Automatic registration with a global registry
///
/// # Syntax
///
/// ```rust,ignore
/// use metrics_macro::prometheus_metrics;
/// use prometheus_client::metrics::counter::Counter;
///
/// prometheus_metrics! {
/// /// Documentation comment for the metric
/// #[labels(label_name = String)] // Optional labels
/// static metric_name: Counter;
/// }
/// ```
///
/// # Supported Metric Types
///
/// - `Counter` - Monotonically increasing counter
/// - `Gauge` - Value that can go up and down
///
/// # Examples
///
/// ## Simple Counter
/// ```rust,ignore
/// use metrics_macro::prometheus_metrics;
/// use prometheus_client::metrics::counter::Counter;
///
/// prometheus_metrics! {
/// /// Total number of requests processed
/// static requests_total: Counter;
/// }
/// ```
///
/// ## Labeled Gauge
/// ```rust,ignore
/// use metrics_macro::prometheus_metrics;
/// use prometheus_client::metrics::gauge::Gauge;
///
/// prometheus_metrics! {
/// /// Current memory usage by component
/// #[labels(component = String, unit = String)]
/// static memory_usage: Gauge;
/// }
/// ```
/// Generate registration code for metrics.
///
/// This macro should be called with a list of metric static variables
/// that should be registered with the global registry.
///
/// # Example
///
/// ```rust,ignore
/// use metrics::{prometheus_metrics, register_metrics, GLOBAL_REGISTRY};
///
/// prometheus_metrics! {
/// static COUNTER: Counter;
/// #[labels(method = String)]
/// static HTTP_REQUESTS: Counter;
/// }
///
/// register_metrics!(COUNTER, HTTP_REQUESTS);
/// ```