Skip to content

Commit de27234

Browse files
authoredMar 7, 2024
feat(metrics): support Gauge<u32, AtomicU32> type (prometheus#191)
Signed-off-by: koushiro <[email protected]>
1 parent 4eecdc3 commit de27234

File tree

6 files changed

+79
-21
lines changed

6 files changed

+79
-21
lines changed
 

‎CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.22.2]
8+
9+
### Added
10+
11+
- Added `Gauge<u32, AtomicU32>` implementation.
12+
See [PR 191].
13+
14+
[PR 191]: https://github.com/prometheus/client_rust/pull/191
15+
716
## [0.22.1]
817

918
### Added

‎Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "prometheus-client"
3-
version = "0.22.1"
3+
version = "0.22.2"
44
authors = ["Max Inden <mail@max-inden.de>"]
55
edition = "2021"
66
description = "Open Metrics client library allowing users to natively instrument applications."

‎src/encoding.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,12 @@ pub trait EncodeGaugeValue {
544544
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error>;
545545
}
546546

547+
impl EncodeGaugeValue for u32 {
548+
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
549+
encoder.encode_u32(*self)
550+
}
551+
}
552+
547553
impl EncodeGaugeValue for i64 {
548554
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
549555
encoder.encode_i64(*self)
@@ -568,13 +574,17 @@ enum GaugeValueEncoderInner<'a> {
568574
}
569575

570576
impl<'a> GaugeValueEncoder<'a> {
571-
fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
572-
for_both_mut!(self, GaugeValueEncoderInner, e, e.encode_f64(v))
577+
fn encode_u32(&mut self, v: u32) -> Result<(), std::fmt::Error> {
578+
for_both_mut!(self, GaugeValueEncoderInner, e, e.encode_u32(v))
573579
}
574580

575581
fn encode_i64(&mut self, v: i64) -> Result<(), std::fmt::Error> {
576582
for_both_mut!(self, GaugeValueEncoderInner, e, e.encode_i64(v))
577583
}
584+
585+
fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
586+
for_both_mut!(self, GaugeValueEncoderInner, e, e.encode_f64(v))
587+
}
578588
}
579589

580590
impl<'a> From<text::GaugeValueEncoder<'a>> for GaugeValueEncoder<'a> {

‎src/encoding/protobuf.rs

+4
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ pub(crate) struct GaugeValueEncoder<'a> {
323323
}
324324

