@@ -208,15 +208,44 @@ impl TonicExporterBuilder {
208
208
}
209
209
210
210
fn build_channel (
211
- & mut self ,
211
+ self ,
212
212
signal_endpoint_var : & str ,
213
213
signal_endpoint_path : & str ,
214
214
signal_timeout_var : & str ,
215
215
signal_compression_var : & str ,
216
216
) -> Result < ( Channel , BoxInterceptor , Option < CompressionEncoding > ) , crate :: Error > {
217
- let config = & mut self . exporter_config ;
218
- let tonic_config : & mut TonicConfig = & mut self . tonic_config ;
217
+ let tonic_config = self . tonic_config ;
218
+ let compression = resolve_compression ( & tonic_config, signal_compression_var ) ? ;
219
219
220
+ let metadata = tonic_config. metadata . unwrap_or_default ( ) ;
221
+ let add_metadata = move |mut req : tonic:: Request < ( ) > | {
222
+ for key_and_value in metadata. iter ( ) {
223
+ match key_and_value {
224
+ KeyAndValueRef :: Ascii ( key, value) => {
225
+ req. metadata_mut ( ) . append ( key, value. to_owned ( ) )
226
+ }
227
+ KeyAndValueRef :: Binary ( key, value) => {
228
+ req. metadata_mut ( ) . append_bin ( key, value. to_owned ( ) )
229
+ }
230
+ } ;
231
+ }
232
+
233
+ Ok ( req)
234
+ } ;
235
+
236
+ let interceptor = match self . interceptor {
237
+ Some ( mut interceptor) => {
238
+ BoxInterceptor ( Box :: new ( move |req| interceptor. call ( add_metadata ( req) ?) ) )
239
+ }
240
+ None => BoxInterceptor ( Box :: new ( add_metadata) ) ,
241
+ } ;
242
+
243
+ // If a custom channel was provided, use that channel instead of creating one
244
+ if let Some ( channel) = self . channel {
245
+ return Ok ( ( channel, interceptor, compression) ) ;
246
+ }
247
+
248
+ let config = self . exporter_config ;
220
249
let endpoint = match env:: var ( signal_endpoint_var)
221
250
. ok ( )
222
251
. or ( env:: var ( OTEL_EXPORTER_OTLP_ENDPOINT ) . ok ( ) )
@@ -225,6 +254,7 @@ impl TonicExporterBuilder {
225
254
None => format ! ( "{}{signal_endpoint_path}" , config. endpoint) ,
226
255
} ;
227
256
257
+ let endpoint = Channel :: from_shared ( endpoint) . map_err ( crate :: Error :: from) ?;
228
258
let timeout = match env:: var ( signal_timeout_var)
229
259
. ok ( )
230
260
. or ( env:: var ( OTEL_EXPORTER_OTLP_TIMEOUT ) . ok ( ) )
@@ -235,12 +265,9 @@ impl TonicExporterBuilder {
235
265
} ,
236
266
None => config. timeout ,
237
267
} ;
238
- let compression = resolve_compression ( tonic_config, signal_compression_var) ?;
239
-
240
- let endpoint = Channel :: from_shared ( endpoint) . map_err ( crate :: Error :: from) ?;
241
268
242
269
#[ cfg( feature = "tls" ) ]
243
- let channel = match tonic_config. tls_config . take ( ) {
270
+ let channel = match tonic_config. tls_config {
244
271
Some ( tls_config) => endpoint
245
272
. tls_config ( tls_config)
246
273
. map_err ( crate :: Error :: from) ?,
@@ -252,36 +279,13 @@ impl TonicExporterBuilder {
252
279
#[ cfg( not( feature = "tls" ) ) ]
253
280
let channel = endpoint. timeout ( timeout) . connect_lazy ( ) ;
254
281
255
- let metadata = tonic_config. metadata . take ( ) . unwrap_or_default ( ) ;
256
- let add_metadata = move |mut req : tonic:: Request < ( ) > | {
257
- for key_and_value in metadata. iter ( ) {
258
- match key_and_value {
259
- KeyAndValueRef :: Ascii ( key, value) => {
260
- req. metadata_mut ( ) . append ( key, value. to_owned ( ) )
261
- }
262
- KeyAndValueRef :: Binary ( key, value) => {
263
- req. metadata_mut ( ) . append_bin ( key, value. to_owned ( ) )
264
- }
265
- } ;
266
- }
267
-
268
- Ok ( req)
269
- } ;
270
-
271
- let interceptor = match self . interceptor . take ( ) {
272
- Some ( mut interceptor) => {
273
- BoxInterceptor ( Box :: new ( move |req| interceptor. call ( add_metadata ( req) ?) ) )
274
- }
275
- None => BoxInterceptor ( Box :: new ( add_metadata) ) ,
276
- } ;
277
-
278
282
Ok ( ( channel, interceptor, compression) )
279
283
}
280
284
281
285
/// Build a new tonic log exporter
282
286
#[ cfg( feature = "logs" ) ]
283
287
pub fn build_log_exporter (
284
- mut self ,
288
+ self ,
285
289
) -> Result < crate :: logs:: LogExporter , opentelemetry:: logs:: LogError > {
286
290
use crate :: exporter:: tonic:: logs:: TonicLogsClient ;
287
291
@@ -300,7 +304,7 @@ impl TonicExporterBuilder {
300
304
/// Build a new tonic metrics exporter
301
305
#[ cfg( feature = "metrics" ) ]
302
306
pub fn build_metrics_exporter (
303
- mut self ,
307
+ self ,
304
308
aggregation_selector : Box < dyn opentelemetry_sdk:: metrics:: reader:: AggregationSelector > ,
305
309
temporality_selector : Box < dyn opentelemetry_sdk:: metrics:: reader:: TemporalitySelector > ,
306
310
) -> opentelemetry:: metrics:: Result < crate :: MetricsExporter > {
@@ -326,7 +330,7 @@ impl TonicExporterBuilder {
326
330
/// Build a new tonic span exporter
327
331
#[ cfg( feature = "trace" ) ]
328
332
pub fn build_span_exporter (
329
- mut self ,
333
+ self ,
330
334
) -> Result < crate :: SpanExporter , opentelemetry:: trace:: TraceError > {
331
335
use crate :: exporter:: tonic:: trace:: TonicTracesClient ;
332
336
0 commit comments