Skip to content

Commit d52dcef

Browse files
cijothomaslalitb
andauthored
fix: MetricExporters use getter methods instead of direct access (#2973)
Co-authored-by: Lalit Kumar Bhasin <[email protected]>
1 parent 8499914 commit d52dcef

File tree

4 files changed

+72
-33
lines changed

4 files changed

+72
-33
lines changed

opentelemetry-proto/src/transform/metrics.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,13 @@ pub mod tonic {
114114
fn from(rm: &ResourceMetrics) -> Self {
115115
ExportMetricsServiceRequest {
116116
resource_metrics: vec![TonicResourceMetrics {
117-
resource: Some((&rm.resource).into()),
117+
resource: Some((rm.resource()).into()),
118118
scope_metrics: rm.scope_metrics().map(Into::into).collect(),
119-
schema_url: rm.resource.schema_url().map(Into::into).unwrap_or_default(),
119+
schema_url: rm
120+
.resource()
121+
.schema_url()
122+
.map(Into::into)
123+
.unwrap_or_default(),
120124
}],
121125
}
122126
}
@@ -135,10 +139,10 @@ pub mod tonic {
135139
impl From<&SdkScopeMetrics> for TonicScopeMetrics {
136140
fn from(sm: &SdkScopeMetrics) -> Self {
137141
TonicScopeMetrics {
138-
scope: Some((&sm.scope, None).into()),
142+
scope: Some((sm.scope(), None).into()),
139143
metrics: sm.metrics().map(Into::into).collect(),
140144
schema_url: sm
141-
.scope
145+
.scope()
142146
.schema_url()
143147
.map(ToOwned::to_owned)
144148
.unwrap_or_default(),
@@ -149,11 +153,11 @@ pub mod tonic {
149153
impl From<&SdkMetric> for TonicMetric {
150154
fn from(metric: &SdkMetric) -> Self {
151155
TonicMetric {
152-
name: metric.name.to_string(),
153-
description: metric.description.to_string(),
154-
unit: metric.unit.to_string(),
156+
name: metric.name().to_string(),
157+
description: metric.description().to_string(),
158+
unit: metric.unit().to_string(),
155159
metadata: vec![], // internal and currently unused
156-
data: Some(match &metric.data {
160+
data: Some(match metric.data() {
157161
AggregatedMetrics::F64(data) => data.into(),
158162
AggregatedMetrics::U64(data) => data.into(),
159163
AggregatedMetrics::I64(data) => data.into(),

opentelemetry-sdk/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ also modified to suppress telemetry before invoking exporters.
6565
- `HistogramDataPoint` no longer exposes `bounds` and `bucket_counts`, but
6666
instead offers `bounds()` and `bucket_counts()` methods that returns an
6767
iterator over the same.
68+
- `Metric` no longer exposes `name`, `description`, `unit`, `data` fields, but
69+
instead offers `name()`, `description()`, `unit()`, and `data()` accessor methods.
70+
- `ResourceMetrics` no longer exposes `resource` field, but instead offers
71+
a `resource()` accessor method.
72+
- `ScopeMetrics` no longer exposes `scope` field, but instead offers
73+
a `scope()` accessor method.
6874

6975
## 0.29.0
7076

opentelemetry-sdk/src/metrics/data/mod.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::Temporality;
1212
#[derive(Debug)]
1313
pub struct ResourceMetrics {
1414
/// The entity that collected the metrics.
15-
pub resource: Resource,
15+
pub(crate) resource: Resource,
1616
/// The collection of metrics with unique [InstrumentationScope]s.
1717
pub(crate) scope_metrics: Vec<ScopeMetrics>,
1818
}
@@ -27,6 +27,11 @@ impl Default for ResourceMetrics {
2727
}
2828

2929
impl ResourceMetrics {
30+
/// Returns a reference to the [Resource] in [ResourceMetrics].
31+
pub fn resource(&self) -> &Resource {
32+
&self.resource
33+
}
34+
3035
/// Returns an iterator over the [ScopeMetrics] in [ResourceMetrics].
3136
pub fn scope_metrics(&self) -> impl Iterator<Item = &ScopeMetrics> {
3237
self.scope_metrics.iter()
@@ -37,12 +42,17 @@ impl ResourceMetrics {
3742
#[derive(Default, Debug)]
3843
pub struct ScopeMetrics {
3944
/// The [InstrumentationScope] that the meter was created with.
40-
pub scope: InstrumentationScope,
45+
pub(crate) scope: InstrumentationScope,
4146
/// The list of aggregations created by the meter.
4247
pub(crate) metrics: Vec<Metric>,
4348
}
4449

4550
impl ScopeMetrics {
51+
/// Returns a reference to the [InstrumentationScope] in [ScopeMetrics].
52+
pub fn scope(&self) -> &InstrumentationScope {
53+
&self.scope
54+
}
55+
4656
/// Returns an iterator over the [Metric]s in [ScopeMetrics].
4757
pub fn metrics(&self) -> impl Iterator<Item = &Metric> {
4858
self.metrics.iter()
@@ -55,13 +65,35 @@ impl ScopeMetrics {
5565
#[derive(Debug)]
5666
pub struct Metric {
5767
/// The name of the instrument that created this data.
58-
pub name: Cow<'static, str>,
68+
pub(crate) name: Cow<'static, str>,
5969
/// The description of the instrument, which can be used in documentation.
60-
pub description: Cow<'static, str>,
70+
pub(crate) description: Cow<'static, str>,
6171
/// The unit in which the instrument reports.
62-
pub unit: Cow<'static, str>,
72+
pub(crate) unit: Cow<'static, str>,
6373
/// The aggregated data from an instrument.
64-
pub data: AggregatedMetrics,
74+
pub(crate) data: AggregatedMetrics,
75+
}
76+
77+
impl Metric {
78+
/// Returns the name of the instrument that created this data.
79+
pub fn name(&self) -> &str {
80+
&self.name
81+
}
82+
83+
/// Returns the description of the instrument.
84+
pub fn description(&self) -> &str {
85+
&self.description
86+
}
87+
88+
/// Returns the unit in which the instrument reports.
89+
pub fn unit(&self) -> &str {
90+
&self.unit
91+
}
92+
93+
/// Returns the aggregated data from the instrument.
94+
pub fn data(&self) -> &AggregatedMetrics {
95+
&self.data
96+
}
6597
}
6698

6799
/// Aggregated metrics data from an instrument

opentelemetry-stdout/src/metrics/exporter.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ impl PushMetricExporter for MetricExporter {
4848
} else {
4949
println!("Metrics");
5050
println!("Resource");
51-
if let Some(schema_url) = metrics.resource.schema_url() {
51+
if let Some(schema_url) = metrics.resource().schema_url() {
5252
println!("\tResource SchemaUrl: {:?}", schema_url);
5353
}
5454

55-
metrics.resource.iter().for_each(|(k, v)| {
55+
metrics.resource().iter().for_each(|(k, v)| {
5656
println!("\t -> {}={:?}", k, v);
5757
});
5858
print_metrics(metrics.scope_metrics());
@@ -82,29 +82,26 @@ impl PushMetricExporter for MetricExporter {
8282
fn print_metrics<'a>(metrics: impl Iterator<Item = &'a ScopeMetrics>) {
8383
for (i, metric) in metrics.enumerate() {
8484
println!("\tInstrumentation Scope #{}", i);
85-
println!("\t\tName : {}", &metric.scope.name());
86-
if let Some(version) = &metric.scope.version() {
85+
let scope = metric.scope();
86+
println!("\t\tName : {}", scope.name());
87+
if let Some(version) = scope.version() {
8788
println!("\t\tVersion : {:?}", version);
8889
}
89-
if let Some(schema_url) = &metric.scope.schema_url() {
90+
if let Some(schema_url) = scope.schema_url() {
9091
println!("\t\tSchemaUrl: {:?}", schema_url);
9192
}
92-
metric
93-
.scope
94-
.attributes()
95-
.enumerate()
96-
.for_each(|(index, kv)| {
97-
if index == 0 {
98-
println!("\t\tScope Attributes:");
99-
}
100-
println!("\t\t\t -> {}: {}", kv.key, kv.value);
101-
});
93+
scope.attributes().enumerate().for_each(|(index, kv)| {
94+
if index == 0 {
95+
println!("\t\tScope Attributes:");
96+
}
97+
println!("\t\t\t -> {}: {}", kv.key, kv.value);
98+
});
10299

103100
metric.metrics().enumerate().for_each(|(i, metric)| {
104101
println!("Metric #{}", i);
105-
println!("\t\tName : {}", &metric.name);
106-
println!("\t\tDescription : {}", &metric.description);
107-
println!("\t\tUnit : {}", &metric.unit);
102+
println!("\t\tName : {}", metric.name());
103+
println!("\t\tDescription : {}", metric.description());
104+
println!("\t\tUnit : {}", metric.unit());
108105

109106
fn print_info<T>(data: &MetricData<T>)
110107
where
@@ -129,7 +126,7 @@ fn print_metrics<'a>(metrics: impl Iterator<Item = &'a ScopeMetrics>) {
129126
}
130127
}
131128
}
132-
match &metric.data {
129+
match metric.data() {
133130
AggregatedMetrics::F64(data) => print_info(data),
134131
AggregatedMetrics::U64(data) => print_info(data),
135132
AggregatedMetrics::I64(data) => print_info(data),

0 commit comments

Comments
 (0)