Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from protobuf to prost #387

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ features = ["nightly"]

[features]
default = ["protobuf"]
gen = ["protobuf-codegen-pure"]
gen = ["prost-build"]
nightly = ["libc"]
process = ["libc", "procfs"]
protobuf = ["prost"]
push = ["reqwest", "libc", "protobuf"]

[dependencies]
Expand All @@ -30,7 +31,7 @@ fnv = "^1.0"
lazy_static = "^1.4"
libc = { version = "^0.2", optional = true }
parking_lot = "^0.11"
protobuf = { version = "^2.0", optional = true }
prost = { version = "^0.7", optional = true }
regex = "^1.3"
reqwest = { version = "^0.11", features = ["blocking"], optional = true }
thiserror = "^1.0"
Expand All @@ -45,7 +46,7 @@ hyper = { version = "^0.14", features = ["server", "http1", "tcp"] }
tokio = { version = "^1.0", features = ["macros", "rt-multi-thread"] }

[build-dependencies]
protobuf-codegen-pure = { version = "^2.0", optional = true }
prost-build = { version = "^0.7", optional = true }

[workspace]
members = ["static-metric"]
Expand Down
14 changes: 7 additions & 7 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

#[cfg(feature = "gen")]
fn generate_protobuf_binding_file() {
protobuf_codegen_pure::run(protobuf_codegen_pure::Args {
out_dir: "proto",
input: &["proto/proto_model.proto"],
includes: &["proto"],
..Default::default()
})
.unwrap();
use prost_build::Config;
use std::path::Path;

let mut cfg = Config::new();
cfg.out_dir(Path::new("proto"));
cfg.compile_protos(&["proto/proto_model.proto"], &["proto"])
.unwrap();
}

