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

[opentelemetry-prometheus]: Prepare 0.17.0 release using opentelemetry 0.24 #1957

Merged
merged 1 commit into from
Jul 24, 2024
Merged
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
8 changes: 8 additions & 0 deletions opentelemetry-prometheus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,14 @@

## vNext

## v0.17.0

### Changed

- Update `opentelemetry` dependency version to 0.24
- Update `opentelemetry_sdk` dependency version to 0.24
- Update `opentelemetry-semantic-conventions` dependency version to 0.16

## v0.16.0

### Added
8 changes: 4 additions & 4 deletions opentelemetry-prometheus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "opentelemetry-prometheus"
version = "0.16.0"
version = "0.17.0"
description = "Prometheus exporter for OpenTelemetry"
homepage = "https://github.com/open-telemetry/opentelemetry-rust"
repository = "https://github.com/open-telemetry/opentelemetry-rust"
@@ -21,13 +21,13 @@ rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
once_cell = { workspace = true }
opentelemetry = { version = "0.23", default-features = false, features = ["metrics"] }
opentelemetry_sdk = { version = "0.23", default-features = false, features = ["metrics"] }
opentelemetry = { version = "0.24", default-features = false, features = ["metrics"] }
opentelemetry_sdk = { version = "0.24", default-features = false, features = ["metrics"] }
prometheus = "0.13"
protobuf = "2.14"

[dev-dependencies]
opentelemetry-semantic-conventions = { version = "0.15" }
opentelemetry-semantic-conventions = { version = "0.16" }
http-body-util = { workspace = true }
hyper = { workspace = true, features = ["full"] }
hyper-util = { workspace = true, features = ["full"] }
2 changes: 1 addition & 1 deletion opentelemetry-prometheus/README.md
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
[`Prometheus`] integration for applications instrumented with [`OpenTelemetry`].

**The development of prometheus exporter has halt until the Opentelemetry metrics API and SDK reaches 1.0. Current
implementation is based on Opentelemetry API and SDK 0.23**.
implementation is based on Opentelemetry API and SDK 0.24**.

