Skip to content

Commit dfd2567

Browse files
Copilotlalitb
andcommitted
WIP: Start fixing orphan rule violations by converting From impls to functions
Co-authored-by: lalitb <[email protected]>
1 parent 7fcc26a commit dfd2567

File tree

3 files changed

+103
-122
lines changed

3 files changed

+103
-122
lines changed

opentelemetry-otlp/src/exporter/tonic/metrics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use tonic::{codegen::CompressionEncoding, service::Interceptor, transport::Chann
1111

1212
use super::BoxInterceptor;
1313
use crate::metric::MetricsClient;
14+
use crate::transform::metrics::tonic::resource_metrics_to_export_request;
1415

1516
pub(crate) struct TonicMetricsClient {
1617
inner: Mutex<Option<ClientInner>>,
@@ -81,7 +82,7 @@ impl MetricsClient for TonicMetricsClient {
8182
.export(Request::from_parts(
8283
metadata,
8384
extensions,
84-
ExportMetricsServiceRequest::from(metrics),
85+
resource_metrics_to_export_request(metrics),
8586
))
8687
.await;
8788

opentelemetry-otlp/src/transform/common.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,22 @@ pub mod tonic {
115115
}
116116
}
117117

118-
impl From<Value> for AnyValue {
119-
fn from(value: Value) -> Self {
120-
AnyValue {
121-
value: match value {
122-
Value::Bool(val) => Some(any_value::Value::BoolValue(val)),
123-
Value::I64(val) => Some(any_value::Value::IntValue(val)),
124-
Value::F64(val) => Some(any_value::Value::DoubleValue(val)),
125-
Value::String(val) => Some(any_value::Value::StringValue(val.to_string())),
126-
Value::Array(array) => Some(any_value::Value::ArrayValue(match array {
127-
Array::Bool(vals) => array_into_proto(vals),
128-
Array::I64(vals) => array_into_proto(vals),
129-
Array::F64(vals) => array_into_proto(vals),
130-
Array::String(vals) => array_into_proto(vals),
131-
_ => unreachable!("Nonexistent array type"), // Needs to be updated when new array types are added
132-
})),
133-
_ => unreachable!("Nonexistent value type"), // Needs to be updated when new value types are added
134-
},
135-
}
118+
pub fn value_to_any_value(value: Value) -> AnyValue {
119+
AnyValue {
120+
value: match value {
121+
Value::Bool(val) => Some(any_value::Value::BoolValue(val)),
122+
Value::I64(val) => Some(any_value::Value::IntValue(val)),
123+
Value::F64(val) => Some(any_value::Value::DoubleValue(val)),
124+
Value::String(val) => Some(any_value::Value::StringValue(val.to_string())),
125+
Value::Array(array) => Some(any_value::Value::ArrayValue(match array {
126+
Array::Bool(vals) => array_into_proto(vals),
127+
Array::I64(vals) => array_into_proto(vals),
128+
Array::F64(vals) => array_into_proto(vals),
129+
Array::String(vals) => array_into_proto(vals),
130+
_ => unreachable!("Nonexistent array type"), // Needs to be updated when new array types are added
131+
})),
132+
_ => unreachable!("Nonexistent value type"), // Needs to be updated when new value types are added
133+
},
136134
}
137135
}
138136

@@ -142,7 +140,7 @@ pub mod tonic {
142140
{
143141
let values = vals
144142
.into_iter()
145-
.map(|val| AnyValue::from(Value::from(val)))
143+
.map(|val| value_to_any_value(Value::from(val)))
146144
.collect();
147145

148146
ArrayValue { values }

opentelemetry-otlp/src/transform/metrics.rs

Lines changed: 84 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub mod tonic {
3838
};
3939
use crate::transform::common::{
4040
to_nanos,
41-
tonic::instrumentation_scope_from_scope_ref_and_target,
41+
tonic::{instrumentation_scope_from_scope_ref_and_target, value_to_any_value},
4242
};
4343

4444
pub fn exemplar_value_from_u64(value: u64) -> exemplar::Value {
@@ -65,110 +65,94 @@ pub mod tonic {
6565
number_data_point::Value::AsDouble(value)
6666
}
6767

68-
impl From<(&Key, &Value)> for KeyValue {
69-
fn from(kv: (&Key, &Value)) -> Self {
70-
KeyValue {
71-
key: kv.0.to_string(),
72-
value: Some(kv.1.clone().into()),
73-
}
68+
pub fn key_value_from_key_value_ref(kv: (&Key, &Value)) -> KeyValue {
69+
KeyValue {
70+
key: kv.0.to_string(),
71+
value: Some(value_to_any_value(kv.1.clone())),
7472
}
7573
}
7674

77-
impl From<&opentelemetry::KeyValue> for KeyValue {
78-
fn from(kv: &opentelemetry::KeyValue) -> Self {
79-
KeyValue {
80-
key: kv.key.to_string(),
81-
value: Some(kv.value.clone().into()),
82-
}
75+
pub fn key_value_from_otel_key_value(kv: &opentelemetry::KeyValue) -> KeyValue {
76+
KeyValue {
77+
key: kv.key.to_string(),
78+
value: Some(value_to_any_value(kv.value.clone())),
8379
}
8480
}
8581

86-
impl From<Temporality> for AggregationTemporality {
87-
fn from(temporality: Temporality) -> Self {
88-
match temporality {
89-
Temporality::Cumulative => AggregationTemporality::Cumulative,
90-
Temporality::Delta => AggregationTemporality::Delta,
91-
other => {
92-
otel_debug!(
93-
name: "AggregationTemporality::Unknown",
94-
message = "Unknown temporality,using default instead.",
95-
unknown_temporality = format!("{:?}", other),
96-
default_temporality = format!("{:?}", Temporality::Cumulative)
97-
);
98-
AggregationTemporality::Cumulative
99-
}
82+
pub fn temporality_to_aggregation_temporality(temporality: Temporality) -> AggregationTemporality {
83+
match temporality {
84+
Temporality::Cumulative => AggregationTemporality::Cumulative,
85+
Temporality::Delta => AggregationTemporality::Delta,
86+
other => {
87+
otel_debug!(
88+
name: "AggregationTemporality::Unknown",
89+
message = "Unknown temporality,using default instead.",
90+
unknown_temporality = format!("{:?}", other),
91+
default_temporality = format!("{:?}", Temporality::Cumulative)
92+
);
93+
AggregationTemporality::Cumulative
10094
}
10195
}
10296
}
10397

104-
impl From<&ResourceMetrics> for ExportMetricsServiceRequest {
105-
fn from(rm: &ResourceMetrics) -> Self {
106-
ExportMetricsServiceRequest {
107-
resource_metrics: vec![TonicResourceMetrics {
108-
resource: Some((rm.resource()).into()),
109-
scope_metrics: rm.scope_metrics().map(Into::into).collect(),
110-
schema_url: rm
111-
.resource()
112-
.schema_url()
113-
.map(Into::into)
114-
.unwrap_or_default(),
115-
}],
116-
}
98+
pub fn resource_metrics_to_export_request(rm: &ResourceMetrics) -> ExportMetricsServiceRequest {
99+
ExportMetricsServiceRequest {
100+
resource_metrics: vec![TonicResourceMetrics {
101+
resource: Some(sdk_resource_to_tonic_resource(rm.resource())),
102+
scope_metrics: rm.scope_metrics().map(sdk_scope_metrics_to_tonic_scope_metrics).collect(),
103+
schema_url: rm
104+
.resource()
105+
.schema_url()
106+
.map(|s| s.to_string())
107+
.unwrap_or_default(),
108+
}],
117109
}
118110
}
119111

120-
impl From<&SdkResource> for TonicResource {
121-
fn from(resource: &SdkResource) -> Self {
122-
TonicResource {
123-
attributes: resource.iter().map(Into::into).collect(),
124-
dropped_attributes_count: 0,
125-
entity_refs: vec![], // internal and currently unused
126-
}
112+
pub fn sdk_resource_to_tonic_resource(resource: &SdkResource) -> TonicResource {
113+
TonicResource {
114+
attributes: resource.iter().map(|kv| key_value_from_otel_key_value(kv)).collect(),
115+
dropped_attributes_count: 0,
116+
entity_refs: vec![], // internal and currently unused
127117
}
128118
}
129119

130-
impl From<&SdkScopeMetrics> for TonicScopeMetrics {
131-
fn from(sm: &SdkScopeMetrics) -> Self {
132-
TonicScopeMetrics {
133-
scope: Some(instrumentation_scope_from_scope_ref_and_target(sm.scope(), None)),
134-
metrics: sm.metrics().map(Into::into).collect(),
135-
schema_url: sm
136-
.scope()
137-
.schema_url()
138-
.map(ToOwned::to_owned)
139-
.unwrap_or_default(),
140-
}
120+
pub fn sdk_scope_metrics_to_tonic_scope_metrics(sm: &SdkScopeMetrics) -> TonicScopeMetrics {
121+
TonicScopeMetrics {
122+
scope: Some(instrumentation_scope_from_scope_ref_and_target(sm.scope(), None)),
123+
metrics: sm.metrics().map(sdk_metric_to_tonic_metric).collect(),
124+
schema_url: sm
125+
.scope()
126+
.schema_url()
127+
.map(ToOwned::to_owned)
128+
.unwrap_or_default(),
141129
}
142130
}
143131

144-
impl From<&SdkMetric> for TonicMetric {
145-
fn from(metric: &SdkMetric) -> Self {
146-
TonicMetric {
147-
name: metric.name().to_string(),
148-
description: metric.description().to_string(),
149-
unit: metric.unit().to_string(),
150-
metadata: vec![], // internal and currently unused
151-
data: Some(match metric.data() {
152-
AggregatedMetrics::F64(data) => data.into(),
153-
AggregatedMetrics::U64(data) => data.into(),
154-
AggregatedMetrics::I64(data) => data.into(),
155-
}),
156-
}
132+
pub fn sdk_metric_to_tonic_metric(metric: &SdkMetric) -> TonicMetric {
133+
TonicMetric {
134+
name: metric.name().to_string(),
135+
description: metric.description().to_string(),
136+
unit: metric.unit().to_string(),
137+
metadata: vec![], // internal and currently unused
138+
data: Some(match metric.data() {
139+
AggregatedMetrics::F64(data) => metric_data_to_tonic_metric_data(data),
140+
AggregatedMetrics::U64(data) => metric_data_to_tonic_metric_data(data),
141+
AggregatedMetrics::I64(data) => metric_data_to_tonic_metric_data(data),
142+
}),
157143
}
158144
}
159145

160-
impl<T> From<&MetricData<T>> for TonicMetricData
146+
pub fn metric_data_to_tonic_metric_data<T>(data: &MetricData<T>) -> TonicMetricData
161147
where
162148
T: Numeric + Debug,
163149
{
164-
fn from(data: &MetricData<T>) -> Self {
165-
match data {
166-
MetricData::Gauge(gauge) => TonicMetricData::Gauge(gauge.into()),
167-
MetricData::Sum(sum) => TonicMetricData::Sum(sum.into()),
168-
MetricData::Histogram(hist) => TonicMetricData::Histogram(hist.into()),
169-
MetricData::ExponentialHistogram(hist) => {
170-
TonicMetricData::ExponentialHistogram(hist.into())
171-
}
150+
match data {
151+
MetricData::Gauge(gauge) => TonicMetricData::Gauge(sdk_gauge_to_tonic_gauge(gauge)),
152+
MetricData::Sum(sum) => TonicMetricData::Sum(sdk_sum_to_tonic_sum(sum)),
153+
MetricData::Histogram(hist) => TonicMetricData::Histogram(sdk_histogram_to_tonic_histogram(hist)),
154+
MetricData::ExponentialHistogram(hist) => {
155+
TonicMetricData::ExponentialHistogram(sdk_exponential_histogram_to_tonic_exponential_histogram(hist))
172156
}
173157
}
174158
}
@@ -196,30 +180,28 @@ pub mod tonic {
196180
}
197181
}
198182

199-
impl<T> From<&SdkHistogram<T>> for TonicHistogram
183+
pub fn sdk_histogram_to_tonic_histogram<T>(hist: &SdkHistogram<T>) -> TonicHistogram
200184
where
201185
T: Numeric,
202186
{
203-
fn from(hist: &SdkHistogram<T>) -> Self {
204-
TonicHistogram {
205-
data_points: hist
206-
.data_points()
207-
.map(|dp| TonicHistogramDataPoint {
208-
attributes: dp.attributes().map(Into::into).collect(),
209-
start_time_unix_nano: to_nanos(hist.start_time()),
210-
time_unix_nano: to_nanos(hist.time()),
211-
count: dp.count(),
212-
sum: Some(dp.sum().into_f64()),
213-
bucket_counts: dp.bucket_counts().collect(),
214-
explicit_bounds: dp.bounds().collect(),
215-
exemplars: dp.exemplars().map(Into::into).collect(),
216-
flags: TonicDataPointFlags::default() as u32,
217-
min: dp.min().map(Numeric::into_f64),
218-
max: dp.max().map(Numeric::into_f64),
219-
})
220-
.collect(),
221-
aggregation_temporality: TonicTemporality::from(hist.temporality()).into(),
222-
}
187+
TonicHistogram {
188+
data_points: hist
189+
.data_points()
190+
.map(|dp| TonicHistogramDataPoint {
191+
attributes: dp.attributes().map(|kv| key_value_from_otel_key_value(kv)).collect(),
192+
start_time_unix_nano: to_nanos(hist.start_time()),
193+
time_unix_nano: to_nanos(hist.time()),
194+
count: dp.count(),
195+
sum: Some(dp.sum().into_f64()),
196+
bucket_counts: dp.bucket_counts().collect(),
197+
explicit_bounds: dp.bounds().collect(),
198+
exemplars: dp.exemplars().map(|ex| sdk_exemplar_to_tonic_exemplar(ex)).collect(),
199+
flags: TonicDataPointFlags::default() as u32,
200+
min: dp.min().map(Numeric::into_f64),
201+
max: dp.max().map(Numeric::into_f64),
202+
})
203+
.collect(),
204+
aggregation_temporality: temporality_to_aggregation_temporality(hist.temporality()).into(),
223205
}
224206
}
225207

@@ -254,7 +236,7 @@ pub mod tonic {
254236
zero_threshold: dp.zero_threshold(),
255237
})
256238
.collect(),
257-
aggregation_temporality: TonicTemporality::from(hist.temporality()).into(),
239+
aggregation_temporality: temporality_to_aggregation_temporality(hist.temporality()).into(),
258240
}
259241
}
260242
}

0 commit comments

Comments
 (0)