forked from prometheus/client_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encoding/: Add Protobuf support (prometheus#83)
Add support for the OpenMetrics Protobuf encoding. Co-authored-by: Max Inden <[email protected]> Co-authored-by: Diva M <[email protected]>
- Loading branch information
1 parent
16993b6
commit ffc2ab6
Showing
20 changed files
with
1,624 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "prometheus-client" | ||
version = "0.18.0" | ||
version = "0.19.0" | ||
authors = ["Max Inden <[email protected]>"] | ||
edition = "2021" | ||
description = "Open Metrics client library allowing users to natively instrument applications." | ||
|
@@ -10,14 +10,20 @@ repository = "https://github.com/prometheus/client_rust" | |
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"] | ||
|
||
[workspace] | ||
members = ["derive-text-encode"] | ||
members = ["derive-encode"] | ||
|
||
[dependencies] | ||
dtoa = "1.0" | ||
itoa = "1.0" | ||
parking_lot = "0.12" | ||
prometheus-client-derive-text-encode = { version = "0.3.0", path = "derive-text-encode" } | ||
prometheus-client-derive-encode = { version = "0.3.0", path = "derive-encode" } | ||
prost = { version = "0.9.0", optional = true } | ||
prost-types = { version = "0.9.0", optional = true } | ||
void = { version = "1.0", optional = true } | ||
|
||
[dev-dependencies] | ||
async-std = { version = "1", features = ["attributes"] } | ||
|
@@ -29,6 +35,9 @@ rand = "0.8.4" | |
tide = "0.16" | ||
actix-web = "4" | ||
|
||
[build-dependencies] | ||
prost-build = { version = "0.9.0", optional = true } | ||
|
||
[[bench]] | ||
name = "family" | ||
harness = false | ||
|
@@ -37,3 +46,10 @@ harness = false | |
name = "text" | ||
path = "benches/encoding/text.rs" | ||
harness = false | ||
required-features = [] | ||
|
||
[[bench]] | ||
name = "proto" | ||
path = "benches/encoding/proto.rs" | ||
harness = false | ||
required-features = ["protobuf"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Benchmark inspired by | ||
// 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::metrics::counter::Counter; | ||
use prometheus_client::metrics::family::Family; | ||
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram}; | ||
use prometheus_client::registry::Registry; | ||
use std::fmt::{Display, Formatter}; | ||
|
||
pub fn proto(c: &mut Criterion) { | ||
c.bench_function("encode", |b| { | ||
#[derive(Clone, Hash, PartialEq, Eq, Encode)] | ||
struct Labels { | ||
path: String, | ||
method: Method, | ||
some_number: u64, | ||
} | ||
|
||
#[derive(Clone, Hash, PartialEq, Eq, Encode)] | ||
enum Method { | ||
Get, | ||
#[allow(dead_code)] | ||
Put, | ||
} | ||
|
||
impl Display for Method { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Method::Get => write!(f, "Get"), | ||
Method::Put => write!(f, "Put"), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Hash, PartialEq, Eq, Encode)] | ||
enum Region { | ||
Africa, | ||
#[allow(dead_code)] | ||
Asia, | ||
} | ||
|
||
let mut registry = Registry::<Box<dyn EncodeMetric>>::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)) | ||
}); | ||
|
||
registry.register( | ||
format!("my_counter{}", i), | ||
"My counter", | ||
Box::new(counter_family.clone()), | ||
); | ||
registry.register( | ||
format!("my_histogram{}", i), | ||
"My histogram", | ||
Box::new(histogram_family.clone()), | ||
); | ||
|
||
for j in 0_u32..100 { | ||
counter_family | ||
.get_or_create(&Labels { | ||
path: format!("/path/{}", i), | ||
method: Method::Get, | ||
some_number: j.into(), | ||
}) | ||
.inc(); | ||
|
||
histogram_family | ||
.get_or_create(&Region::Africa) | ||
.observe(j.into()); | ||
} | ||
} | ||
|
||
b.iter(|| { | ||
let metric_set = encode(®istry); | ||
black_box(metric_set); | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group!(benches, proto); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use std::io::Result; | ||
|
||
fn main() -> Result<()> { | ||
#[cfg(feature = "protobuf")] | ||
prost_build::compile_protos( | ||
&["src/encoding/proto/openmetrics_data_model.proto"], | ||
&["src/encoding/proto/"], | ||
)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
[package] | ||
name = "prometheus-client-derive-text-encode" | ||
version = "0.3.0" | ||
name = "prometheus-client-derive-encode" | ||
version = "0.3.1" | ||
authors = ["Max Inden <[email protected]>"] | ||
edition = "2021" | ||
description = "Auxiliary crate to derive text Encode trait from prometheus-client." | ||
description = "Auxiliary crate to derive Encode trait from prometheus-client." | ||
license = "Apache-2.0 OR MIT" | ||
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] | ||
|
@@ -17,7 +20,7 @@ quote = "1" | |
syn = "1" | ||
|
||
[dev-dependencies] | ||
prometheus-client = { path = "../" } | ||
prometheus-client = { path = "../", features = ["protobuf"] } | ||
|
||
[lib] | ||
proc-macro = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.