@@ -90,6 +90,93 @@ impl Counter {
90
90
}
91
91
}
92
92
93
+ /// Open Metrics [`Gauge`].
94
+ #[ derive( Debug , Clone ) ]
95
+ pub struct Gauge {
96
+ /// The actual prometheus gauge.
97
+ #[ cfg( feature = "metrics" ) ]
98
+ pub gauge : prometheus_client:: metrics:: gauge:: Gauge ,
99
+ /// What this gauge tracks.
100
+ pub description : & ' static str ,
101
+ }
102
+ impl Gauge {
103
+ /// Constructs a new gauge, based on the given `description`.
104
+ pub fn new ( description : & ' static str ) -> Self {
105
+ Self {
106
+ #[ cfg( feature = "metrics" ) ]
107
+ gauge : Default :: default ( ) ,
108
+ description,
109
+ }
110
+ }
111
+
112
+ /// Increase the [`Gauge`] by 1, returning the previous value.
113
+ pub fn inc ( & self ) -> i64 {
114
+ #[ cfg( feature = "metrics" ) ]
115
+ {
116
+ self . gauge . inc ( )
117
+ }
118
+ #[ cfg( not( feature = "metrics" ) ) ]
119
+ 0
120
+ }
121
+ /// Increase the [`Gauge`] by `i64`, returning the previous value.
122
+ #[ cfg( feature = "metrics" ) ]
123
+ pub fn inc_by ( & self , v : i64 ) -> i64 {
124
+ self . gauge . inc_by ( v)
125
+ }
126
+ /// Increase the [`Gauge`] by `i64`, returning the previous value.
127
+ #[ cfg( not( feature = "metrics" ) ) ]
128
+ pub fn inc_by ( & self , _v : u64 ) -> u64 {
129
+ 0
130
+ }
131
+
132
+ /// Decrease the [`Gauge`] by 1, returning the previous value.
133
+ pub fn dec ( & self ) -> i64 {
134
+ #[ cfg( feature = "metrics" ) ]
135
+ {
136
+ self . gauge . dec ( )
137
+ }
138
+ #[ cfg( not( feature = "metrics" ) ) ]
139
+ 0
140
+ }
141
+ /// Decrease the [`Gauge`] by `i64`, returning the previous value.
142
+ #[ cfg( feature = "metrics" ) ]
143
+ pub fn dec_by ( & self , v : i64 ) -> i64 {
144
+ self . gauge . dec_by ( v)
145
+ }
146
+ /// Decrease the [`Gauge`] by `i64`, returning the previous value.
147
+ #[ cfg( not( feature = "metrics" ) ) ]
148
+ pub fn dec_by ( & self , _v : u64 ) -> u64 {
149
+ 0
150
+ }
151
+
152
+ /// Set the [`Gauge`] value.
153
+ #[ cfg( feature = "metrics" ) ]
154
+ pub fn set ( & self , v : i64 ) -> i64 {
155
+ self . gauge
156
+ . inner ( )
157
+ . store ( v, std:: sync:: atomic:: Ordering :: Relaxed ) ;
158
+ v
159
+ }
160
+ /// Set the [`Gauge`] value.
161
+ #[ cfg( not( feature = "metrics" ) ) ]
162
+ pub fn set ( & self , _v : i64 ) -> i64 {
163
+ 0
164
+ }
165
+
166
+ /// Get the [`Gauge`] value.
167
+ #[ cfg( feature = "metrics" ) ]
168
+ pub fn get ( & self ) -> i64 {
169
+ self . gauge
170
+ . inner ( )
171
+ . load ( std:: sync:: atomic:: Ordering :: Relaxed )
172
+ }
173
+ /// Get the [`Gauge`] value.
174
+ #[ cfg( not( feature = "metrics" ) ) ]
175
+ pub fn get ( & self ) -> i64 {
176
+ 0
177
+ }
178
+ }
179
+
93
180
/// Description of a group of metrics.
94
181
pub trait Metric :
95
182
Default + struct_iterable:: Iterable + Sized + std:: fmt:: Debug + ' static + Send + Sync
0 commit comments