Skip to content

Commit 45fed14

Browse files
authored
protobuf: move from OpenMetrics to Prometheus proto (#298)
* dependencies: update prost to 0.14 Signed-off-by: John Howard <john.howard@solo.io> * protobuf: allow compiling without `protoc` Our goal is to have our builds compile without external dependencies. `protox` helps achieve this. Signed-off-by: John Howard <john.howard@solo.io> * protobuf: move from openmetrics to protobuf Signed-off-by: John Howard <john.howard@solo.io> * Update changelog Signed-off-by: John Howard <john.howard@solo.io> * Bring back legacy proto Signed-off-by: John Howard <john.howard@solo.io> * Retain old 'protobuf' feature, make explicit types Signed-off-by: John Howard <john.howard@solo.io> --------- Signed-off-by: John Howard <john.howard@solo.io>
1 parent 4a6d40a commit 45fed14

12 files changed

Lines changed: 1238 additions & 87 deletions

File tree

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ 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.25.0]
8+
9+
### Added
10+
11+
- Added an off-by-default `protobuf-protox` feature to build protobuf support
12+
without requiring an external `protoc` binary.
13+
14+
### Changed
15+
16+
- Updated `prost`, `prost-build`, and `prost-types` dependencies to `v0.14`.
17+
- The `prometheus_protobuf` feature generates and encodes Prometheus
18+
`io.prometheus.client` protobuf messages from `metrics.proto` rather than the
19+
OpenMetrics protobuf data model.
20+
See [Issue](https://github.com/prometheus/OpenMetrics/issues/296) for more context.
21+
The `protobuf` and `openmetrics_protobuf` features retain the old, deprecated, OpenMetrics protobuf support which will
22+
be removed in a future release.
23+
724
## [0.24.1]
825

926
### Added

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The `build.rs` script in this library depends upon the
66
[Protocol Buffers compiler][protoc]. Be sure that `protoc` is installed and
77
available within your `PATH`.
88

9+
If you enable the off-by-default `protobuf-protox` feature, the build uses
10+
`protox` instead and does not require `protoc`.
11+
912
[protoc]: https://docs.rs/prost-build/latest/prost_build/#sourcing-protoc
1013

1114
## Python Dependencies

Cargo.toml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "prometheus-client"
3-
version = "0.24.1"
3+
version = "0.25.0"
44
authors = ["Max Inden <mail@max-inden.de>"]
55
edition = "2021"
66
description = "Open Metrics client library allowing users to natively instrument applications."
@@ -12,7 +12,10 @@ documentation = "https://docs.rs/prometheus-client"
1212

1313
[features]
1414
default = []
15-
protobuf = ["dep:prost", "dep:prost-types", "dep:prost-build"]
15+
prometheus_protobuf = ["dep:prost", "dep:prost-types", "dep:prost-build"]
16+
openmetrics_protobuf = ["dep:prost", "dep:prost-types", "dep:prost-build"]
17+
protobuf = ["openmetrics_protobuf"]
18+
protobuf-protox = ["dep:protox"]
1619

1720
# This feature provides additional APIs for testing downstream code using
1821
# `prometheus-client`.
@@ -29,8 +32,8 @@ dtoa = "1.0"
2932
itoa = "1.0"
3033
parking_lot = "0.12"
3134
prometheus-client-derive-encode = { version = "0.5.0", path = "derive-encode" }
32-
prost = { version = "0.12.0", optional = true }
33-
prost-types = { version = "0.12.0", optional = true }
35+
prost = { version = "0.14", optional = true }
36+
prost-types = { version = "0.14", optional = true }
3437

3538
[dev-dependencies]
3639
async-std = { version = "1", features = ["attributes"] }
@@ -49,7 +52,8 @@ hyper-util = { version = "0.1.3", features = ["tokio"] }
4952
http-body-util = "0.1.1"
5053

5154
[build-dependencies]
52-
prost-build = { version = "0.12.0", optional = true }
55+
prost-build = { version = "0.14", optional = true }
56+
protox = { version = "0.9.1", optional = true }
5357

5458
[[bench]]
5559
name = "baseline"
@@ -73,7 +77,7 @@ required-features = []
7377
name = "proto"
7478
path = "benches/encoding/proto.rs"
7579
harness = false
76-
required-features = ["protobuf"]
80+
required-features = ["prometheus_protobuf"]
7781

7882
# Passing arguments to the docsrs builder in order to properly document cfg's.
7983
# More information: https://docs.rs/about/builds#cross-compiling

benches/encoding/proto.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// https://github.com/tikv/rust-prometheus/blob/ab1ca7285d3463504381a5025ae1951e020d6796/benches/text_encoder.rs:write
33

44
use criterion::{black_box, criterion_group, criterion_main, Criterion};
5-
use prometheus_client::encoding::protobuf;
5+
use prometheus_client::encoding::prometheus_protobuf;
66
use prometheus_client::metrics::counter::Counter;
77
use prometheus_client::metrics::family::Family;
88
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
@@ -75,7 +75,7 @@ pub fn proto(c: &mut Criterion) {
7575
}
7676

7777
b.iter(|| {
78-
let metric_set = protobuf::encode(&registry).unwrap();
78+
let metric_set = prometheus_protobuf::encode(&registry).unwrap();
7979
black_box(metric_set);
8080
})
8181
});

