Skip to content

Commit 813b459

Browse files
committed
Merge branch 'master' into ftorres/utf-8
2 parents 598fe97 + 9a74e99 commit 813b459

File tree

5 files changed

+64
-6
lines changed

5 files changed

+64
-6
lines changed

CHANGELOG.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@ 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.23.0] - unreleased
7+
## [0.23.1] - unreleased
88

9+
### Changed
10+
11+
- `Histogram::new` now accepts an `IntoIterator` argument, rather than an `Iterator`.
12+
See [PR 243].
13+
14+
[PR 243]: https://github.com/prometheus/client_rust/pull/243
15+
16+
## [0.23.0]
917

1018
### Changed
1119

@@ -34,12 +42,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3442
- Supoort `Arc<String>` for `EncodeLabelValue`.
3543
See [PR 217].
3644

45+
- Add `histogram::exponential_buckets_range`.
46+
See [PR 233].
47+
3748
- Added `get` method to `Family`.
3849
See [PR 234].
3950

4051
[PR 173]: https://github.com/prometheus/client_rust/pull/173
4152
[PR 216]: https://github.com/prometheus/client_rust/pull/216
4253
[PR 217]: https://github.com/prometheus/client_rust/pull/217
54+
[PR 233]: https://github.com/prometheus/client_rust/pull/233
4355
[PR 234]: https://github.com/prometheus/client_rust/pull/234
4456

4557
### Fixed

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.23.0"
3+
version = "0.23.1"
44
authors = ["Max Inden <[email protected]>"]
55
edition = "2021"
66
description = "Open Metrics client library allowing users to natively instrument applications."

src/encoding/protobuf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! # counter.clone(),
1515
//! # );
1616
//! # counter.inc();
17-
//! // Returns `MetricSet`, the top-level container type. Please refer to [openmetrics_data_model.proto](https://github.com/OpenObservability/OpenMetrics/blob/main/proto/openmetrics_data_model.proto) for details.
17+
//! // Returns `MetricSet`, the top-level container type. Please refer to [openmetrics_data_model.proto](https://github.com/prometheus/OpenMetrics/blob/v1.0.0/proto/openmetrics_data_model.proto) for details.
1818
//! let metric_set = encode(&registry).unwrap();
1919
//!
2020
//! let family = metric_set.metric_families.first().unwrap();

src/encoding/text.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use std::fmt::Write;
5656
///
5757
/// Use [`encode_registry`] or [`encode_eof`] if partial encoding is needed.
5858
///
59-
/// See [OpenMetrics exposition format](https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#text-format)
59+
/// See [OpenMetrics exposition format](https://github.com/prometheus/OpenMetrics/blob/v1.0.0/specification/OpenMetrics.md#text-format)
6060
/// for additional details.
6161
///
6262
/// # Examples

src/metrics/histogram.rs

+48-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::sync::Arc;
2828
/// let custom_buckets = [
2929
/// 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
3030
/// ];
31-
/// let histogram = Histogram::new(custom_buckets.into_iter());
31+
/// let histogram = Histogram::new(custom_buckets);
3232
/// histogram.observe(4.2);
3333
/// ```
3434
// TODO: Consider using atomics. See
@@ -57,7 +57,12 @@ pub(crate) struct Inner {
5757

5858
impl Histogram {
5959
/// Create a new [`Histogram`].
60-
pub fn new(buckets: impl Iterator<Item = f64>) -> Self {
60+
///
61+
/// ```rust
62+
/// # use prometheus_client::metrics::histogram::Histogram;
63+
/// let histogram = Histogram::new([10.0, 100.0, 1_000.0]);
64+
/// ```
65+
pub fn new(buckets: impl IntoIterator<Item = f64>) -> Self {
6166
Self {
6267
inner: Arc::new(RwLock::new(Inner {
6368
sum: Default::default(),
@@ -122,6 +127,30 @@ pub fn exponential_buckets(start: f64, factor: f64, length: u16) -> impl Iterato
122127
.take(length.into())
123128
}
124129

130+
/// Exponential bucket distribution within a range
131+
///
132+
/// Creates `length` buckets, where the lowest bucket is `min` and the highest bucket is `max`.
133+
///
134+
/// If `length` is less than 1, or `min` is less than or equal to 0, an empty iterator is returned.
135+
pub fn exponential_buckets_range(min: f64, max: f64, length: u16) -> impl Iterator<Item = f64> {
136+
let mut len_observed = length;
137+
let mut min_bucket = min;
138+
// length needs a positive length and min needs to be greater than 0
139+
// set len_observed to 0 and min_bucket to 1.0
140+
// this will return an empty iterator in the result
141+
if length < 1 || min <= 0.0 {
142+
len_observed = 0;
143+
min_bucket = 1.0;
144+
}
145+
// We know max/min and highest bucket. Solve for growth_factor.
146+
let growth_factor = (max / min_bucket).powf(1.0 / (len_observed as f64 - 1.0));
147+
148+
iter::repeat(())
149+
.enumerate()
150+
.map(move |(i, _)| min_bucket * growth_factor.powf(i as f64))
151+
.take(len_observed.into())
152+
}
153+
125154
/// Linear bucket distribution.
126155
pub fn linear_buckets(start: f64, width: f64, length: u16) -> impl Iterator<Item = f64> {
127156
iter::repeat(())
@@ -166,4 +195,21 @@ mod tests {
166195
linear_buckets(0.0, 1.0, 10).collect::<Vec<_>>()
167196
);
168197
}
198+
199+
#[test]
200+
fn exponential_range() {
201+
assert_eq!(
202+
vec![1.0, 2.0, 4.0, 8.0, 16.0, 32.0],
203+
exponential_buckets_range(1.0, 32.0, 6).collect::<Vec<_>>()
204+
);
205+
}
206+
207+
#[test]
208+
fn exponential_range_incorrect() {
209+
let res = exponential_buckets_range(1.0, 32.0, 0).collect::<Vec<_>>();
210+
assert!(res.is_empty());
211+
212+
let res = exponential_buckets_range(0.0, 32.0, 6).collect::<Vec<_>>();
213+
assert!(res.is_empty());
214+
}
169215
}

0 commit comments

Comments
 (0)