Skip to content

Commit 2bfde32

Browse files
authored
Merge branch 'main' into preallocate-attribute-mem
2 parents b9a90c2 + 383c7d1 commit 2bfde32

File tree

15 files changed

+128
-61
lines changed

15 files changed

+128
-61
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ temp-env = "0.3.6"
4242
thiserror = { version = "1", default-features = false }
4343
tonic = { version = "0.12", default-features = false }
4444
tonic-build = "0.12"
45-
tokio = { version = "1", default-features = false }
45+
tokio = { version = "~1.38.0", default-features = false } #1.39 needs msrv bump to rustc 1.70
4646
tokio-stream = "0.1.1"
4747
tracing = { version = "0.1", default-features = false }
4848
tracing-core = { version = "0.1", default-features = false }

opentelemetry-otlp/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ default = ["grpc-tonic", "trace", "metrics", "logs"]
6565
# grpc using tonic
6666
grpc-tonic = ["tonic", "prost", "http", "tokio", "opentelemetry-proto/gen-tonic"]
6767
gzip-tonic = ["tonic/gzip"]
68+
zstd-tonic = ["tonic/zstd"]
6869
tls = ["tonic/tls"]
6970
tls-roots = ["tls", "tonic/tls-roots"]
7071
tls-webpki-roots = ["tls", "tonic/tls-webpki-roots"]

