Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Always use dynamic dispatch on Registry, i.e. remove generic type parameter `M` from `Registry`.

- Move`Encode` trait from `prometheus_client::encoding::text` to `prometheus_client::encoding`. See [PR 83].

[PR 83]: https://github.com/prometheus/client_rust/pull/83
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ homepage = "https://github.com/prometheus/client_rust"
documentation = "https://docs.rs/prometheus-client"

[features]
protobuf = ["dep:prost", "dep:prost-types", "dep:prost-build", "dep:void", "prometheus-client-derive-encode/protobuf"]
default = []
protobuf = ["dep:prost", "dep:prost-types", "dep:prost-build"]

[workspace]
members = ["derive-encode"]
Expand All @@ -23,7 +24,6 @@ parking_lot = "0.12"
prometheus-client-derive-encode = { version = "0.3.0", path = "derive-encode" }
prost = { version = "0.11.0", optional = true }
prost-types = { version = "0.11.0", optional = true }
void = { version = "1.0", optional = true }

[dev-dependencies]
async-std = { version = "1", features = ["attributes"] }
Expand Down
40 changes: 24 additions & 16 deletions benches/encoding/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
// https://github.com/tikv/rust-prometheus/blob/ab1ca7285d3463504381a5025ae1951e020d6796/benches/text_encoder.rs:write

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use prometheus_client::encoding::proto::{encode, EncodeMetric};
use prometheus_client::encoding::Encode;
use prometheus_client::encoding::protobuf::encode;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
use prometheus_client::registry::Registry;
use prometheus_client_derive_encode::{EncodeLabelSet, EncodeLabelValue};
use std::fmt::{Display, Formatter};

pub fn proto(c: &mut Criterion) {
c.bench_function("encode", |b| {
#[derive(Clone, Hash, PartialEq, Eq, Encode)]
struct Labels {
#[derive(Debug, Clone, Hash, PartialEq, Eq, EncodeLabelSet)]
struct CounterLabels {
path: String,
method: Method,
some_number: u64,
}

#[derive(Clone, Hash, PartialEq, Eq, Encode)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, EncodeLabelValue)]
enum Method {
Get,
#[allow(dead_code)]
Expand All @@ -35,50 +35,58 @@ pub fn proto(c: &mut Criterion) {
}
}

#[derive(Clone, Hash, PartialEq, Eq, Encode)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, EncodeLabelSet)]
struct HistogramLabels {
region: Region,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, EncodeLabelValue)]
enum Region {
Africa,
#[allow(dead_code)]
Asia,
}

let mut registry = Registry::<Box<dyn EncodeMetric>>::default();
let mut registry = Registry::default();

for i in 0..100 {
let counter_family = Family::<Labels, Counter>::default();
let histogram_family = Family::<Region, Histogram>::new_with_constructor(|| {
Histogram::new(exponential_buckets(1.0, 2.0, 10))
});
let counter_family = Family::<CounterLabels, Counter>::default();
let histogram_family =
Family::<HistogramLabels, Histogram>::new_with_constructor(|| {
Histogram::new(exponential_buckets(1.0, 2.0, 10))
});

registry.register(
format!("my_counter{}", i),
"My counter",
Box::new(counter_family.clone()),
counter_family.clone(),
);
registry.register(
format!("my_histogram{}", i),
"My histogram",
Box::new(histogram_family.clone()),
histogram_family.clone(),
);

for j in 0_u32..100 {
counter_family
.get_or_create(&Labels {
.get_or_create(&CounterLabels {
path: format!("/path/{}", i),
method: Method::Get,
some_number: j.into(),
})
.inc();

histogram_family
.get_or_create(&Region::Africa)
.get_or_create(&HistogramLabels {
region: Region::Africa,
})
.observe(j.into());
}
}

b.iter(|| {
let metric_set = encode(&registry);
black_box(metric_set);
black_box(metric_set).unwrap();
})
});
}
Expand Down
52 changes: 19 additions & 33 deletions benches/encoding/text.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
// Benchmark inspired by https://github.com/tikv/rust-prometheus/blob/ab1ca7285d3463504381a5025ae1951e020d6796/benches/text_encoder.rs

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use prometheus_client::encoding::text::{encode, EncodeMetric};
use prometheus_client::encoding::Encode;
use prometheus_client::encoding::text::encode;
use prometheus_client::encoding::LabelValueEncoder;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
use prometheus_client::registry::Registry;
use std::io::Write;
use prometheus_client_derive_encode::{EncodeLabelSet, EncodeLabelValue};
use std::fmt::{Error, Write};

pub fn text(c: &mut Criterion) {
c.bench_function("encode", |b| {
#[derive(Clone, Hash, PartialEq, Eq, Encode)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, EncodeLabelSet)]
struct Labels {
method: Method,
status: Status,
some_number: u64,
}

#[derive(Clone, Hash, PartialEq, Eq, Encode)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, EncodeLabelValue)]
enum Method {
Get,
#[allow(dead_code)]
Put,
}

#[derive(Clone, Hash, PartialEq, Eq)]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
enum Status {
Two,
#[allow(dead_code)]
Expand All @@ -34,34 +35,19 @@ pub fn text(c: &mut Criterion) {
Five,
}

impl prometheus_client::encoding::text::Encode for Status {
fn encode(&self, writer: &mut dyn Write) -> Result<(), std::io::Error> {
let status = match self {
Status::Two => b"200",
Status::Four => b"400",
Status::Five => b"500",
};
writer.write_all(status)?;
Ok(())
}
}
impl prometheus_client::encoding::EncodeLabelValue for Status {
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), Error> {
encoder.write_str(match self {
Status::Two => "200",
Status::Four => "400",
Status::Five => "500",
})?;

#[cfg(feature = "protobuf")]
impl prometheus_client::encoding::proto::EncodeLabels for Status {
fn encode(&self, labels: &mut Vec<prometheus_client::encoding::proto::Label>) {
let value = match self {
Status::Two => "200".to_string(),
Status::Four => "400".to_string(),
Status::Five => "500".to_string(),
};
labels.push(prometheus_client::encoding::proto::Label {
name: "status".to_string(),
value,
});
Ok(())
}
}

let mut registry = Registry::<Box<dyn EncodeMetric>>::default();
let mut registry = Registry::default();

for i in 0..100 {
let counter_family = Family::<Labels, Counter>::default();
Expand All @@ -72,12 +58,12 @@ pub fn text(c: &mut Criterion) {
registry.register(
format!("my_counter_{}", i),
"My counter",
Box::new(counter_family.clone()),
counter_family.clone(),
);
registry.register(
format!("my_histogram_{}", i),
"My histogram",
Box::new(histogram_family.clone()),
histogram_family.clone(),
);

for j in 0u32..100 {
Expand All @@ -98,7 +84,7 @@ pub fn text(c: &mut Criterion) {
}
}

let mut buffer = vec![];
let mut buffer = String::new();

b.iter(|| {
encode(&mut buffer, &registry).unwrap();
Expand Down
3 changes: 0 additions & 3 deletions derive-encode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ repository = "https://github.com/prometheus/client_rust"
homepage = "https://github.com/prometheus/client_rust"
documentation = "https://docs.rs/prometheus-client-derive-text-encode"

[features]
protobuf = []

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
Loading