Skip to content

Commit 0c5e7c1

Browse files
committed
add some coverage for advanced views
1 parent e7c5ba2 commit 0c5e7c1

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

opentelemetry-sdk/src/metrics/meter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ where
683683
name,
684684
description: description.unwrap_or_default(),
685685
unit: unit.unwrap_or_default(),
686-
kind: kind,
686+
kind,
687687
scope: self.meter.scope.clone(),
688688
};
689689

opentelemetry-sdk/src/metrics/mod.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,108 @@ mod tests {
16871687
);
16881688
}
16891689

1690+
// Following are just a basic set of advanced View tests - Views bring a lot
1691+
// of permutations and combinations, and we need
1692+
// to expand coverage for more scenarios in future.
1693+
// It is best to first split this file into multiple files
1694+
// based on scenarios (eg: regular aggregation, cardinality, views, view_advanced, etc)
1695+
// and then add more tests for each of the scenarios.
1696+
#[test]
1697+
fn test_view_single_instrument_multiple_stream() {
1698+
// Run this test with stdout enabled to see output.
1699+
// cargo test test_view_multiple_stream --all-features
1700+
1701+
// Each of the views match the instrument name "my_counter" and create a
1702+
// new stream with a different name. In other words, View can be used to
1703+
// create multiple streams for the same instrument.
1704+
1705+
let view1 = |i: &Instrument| {
1706+
if i.name() == "my_counter" {
1707+
Some(Stream::builder().with_name("my_counter_1").build().unwrap())
1708+
} else {
1709+
None
1710+
}
1711+
};
1712+
1713+
let view2 = |i: &Instrument| {
1714+
if i.name() == "my_counter" {
1715+
Some(Stream::builder().with_name("my_counter_2").build().unwrap())
1716+
} else {
1717+
None
1718+
}
1719+
};
1720+
1721+
// Arrange
1722+
let exporter = InMemoryMetricExporter::default();
1723+
let meter_provider = SdkMeterProvider::builder()
1724+
.with_periodic_exporter(exporter.clone())
1725+
.with_view(view1)
1726+
.with_view(view2)
1727+
.build();
1728+
1729+
// Act
1730+
let meter = meter_provider.meter("test");
1731+
let counter = meter.f64_counter("my_counter").build();
1732+
1733+
counter.add(1.5, &[KeyValue::new("key1", "value1")]);
1734+
meter_provider.force_flush().unwrap();
1735+
1736+
// Assert
1737+
let resource_metrics = exporter
1738+
.get_finished_metrics()
1739+
.expect("metrics are expected to be exported.");
1740+
assert_eq!(resource_metrics.len(), 1);
1741+
assert_eq!(resource_metrics[0].scope_metrics.len(), 1);
1742+
let metrics = &resource_metrics[0].scope_metrics[0].metrics;
1743+
assert_eq!(metrics.len(), 2);
1744+
assert_eq!(metrics[0].name, "my_counter_1");
1745+
assert_eq!(metrics[1].name, "my_counter_2");
1746+
}
1747+
1748+
#[test]
1749+
fn test_view_multiple_instrument_single_stream() {
1750+
// Run this test with stdout enabled to see output.
1751+
// cargo test test_view_multiple_instrument_single_stream --all-features
1752+
1753+
// The view matches the instrument name "my_counter1" and "my_counter1"
1754+
// and create a single new stream for both. In other words, View can be used to
1755+
// "merge" multiple instruments into a single stream.
1756+
let view = |i: &Instrument| {
1757+
if i.name() == "my_counter1" || i.name() == "my_counter2" {
1758+
Some(Stream::builder().with_name("my_counter").build().unwrap())
1759+
} else {
1760+
None
1761+
}
1762+
};
1763+
1764+
// Arrange
1765+
let exporter = InMemoryMetricExporter::default();
1766+
let meter_provider = SdkMeterProvider::builder()
1767+
.with_periodic_exporter(exporter.clone())
1768+
.with_view(view)
1769+
.build();
1770+
1771+
// Act
1772+
let meter = meter_provider.meter("test");
1773+
let counter1 = meter.f64_counter("my_counter1").build();
1774+
let counter2 = meter.f64_counter("my_counter2").build();
1775+
1776+
counter1.add(1.5, &[KeyValue::new("key1", "value1")]);
1777+
counter2.add(1.5, &[KeyValue::new("key1", "value1")]);
1778+
meter_provider.force_flush().unwrap();
1779+
1780+
// Assert
1781+
let resource_metrics = exporter
1782+
.get_finished_metrics()
1783+
.expect("metrics are expected to be exported.");
1784+
assert_eq!(resource_metrics.len(), 1);
1785+
assert_eq!(resource_metrics[0].scope_metrics.len(), 1);
1786+
let metrics = &resource_metrics[0].scope_metrics[0].metrics;
1787+
assert_eq!(metrics.len(), 1);
1788+
assert_eq!(metrics[0].name, "my_counter");
1789+
// TODO: Assert that the data points are aggregated correctly.
1790+
}
1791+
16901792
fn asynchronous_instruments_cumulative_data_points_only_from_last_measurement_helper(
16911793
instrument_name: &'static str,
16921794
should_not_emit: bool,

0 commit comments

Comments
 (0)