#[cfg(not(feature = "gen"))]
Expand Down
94 changes: 94 additions & 0 deletions proto/io.prometheus.client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#[derive(Clone, PartialEq, ::prost::Message)]
Folyd marked this conversation as resolved.
Show resolved Hide resolved
pub struct LabelPair {
#[prost(string, optional, tag="1")]
pub name: ::core::option::Option<::prost::alloc::string::String>,
#[prost(string, optional, tag="2")]
pub value: ::core::option::Option<::prost::alloc::string::String>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Gauge {
#[prost(double, optional, tag="1")]
pub value: ::core::option::Option<f64>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Counter {
#[prost(double, optional, tag="1")]
pub value: ::core::option::Option<f64>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Quantile {
#[prost(double, optional, tag="1")]
pub quantile: ::core::option::Option<f64>,
#[prost(double, optional, tag="2")]
pub value: ::core::option::Option<f64>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Summary {
#[prost(uint64, optional, tag="1")]
pub sample_count: ::core::option::Option<u64>,
#[prost(double, optional, tag="2")]
pub sample_sum: ::core::option::Option<f64>,
#[prost(message, repeated, tag="3")]
pub quantile: ::prost::alloc::vec::Vec<Quantile>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Untyped {
#[prost(double, optional, tag="1")]
pub value: ::core::option::Option<f64>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Histogram {
#[prost(uint64, optional, tag="1")]
pub sample_count: ::core::option::Option<u64>,
#[prost(double, optional, tag="2")]
pub sample_sum: ::core::option::Option<f64>,
/// Ordered in increasing order of upper_bound, +Inf bucket is optional.
#[prost(message, repeated, tag="3")]
pub bucket: ::prost::alloc::vec::Vec<Bucket>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Bucket {
/// Cumulative in increasing order.
#[prost(uint64, optional, tag="1")]
pub cumulative_count: ::core::option::Option<u64>,
/// Inclusive.
#[prost(double, optional, tag="2")]
pub upper_bound: ::core::option::Option<f64>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Metric {
#[prost(message, repeated, tag="1")]
pub label: ::prost::alloc::vec::Vec<LabelPair>,
#[prost(message, optional, tag="2")]
pub gauge: ::core::option::Option<Gauge>,
#[prost(message, optional, tag="3")]
pub counter: ::core::option::Option<Counter>,
#[prost(message, optional, tag="4")]
pub summary: ::core::option::Option<Summary>,
#[prost(message, optional, tag="5")]
pub untyped: ::core::option::Option<Untyped>,
#[prost(message, optional, tag="7")]
pub histogram: ::core::option::Option<Histogram>,
#[prost(int64, optional, tag="6")]
pub timestamp_ms: ::core::option::Option<i64>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct MetricFamily {
#[prost(string, optional, tag="1")]
pub name: ::core::option::Option<::prost::alloc::string::String>,
#[prost(string, optional, tag="2")]
pub help: ::core::option::Option<::prost::alloc::string::String>,
#[prost(enumeration="MetricType", optional, tag="3")]
pub r#type: ::core::option::Option<i32>,
#[prost(message, repeated, tag="4")]
pub metric: ::prost::alloc::vec::Vec<Metric>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum MetricType {
Counter = 0,
Gauge = 1,
Summary = 2,
Untyped = 3,
Histogram = 4,
}
14 changes: 7 additions & 7 deletions src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<P: Atomic> GenericCounterVec<P> {
let variable_names = label_names.iter().map(|s| (*s).to_owned()).collect();
let opts = opts.variable_labels(variable_names);
let metric_vec =
MetricVec::create(proto::MetricType::COUNTER, CounterVecBuilder::new(), opts)?;
MetricVec::create(proto::MetricType::Counter, CounterVecBuilder::new(), opts)?;

Ok(metric_vec as Self)
}
Expand Down Expand Up @@ -343,9 +343,9 @@ mod tests {
assert_eq!(mfs.len(), 1);

let mf = mfs.pop().unwrap();
let m = mf.get_metric().get(0).unwrap();
assert_eq!(m.get_label().len(), 2);
assert_eq!(m.get_counter().get_value() as u64, 43);
let m = mf.metric.get(0).unwrap();
assert_eq!(m.label.len(), 2);
assert_eq!(m.counter.as_ref().unwrap().value.unwrap_or(0.0) as u64, 43);

counter.reset();
assert_eq!(counter.get() as u64, 0);
Expand All @@ -363,9 +363,9 @@ mod tests {
assert_eq!(mfs.len(), 1);

let mf = mfs.pop().unwrap();
let m = mf.get_metric().get(0).unwrap();
assert_eq!(m.get_label().len(), 0);
assert_eq!(m.get_counter().get_value() as u64, 12);
let m = mf.metric.get(0).unwrap();
assert_eq!(m.label.len(), 0);
assert_eq!(m.counter.as_ref().unwrap().value.unwrap_or(0.0) as u64, 12);

counter.reset();
assert_eq!(counter.get() as u64, 0);
Expand Down
7 changes: 4 additions & 3 deletions src/desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,10 @@ impl Desc {
desc.dim_hash = lh.finish();

for (key, value) in const_labels {
let mut label_pair = LabelPair::default();
label_pair.set_name(key);
label_pair.set_value(value);
let label_pair = LabelPair {
name: Some(key),
value: Some(value),
};
desc.const_label_pairs.push(label_pair);
}

Expand Down
8 changes: 4 additions & 4 deletions src/encoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ pub trait Encoder {
}

fn check_metric_family(mf: &MetricFamily) -> Result<()> {
if mf.get_metric().is_empty() {
if mf.metric.is_empty() {
return Err(Error::Msg(format!("MetricFamily has no metrics: {:?}", mf)));
}
if mf.get_name().is_empty() {
if mf.name.is_none() {
return Err(Error::Msg(format!("MetricFamily has no name: {:?}", mf)));
}
Ok(())
Expand Down Expand Up @@ -67,7 +67,7 @@ mod tests {
let mut mfs = cv.collect();

// Empty name
(&mut mfs[0]).clear_name();
(&mut mfs[0]).name.take();
check_metric_family(&mfs[0]).unwrap_err();
pb_encoder.encode(&mfs, &mut writer).unwrap_err();
assert_eq!(writer.len(), 0);
Expand All @@ -94,7 +94,7 @@ mod tests {
let mut mfs = cv.collect();

// Empty name
(&mut mfs[0]).clear_name();
(&mut mfs[0]).name.take();
check_metric_family(&mfs[0]).unwrap_err();
text_encoder.encode(&mfs, &mut writer).unwrap_err();
assert_eq!(writer.len(), 0);
Expand Down
6 changes: 4 additions & 2 deletions src/encoder/pb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::io::Write;

use protobuf::Message;
use prost::Message;

use crate::errors::Result;
use crate::proto::MetricFamily;
Expand Down Expand Up @@ -31,7 +31,9 @@ impl Encoder for ProtobufEncoder {
for mf in metric_families {
// Fail-fast checks.
check_metric_family(mf)?;
mf.write_length_delimited_to_writer(writer)?;
let mut buf = vec![];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bummer that we need this intermediary buffer. We could change the Encoder trait, requiring writer to also implement BufMut. But that would require a new dependency bytes on our public interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. Actually, I did change to the BufMut trait in my local branch. I'm haven't submit that commit right now. What do you think? @breeswish @hdost

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep non-protobuf feature clean.. This immediate buffer only allocate once each encode call and I think the performance seems to be fine.

mf.encode_length_delimited(&mut buf)?;
writer.write_all(buf.as_slice())?;
}
Ok(())
}
Expand Down