[![Crates.io: opentelemetry-prometheus](https://img.shields.io/crates/v/opentelemetry-prometheus.svg)](https://crates.io/crates/opentelemetry-prometheus)
[![Documentation](https://docs.rs/opentelemetry-prometheus/badge.svg)](https://docs.rs/opentelemetry-prometheus)
6 changes: 3 additions & 3 deletions opentelemetry-prometheus/examples/hyper.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ use hyper::{
use hyper_util::rt::{TokioExecutor, TokioIo};
use once_cell::sync::Lazy;
use opentelemetry::{
metrics::{Counter, Histogram, MeterProvider as _, Unit},
metrics::{Counter, Histogram, MeterProvider as _},
KeyValue,
};
use opentelemetry_sdk::metrics::SdkMeterProvider;
@@ -88,12 +88,12 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.init(),
http_body_gauge: meter
.u64_histogram("example.http_response_size")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("The metrics HTTP response sizes in bytes.")
.init(),
http_req_histogram: meter
.f64_histogram("example.http_request_duration")
.with_unit(Unit::new("ms"))
.with_unit("ms")
.with_description("The HTTP request latencies in milliseconds.")
.init(),
});
15 changes: 12 additions & 3 deletions opentelemetry-prometheus/src/lib.rs
Original file line number Diff line number Diff line change
@@ -457,7 +457,10 @@
// See: https://github.com/tikv/rust-prometheus/issues/393

for dp in &histogram.data_points {
let kvs = get_attrs(&mut dp.attributes.iter(), extra);
let kvs = get_attrs(
&mut dp.attributes.iter().map(|kv| (&kv.key, &kv.value)),
extra,
);
let bounds_len = dp.bounds.len();
let (bucket, _) = dp.bounds.iter().enumerate().fold(
(Vec::with_capacity(bounds_len), 0),
@@ -503,7 +506,10 @@
};

for dp in &sum.data_points {
let kvs = get_attrs(&mut dp.attributes.iter(), extra);
let kvs = get_attrs(
&mut dp.attributes.iter().map(|kv| (&kv.key, &kv.value)),
extra,
);

let mut pm = prometheus::proto::Metric::default();
pm.set_label(protobuf::RepeatedField::from_vec(kvs));
@@ -535,7 +541,10 @@
name: Cow<'static, str>,
) {
for dp in &gauge.data_points {
let kvs = get_attrs(&mut dp.attributes.iter(), extra);
let kvs = get_attrs(
&mut dp.attributes.iter().map(|kv| (&kv.key, &kv.value)),
extra,
);

Check warning on line 547 in opentelemetry-prometheus/src/lib.rs

Codecov / codecov/patch

opentelemetry-prometheus/src/lib.rs#L544-L547

Added lines #L544 - L547 were not covered by tests

let mut g = prometheus::proto::Gauge::default();
g.set_value(dp.value.as_f64());
2 changes: 1 addition & 1 deletion opentelemetry-prometheus/src/resource_selector.rs
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ impl ResourceSelector {
ResourceSelector::All => get_attrs(&mut resource.iter(), &[]),
ResourceSelector::None => Vec::new(),
ResourceSelector::KeyAllowList(keys) => {
get_attrs(&mut resource.iter().filter(|(k, _)| keys.contains(k)), &[])
get_attrs(&mut resource.iter().filter(|(k, _)| keys.contains(*k)), &[])
}
}
}
14 changes: 6 additions & 8 deletions opentelemetry-prometheus/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use opentelemetry::metrics::Unit;
use std::borrow::Cow;

const NON_APPLICABLE_ON_PER_UNIT: [&str; 8] = ["1", "d", "h", "min", "s", "ms", "us", "ns"];

pub(crate) fn get_unit_suffixes(unit: &Unit) -> Option<Cow<'static, str>> {
pub(crate) fn get_unit_suffixes(unit: &str) -> Option<Cow<'static, str>> {
// no unit return early
if unit.as_str().is_empty() {
if unit.is_empty() {
return None;
}

// direct match with known units
if let Some(matched) = get_prom_units(unit.as_str()) {
if let Some(matched) = get_prom_units(unit) {
return Some(Cow::Borrowed(matched));
}

@@ -20,7 +19,7 @@ pub(crate) fn get_unit_suffixes(unit: &Unit) -> Option<Cow<'static, str>> {
// e.g
// "test/y" => "per_year"
// "km/s" => "kilometers_per_second"
if let Some((first, second)) = unit.as_str().split_once('/') {
if let Some((first, second)) = unit.split_once('/') {
return match (
NON_APPLICABLE_ON_PER_UNIT.contains(&first),
get_prom_units(first),
@@ -193,9 +192,8 @@ mod tests {
// annotations
("{request}", None),
];
for (unit_str, expected_suffix) in test_cases {
let unit = Unit::new(unit_str);
assert_eq!(get_unit_suffixes(&unit), expected_suffix);
for (unit, expected_suffix) in test_cases {
assert_eq!(get_unit_suffixes(unit), expected_suffix);
}
}
}
68 changes: 34 additions & 34 deletions opentelemetry-prometheus/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use std::fs;
use std::path::Path;
use std::time::Duration;

use opentelemetry::metrics::{Meter, MeterProvider as _, Unit};
use opentelemetry::metrics::{Meter, MeterProvider as _};
use opentelemetry::Key;
use opentelemetry::KeyValue;
use opentelemetry_prometheus::{ExporterBuilder, ResourceSelector};
@@ -54,7 +54,7 @@ fn prometheus_exporter_integration() {
let counter = meter
.f64_counter("foo")
.with_description("a simple counter")
.with_unit(Unit::new("ms"))
.with_unit("ms")
.init();
counter.add(5.0, &attrs);
counter.add(10.3, &attrs);
@@ -83,7 +83,7 @@ fn prometheus_exporter_integration() {
let counter = meter
.f64_counter("foo")
.with_description("a simple counter without a total suffix")
.with_unit(Unit::new("ms"))
.with_unit("ms")
.init();
counter.add(5.0, &attrs);
counter.add(10.3, &attrs);
@@ -106,7 +106,7 @@ fn prometheus_exporter_integration() {
let gauge = meter
.f64_up_down_counter("bar")
.with_description("a fun little gauge")
.with_unit(Unit::new("1"))
.with_unit("1")
.init();
gauge.add(1.0, &attrs);
gauge.add(-0.25, &attrs);
@@ -121,7 +121,7 @@ fn prometheus_exporter_integration() {
let histogram = meter
.f64_histogram("histogram_baz")
.with_description("a very nice histogram")
.with_unit(Unit::new("By"))
.with_unit("By")
.init();
histogram.record(23.0, &attrs);
histogram.record(7.0, &attrs);
@@ -147,7 +147,7 @@ fn prometheus_exporter_integration() {
.f64_counter("foo")
.with_description("a sanitary counter")
// This unit is not added to
.with_unit(Unit::new("By"))
.with_unit("By")
.init();
counter.add(5.0, &attrs);
counter.add(10.3, &attrs);
@@ -261,7 +261,7 @@ fn prometheus_exporter_integration() {
let gauge = meter
.i64_up_down_counter("bar")
.with_description("a fun little gauge")
.with_unit(Unit::new("1"))
.with_unit("1")
.init();
gauge.add(2, &attrs);
gauge.add(-1, &attrs);
@@ -279,7 +279,7 @@ fn prometheus_exporter_integration() {
let counter = meter
.u64_counter("bar")
.with_description("a fun little counter")
.with_unit(Unit::new("By"))
.with_unit("By")
.init();
counter.add(2, &attrs);
counter.add(1, &attrs);
@@ -317,7 +317,7 @@ fn prometheus_exporter_integration() {
let gauge = meter
.i64_up_down_counter("bar")
.with_description("a fun little gauge")
.with_unit(Unit::new("1"))
.with_unit("1")
.init();
gauge.add(2, &attrs);
gauge.add(-1, &attrs);
@@ -334,7 +334,7 @@ fn prometheus_exporter_integration() {
let gauge = meter
.i64_up_down_counter("bar")
.with_description("a fun little gauge")
.with_unit(Unit::new("1"))
.with_unit("1")
.init();
gauge.add(2, &attrs);
gauge.add(-1, &attrs);
@@ -456,7 +456,7 @@ fn multiple_scopes() {
Some(vec![KeyValue::new("k", "v")]),
)
.u64_counter("foo")
.with_unit(Unit::new("ms"))
.with_unit("ms")
.with_description("meter foo counter")
.init();
foo_counter.add(100, &[KeyValue::new("type", "foo")]);
@@ -469,7 +469,7 @@ fn multiple_scopes() {
Some(vec![KeyValue::new("k", "v")]),
)
.u64_counter("bar")
.with_unit(Unit::new("ms"))
.with_unit("ms")
.with_description("meter bar counter")
.init();
bar_counter.add(200, &[KeyValue::new("type", "bar")]);
@@ -507,15 +507,15 @@ fn duplicate_metrics() {
record_metrics: Box::new(|meter_a, meter_b| {
let foo_a = meter_a
.u64_counter("foo")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter counter foo")
.init();

foo_a.add(100, &[KeyValue::new("A", "B")]);

let foo_b = meter_b
.u64_counter("foo")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter counter foo")
.init();

@@ -529,15 +529,15 @@ fn duplicate_metrics() {
record_metrics: Box::new(|meter_a, meter_b| {
let foo_a = meter_a
.i64_up_down_counter("foo")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter gauge foo")
.init();

foo_a.add(100, &[KeyValue::new("A", "B")]);

let foo_b = meter_b
.i64_up_down_counter("foo")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter gauge foo")
.init();

@@ -551,15 +551,15 @@ fn duplicate_metrics() {
record_metrics: Box::new(|meter_a, meter_b| {
let foo_a = meter_a
.u64_histogram("foo")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter histogram foo")
.init();

foo_a.record(100, &[KeyValue::new("A", "B")]);

let foo_b = meter_b
.u64_histogram("foo")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter histogram foo")
.init();

@@ -573,15 +573,15 @@ fn duplicate_metrics() {
record_metrics: Box::new(|meter_a, meter_b| {
let bar_a = meter_a
.u64_counter("bar")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter a bar")
.init();

bar_a.add(100, &[KeyValue::new("type", "bar")]);

let bar_b = meter_b
.u64_counter("bar")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter b bar")
.init();

@@ -598,15 +598,15 @@ fn duplicate_metrics() {
record_metrics: Box::new(|meter_a, meter_b| {
let bar_a = meter_a
.i64_up_down_counter("bar")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter a bar")
.init();

bar_a.add(100, &[KeyValue::new("type", "bar")]);

let bar_b = meter_b
.i64_up_down_counter("bar")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter b bar")
.init();

@@ -623,15 +623,15 @@ fn duplicate_metrics() {
record_metrics: Box::new(|meter_a, meter_b| {
let bar_a = meter_a
.u64_histogram("bar")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter a bar")
.init();

bar_a.record(100, &[KeyValue::new("A", "B")]);

let bar_b = meter_b
.u64_histogram("bar")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter b bar")
.init();

@@ -648,15 +648,15 @@ fn duplicate_metrics() {
record_metrics: Box::new(|meter_a, meter_b| {
let baz_a = meter_a
.u64_counter("bar")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter bar")
.init();

baz_a.add(100, &[KeyValue::new("type", "bar")]);

let baz_b = meter_b
.u64_counter("bar")
.with_unit(Unit::new("ms"))
.with_unit("ms")
.with_description("meter bar")
.init();

@@ -671,15 +671,15 @@ fn duplicate_metrics() {
record_metrics: Box::new(|meter_a, meter_b| {
let bar_a = meter_a
.i64_up_down_counter("bar")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter gauge bar")
.init();

bar_a.add(100, &[KeyValue::new("type", "bar")]);

let bar_b = meter_b
.i64_up_down_counter("bar")
.with_unit(Unit::new("ms"))
.with_unit("ms")
.with_description("meter gauge bar")
.init();

@@ -694,15 +694,15 @@ fn duplicate_metrics() {
record_metrics: Box::new(|meter_a, meter_b| {
let bar_a = meter_a
.u64_histogram("bar")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter histogram bar")
.init();

bar_a.record(100, &[KeyValue::new("A", "B")]);

let bar_b = meter_b
.u64_histogram("bar")
.with_unit(Unit::new("ms"))
.with_unit("ms")
.with_description("meter histogram bar")
.init();

@@ -717,15 +717,15 @@ fn duplicate_metrics() {
record_metrics: Box::new(|meter_a, _meter_b| {
let counter = meter_a
.u64_counter("foo")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter foo")
.init();

counter.add(100, &[KeyValue::new("type", "foo")]);

let gauge = meter_a
.i64_up_down_counter("foo_total")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter foo")
.init();

@@ -743,15 +743,15 @@ fn duplicate_metrics() {
record_metrics: Box::new(|meter_a, _meter_b| {
let foo_a = meter_a
.i64_up_down_counter("foo")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter gauge foo")
.init();

foo_a.add(100, &[KeyValue::new("A", "B")]);

let foo_histogram_a = meter_a
.u64_histogram("foo")
.with_unit(Unit::new("By"))
.with_unit("By")
.with_description("meter histogram foo")
.init();