Skip to content

Commit

Permalink
Merge branch 'main' into fix/random_tests_promethus
Browse files Browse the repository at this point in the history
  • Loading branch information
ouertani authored Oct 22, 2024
2 parents 9211603 + ea4b5e4 commit f4ec413
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 90 deletions.
4 changes: 4 additions & 0 deletions opentelemetry-prometheus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## vNext

- Bump MSRV to 1.70 [#2179](https://github.com/open-telemetry/opentelemetry-rust/pull/2179)
- Update `opentelemetry` dependency version to 0.26
- Update `opentelemetry_sdk` dependency version to 0.26
- Update `opentelemetry-semantic-conventions` dependency version to 0.26


## v0.17.0

Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-prometheus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ rustdoc-args = ["--cfg", "docsrs"]

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

[dev-dependencies]
opentelemetry-semantic-conventions = { version = "0.16" }
opentelemetry-semantic-conventions = { version = "0.26" }
http-body-util = { workspace = true }
hyper = { workspace = true, features = ["full"] }
hyper-util = { workspace = true, features = ["full"] }
Expand Down
24 changes: 1 addition & 23 deletions opentelemetry-prometheus/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use core::fmt;
use once_cell::sync::OnceCell;
use opentelemetry::metrics::{MetricsError, Result};
use opentelemetry_sdk::metrics::{
reader::{AggregationSelector, MetricProducer},
ManualReaderBuilder,
};
use opentelemetry_sdk::metrics::ManualReaderBuilder;
use std::sync::{Arc, Mutex};

use crate::{Collector, PrometheusExporter, ResourceSelector};
Expand Down Expand Up @@ -105,16 +102,6 @@ impl ExporterBuilder {
self
}

/// Configure the [AggregationSelector] the exporter will use.
///
/// If no selector is provided, the [DefaultAggregationSelector] is used.
///
/// [DefaultAggregationSelector]: opentelemetry_sdk::metrics::reader::DefaultAggregationSelector
pub fn with_aggregation_selector(mut self, agg: impl AggregationSelector + 'static) -> Self {
self.reader = self.reader.with_aggregation_selector(agg);
self
}

/// Configures whether to export resource as attributes with every metric.
///
/// Note that this is orthogonal to the `target_info` metric, which can be disabled using `without_target_info`.
Expand All @@ -128,15 +115,6 @@ impl ExporterBuilder {
self
}

/// Registers an external [MetricProducer] with this reader.
///
/// The producer is used as a source of aggregated metric data which is
/// incorporated into metrics collected from the SDK.
pub fn with_producer(mut self, producer: impl MetricProducer + 'static) -> Self {
self.reader = self.reader.with_producer(producer);
self
}

/// Creates a new [PrometheusExporter] from this configuration.
pub fn build(self) -> Result<PrometheusExporter> {
let reader = Arc::new(self.reader.build());
Expand Down
10 changes: 2 additions & 8 deletions opentelemetry-prometheus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ use opentelemetry::{
use opentelemetry_sdk::{
metrics::{
data::{self, ResourceMetrics, Temporality},
reader::{AggregationSelector, MetricReader, TemporalitySelector},
Aggregation, InstrumentKind, ManualReader, Pipeline,
reader::{MetricReader, TemporalitySelector},
InstrumentKind, ManualReader, Pipeline,
},
Resource, Scope,
};
Expand Down Expand Up @@ -160,12 +160,6 @@ impl TemporalitySelector for PrometheusExporter {
}
}

impl AggregationSelector for PrometheusExporter {
fn aggregation(&self, kind: InstrumentKind) -> Aggregation {
self.reader.aggregation(kind)
}
}

impl MetricReader for PrometheusExporter {
fn register_pipeline(&self, pipeline: Weak<Pipeline>) {
self.reader.register_pipeline(pipeline)
Expand Down
89 changes: 45 additions & 44 deletions opentelemetry-prometheus/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use opentelemetry_sdk::Resource;
use opentelemetry_semantic_conventions::resource::{SERVICE_NAME, TELEMETRY_SDK_VERSION};
use prometheus::{Encoder, TextEncoder};

#[ignore = "https://github.com/open-telemetry/opentelemetry-rust/pull/2224"]
#[test]
fn prometheus_exporter_integration() {
struct TestCase {
Expand Down Expand Up @@ -46,10 +47,10 @@ fn prometheus_exporter_integration() {
expected_file: "counter.txt",
record_metrics: Box::new(|meter| {
let attrs = vec![
Key::new("A").string("B"),
Key::new("C").string("D"),
Key::new("E").bool(true),
Key::new("F").i64(42),
KeyValue::new("A", "B"),
KeyValue::new("C", "D"),
KeyValue::new("E", true),
KeyValue::new("F", 42),
];
let counter = meter
.f64_counter("foo")
Expand All @@ -60,10 +61,10 @@ fn prometheus_exporter_integration() {
counter.add(10.3, &attrs);
counter.add(9.0, &attrs);
let attrs2 = vec![
Key::new("A").string("D"),
Key::new("C").string("B"),
Key::new("E").bool(true),
Key::new("F").i64(42),
KeyValue::new("A", "D"),
KeyValue::new("C", "B"),
KeyValue::new("E", true),
KeyValue::new("F", 42),
];
counter.add(5.0, &attrs2);
}),
Expand All @@ -75,10 +76,10 @@ fn prometheus_exporter_integration() {
builder: ExporterBuilder::default().without_counter_suffixes(),
record_metrics: Box::new(|meter| {
let attrs = vec![
Key::new("A").string("B"),
Key::new("C").string("D"),
Key::new("E").bool(true),
Key::new("F").i64(42),
KeyValue::new("A", "B"),
KeyValue::new("C", "D"),
KeyValue::new("E", true),
KeyValue::new("F", 42),
];
let counter = meter
.f64_counter("foo")
Expand All @@ -89,10 +90,10 @@ fn prometheus_exporter_integration() {
counter.add(10.3, &attrs);
counter.add(9.0, &attrs);
let attrs2 = vec![
Key::new("A").string("D"),
Key::new("C").string("B"),
Key::new("E").bool(true),
Key::new("F").i64(42),
KeyValue::new("A", "D"),
KeyValue::new("C", "B"),
KeyValue::new("E", true),
KeyValue::new("F", 42),
];
counter.add(5.0, &attrs2);
}),
Expand All @@ -102,7 +103,7 @@ fn prometheus_exporter_integration() {
name: "gauge",
expected_file: "gauge.txt",
record_metrics: Box::new(|meter| {
let attrs = vec![Key::new("A").string("B"), Key::new("C").string("D")];
let attrs = vec![KeyValue::new("A", "B"), KeyValue::new("C", "D")];
let gauge = meter
.f64_up_down_counter("bar")
.with_description("a fun little gauge")
Expand All @@ -117,7 +118,7 @@ fn prometheus_exporter_integration() {
name: "histogram",
expected_file: "histogram.txt",
record_metrics: Box::new(|meter| {
let attrs = vec![Key::new("A").string("B"), Key::new("C").string("D")];
let attrs = vec![KeyValue::new("A", "B"), KeyValue::new("C", "D")];
let histogram = meter
.f64_histogram("histogram_baz")
.with_description("a very nice histogram")
Expand All @@ -137,11 +138,11 @@ fn prometheus_exporter_integration() {
record_metrics: Box::new(|meter| {
let attrs = vec![
// exact match, value should be overwritten
Key::new("A.B").string("X"),
Key::new("A.B").string("Q"),
KeyValue::new("A.B", "X"),
KeyValue::new("A.B", "Q"),
// unintended match due to sanitization, values should be concatenated
Key::new("C.D").string("Y"),
Key::new("C/D").string("Z"),
KeyValue::new("C.D", "Y"),
KeyValue::new("C/D", "Z"),
];
let counter = meter
.f64_counter("foo")
Expand All @@ -159,7 +160,7 @@ fn prometheus_exporter_integration() {
name: "invalid instruments are renamed",
expected_file: "sanitized_names.txt",
record_metrics: Box::new(|meter| {
let attrs = vec![Key::new("A").string("B"), Key::new("C").string("D")];
let attrs = vec![KeyValue::new("A", "B"), KeyValue::new("C", "D")];
// Valid.
let mut gauge = meter
.f64_up_down_counter("bar")
Expand Down Expand Up @@ -195,10 +196,10 @@ fn prometheus_exporter_integration() {
expected_file: "empty_resource.txt",
record_metrics: Box::new(|meter| {
let attrs = vec![
Key::new("A").string("B"),
Key::new("C").string("D"),
Key::new("E").bool(true),
Key::new("F").i64(42),
KeyValue::new("A", "B"),
KeyValue::new("C", "D"),
KeyValue::new("E", true),
KeyValue::new("F", 42),
];
let counter = meter
.f64_counter("foo")
Expand All @@ -212,14 +213,14 @@ fn prometheus_exporter_integration() {
},
TestCase {
name: "custom resource",
custom_resource_attrs: vec![Key::new("A").string("B"), Key::new("C").string("D")],
custom_resource_attrs: vec![KeyValue::new("A", "B"), KeyValue::new("C", "D")],
expected_file: "custom_resource.txt",
record_metrics: Box::new(|meter| {
let attrs = vec![
Key::new("A").string("B"),
Key::new("C").string("D"),
Key::new("E").bool(true),
Key::new("F").i64(42),
KeyValue::new("A", "B"),
KeyValue::new("C", "D"),
KeyValue::new("E", true),
KeyValue::new("F", 42),
];
let counter = meter
.f64_counter("foo")
Expand All @@ -237,10 +238,10 @@ fn prometheus_exporter_integration() {
expected_file: "without_target_info.txt",
record_metrics: Box::new(|meter| {
let attrs = vec![
Key::new("A").string("B"),
Key::new("C").string("D"),
Key::new("E").bool(true),
Key::new("F").i64(42),
KeyValue::new("A", "B"),
KeyValue::new("C", "D"),
KeyValue::new("E", true),
KeyValue::new("F", 42),
];
let counter = meter
.f64_counter("foo")
Expand All @@ -257,7 +258,7 @@ fn prometheus_exporter_integration() {
builder: ExporterBuilder::default().without_scope_info(),
expected_file: "without_scope_info.txt",
record_metrics: Box::new(|meter| {
let attrs = vec![Key::new("A").string("B"), Key::new("C").string("D")];
let attrs = vec![KeyValue::new("A", "B"), KeyValue::new("C", "D")];
let gauge = meter
.i64_up_down_counter("bar")
.with_description("a fun little gauge")
Expand All @@ -275,7 +276,7 @@ fn prometheus_exporter_integration() {
.without_target_info(),
expected_file: "without_scope_and_target_info.txt",
record_metrics: Box::new(|meter| {
let attrs = vec![Key::new("A").string("B"), Key::new("C").string("D")];
let attrs = vec![KeyValue::new("A", "B"), KeyValue::new("C", "D")];
let counter = meter
.u64_counter("bar")
.with_description("a fun little counter")
Expand All @@ -292,10 +293,10 @@ fn prometheus_exporter_integration() {
expected_file: "with_namespace.txt",
record_metrics: Box::new(|meter| {
let attrs = vec![
Key::new("A").string("B"),
Key::new("C").string("D"),
Key::new("E").bool(true),
Key::new("F").i64(42),
KeyValue::new("A", "B"),
KeyValue::new("C", "D"),
KeyValue::new("E", true),
KeyValue::new("F", 42),
];
let counter = meter
.f64_counter("foo")
Expand All @@ -313,7 +314,7 @@ fn prometheus_exporter_integration() {
builder: ExporterBuilder::default().with_resource_selector(ResourceSelector::All),
expected_file: "resource_in_every_metrics.txt",
record_metrics: Box::new(|meter| {
let attrs = vec![Key::new("A").string("B"), Key::new("C").string("D")];
let attrs = vec![KeyValue::new("A", "B"), KeyValue::new("C", "D")];
let gauge = meter
.i64_up_down_counter("bar")
.with_description("a fun little gauge")
Expand All @@ -330,7 +331,7 @@ fn prometheus_exporter_integration() {
.with_resource_selector(HashSet::from([Key::new("service.name")])),
expected_file: "select_resource_in_every_metrics.txt",
record_metrics: Box::new(|meter| {
let attrs = vec![Key::new("A").string("B"), Key::new("C").string("D")];
let attrs = vec![KeyValue::new("A", "B"), KeyValue::new("C", "D")];
let gauge = meter
.i64_up_down_counter("bar")
.with_description("a fun little gauge")
Expand Down
Loading

0 comments on commit f4ec413

Please sign in to comment.