build.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
1-
use std::io::Result;
2-
3-
fn main() -> Result<()> {
4-
#[cfg(feature = "protobuf")]
5-
prost_build::compile_protos(
6-
&["src/encoding/proto/openmetrics_data_model.proto"],
7-
&["src/encoding/proto/"],
8-
)?;
1+
use std::error::Error;
2+
3+
fn main() -> Result<(), Box<dyn Error>> {
4+
#[cfg(any(feature = "prometheus_protobuf", feature = "openmetrics_protobuf"))]
5+
compile_protos()?;
6+
7+
Ok(())
8+
}
9+
10+
#[allow(clippy::vec_init_then_push)] // False positive due to feature flags
11+
#[cfg(any(feature = "prometheus_protobuf", feature = "openmetrics_protobuf"))]
12+
fn compile_protos() -> Result<(), Box<dyn Error>> {
13+
let mut protos = Vec::new();
14+
15+
#[cfg(feature = "prometheus_protobuf")]
16+
protos.push("src/encoding/proto/metrics.proto");
17+
#[cfg(feature = "openmetrics_protobuf")]
18+
protos.push("src/encoding/proto/openmetrics_data_model.proto");
19+
20+
let includes = ["src/encoding/proto/"];
21+
22+
#[cfg(feature = "protobuf-protox")]
23+
prost_build::compile_fds(protox::compile(&protos, includes)?)?;
24+
25+
#[cfg(not(feature = "protobuf-protox"))]
26+
prost_build::compile_protos(&protos, &includes)?;
27+
28+
for path in &protos {
29+
println!("cargo:rerun-if-changed={}", path);
30+
}
31+
for path in &includes {
32+
println!("cargo:rerun-if-changed={}", path);
33+
}
934

1035
Ok(())
1136
}

derive-encode/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ quote = "1"
1717
syn = "2"
1818

1919
[dev-dependencies]
20-
prometheus-client = { path = "../", features = ["protobuf"] }
20+
prometheus-client = { path = "../", features = ["prometheus_protobuf"] }
2121
trybuild = "1"
2222

2323
[lib]

derive-encode/tests/lib.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ fn basic_flow() {
4747

4848
mod protobuf {
4949
use crate::{Labels, Method};
50-
use prometheus_client::encoding::protobuf::encode;
51-
use prometheus_client::encoding::protobuf::openmetrics_data_model;
50+
use prometheus_client::encoding::prometheus_protobuf::encode;
51+
use prometheus_client::encoding::prometheus_protobuf::prometheus_data_model;
5252
use prometheus_client::metrics::counter::Counter;
5353
use prometheus_client::metrics::family::Family;
5454
use prometheus_client::registry::Registry;
@@ -67,17 +67,15 @@ mod protobuf {
6767
})
6868
.inc();
6969

70-
// Encode all metrics in the registry in the OpenMetrics protobuf format.
71-
let mut metric_set = encode(&registry).unwrap();
72-
let mut family: openmetrics_data_model::MetricFamily =
73-
metric_set.metric_families.pop().unwrap();
74-
let metric: openmetrics_data_model::Metric = family.metrics.pop().unwrap();
70+
let mut metric_families = encode(&registry).unwrap();
71+
let mut family: prometheus_data_model::MetricFamily = metric_families.pop().unwrap();
72+
let metric: prometheus_data_model::Metric = family.metric.pop().unwrap();
7573

76-
let method = &metric.labels[0];
74+
let method = &metric.label[0];
7775
assert_eq!("method", method.name);
7876
assert_eq!("Get", method.value);
7977

80-
let path = &metric.labels[1];
78+
let path = &metric.label[1];
8179
assert_eq!("path", path.name);
8280
assert_eq!("/metrics", path.value);
8381
}
@@ -96,13 +94,11 @@ mod protobuf {
9694
})
9795
.inc();
9896

99-
// Encode all metrics in the registry in the OpenMetrics protobuf format.
100-
let mut metric_set = encode(&registry).unwrap();
101-
let mut family: openmetrics_data_model::MetricFamily =
102-
metric_set.metric_families.pop().unwrap();
103-
let metric: openmetrics_data_model::Metric = family.metrics.pop().unwrap();
97+
let mut metric_families = encode(&registry).unwrap();
98+
let mut family: prometheus_data_model::MetricFamily = metric_families.pop().unwrap();
99+
let metric: prometheus_data_model::Metric = family.metric.pop().unwrap();
104100

105-
let label = &metric.labels[0];
101+
let label = &metric.label[0];
106102
assert_eq!("method", label.name);
107103
assert_eq!("Get", label.value);
108104
}

0 commit comments

Comments
 (0)