opentelemetry-otlp/src/exporter/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,15 @@ impl Default for ExportConfig {
9898
pub enum Compression {
9999
/// Compresses data using gzip.
100100
Gzip,
101+
/// Compresses data using zstd.
102+
Zstd,
101103
}
102104

103105
impl Display for Compression {
104106
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
105107
match self {
106108
Compression::Gzip => write!(f, "gzip"),
109+
Compression::Zstd => write!(f, "zstd"),
107110
}
108111
}
109112
}
@@ -114,6 +117,7 @@ impl FromStr for Compression {
114117
fn from_str(s: &str) -> Result<Self, Self::Err> {
115118
match s {
116119
"gzip" => Ok(Compression::Gzip),
120+
"zstd" => Ok(Compression::Zstd),
117121
_ => Err(Error::UnsupportedCompressionAlgorithm(s.to_string())),
118122
}
119123
}

opentelemetry-otlp/src/exporter/tonic/mod.rs

+32-4
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,16 @@ impl TryFrom<Compression> for tonic::codec::CompressionEncoding {
5252
#[cfg(feature = "gzip-tonic")]
5353
Compression::Gzip => Ok(tonic::codec::CompressionEncoding::Gzip),
5454
#[cfg(not(feature = "gzip-tonic"))]
55-
Compression::Gzip => Err(crate::Error::UnsupportedCompressionAlgorithm(
56-
value.to_string(),
55+
Compression::Gzip => Err(crate::Error::FeatureRequiredForCompressionAlgorithm(
56+
"gzip-tonic",
57+
Compression::Gzip,
58+
)),
59+
#[cfg(feature = "zstd-tonic")]
60+
Compression::Zstd => Ok(tonic::codec::CompressionEncoding::Zstd),
61+
#[cfg(not(feature = "zstd-tonic"))]
62+
Compression::Zstd => Err(crate::Error::FeatureRequiredForCompressionAlgorithm(
63+
"zstd-tonic",
64+
Compression::Zstd,
5765
)),
5866
}
5967
}
@@ -399,7 +407,7 @@ fn parse_headers_from_env(signal_headers_var: &str) -> HeaderMap {
399407
#[cfg(test)]
400408
mod tests {
401409
use crate::exporter::tests::run_env_test;
402-
#[cfg(feature = "gzip-tonic")]
410+
#[cfg(feature = "grpc-tonic")]
403411
use crate::exporter::Compression;
404412
use crate::TonicExporterBuilder;
405413
use crate::{OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_TRACES_HEADERS};
@@ -438,14 +446,34 @@ mod tests {
438446

439447
#[test]
440448
#[cfg(feature = "gzip-tonic")]
441-
fn test_with_compression() {
449+
fn test_with_gzip_compression() {
442450
// metadata should merge with the current one with priority instead of just replacing it
443451
let mut metadata = MetadataMap::new();
444452
metadata.insert("foo", "bar".parse().unwrap());
445453
let builder = TonicExporterBuilder::default().with_compression(Compression::Gzip);
446454
assert_eq!(builder.tonic_config.compression.unwrap(), Compression::Gzip);
447455
}
448456

457+
#[test]
458+
#[cfg(feature = "zstd-tonic")]
459+
fn test_with_zstd_compression() {
460+
let builder = TonicExporterBuilder::default().with_compression(Compression::Zstd);
461+
assert_eq!(builder.tonic_config.compression.unwrap(), Compression::Zstd);
462+
}
463+
464+
#[test]
465+
#[cfg(feature = "grpc-tonic")]
466+
fn test_convert_compression() {
467+
#[cfg(feature = "gzip-tonic")]
468+
assert!(tonic::codec::CompressionEncoding::try_from(Compression::Gzip).is_ok());
469+
#[cfg(not(feature = "gzip-tonic"))]
470+
assert!(tonic::codec::CompressionEncoding::try_from(Compression::Gzip).is_err());
471+
#[cfg(feature = "zstd-tonic")]
472+
assert!(tonic::codec::CompressionEncoding::try_from(Compression::Zstd).is_ok());
473+
#[cfg(not(feature = "zstd-tonic"))]
474+
assert!(tonic::codec::CompressionEncoding::try_from(Compression::Zstd).is_err());
475+
}
476+
449477
#[test]
450478
fn test_parse_headers_from_env() {
451479
run_env_test(

opentelemetry-otlp/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
//! For users uses `tonic` as grpc layer:
9898
//! * `grpc-tonic`: Use `tonic` as grpc layer. This is enabled by default.
9999
//! * `gzip-tonic`: Use gzip compression for `tonic` grpc layer.
100+
//! * `zstd-tonic`: Use zstd compression for `tonic` grpc layer.
100101
//! * `tls-tonic`: Enable TLS.
101102
//! * `tls-roots`: Adds system trust roots to rustls-based gRPC clients using the rustls-native-certs crate
102103
//! * `tls-webkpi-roots`: Embeds Mozilla's trust roots to rustls-based gRPC clients using the webkpi-roots crate
@@ -372,6 +373,11 @@ pub enum Error {
372373
/// Unsupported compression algorithm.
373374
#[error("unsupported compression algorithm '{0}'")]
374375
UnsupportedCompressionAlgorithm(String),
376+
377+
/// Feature required to use the specified compression algorithm.
378+
#[cfg(any(not(feature = "gzip-tonic"), not(feature = "zstd-tonic")))]
379+
#[error("feature '{0}' is required to use the compression algorithm '{1}'")]
380+
FeatureRequiredForCompressionAlgorithm(&'static str, Compression),
375381
}
376382

377383
#[cfg(feature = "grpc-tonic")]

opentelemetry-prometheus/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## vNext
44

5+
## v0.17.0
6+
7+
### Changed
8+
9+
- Update `opentelemetry` dependency version to 0.24
10+
- Update `opentelemetry_sdk` dependency version to 0.24
11+
- Update `opentelemetry-semantic-conventions` dependency version to 0.16
12+
513
## v0.16.0
614

715
### Added

opentelemetry-prometheus/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "opentelemetry-prometheus"
3-
version = "0.16.0"
3+
version = "0.17.0"
44
description = "Prometheus exporter for OpenTelemetry"
55
homepage = "https://github.com/open-telemetry/opentelemetry-rust"
66
repository = "https://github.com/open-telemetry/opentelemetry-rust"
@@ -21,13 +21,13 @@ rustdoc-args = ["--cfg", "docsrs"]
2121

2222
[dependencies]
2323
once_cell = { workspace = true }
24-
opentelemetry = { version = "0.23", default-features = false, features = ["metrics"] }
25-
opentelemetry_sdk = { version = "0.23", default-features = false, features = ["metrics"] }
24+
opentelemetry = { version = "0.24", default-features = false, features = ["metrics"] }
25+
opentelemetry_sdk = { version = "0.24", default-features = false, features = ["metrics"] }
2626
prometheus = "0.13"
2727
protobuf = "2.14"
2828

2929
[dev-dependencies]
30-
opentelemetry-semantic-conventions = { version = "0.15" }
30+
opentelemetry-semantic-conventions = { version = "0.16" }
3131
http-body-util = { workspace = true }
3232
hyper = { workspace = true, features = ["full"] }
3333
hyper-util = { workspace = true, features = ["full"] }

opentelemetry-prometheus/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[`Prometheus`] integration for applications instrumented with [`OpenTelemetry`].
88

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

1212
[![Crates.io: opentelemetry-prometheus](https://img.shields.io/crates/v/opentelemetry-prometheus.svg)](https://crates.io/crates/opentelemetry-prometheus)
1313
[![Documentation](https://docs.rs/opentelemetry-prometheus/badge.svg)](https://docs.rs/opentelemetry-prometheus)

opentelemetry-prometheus/examples/hyper.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hyper::{
88
use hyper_util::rt::{TokioExecutor, TokioIo};
99
use once_cell::sync::Lazy;
1010
use opentelemetry::{
11-
metrics::{Counter, Histogram, MeterProvider as _, Unit},
11+
metrics::{Counter, Histogram, MeterProvider as _},
1212
KeyValue,
1313
};
1414
use opentelemetry_sdk::metrics::SdkMeterProvider;
@@ -88,12 +88,12 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
8888
.init(),
8989
http_body_gauge: meter
9090
.u64_histogram("example.http_response_size")
91-
.with_unit(Unit::new("By"))
91+
.with_unit("By")
9292
.with_description("The metrics HTTP response sizes in bytes.")
9393
.init(),
9494
http_req_histogram: meter
9595
.f64_histogram("example.http_request_duration")
96-
.with_unit(Unit::new("ms"))
96+
.with_unit("ms")
9797
.with_description("The HTTP request latencies in milliseconds.")
9898
.init(),
9999
});

opentelemetry-prometheus/src/lib.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,10 @@ fn add_histogram_metric<T: Numeric>(
457457
// See: https://github.com/tikv/rust-prometheus/issues/393
458458

459459
for dp in &histogram.data_points {
460-
let kvs = get_attrs(&mut dp.attributes.iter(), extra);
460+
let kvs = get_attrs(
461+
&mut dp.attributes.iter().map(|kv| (&kv.key, &kv.value)),
462+
extra,
463+
);
461464
let bounds_len = dp.bounds.len();
462465
let (bucket, _) = dp.bounds.iter().enumerate().fold(
463466
(Vec::with_capacity(bounds_len), 0),
@@ -503,7 +506,10 @@ fn add_sum_metric<T: Numeric>(
503506
};
504507

505508
for dp in &sum.data_points {
506-
let kvs = get_attrs(&mut dp.attributes.iter(), extra);
509+
let kvs = get_attrs(
510+
&mut dp.attributes.iter().map(|kv| (&kv.key, &kv.value)),
511+
extra,
512+
);
507513

508514
let mut pm = prometheus::proto::Metric::default();
509515
pm.set_label(protobuf::RepeatedField::from_vec(kvs));
@@ -535,7 +541,10 @@ fn add_gauge_metric<T: Numeric>(
535541
name: Cow<'static, str>,
536542
) {
537543
for dp in &gauge.data_points {
538-
let kvs = get_attrs(&mut dp.attributes.iter(), extra);
544+
let kvs = get_attrs(
545+
&mut dp.attributes.iter().map(|kv| (&kv.key, &kv.value)),
546+
extra,
547+
);
539548

540549
let mut g = prometheus::proto::Gauge::default();
541550
g.set_value(dp.value.as_f64());

opentelemetry-prometheus/src/resource_selector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl ResourceSelector {
3636
ResourceSelector::All => get_attrs(&mut resource.iter(), &[]),
3737
ResourceSelector::None => Vec::new(),
3838
ResourceSelector::KeyAllowList(keys) => {
39-
get_attrs(&mut resource.iter().filter(|(k, _)| keys.contains(k)), &[])
39+
get_attrs(&mut resource.iter().filter(|(k, _)| keys.contains(*k)), &[])
4040
}
4141
}
4242
}

opentelemetry-prometheus/src/utils.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
use opentelemetry::metrics::Unit;
21
use std::borrow::Cow;
32

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

6-
pub(crate) fn get_unit_suffixes(unit: &Unit) -> Option<Cow<'static, str>> {
5+
pub(crate) fn get_unit_suffixes(unit: &str) -> Option<Cow<'static, str>> {
76
// no unit return early
8-
if unit.as_str().is_empty() {
7+
if unit.is_empty() {
98
return None;
109
}
1110

1211
// direct match with known units
13-
if let Some(matched) = get_prom_units(unit.as_str()) {
12+
if let Some(matched) = get_prom_units(unit) {
1413
return Some(Cow::Borrowed(matched));
1514
}
1615

@@ -20,7 +19,7 @@ pub(crate) fn get_unit_suffixes(unit: &Unit) -> Option<Cow<'static, str>> {
2019
// e.g
2120
// "test/y" => "per_year"
2221
// "km/s" => "kilometers_per_second"
23-
if let Some((first, second)) = unit.as_str().split_once('/') {
22+
if let Some((first, second)) = unit.split_once('/') {
2423
return match (
2524
NON_APPLICABLE_ON_PER_UNIT.contains(&first),
2625
get_prom_units(first),
@@ -193,9 +192,8 @@ mod tests {
193192
// annotations
194193
("{request}", None),
195194
];
196-
for (unit_str, expected_suffix) in test_cases {
197-
let unit = Unit::new(unit_str);
198-
assert_eq!(get_unit_suffixes(&unit), expected_suffix);
195+
for (unit, expected_suffix) in test_cases {
196+
assert_eq!(get_unit_suffixes(unit), expected_suffix);
199197
}
200198
}
201199
}

0 commit comments

Comments
 (0)