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
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.25.0]

### Added

- Added an off-by-default `protobuf-protox` feature to build protobuf support
without requiring an external `protoc` binary.

### Changed

- Updated `prost`, `prost-build`, and `prost-types` dependencies to `v0.14`.
- The `prometheus_protobuf` feature generates and encodes Prometheus
`io.prometheus.client` protobuf messages from `metrics.proto` rather than the
OpenMetrics protobuf data model.
See [Issue](https://github.com/prometheus/OpenMetrics/issues/296) for more context.
The `protobuf` and `openmetrics_protobuf` features retain the old, deprecated, OpenMetrics protobuf support which will
be removed in a future release.

## [0.24.1]

### Added
Expand Down
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The `build.rs` script in this library depends upon the
[Protocol Buffers compiler][protoc]. Be sure that `protoc` is installed and
available within your `PATH`.

If you enable the off-by-default `protobuf-protox` feature, the build uses
`protox` instead and does not require `protoc`.

[protoc]: https://docs.rs/prost-build/latest/prost_build/#sourcing-protoc

## Python Dependencies
Expand Down
16 changes: 10 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prometheus-client"
version = "0.24.1"
version = "0.25.0"
authors = ["Max Inden <mail@max-inden.de>"]
edition = "2021"
description = "Open Metrics client library allowing users to natively instrument applications."
Expand All @@ -12,7 +12,10 @@ documentation = "https://docs.rs/prometheus-client"

[features]
default = []
protobuf = ["dep:prost", "dep:prost-types", "dep:prost-build"]
prometheus_protobuf = ["dep:prost", "dep:prost-types", "dep:prost-build"]
openmetrics_protobuf = ["dep:prost", "dep:prost-types", "dep:prost-build"]
protobuf = ["openmetrics_protobuf"]
protobuf-protox = ["dep:protox"]

# This feature provides additional APIs for testing downstream code using
# `prometheus-client`.
Expand All @@ -29,8 +32,8 @@ dtoa = "1.0"
itoa = "1.0"
parking_lot = "0.12"
prometheus-client-derive-encode = { version = "0.5.0", path = "derive-encode" }
prost = { version = "0.12.0", optional = true }
prost-types = { version = "0.12.0", optional = true }
prost = { version = "0.14", optional = true }
prost-types = { version = "0.14", optional = true }

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

[build-dependencies]
prost-build = { version = "0.12.0", optional = true }
prost-build = { version = "0.14", optional = true }
protox = { version = "0.9.1", optional = true }

[[bench]]
name = "baseline"
Expand All @@ -73,7 +77,7 @@ required-features = []
name = "proto"
path = "benches/encoding/proto.rs"
harness = false
required-features = ["protobuf"]
required-features = ["prometheus_protobuf"]

# Passing arguments to the docsrs builder in order to properly document cfg's.
# More information: https://docs.rs/about/builds#cross-compiling
Expand Down
4 changes: 2 additions & 2 deletions benches/encoding/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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::protobuf;
use prometheus_client::encoding::prometheus_protobuf;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
Expand Down Expand Up @@ -75,7 +75,7 @@ pub fn proto(c: &mut Criterion) {
}

b.iter(|| {
let metric_set = protobuf::encode(&registry).unwrap();
let metric_set = prometheus_protobuf::encode(&registry).unwrap();
black_box(metric_set);
})
});
Expand Down
41 changes: 33 additions & 8 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
use std::io::Result;

fn main() -> Result<()> {
#[cfg(feature = "protobuf")]
prost_build::compile_protos(
&["src/encoding/proto/openmetrics_data_model.proto"],
&["src/encoding/proto/"],
)?;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
#[cfg(any(feature = "prometheus_protobuf", feature = "openmetrics_protobuf"))]
compile_protos()?;

Ok(())
}

#[allow(clippy::vec_init_then_push)] // False positive due to feature flags
#[cfg(any(feature = "prometheus_protobuf", feature = "openmetrics_protobuf"))]
fn compile_protos() -> Result<(), Box<dyn Error>> {
let mut protos = Vec::new();

#[cfg(feature = "prometheus_protobuf")]
protos.push("src/encoding/proto/metrics.proto");
#[cfg(feature = "openmetrics_protobuf")]
protos.push("src/encoding/proto/openmetrics_data_model.proto");

let includes = ["src/encoding/proto/"];

#[cfg(feature = "protobuf-protox")]
prost_build::compile_fds(protox::compile(&protos, includes)?)?;

#[cfg(not(feature = "protobuf-protox"))]
prost_build::compile_protos(&protos, &includes)?;

for path in &protos {
println!("cargo:rerun-if-changed={}", path);
}
for path in &includes {
println!("cargo:rerun-if-changed={}", path);
}

Ok(())
}
2 changes: 1 addition & 1 deletion derive-encode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ quote = "1"
syn = "2"

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

[lib]
Expand Down
26 changes: 11 additions & 15 deletions derive-encode/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ fn basic_flow() {

mod protobuf {
use crate::{Labels, Method};
use prometheus_client::encoding::protobuf::encode;
use prometheus_client::encoding::protobuf::openmetrics_data_model;
use prometheus_client::encoding::prometheus_protobuf::encode;
use prometheus_client::encoding::prometheus_protobuf::prometheus_data_model;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::registry::Registry;
Expand All @@ -67,17 +67,15 @@ mod protobuf {
})
.inc();

// Encode all metrics in the registry in the OpenMetrics protobuf format.
let mut metric_set = encode(&registry).unwrap();
let mut family: openmetrics_data_model::MetricFamily =
metric_set.metric_families.pop().unwrap();
let metric: openmetrics_data_model::Metric = family.metrics.pop().unwrap();
let mut metric_families = encode(&registry).unwrap();
let mut family: prometheus_data_model::MetricFamily = metric_families.pop().unwrap();
let metric: prometheus_data_model::Metric = family.metric.pop().unwrap();

let method = &metric.labels[0];
let method = &metric.label[0];
assert_eq!("method", method.name);
assert_eq!("Get", method.value);

let path = &metric.labels[1];
let path = &metric.label[1];
assert_eq!("path", path.name);
assert_eq!("/metrics", path.value);
}
Expand All @@ -96,13 +94,11 @@ mod protobuf {
})
.inc();

// Encode all metrics in the registry in the OpenMetrics protobuf format.
let mut metric_set = encode(&registry).unwrap();
let mut family: openmetrics_data_model::MetricFamily =
metric_set.metric_families.pop().unwrap();
let metric: openmetrics_data_model::Metric = family.metrics.pop().unwrap();
let mut metric_families = encode(&registry).unwrap();
let mut family: prometheus_data_model::MetricFamily = metric_families.pop().unwrap();
let metric: prometheus_data_model::Metric = family.metric.pop().unwrap();

let label = &metric.labels[0];
let label = &metric.label[0];
assert_eq!("method", label.name);
assert_eq!("Get", label.value);
}
Expand Down
Loading
Loading