Line data Source code
1 : //! We re-export those from prometheus crate to
2 : //! make sure that we use the same dep version everywhere.
3 : //! Otherwise, we might not see all metrics registered via
4 : //! a default registry.
5 : #![deny(clippy::undocumented_unsafe_blocks)]
6 :
7 : use measured::{
8 : label::{LabelGroupSet, LabelGroupVisitor, LabelName, NoLabels},
9 : metric::{
10 : counter::CounterState,
11 : gauge::GaugeState,
12 : group::Encoding,
13 : name::{MetricName, MetricNameEncoder},
14 : MetricEncoding, MetricFamilyEncoding,
15 : },
16 : FixedCardinalityLabel, LabelGroup, MetricGroup,
17 : };
18 : use once_cell::sync::Lazy;
19 : use prometheus::core::{
20 : Atomic, AtomicU64, Collector, GenericCounter, GenericCounterVec, GenericGauge, GenericGaugeVec,
21 : };
22 : pub use prometheus::opts;
23 : pub use prometheus::register;
24 : pub use prometheus::Error;
25 : use prometheus::Registry;
26 : pub use prometheus::{core, default_registry, proto};
27 : pub use prometheus::{exponential_buckets, linear_buckets};
28 : pub use prometheus::{register_counter_vec, Counter, CounterVec};
29 : pub use prometheus::{register_gauge, Gauge};
30 : pub use prometheus::{register_gauge_vec, GaugeVec};
31 : pub use prometheus::{register_histogram, Histogram};
32 : pub use prometheus::{register_histogram_vec, HistogramVec};
33 : pub use prometheus::{register_int_counter, IntCounter};
34 : pub use prometheus::{register_int_counter_vec, IntCounterVec};
35 : pub use prometheus::{register_int_gauge, IntGauge};
36 : pub use prometheus::{register_int_gauge_vec, IntGaugeVec};
37 : pub use prometheus::{Encoder, TextEncoder};
38 :
39 : pub mod launch_timestamp;
40 : mod wrappers;
41 : pub use wrappers::{CountedReader, CountedWriter};
42 : mod hll;
43 : pub use hll::{HyperLogLog, HyperLogLogState, HyperLogLogVec};
44 : #[cfg(target_os = "linux")]
45 : pub mod more_process_metrics;
46 :
47 : pub type UIntGauge = GenericGauge<AtomicU64>;
48 : pub type UIntGaugeVec = GenericGaugeVec<AtomicU64>;
49 :
50 : #[macro_export]
51 : macro_rules! register_uint_gauge_vec {
52 : ($NAME:expr, $HELP:expr, $LABELS_NAMES:expr $(,)?) => {{
53 : let gauge_vec = UIntGaugeVec::new($crate::opts!($NAME, $HELP), $LABELS_NAMES).unwrap();
54 5922 : $crate::register(Box::new(gauge_vec.clone())).map(|_| gauge_vec)
55 : }};
56 : }
57 :
58 : #[macro_export]
59 : macro_rules! register_uint_gauge {
60 : ($NAME:expr, $HELP:expr $(,)?) => {{
61 : let gauge = $crate::UIntGauge::new($NAME, $HELP).unwrap();
62 1764 : $crate::register(Box::new(gauge.clone())).map(|_| gauge)
63 : }};
64 : }
65 :
66 : /// Special internal registry, to collect metrics independently from the default registry.
67 : /// Was introduced to fix deadlock with lazy registration of metrics in the default registry.
68 : static INTERNAL_REGISTRY: Lazy<Registry> = Lazy::new(Registry::new);
69 :
70 : /// Register a collector in the internal registry. MUST be called before the first call to `gather()`.
71 : ///
72 : /// Otherwise, we can have a deadlock in the `gather()` call, trying to register a new collector
73 : /// while holding the lock.
74 0 : pub fn register_internal(c: Box<dyn Collector>) -> prometheus::Result<()> {
75 0 : INTERNAL_REGISTRY.register(c)
76 0 : }
77 :
78 : /// Gathers all Prometheus metrics and records the I/O stats just before that.
79 : ///
80 : /// Metrics gathering is a relatively simple and standalone operation, so
81 : /// it might be fine to do it this way to keep things simple.
82 0 : pub fn gather() -> Vec<prometheus::proto::MetricFamily> {
83 0 : update_rusage_metrics();
84 0 : let mut mfs = prometheus::gather();
85 0 : let mut internal_mfs = INTERNAL_REGISTRY.gather();
86 0 : mfs.append(&mut internal_mfs);
87 0 : mfs
88 0 : }
89 :
90 0 : static DISK_IO_BYTES: Lazy<IntGaugeVec> = Lazy::new(|| {
91 0 : register_int_gauge_vec!(
92 0 : "libmetrics_disk_io_bytes_total",
93 0 : "Bytes written and read from disk, grouped by the operation (read|write)",
94 0 : &["io_operation"]
95 0 : )
96 0 : .expect("Failed to register disk i/o bytes int gauge vec")
97 0 : });
98 :
99 0 : static MAXRSS_KB: Lazy<IntGauge> = Lazy::new(|| {
100 0 : register_int_gauge!(
101 0 : "libmetrics_maxrss_kb",
102 0 : "Memory usage (Maximum Resident Set Size)"
103 0 : )
104 0 : .expect("Failed to register maxrss_kb int gauge")
105 0 : });
106 :
107 : /// Most common fsync latency is 50 µs - 100 µs, but it can be much higher,
108 : /// especially during many concurrent disk operations.
109 : pub const DISK_FSYNC_SECONDS_BUCKETS: &[f64] =
110 : &[0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1.0, 5.0, 10.0, 30.0];
111 :
112 : pub struct BuildInfo {
113 : pub revision: &'static str,
114 : pub build_tag: &'static str,
115 : }
116 :
117 : // todo: allow label group without the set
118 : impl LabelGroup for BuildInfo {
119 0 : fn visit_values(&self, v: &mut impl LabelGroupVisitor) {
120 : const REVISION: &LabelName = LabelName::from_str("revision");
121 0 : v.write_value(REVISION, &self.revision);
122 : const BUILD_TAG: &LabelName = LabelName::from_str("build_tag");
123 0 : v.write_value(BUILD_TAG, &self.build_tag);
124 0 : }
125 : }
126 :
127 : impl<T: Encoding> MetricFamilyEncoding<T> for BuildInfo
128 : where
129 : GaugeState: MetricEncoding<T>,
130 : {
131 0 : fn collect_family_into(
132 0 : &self,
133 0 : name: impl measured::metric::name::MetricNameEncoder,
134 0 : enc: &mut T,
135 0 : ) -> Result<(), T::Err> {
136 0 : enc.write_help(&name, "Build/version information")?;
137 0 : GaugeState::write_type(&name, enc)?;
138 0 : GaugeState {
139 0 : count: std::sync::atomic::AtomicI64::new(1),
140 0 : }
141 0 : .collect_into(&(), self, name, enc)
142 0 : }
143 : }
144 :
145 0 : #[derive(MetricGroup)]
146 : #[metric(new(build_info: BuildInfo))]
147 : pub struct NeonMetrics {
148 : #[cfg(target_os = "linux")]
149 : #[metric(namespace = "process")]
150 : #[metric(init = measured_process::ProcessCollector::for_self())]
151 : process: measured_process::ProcessCollector,
152 :
153 : #[metric(namespace = "libmetrics")]
154 : #[metric(init = LibMetrics::new(build_info))]
155 : libmetrics: LibMetrics,
156 : }
157 :
158 0 : #[derive(MetricGroup)]
159 : #[metric(new(build_info: BuildInfo))]
160 : pub struct LibMetrics {
161 : #[metric(init = build_info)]
162 : build_info: BuildInfo,
163 :
164 : #[metric(flatten)]
165 : rusage: Rusage,
166 :
167 : serve_count: CollectionCounter,
168 : }
169 :
170 0 : fn write_gauge<Enc: Encoding>(
171 0 : x: i64,
172 0 : labels: impl LabelGroup,
173 0 : name: impl MetricNameEncoder,
174 0 : enc: &mut Enc,
175 0 : ) -> Result<(), Enc::Err>
176 0 : where
177 0 : GaugeState: MetricEncoding<Enc>,
178 0 : {
179 0 : GaugeState::new(x).collect_into(&(), labels, name, enc)
180 0 : }
181 :
182 : #[derive(Default)]
183 : struct Rusage;
184 :
185 0 : #[derive(FixedCardinalityLabel, Clone, Copy)]
186 : #[label(singleton = "io_operation")]
187 : enum IoOp {
188 : Read,
189 : Write,
190 : }
191 :
192 : impl<T: Encoding> MetricGroup<T> for Rusage
193 : where
194 : GaugeState: MetricEncoding<T>,
195 : {
196 0 : fn collect_group_into(&self, enc: &mut T) -> Result<(), T::Err> {
197 : const DISK_IO: &MetricName = MetricName::from_str("disk_io_bytes_total");
198 : const MAXRSS: &MetricName = MetricName::from_str("maxrss_kb");
199 :
200 0 : let ru = get_rusage_stats();
201 0 :
202 0 : enc.write_help(
203 0 : DISK_IO,
204 0 : "Bytes written and read from disk, grouped by the operation (read|write)",
205 0 : )?;
206 0 : GaugeState::write_type(DISK_IO, enc)?;
207 0 : write_gauge(ru.ru_inblock * BYTES_IN_BLOCK, IoOp::Read, DISK_IO, enc)?;
208 0 : write_gauge(ru.ru_oublock * BYTES_IN_BLOCK, IoOp::Write, DISK_IO, enc)?;
209 :
210 0 : enc.write_help(MAXRSS, "Memory usage (Maximum Resident Set Size)")?;
211 0 : GaugeState::write_type(MAXRSS, enc)?;
212 0 : write_gauge(ru.ru_maxrss, IoOp::Read, MAXRSS, enc)?;
213 :
214 0 : Ok(())
215 0 : }
216 : }
217 :
218 : #[derive(Default)]
219 : struct CollectionCounter(CounterState);
220 :
221 : impl<T: Encoding> MetricFamilyEncoding<T> for CollectionCounter
222 : where
223 : CounterState: MetricEncoding<T>,
224 : {
225 0 : fn collect_family_into(
226 0 : &self,
227 0 : name: impl measured::metric::name::MetricNameEncoder,
228 0 : enc: &mut T,
229 0 : ) -> Result<(), T::Err> {
230 0 : self.0.inc();
231 0 : enc.write_help(&name, "Number of metric requests made")?;
232 0 : self.0.collect_into(&(), NoLabels, name, enc)
233 0 : }
234 : }
235 :
236 0 : pub fn set_build_info_metric(revision: &str, build_tag: &str) {
237 0 : let metric = register_int_gauge_vec!(
238 0 : "libmetrics_build_info",
239 0 : "Build/version information",
240 0 : &["revision", "build_tag"]
241 0 : )
242 0 : .expect("Failed to register build info metric");
243 0 : metric.with_label_values(&[revision, build_tag]).set(1);
244 0 : }
245 : const BYTES_IN_BLOCK: i64 = 512;
246 :
247 : // Records I/O stats in a "cross-platform" way.
248 : // Compiles both on macOS and Linux, but current macOS implementation always returns 0 as values for I/O stats.
249 : // An alternative is to read procfs (`/proc/[pid]/io`) which does not work under macOS at all, hence abandoned.
250 : //
251 : // Uses https://www.freebsd.org/cgi/man.cgi?query=getrusage to retrieve the number of block operations
252 : // performed by the process.
253 : // We know the size of the block, so we can determine the I/O bytes out of it.
254 : // The value might be not 100% exact, but should be fine for Prometheus metrics in this case.
255 0 : fn update_rusage_metrics() {
256 0 : let rusage_stats = get_rusage_stats();
257 0 :
258 0 : DISK_IO_BYTES
259 0 : .with_label_values(&["read"])
260 0 : .set(rusage_stats.ru_inblock * BYTES_IN_BLOCK);
261 0 : DISK_IO_BYTES
262 0 : .with_label_values(&["write"])
263 0 : .set(rusage_stats.ru_oublock * BYTES_IN_BLOCK);
264 0 :
265 0 : // On macOS, the unit of maxrss is bytes; on Linux, it's kilobytes. https://stackoverflow.com/a/59915669
266 0 : #[cfg(target_os = "macos")]
267 0 : {
268 0 : MAXRSS_KB.set(rusage_stats.ru_maxrss / 1024);
269 0 : }
270 0 : #[cfg(not(target_os = "macos"))]
271 0 : {
272 0 : MAXRSS_KB.set(rusage_stats.ru_maxrss);
273 0 : }
274 0 : }
275 :
276 0 : fn get_rusage_stats() -> libc::rusage {
277 0 : let mut rusage = std::mem::MaybeUninit::uninit();
278 0 :
279 0 : // SAFETY: kernel will initialize the struct for us
280 0 : unsafe {
281 0 : let ret = libc::getrusage(libc::RUSAGE_SELF, rusage.as_mut_ptr());
282 0 : assert!(ret == 0, "getrusage failed: bad args");
283 0 : rusage.assume_init()
284 0 : }
285 0 : }
286 :
287 : /// Create an [`IntCounterPairVec`] and registers to default registry.
288 : #[macro_export(local_inner_macros)]
289 : macro_rules! register_int_counter_pair_vec {
290 : ($NAME1:expr, $HELP1:expr, $NAME2:expr, $HELP2:expr, $LABELS_NAMES:expr $(,)?) => {{
291 : match (
292 : $crate::register_int_counter_vec!($NAME1, $HELP1, $LABELS_NAMES),
293 : $crate::register_int_counter_vec!($NAME2, $HELP2, $LABELS_NAMES),
294 : ) {
295 : (Ok(inc), Ok(dec)) => Ok($crate::IntCounterPairVec::new(inc, dec)),
296 : (Err(e), _) | (_, Err(e)) => Err(e),
297 : }
298 : }};
299 : }
300 :
301 : /// Create an [`IntCounterPair`] and registers to default registry.
302 : #[macro_export(local_inner_macros)]
303 : macro_rules! register_int_counter_pair {
304 : ($NAME1:expr, $HELP1:expr, $NAME2:expr, $HELP2:expr $(,)?) => {{
305 : match (
306 : $crate::register_int_counter!($NAME1, $HELP1),
307 : $crate::register_int_counter!($NAME2, $HELP2),
308 : ) {
309 : (Ok(inc), Ok(dec)) => Ok($crate::IntCounterPair::new(inc, dec)),
310 : (Err(e), _) | (_, Err(e)) => Err(e),
311 : }
312 : }};
313 : }
314 :
315 : /// A Pair of [`GenericCounterVec`]s. Like an [`GenericGaugeVec`] but will always observe changes
316 : pub struct GenericCounterPairVec<P: Atomic> {
317 : inc: GenericCounterVec<P>,
318 : dec: GenericCounterVec<P>,
319 : }
320 :
321 : /// A Pair of [`GenericCounter`]s. Like an [`GenericGauge`] but will always observe changes
322 : pub struct GenericCounterPair<P: Atomic> {
323 : inc: GenericCounter<P>,
324 : dec: GenericCounter<P>,
325 : }
326 :
327 : impl<P: Atomic> GenericCounterPairVec<P> {
328 564 : pub fn new(inc: GenericCounterVec<P>, dec: GenericCounterVec<P>) -> Self {
329 564 : Self { inc, dec }
330 564 : }
331 :
332 : /// `get_metric_with_label_values` returns the [`GenericCounterPair<P>`] for the given slice
333 : /// of label values (same order as the VariableLabels in Desc). If that combination of
334 : /// label values is accessed for the first time, a new [`GenericCounterPair<P>`] is created.
335 : ///
336 : /// An error is returned if the number of label values is not the same as the
337 : /// number of VariableLabels in Desc.
338 2783 : pub fn get_metric_with_label_values(
339 2783 : &self,
340 2783 : vals: &[&str],
341 2783 : ) -> prometheus::Result<GenericCounterPair<P>> {
342 2783 : Ok(GenericCounterPair {
343 2783 : inc: self.inc.get_metric_with_label_values(vals)?,
344 2783 : dec: self.dec.get_metric_with_label_values(vals)?,
345 : })
346 2783 : }
347 :
348 : /// `with_label_values` works as `get_metric_with_label_values`, but panics if an error
349 : /// occurs.
350 540 : pub fn with_label_values(&self, vals: &[&str]) -> GenericCounterPair<P> {
351 540 : self.get_metric_with_label_values(vals).unwrap()
352 540 : }
353 :
354 72 : pub fn remove_label_values(&self, res: &mut [prometheus::Result<()>; 2], vals: &[&str]) {
355 72 : res[0] = self.inc.remove_label_values(vals);
356 72 : res[1] = self.dec.remove_label_values(vals);
357 72 : }
358 : }
359 :
360 : impl<P: Atomic> GenericCounterPair<P> {
361 0 : pub fn new(inc: GenericCounter<P>, dec: GenericCounter<P>) -> Self {
362 0 : Self { inc, dec }
363 0 : }
364 :
365 : /// Increment the gauge by 1, returning a guard that decrements by 1 on drop.
366 1092 : pub fn guard(&self) -> GenericCounterPairGuard<P> {
367 1092 : self.inc.inc();
368 1092 : GenericCounterPairGuard(self.dec.clone())
369 1092 : }
370 :
371 : /// Increment the gauge by n, returning a guard that decrements by n on drop.
372 0 : pub fn guard_by(&self, n: P::T) -> GenericCounterPairGuardBy<P> {
373 0 : self.inc.inc_by(n);
374 0 : GenericCounterPairGuardBy(self.dec.clone(), n)
375 0 : }
376 :
377 : /// Increase the gauge by 1.
378 : #[inline]
379 10389 : pub fn inc(&self) {
380 10389 : self.inc.inc();
381 10389 : }
382 :
383 : /// Decrease the gauge by 1.
384 : #[inline]
385 8731 : pub fn dec(&self) {
386 8731 : self.dec.inc();
387 8731 : }
388 :
389 : /// Add the given value to the gauge. (The value can be
390 : /// negative, resulting in a decrement of the gauge.)
391 : #[inline]
392 0 : pub fn inc_by(&self, v: P::T) {
393 0 : self.inc.inc_by(v);
394 0 : }
395 :
396 : /// Subtract the given value from the gauge. (The value can be
397 : /// negative, resulting in an increment of the gauge.)
398 : #[inline]
399 0 : pub fn dec_by(&self, v: P::T) {
400 0 : self.dec.inc_by(v);
401 0 : }
402 : }
403 :
404 : impl<P: Atomic> Clone for GenericCounterPair<P> {
405 19042 : fn clone(&self) -> Self {
406 19042 : Self {
407 19042 : inc: self.inc.clone(),
408 19042 : dec: self.dec.clone(),
409 19042 : }
410 19042 : }
411 : }
412 :
413 : /// Guard returned by [`GenericCounterPair::guard`]
414 : pub struct GenericCounterPairGuard<P: Atomic>(GenericCounter<P>);
415 :
416 : impl<P: Atomic> Drop for GenericCounterPairGuard<P> {
417 1092 : fn drop(&mut self) {
418 1092 : self.0.inc();
419 1092 : }
420 : }
421 : /// Guard returned by [`GenericCounterPair::guard_by`]
422 : pub struct GenericCounterPairGuardBy<P: Atomic>(GenericCounter<P>, P::T);
423 :
424 : impl<P: Atomic> Drop for GenericCounterPairGuardBy<P> {
425 0 : fn drop(&mut self) {
426 0 : self.0.inc_by(self.1);
427 0 : }
428 : }
429 :
430 : /// A Pair of [`IntCounterVec`]s. Like an [`IntGaugeVec`] but will always observe changes
431 : pub type IntCounterPairVec = GenericCounterPairVec<AtomicU64>;
432 :
433 : /// A Pair of [`IntCounter`]s. Like an [`IntGauge`] but will always observe changes
434 : pub type IntCounterPair = GenericCounterPair<AtomicU64>;
435 :
436 : /// A guard for [`IntCounterPair`] that will decrement the gauge on drop
437 : pub type IntCounterPairGuard = GenericCounterPairGuard<AtomicU64>;
438 :
439 : pub trait CounterPairAssoc {
440 : const INC_NAME: &'static MetricName;
441 : const DEC_NAME: &'static MetricName;
442 :
443 : const INC_HELP: &'static str;
444 : const DEC_HELP: &'static str;
445 :
446 : type LabelGroupSet: LabelGroupSet;
447 : }
448 :
449 : pub struct CounterPairVec<A: CounterPairAssoc> {
450 : vec: measured::metric::MetricVec<MeasuredCounterPairState, A::LabelGroupSet>,
451 : }
452 :
453 : impl<A: CounterPairAssoc> Default for CounterPairVec<A>
454 : where
455 : A::LabelGroupSet: Default,
456 : {
457 120 : fn default() -> Self {
458 120 : Self {
459 120 : vec: Default::default(),
460 120 : }
461 120 : }
462 : }
463 :
464 : impl<A: CounterPairAssoc> CounterPairVec<A> {
465 0 : pub fn guard(
466 0 : &self,
467 0 : labels: <A::LabelGroupSet as LabelGroupSet>::Group<'_>,
468 0 : ) -> MeasuredCounterPairGuard<'_, A> {
469 0 : let id = self.vec.with_labels(labels);
470 0 : self.vec.get_metric(id).inc.inc();
471 0 : MeasuredCounterPairGuard { vec: &self.vec, id }
472 0 : }
473 0 : pub fn inc(&self, labels: <A::LabelGroupSet as LabelGroupSet>::Group<'_>) {
474 0 : let id = self.vec.with_labels(labels);
475 0 : self.vec.get_metric(id).inc.inc();
476 0 : }
477 0 : pub fn dec(&self, labels: <A::LabelGroupSet as LabelGroupSet>::Group<'_>) {
478 0 : let id = self.vec.with_labels(labels);
479 0 : self.vec.get_metric(id).dec.inc();
480 0 : }
481 0 : pub fn remove_metric(
482 0 : &self,
483 0 : labels: <A::LabelGroupSet as LabelGroupSet>::Group<'_>,
484 0 : ) -> Option<MeasuredCounterPairState> {
485 0 : let id = self.vec.with_labels(labels);
486 0 : self.vec.remove_metric(id)
487 0 : }
488 :
489 0 : pub fn sample(&self, labels: <A::LabelGroupSet as LabelGroupSet>::Group<'_>) -> u64 {
490 0 : let id = self.vec.with_labels(labels);
491 0 : let metric = self.vec.get_metric(id);
492 0 :
493 0 : let inc = metric.inc.count.load(std::sync::atomic::Ordering::Relaxed);
494 0 : let dec = metric.dec.count.load(std::sync::atomic::Ordering::Relaxed);
495 0 : inc.saturating_sub(dec)
496 0 : }
497 : }
498 :
499 : impl<T, A> ::measured::metric::group::MetricGroup<T> for CounterPairVec<A>
500 : where
501 : T: ::measured::metric::group::Encoding,
502 : A: CounterPairAssoc,
503 : ::measured::metric::counter::CounterState: ::measured::metric::MetricEncoding<T>,
504 : {
505 0 : fn collect_group_into(&self, enc: &mut T) -> Result<(), T::Err> {
506 0 : // write decrement first to avoid a race condition where inc - dec < 0
507 0 : T::write_help(enc, A::DEC_NAME, A::DEC_HELP)?;
508 0 : self.vec
509 0 : .collect_family_into(A::DEC_NAME, &mut Dec(&mut *enc))?;
510 :
511 0 : T::write_help(enc, A::INC_NAME, A::INC_HELP)?;
512 0 : self.vec
513 0 : .collect_family_into(A::INC_NAME, &mut Inc(&mut *enc))?;
514 :
515 0 : Ok(())
516 0 : }
517 : }
518 :
519 : #[derive(MetricGroup, Default)]
520 : pub struct MeasuredCounterPairState {
521 : pub inc: CounterState,
522 : pub dec: CounterState,
523 : }
524 :
525 : impl measured::metric::MetricType for MeasuredCounterPairState {
526 : type Metadata = ();
527 : }
528 :
529 : pub struct MeasuredCounterPairGuard<'a, A: CounterPairAssoc> {
530 : vec: &'a measured::metric::MetricVec<MeasuredCounterPairState, A::LabelGroupSet>,
531 : id: measured::metric::LabelId<A::LabelGroupSet>,
532 : }
533 :
534 : impl<A: CounterPairAssoc> Drop for MeasuredCounterPairGuard<'_, A> {
535 0 : fn drop(&mut self) {
536 0 : self.vec.get_metric(self.id).dec.inc();
537 0 : }
538 : }
539 :
540 : /// [`MetricEncoding`] for [`MeasuredCounterPairState`] that only writes the inc counter to the inner encoder.
541 : struct Inc<T>(T);
542 : /// [`MetricEncoding`] for [`MeasuredCounterPairState`] that only writes the dec counter to the inner encoder.
543 : struct Dec<T>(T);
544 :
545 : impl<T: Encoding> Encoding for Inc<T> {
546 : type Err = T::Err;
547 :
548 0 : fn write_help(&mut self, name: impl MetricNameEncoder, help: &str) -> Result<(), Self::Err> {
549 0 : self.0.write_help(name, help)
550 0 : }
551 : }
552 :
553 : impl<T: Encoding> MetricEncoding<Inc<T>> for MeasuredCounterPairState
554 : where
555 : CounterState: MetricEncoding<T>,
556 : {
557 0 : fn write_type(name: impl MetricNameEncoder, enc: &mut Inc<T>) -> Result<(), T::Err> {
558 0 : CounterState::write_type(name, &mut enc.0)
559 0 : }
560 0 : fn collect_into(
561 0 : &self,
562 0 : metadata: &(),
563 0 : labels: impl LabelGroup,
564 0 : name: impl MetricNameEncoder,
565 0 : enc: &mut Inc<T>,
566 0 : ) -> Result<(), T::Err> {
567 0 : self.inc.collect_into(metadata, labels, name, &mut enc.0)
568 0 : }
569 : }
570 :
571 : impl<T: Encoding> Encoding for Dec<T> {
572 : type Err = T::Err;
573 :
574 0 : fn write_help(&mut self, name: impl MetricNameEncoder, help: &str) -> Result<(), Self::Err> {
575 0 : self.0.write_help(name, help)
576 0 : }
577 : }
578 :
579 : /// Write the dec counter to the encoder
580 : impl<T: Encoding> MetricEncoding<Dec<T>> for MeasuredCounterPairState
581 : where
582 : CounterState: MetricEncoding<T>,
583 : {
584 0 : fn write_type(name: impl MetricNameEncoder, enc: &mut Dec<T>) -> Result<(), T::Err> {
585 0 : CounterState::write_type(name, &mut enc.0)
586 0 : }
587 0 : fn collect_into(
588 0 : &self,
589 0 : metadata: &(),
590 0 : labels: impl LabelGroup,
591 0 : name: impl MetricNameEncoder,
592 0 : enc: &mut Dec<T>,
593 0 : ) -> Result<(), T::Err> {
594 0 : self.dec.collect_into(metadata, labels, name, &mut enc.0)
595 0 : }
596 : }
|