@@ -38,7 +38,7 @@ pub mod tonic {
38
38
} ;
39
39
use crate :: transform:: common:: {
40
40
to_nanos,
41
- tonic:: instrumentation_scope_from_scope_ref_and_target,
41
+ tonic:: { instrumentation_scope_from_scope_ref_and_target, value_to_any_value } ,
42
42
} ;
43
43
44
44
pub fn exemplar_value_from_u64 ( value : u64 ) -> exemplar:: Value {
@@ -65,110 +65,94 @@ pub mod tonic {
65
65
number_data_point:: Value :: AsDouble ( value)
66
66
}
67
67
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 ( ) ) ) ,
74
72
}
75
73
}
76
74
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 ( ) ) ) ,
83
79
}
84
80
}
85
81
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
100
94
}
101
95
}
102
96
}
103
97
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
+ } ] ,
117
109
}
118
110
}
119
111
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
127
117
}
128
118
}
129
119
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 ( ) ,
141
129
}
142
130
}
143
131
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
+ } ) ,
157
143
}
158
144
}
159
145
160
- impl < T > From < & MetricData < T > > for TonicMetricData
146
+ pub fn metric_data_to_tonic_metric_data < T > ( data : & MetricData < T > ) -> TonicMetricData
161
147
where
162
148
T : Numeric + Debug ,
163
149
{
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) )
172
156
}
173
157
}
174
158
}
@@ -196,30 +180,28 @@ pub mod tonic {
196
180
}
197
181
}
198
182
199
- impl < T > From < & SdkHistogram < T > > for TonicHistogram
183
+ pub fn sdk_histogram_to_tonic_histogram < T > ( hist : & SdkHistogram < T > ) -> TonicHistogram
200
184
where
201
185
T : Numeric ,
202
186
{
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 ( ) ,
223
205
}
224
206
}
225
207
@@ -254,7 +236,7 @@ pub mod tonic {
254
236
zero_threshold : dp. zero_threshold ( ) ,
255
237
} )
256
238
. collect ( ) ,
257
- aggregation_temporality : TonicTemporality :: from ( hist. temporality ( ) ) . into ( ) ,
239
+ aggregation_temporality : temporality_to_aggregation_temporality ( hist. temporality ( ) ) . into ( ) ,
258
240
}
259
241
}
260
242
}
0 commit comments