Skip to content

Commit 0156146

Browse files
kudlatyamrothlalitbcijothomasttys3
authored
feat: update otel versions for prometheus to 0.27 (#2309)
Co-authored-by: Lalit Kumar Bhasin <[email protected]> Co-authored-by: Cijo Thomas <[email protected]> Co-authored-by: ttys3 <[email protected]>
1 parent 96b7acc commit 0156146

File tree

7 files changed

+160
-136
lines changed

7 files changed

+160
-136
lines changed

opentelemetry-prometheus/CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
## vNext
44

5-
- Bump MSRV to 1.70 [#2179](https://github.com/open-telemetry/opentelemetry-rust/pull/2179)
6-
- Update `opentelemetry` dependency version to 0.26
7-
- Update `opentelemetry_sdk` dependency version to 0.26
8-
- Update `opentelemetry-semantic-conventions` dependency version to 0.26
5+
## v0.27.0
6+
7+
- Update `opentelemetry` dependency version to 0.27
8+
- Update `opentelemetry_sdk` dependency version to 0.27
9+
- Update `opentelemetry-semantic-conventions` dependency version to 0.27
910

1011

1112
## v0.17.0

opentelemetry-prometheus/Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "opentelemetry-prometheus"
3-
version = "0.17.0"
3+
version = "0.27.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,17 +21,20 @@ rustdoc-args = ["--cfg", "docsrs"]
2121

2222
[dependencies]
2323
once_cell = { workspace = true }
24-
opentelemetry = { version = "0.26", default-features = false, features = ["metrics"] }
25-
opentelemetry_sdk = { version = "0.26", default-features = false, features = ["metrics"] }
24+
opentelemetry = { version = "0.27", default-features = false, features = ["metrics"] }
25+
opentelemetry_sdk = { version = "0.27", default-features = false, features = ["metrics"] }
2626
prometheus = "0.13"
2727
protobuf = "2.14"
28+
tracing = {workspace = true, optional = true} # optional for opentelemetry internal logging
2829

2930
[dev-dependencies]
30-
opentelemetry-semantic-conventions = { version = "0.26" }
31+
opentelemetry-semantic-conventions = { version = "0.27" }
3132
http-body-util = { workspace = true }
3233
hyper = { workspace = true, features = ["full"] }
3334
hyper-util = { workspace = true, features = ["full"] }
3435
tokio = { workspace = true, features = ["full"] }
3536

3637
[features]
38+
default = ["internal-logs"]
3739
prometheus-encoding = []
40+
internal-logs = ["tracing"]

opentelemetry-prometheus/README.md

Lines changed: 1 addition & 1 deletion
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.24**.
10+
implementation is based on Opentelemetry API and SDK 0.27**.
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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
8585
http_counter: meter
8686
.u64_counter("http_requests_total")
8787
.with_description("Total number of HTTP requests made.")
88-
.init(),
88+
.build(),
8989
http_body_gauge: meter
9090
.u64_histogram("example.http_response_size")
9191
.with_unit("By")
9292
.with_description("The metrics HTTP response sizes in bytes.")
93-
.init(),
93+
.build(),
9494
http_req_histogram: meter
9595
.f64_histogram("example.http_request_duration")
9696
.with_unit("ms")
9797
.with_description("The HTTP request latencies in milliseconds.")
98-
.init(),
98+
.build(),
9999
});
100100

101101
let addr: SocketAddr = ([127, 0, 0, 1], 3000).into();

opentelemetry-prometheus/src/config.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use core::fmt;
22
use once_cell::sync::OnceCell;
3-
use opentelemetry::metrics::{MetricsError, Result};
4-
use opentelemetry_sdk::metrics::ManualReaderBuilder;
3+
use opentelemetry_sdk::metrics::{ManualReaderBuilder, MetricError, MetricResult};
54
use std::sync::{Arc, Mutex};
65

76
use crate::{Collector, PrometheusExporter, ResourceSelector};
@@ -116,7 +115,7 @@ impl ExporterBuilder {
116115
}
117116

