Skip to content

fix: MetricExporters use getter methods instead of direct access #2973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions opentelemetry-proto/src/transform/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,13 @@
fn from(rm: &ResourceMetrics) -> Self {
ExportMetricsServiceRequest {
resource_metrics: vec![TonicResourceMetrics {
resource: Some((&rm.resource).into()),
resource: Some((rm.resource()).into()),

Check warning on line 117 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L117

Added line #L117 was not covered by tests
scope_metrics: rm.scope_metrics().map(Into::into).collect(),
schema_url: rm.resource.schema_url().map(Into::into).unwrap_or_default(),
schema_url: rm
.resource()
.schema_url()
.map(Into::into)
.unwrap_or_default(),

Check warning on line 123 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L119-L123

Added lines #L119 - L123 were not covered by tests
}],
}
}
Expand All @@ -135,10 +139,10 @@
impl From<&SdkScopeMetrics> for TonicScopeMetrics {
fn from(sm: &SdkScopeMetrics) -> Self {
TonicScopeMetrics {
scope: Some((&sm.scope, None).into()),
scope: Some((sm.scope(), None).into()),

Check warning on line 142 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L142

Added line #L142 was not covered by tests
metrics: sm.metrics().map(Into::into).collect(),
schema_url: sm
.scope
.scope()

Check warning on line 145 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L145

Added line #L145 was not covered by tests
.schema_url()
.map(ToOwned::to_owned)
.unwrap_or_default(),
Expand All @@ -149,11 +153,11 @@
impl From<&SdkMetric> for TonicMetric {
fn from(metric: &SdkMetric) -> Self {
TonicMetric {
name: metric.name.to_string(),
description: metric.description.to_string(),
unit: metric.unit.to_string(),
name: metric.name().to_string(),
description: metric.description().to_string(),
unit: metric.unit().to_string(),

Check warning on line 158 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L156-L158

Added lines #L156 - L158 were not covered by tests
metadata: vec![], // internal and currently unused
data: Some(match &metric.data {
data: Some(match metric.data() {

Check warning on line 160 in opentelemetry-proto/src/transform/metrics.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-proto/src/transform/metrics.rs#L160

Added line #L160 was not covered by tests
AggregatedMetrics::F64(data) => data.into(),
AggregatedMetrics::U64(data) => data.into(),
AggregatedMetrics::I64(data) => data.into(),
Expand Down
6 changes: 6 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ also modified to suppress telemetry before invoking exporters.
- `HistogramDataPoint` no longer exposes `bounds` and `bucket_counts`, but
instead offers `bounds()` and `bucket_counts()` methods that returns an
iterator over the same.
- `Metric` no longer exposes `name`, `description`, `unit`, `data` fields, but
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The individual instrument and their datapoint related structs (such as Sum, SumDataPoint, etc.) also have some public fields where we can use getters.

instead offers `name()`, `description()`, `unit()`, and `data()` accessor methods.
- `ResourceMetrics` no longer exposes `resource` field, but instead offers
a `resource()` accessor method.
- `ScopeMetrics` no longer exposes `scope` field, but instead offers
a `scope()` accessor method.

## 0.29.0

Expand Down
44 changes: 38 additions & 6 deletions opentelemetry-sdk/src/metrics/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#[derive(Debug)]
pub struct ResourceMetrics {
/// The entity that collected the metrics.
pub resource: Resource,
pub(crate) resource: Resource,
/// The collection of metrics with unique [InstrumentationScope]s.
pub(crate) scope_metrics: Vec<ScopeMetrics>,
}
Expand All @@ -27,6 +27,11 @@
}

impl ResourceMetrics {
/// Returns a reference to the [Resource] in [ResourceMetrics].
pub fn resource(&self) -> &Resource {
&self.resource
}

Check warning on line 33 in opentelemetry-sdk/src/metrics/data/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/data/mod.rs#L31-L33

Added lines #L31 - L33 were not covered by tests

/// Returns an iterator over the [ScopeMetrics] in [ResourceMetrics].
pub fn scope_metrics(&self) -> impl Iterator<Item = &ScopeMetrics> {
self.scope_metrics.iter()
Expand All @@ -37,12 +42,17 @@
#[derive(Default, Debug)]
pub struct ScopeMetrics {
/// The [InstrumentationScope] that the meter was created with.
pub scope: InstrumentationScope,
pub(crate) scope: InstrumentationScope,
/// The list of aggregations created by the meter.
pub(crate) metrics: Vec<Metric>,
}

impl ScopeMetrics {
/// Returns a reference to the [InstrumentationScope] in [ScopeMetrics].
pub fn scope(&self) -> &InstrumentationScope {
&self.scope
}

Check warning on line 54 in opentelemetry-sdk/src/metrics/data/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/data/mod.rs#L52-L54

Added lines #L52 - L54 were not covered by tests

/// Returns an iterator over the [Metric]s in [ScopeMetrics].
pub fn metrics(&self) -> impl Iterator<Item = &Metric> {
self.metrics.iter()
Expand All @@ -55,13 +65,35 @@
#[derive(Debug)]
pub struct Metric {
/// The name of the instrument that created this data.
pub name: Cow<'static, str>,
pub(crate) name: Cow<'static, str>,
/// The description of the instrument, which can be used in documentation.
pub description: Cow<'static, str>,
pub(crate) description: Cow<'static, str>,
/// The unit in which the instrument reports.
pub unit: Cow<'static, str>,
pub(crate) unit: Cow<'static, str>,
/// The aggregated data from an instrument.
pub data: AggregatedMetrics,
pub(crate) data: AggregatedMetrics,
}

impl Metric {
/// Returns the name of the instrument that created this data.
pub fn name(&self) -> &str {
&self.name
}

Check warning on line 81 in opentelemetry-sdk/src/metrics/data/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/data/mod.rs#L79-L81

Added lines #L79 - L81 were not covered by tests

/// Returns the description of the instrument.
pub fn description(&self) -> &str {
&self.description
}

Check warning on line 86 in opentelemetry-sdk/src/metrics/data/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/data/mod.rs#L84-L86

Added lines #L84 - L86 were not covered by tests

/// Returns the unit in which the instrument reports.
pub fn unit(&self) -> &str {
&self.unit
}

Check warning on line 91 in opentelemetry-sdk/src/metrics/data/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/data/mod.rs#L89-L91

Added lines #L89 - L91 were not covered by tests

/// Returns the aggregated data from the instrument.
pub fn data(&self) -> &AggregatedMetrics {
&self.data
}

Check warning on line 96 in opentelemetry-sdk/src/metrics/data/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/data/mod.rs#L94-L96

Added lines #L94 - L96 were not covered by tests
}

/// Aggregated metrics data from an instrument
Expand Down
35 changes: 16 additions & 19 deletions opentelemetry-stdout/src/metrics/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
} else {
println!("Metrics");
println!("Resource");
if let Some(schema_url) = metrics.resource.schema_url() {
if let Some(schema_url) = metrics.resource().schema_url() {

Check warning on line 51 in opentelemetry-stdout/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/exporter.rs#L51

Added line #L51 was not covered by tests
println!("\tResource SchemaUrl: {:?}", schema_url);
}

metrics.resource.iter().for_each(|(k, v)| {
metrics.resource().iter().for_each(|(k, v)| {

Check warning on line 55 in opentelemetry-stdout/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/exporter.rs#L55

Added line #L55 was not covered by tests
println!("\t -> {}={:?}", k, v);
});
print_metrics(metrics.scope_metrics());
Expand Down Expand Up @@ -82,29 +82,26 @@
fn print_metrics<'a>(metrics: impl Iterator<Item = &'a ScopeMetrics>) {
for (i, metric) in metrics.enumerate() {
println!("\tInstrumentation Scope #{}", i);
println!("\t\tName : {}", &metric.scope.name());
if let Some(version) = &metric.scope.version() {
let scope = metric.scope();
println!("\t\tName : {}", scope.name());
if let Some(version) = scope.version() {

Check warning on line 87 in opentelemetry-stdout/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/exporter.rs#L85-L87

Added lines #L85 - L87 were not covered by tests
println!("\t\tVersion : {:?}", version);
}
if let Some(schema_url) = &metric.scope.schema_url() {
if let Some(schema_url) = scope.schema_url() {

Check warning on line 90 in opentelemetry-stdout/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/exporter.rs#L90

Added line #L90 was not covered by tests
println!("\t\tSchemaUrl: {:?}", schema_url);
}
metric
.scope
.attributes()
.enumerate()
.for_each(|(index, kv)| {
if index == 0 {
println!("\t\tScope Attributes:");
}
println!("\t\t\t -> {}: {}", kv.key, kv.value);
});
scope.attributes().enumerate().for_each(|(index, kv)| {
if index == 0 {
println!("\t\tScope Attributes:");
}
println!("\t\t\t -> {}: {}", kv.key, kv.value);
});

Check warning on line 98 in opentelemetry-stdout/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/exporter.rs#L93-L98

Added lines #L93 - L98 were not covered by tests

metric.metrics().enumerate().for_each(|(i, metric)| {
println!("Metric #{}", i);
println!("\t\tName : {}", &metric.name);
println!("\t\tDescription : {}", &metric.description);
println!("\t\tUnit : {}", &metric.unit);
println!("\t\tName : {}", metric.name());
println!("\t\tDescription : {}", metric.description());
println!("\t\tUnit : {}", metric.unit());

Check warning on line 104 in opentelemetry-stdout/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/exporter.rs#L102-L104

Added lines #L102 - L104 were not covered by tests

fn print_info<T>(data: &MetricData<T>)
where
Expand All @@ -129,7 +126,7 @@
}
}
}
match &metric.data {
match metric.data() {

Check warning on line 129 in opentelemetry-stdout/src/metrics/exporter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-stdout/src/metrics/exporter.rs#L129

Added line #L129 was not covered by tests
AggregatedMetrics::F64(data) => print_info(data),
AggregatedMetrics::U64(data) => print_info(data),
AggregatedMetrics::I64(data) => print_info(data),
Expand Down