325325
impl<'a> GaugeValueEncoder<'a> {
326+
pub fn encode_u32(&mut self, v: u32) -> Result<(), std::fmt::Error> {
327+
self.encode_i64(v as i64)
328+
}
329+
326330
pub fn encode_i64(&mut self, v: i64) -> Result<(), std::fmt::Error> {
327331
*self.value = openmetrics_data_model::gauge_value::Value::IntValue(v);
328332
Ok(())

‎src/encoding/text.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,9 @@ impl<'a> std::fmt::Debug for GaugeValueEncoder<'a> {
423423
}
424424

425425
impl<'a> GaugeValueEncoder<'a> {
426-
pub fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
426+
pub fn encode_u32(&mut self, v: u32) -> Result<(), std::fmt::Error> {
427427
self.writer.write_str(" ")?;
428-
self.writer.write_str(dtoa::Buffer::new().format(v))?;
428+
self.writer.write_str(itoa::Buffer::new().format(v))?;
429429
Ok(())
430430
}
431431

@@ -434,6 +434,12 @@ impl<'a> GaugeValueEncoder<'a> {
434434
self.writer.write_str(itoa::Buffer::new().format(v))?;
435435
Ok(())
436436
}
437+
438+
pub fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
439+
self.writer.write_str(" ")?;
440+
self.writer.write_str(dtoa::Buffer::new().format(v))?;
441+
Ok(())
442+
}
437443
}
438444

439445
pub(crate) struct ExemplarValueEncoder<'a> {
@@ -565,6 +571,7 @@ mod tests {
565571
use crate::metrics::{counter::Counter, exemplar::CounterWithExemplar};
566572
use pyo3::{prelude::*, types::PyModule};
567573
use std::borrow::Cow;
574+
use std::sync::atomic::AtomicU32;
568575

569576
#[test]
570577
fn encode_counter() {
@@ -632,6 +639,8 @@ mod tests {
632639
let mut registry = Registry::default();
633640
let gauge: Gauge = Gauge::default();
634641
registry.register("my_gauge", "My gauge", gauge);
642+
let gauge = Gauge::<u32, AtomicU32>::default();
643+
registry.register("u32_gauge", "Gauge::<u32, AtomicU32>", gauge);
635644

636645
let mut encoded = String::new();
637646

‎src/metrics/gauge.rs

+42-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::encoding::{EncodeGaugeValue, EncodeMetric, MetricEncoder};
66

77
use super::{MetricType, TypedMetric};
88
use std::marker::PhantomData;
9-
use std::sync::atomic::{AtomicI32, Ordering};
9+
use std::sync::atomic::{AtomicI32, AtomicU32, Ordering};
1010
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
1111
use std::sync::atomic::{AtomicI64, AtomicU64};
1212
use std::sync::Arc;
@@ -134,55 +134,81 @@ pub trait Atomic<N> {
134134
fn get(&self) -> N;
135135
}
136136

137-
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
138-
impl Atomic<i64> for AtomicI64 {
139-
fn inc(&self) -> i64 {
137+
impl Atomic<i32> for AtomicI32 {
138+
fn inc(&self) -> i32 {
140139
self.inc_by(1)
141140
}
142141

143-
fn inc_by(&self, v: i64) -> i64 {
142+
fn inc_by(&self, v: i32) -> i32 {
144143
self.fetch_add(v, Ordering::Relaxed)
145144
}
146145

147-
fn dec(&self) -> i64 {
146+
fn dec(&self) -> i32 {
148147
self.dec_by(1)
149148
}
150149

151-
fn dec_by(&self, v: i64) -> i64 {
150+
fn dec_by(&self, v: i32) -> i32 {
152151
self.fetch_sub(v, Ordering::Relaxed)
153152
}
154153

155-
fn set(&self, v: i64) -> i64 {
154+
fn set(&self, v: i32) -> i32 {
156155
self.swap(v, Ordering::Relaxed)
157156
}
158157

159-
fn get(&self) -> i64 {
158+
fn get(&self) -> i32 {
160159
self.load(Ordering::Relaxed)
161160
}
162161
}
163162

164-
impl Atomic<i32> for AtomicI32 {
165-
fn inc(&self) -> i32 {
163+
impl Atomic<u32> for AtomicU32 {
164+
fn inc(&self) -> u32 {
166165
self.inc_by(1)
167166
}
168167

169-
fn inc_by(&self, v: i32) -> i32 {
168+
fn inc_by(&self, v: u32) -> u32 {
170169
self.fetch_add(v, Ordering::Relaxed)
171170
}
172171

173-
fn dec(&self) -> i32 {
172+
fn dec(&self) -> u32 {
174173
self.dec_by(1)
175174
}
176175

177-
fn dec_by(&self, v: i32) -> i32 {
176+
fn dec_by(&self, v: u32) -> u32 {
178177
self.fetch_sub(v, Ordering::Relaxed)
179178
}
180179

181-
fn set(&self, v: i32) -> i32 {
180+
fn set(&self, v: u32) -> u32 {
182181
self.swap(v, Ordering::Relaxed)
183182
}
184183

185-
fn get(&self) -> i32 {
184+
fn get(&self) -> u32 {
185+
self.load(Ordering::Relaxed)
186+
}
187+
}
188+
189+
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
190+
impl Atomic<i64> for AtomicI64 {
191+
fn inc(&self) -> i64 {
192+
self.inc_by(1)
193+
}
194+
195+
fn inc_by(&self, v: i64) -> i64 {
196+
self.fetch_add(v, Ordering::Relaxed)
197+
}
198+
199+
fn dec(&self) -> i64 {
200+
self.dec_by(1)
201+
}
202+
203+
fn dec_by(&self, v: i64) -> i64 {
204+
self.fetch_sub(v, Ordering::Relaxed)
205+
}
206+
207+
fn set(&self, v: i64) -> i64 {
208+
self.swap(v, Ordering::Relaxed)
209+
}
210+
211+
fn get(&self) -> i64 {
186212
self.load(Ordering::Relaxed)
187213
}
188214
}

0 commit comments

Comments
 (0)
Please sign in to comment.