118117
/// Creates a new [PrometheusExporter] from this configuration.
119-
pub fn build(self) -> Result<PrometheusExporter> {
118+
pub fn build(self) -> MetricResult<PrometheusExporter> {
120119
let reader = Arc::new(self.reader.build());
121120

122121
let collector = Collector {
@@ -135,7 +134,7 @@ impl ExporterBuilder {
135134
let registry = self.registry.unwrap_or_default();
136135
registry
137136
.register(Box::new(collector))
138-
.map_err(|e| MetricsError::Other(e.to_string()))?;
137+
.map_err(|e| MetricError::Other(e.to_string()))?;
139138

140139
Ok(PrometheusExporter { reader })
141140
}

opentelemetry-prometheus/src/lib.rs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
//! let counter = meter
2929
//! .u64_counter("a.counter")
3030
//! .with_description("Counts things")
31-
//! .init();
31+
//! .build();
3232
//! let histogram = meter
3333
//! .u64_histogram("a.histogram")
3434
//! .with_description("Records values")
35-
//! .init();
35+
//! .build();
3636
//!
3737
//! counter.add(100, &[KeyValue::new("key", "value")]);
3838
//! histogram.record(100, &[KeyValue::new("key", "value")]);
@@ -97,18 +97,14 @@
9797
#![cfg_attr(test, deny(warnings))]
9898

9999
use once_cell::sync::{Lazy, OnceCell};
100-
use opentelemetry::{
101-
global,
102-
metrics::{MetricsError, Result},
103-
Key, Value,
104-
};
100+
use opentelemetry::{otel_error, otel_warn, InstrumentationScope, Key, Value};
105101
use opentelemetry_sdk::{
106102
metrics::{
107-
data::{self, ResourceMetrics, Temporality},
108-
reader::{MetricReader, TemporalitySelector},
109-
InstrumentKind, ManualReader, Pipeline,
103+
data::{self, ResourceMetrics},
104+
reader::MetricReader,
105+
InstrumentKind, ManualReader, MetricResult, Pipeline, Temporality,
110106
},
111-
Resource, Scope,
107+
Resource,
112108
};
113109
use prometheus::{
114110
core::Desc,
@@ -152,30 +148,28 @@ pub struct PrometheusExporter {
152148
reader: Arc<ManualReader>,
153149
}
154150

155-
impl TemporalitySelector for PrometheusExporter {
156-
/// Note: Prometheus only supports cumulative temporality so this will always be
157-
/// [Temporality::Cumulative].
158-
fn temporality(&self, kind: InstrumentKind) -> Temporality {
159-
self.reader.temporality(kind)
160-
}
161-
}
162-
163151
impl MetricReader for PrometheusExporter {
164152
fn register_pipeline(&self, pipeline: Weak<Pipeline>) {
165153
self.reader.register_pipeline(pipeline)
166154
}
167155

168-
fn collect(&self, rm: &mut ResourceMetrics) -> Result<()> {
156+
fn collect(&self, rm: &mut ResourceMetrics) -> MetricResult<()> {
169157
self.reader.collect(rm)
170158
}
171159

172-
fn force_flush(&self) -> Result<()> {
160+
fn force_flush(&self) -> MetricResult<()> {
173161
self.reader.force_flush()
174162
}
175163

176-
fn shutdown(&self) -> Result<()> {
164+
fn shutdown(&self) -> MetricResult<()> {
177165
self.reader.shutdown()
178166
}
167+
168+
/// Note: Prometheus only supports cumulative temporality, so this will always be
169+
/// [Temporality::Cumulative].
170+
fn temporality(&self, _kind: InstrumentKind) -> Temporality {
171+
Temporality::Cumulative
172+
}
179173
}
180174

181175
struct Collector {
@@ -193,7 +187,7 @@ struct Collector {
193187

194188
#[derive(Default)]
195189
struct CollectorInner {
196-
scope_infos: HashMap<Scope, MetricFamily>,
190+
scope_infos: HashMap<InstrumentationScope, MetricFamily>,
197191
metric_families: HashMap<String, MetricFamily>,
198192
}
199193

@@ -281,7 +275,10 @@ impl prometheus::core::Collector for Collector {
281275
let mut inner = match self.inner.lock() {
282276
Ok(guard) => guard,
283277
Err(err) => {
284-
global::handle_error(err);
278+
otel_error!(
279+
name: "MetricScrapeFailed",
280+
message = err.to_string(),
281+
);
285282
return Vec::new();
286283
}
287284
};
@@ -291,7 +288,10 @@ impl prometheus::core::Collector for Collector {
291288
scope_metrics: vec![],
292289
};
293290
if let Err(err) = self.reader.collect(&mut metrics) {
294-
global::handle_error(err);
291+
otel_error!(
292+
name: "MetricScrapeFailed",
293+
message = err.to_string(),
294+
);
295295
return vec![];
296296
}
297297
let mut res = Vec::with_capacity(metrics.scope_metrics.len() + 1);
@@ -311,7 +311,7 @@ impl prometheus::core::Collector for Collector {
311311

312312
for scope_metrics in metrics.scope_metrics {
313313
let scope_labels = if !self.disable_scope_info {
314-
if !scope_metrics.scope.attributes.is_empty() {
314+
if scope_metrics.scope.attributes().count() > 0 {
315315
let scope_info = inner
316316
.scope_infos
317317
.entry(scope_metrics.scope.clone())
@@ -320,12 +320,12 @@ impl prometheus::core::Collector for Collector {
320320
}
321321

322322
let mut labels =
323-
Vec::with_capacity(1 + scope_metrics.scope.version.is_some() as usize);
323+
Vec::with_capacity(1 + scope_metrics.scope.version().is_some() as usize);
324324
let mut name = LabelPair::new();
325325
name.set_name(SCOPE_INFO_KEYS[0].into());
326-
name.set_value(scope_metrics.scope.name.to_string());
326+
name.set_value(scope_metrics.scope.name().to_string());
327327
labels.push(name);
328-
if let Some(version) = &scope_metrics.scope.version {
328+
if let Some(version) = &scope_metrics.scope.version() {
329329
let mut l_version = LabelPair::new();
330330
l_version.set_name(SCOPE_INFO_KEYS[1].into());
331331
l_version.set_value(version.to_string());
@@ -421,11 +421,19 @@ fn validate_metrics(
421421
) -> (bool, Option<String>) {
422422
if let Some(existing) = mfs.get(name) {
423423
if existing.get_field_type() != metric_type {
424-
global::handle_error(MetricsError::Other(format!("Instrument type conflict, using existing type definition. Instrument {name}, Existing: {:?}, dropped: {:?}", existing.get_field_type(), metric_type)));
424+
otel_warn!(
425+
name: "MetricValidationFailed",
426+
message = "Instrument type conflict, using existing type definition",
427+
metric_type = format!("Instrument {name}, Existing: {:?}, dropped: {:?}", existing.get_field_type(), metric_type).as_str(),
428+
);
425429
return (true, None);
426430
}
427431
if existing.get_help() != description {
428-
global::handle_error(MetricsError::Other(format!("Instrument description conflict, using existing. Instrument {name}, Existing: {:?}, dropped: {:?}", existing.get_help(), description)));
432+
otel_warn!(
433+
name: "MetricValidationFailed",
434+
message = "Instrument description conflict, using existing",
435+
metric_description = format!("Instrument {name}, Existing: {:?}, dropped: {:?}", existing.get_help().to_string(), description.to_string()).as_str(),
436+
);
429437
return (false, Some(existing.get_help().to_string()));
430438
}
431439
(false, None)
@@ -578,16 +586,16 @@ fn create_info_metric(
578586
mf
579587
}
580588

581-
fn create_scope_info_metric(scope: &Scope) -> MetricFamily {
589+
fn create_scope_info_metric(scope: &InstrumentationScope) -> MetricFamily {
582590
let mut g = prometheus::proto::Gauge::default();
583591
g.set_value(1.0);
584592

585-
let mut labels = Vec::with_capacity(1 + scope.version.is_some() as usize);
593+
let mut labels = Vec::with_capacity(1 + scope.version().is_some() as usize);
586594
let mut name = LabelPair::new();
587595
name.set_name(SCOPE_INFO_KEYS[0].into());
588-
name.set_value(scope.name.to_string());
596+
name.set_value(scope.name().to_string());
589597
labels.push(name);
590-
if let Some(version) = &scope.version {
598+
if let Some(version) = &scope.version() {
591599
let mut v_label = LabelPair::new();
592600
v_label.set_name(SCOPE_INFO_KEYS[1].into());
593601
v_label.set_value(version.to_string());

0 commit comments

Comments
 (0)