Skip to content

Commit

Permalink
feat: omit empty otel_scope_info and otel_target_info metrics (#1428)
Browse files Browse the repository at this point in the history
Fixes #1282 

* omit otel_scope_info and otel_target_info metrics when scope
attributes and resource attributes are empty.
* Update `CHANGELOG.md`
  • Loading branch information
zengxilong authored Dec 6, 2023
1 parent ec38660 commit 1a3f368
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
4 changes: 4 additions & 0 deletions opentelemetry-prometheus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## vNext

### Changed

- Omit empty `otel_scope_info` and `otel_target_info` metrics [#1428](https://github.com/open-telemetry/opentelemetry-rust/pull/1428)

## v0.14.1

### Fixed
Expand Down
14 changes: 8 additions & 6 deletions opentelemetry-prometheus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,19 @@ impl prometheus::core::Collector for Collector {
// Resource should be immutable, we don't need to compute again
create_info_metric(TARGET_INFO_NAME, TARGET_INFO_DESCRIPTION, &metrics.resource)
});
if !self.disable_target_info {
if !self.disable_target_info && !metrics.resource.is_empty() {
res.push(target_info.clone())
}

for scope_metrics in metrics.scope_metrics {
let scope_labels = if !self.disable_scope_info {
let scope_info = inner
.scope_infos
.entry(scope_metrics.scope.clone())
.or_insert_with_key(create_scope_info_metric);
res.push(scope_info.clone());
if !scope_metrics.scope.attributes.is_empty() {
let scope_info = inner
.scope_infos
.entry(scope_metrics.scope.clone())
.or_insert_with_key(create_scope_info_metric);
res.push(scope_info.clone());
}

let mut labels =
Vec::with_capacity(1 + scope_metrics.scope.version.is_some() as usize);
Expand Down
3 changes: 0 additions & 3 deletions opentelemetry-prometheus/tests/data/empty_resource.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@ foo_total{A="B",C="D",E="true",F="42",otel_scope_name="testmeter",otel_scope_ver
# HELP otel_scope_info Instrumentation Scope metadata
# TYPE otel_scope_info gauge
otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1
# HELP target_info Target metadata
# TYPE target_info gauge
target_info 1
36 changes: 30 additions & 6 deletions opentelemetry-prometheus/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,12 @@ fn prometheus_exporter_integration() {
.unwrap(),
)
.build();
let meter =
provider.versioned_meter("testmeter", Some("v0.1.0"), None::<&'static str>, None);
let meter = provider.versioned_meter(
"testmeter",
Some("v0.1.0"),
None::<&'static str>,
Some(vec![KeyValue::new("k", "v")]),
);
(tc.record_metrics)(meter);

let content = fs::read_to_string(Path::new("./tests/data").join(tc.expected_file))
Expand Down Expand Up @@ -401,15 +405,25 @@ fn multiple_scopes() {
.build();

let foo_counter = provider
.versioned_meter("meterfoo", Some("v0.1.0"), None::<&'static str>, None)
.versioned_meter(
"meterfoo",
Some("v0.1.0"),
None::<&'static str>,
Some(vec![KeyValue::new("k", "v")]),
)
.u64_counter("foo")
.with_unit(Unit::new("ms"))
.with_description("meter foo counter")
.init();
foo_counter.add(100, &[KeyValue::new("type", "foo")]);

let bar_counter = provider
.versioned_meter("meterbar", Some("v0.1.0"), None::<&'static str>, None)
.versioned_meter(
"meterbar",
Some("v0.1.0"),
None::<&'static str>,
Some(vec![KeyValue::new("k", "v")]),
)
.u64_counter("bar")
.with_unit(Unit::new("ms"))
.with_description("meter bar counter")
Expand Down Expand Up @@ -735,8 +749,18 @@ fn duplicate_metrics() {
.with_reader(exporter)
.build();

let meter_a = provider.versioned_meter("ma", Some("v0.1.0"), None::<&'static str>, None);
let meter_b = provider.versioned_meter("mb", Some("v0.1.0"), None::<&'static str>, None);
let meter_a = provider.versioned_meter(
"ma",
Some("v0.1.0"),
None::<&'static str>,
Some(vec![KeyValue::new("k", "v")]),
);
let meter_b = provider.versioned_meter(
"mb",
Some("v0.1.0"),
None::<&'static str>,
Some(vec![KeyValue::new("k", "v")]),
);

(tc.record_metrics)(meter_a, meter_b);

Expand Down

0 comments on commit 1a3f368

Please sign in to comment.