1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//! # OTLP - Span Exporter
//!
//! Defines a [SpanExporter] to send trace data via the OpenTelemetry Protocol (OTLP)

#[cfg(feature = "tonic")]
use crate::proto::collector::trace::v1::{
    trace_service_client::TraceServiceClient, ExportTraceServiceRequest,
};

#[cfg(feature = "tonic")]
use tonic::{
    metadata::{KeyAndValueRef, MetadataMap},
    transport::Channel,
    Request,
};

#[cfg(all(feature = "tonic", feature = "tls"))]
use tonic::transport::ClientTlsConfig;

#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
use crate::proto::grpcio::trace_service::ExportTraceServiceRequest;

#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
use crate::proto::grpcio::trace_service_grpc::TraceServiceClient;

#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
use grpcio::{
    CallOption, Channel, ChannelBuilder, ChannelCredentialsBuilder, Environment, MetadataBuilder,
};

#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
use protobuf::RepeatedField;

use async_trait::async_trait;

#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
use std::collections::HashMap;

use std::fmt;
use std::fmt::Debug;

#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
use std::sync::Arc;

use crate::Protocol;
use opentelemetry::sdk::export::trace::{ExportResult, SpanData, SpanExporter};
use std::time::Duration;

/// Exporter that sends data in OTLP format.
pub struct TraceExporter {
    #[cfg(feature = "tonic")]
    metadata: Option<MetadataMap>,

    #[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
    headers: Option<HashMap<String, String>>,

    timeout: Duration,

    #[cfg(feature = "tonic")]
    trace_exporter: TraceServiceClient<Channel>,

    #[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
    trace_exporter: TraceServiceClient,

    #[cfg(all(feature = "default", not(feature = "async")))]
    runtime: tokio::runtime::Runtime,
}

/// Configuration for the OTLP exporter.
#[derive(Debug)]
pub struct ExporterConfig {
    /// The address of the OTLP collector. If not set, the default address is used.
    pub endpoint: String,

    /// The protocol to use when communicating with the collector.
    pub protocol: Protocol,

    #[cfg(all(feature = "tonic", feature = "tls"))]
    /// TLS settings for the collector endpoint.
    pub tls_config: Option<ClientTlsConfig>,

    #[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
    /// The credentials to use when communicating with the collector.
    pub credentials: Option<Credentials>,

    /// Custom metadata entries to send to the collector.
    #[cfg(feature = "tonic")]
    pub metadata: Option<MetadataMap>,

    /// Additional headers to send to the collector.
    #[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
    pub headers: Option<HashMap<String, String>>,

    /// The compression algorithm to use when communicating with the collector.
    #[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
    pub compression: Option<Compression>,

    /// The timeout to the collector.
    pub timeout: Duration,

    /// The number of GRPC worker threads to poll queues.
    #[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
    pub completion_queue_count: usize,

    /// The Tokio runtime.
    #[cfg(all(feature = "tonic", not(feature = "async")))]
    pub runtime: Option<tokio::runtime::Runtime>,
}

/// Credential configuration for authenticated requests.
#[derive(Debug)]
#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
pub struct Credentials {
    /// Credential cert
    pub cert: String,
    /// Credential key
    pub key: String,
}

/// The compression algorithm to use when sending data.
#[derive(Clone, Copy, Debug)]
#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
pub enum Compression {
    /// Compresses data using gzip.
    Gzip,
}

#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
impl Into<grpcio::CompressionAlgorithms> for Compression {
    fn into(self) -> grpcio::CompressionAlgorithms {
        match self {
            Compression::Gzip => grpcio::CompressionAlgorithms::GRPC_COMPRESS_GZIP,
        }
    }
}

const DEFAULT_OTLP_PORT: u16 = 4317;

impl Default for ExporterConfig {
    #[cfg(feature = "tonic")]
    fn default() -> Self {
        ExporterConfig {
            endpoint: format!("localhost:{}", DEFAULT_OTLP_PORT),
            protocol: Protocol::Grpc,
            #[cfg(all(feature = "tonic", feature = "tls"))]
            tls_config: None,
            metadata: None,
            timeout: Duration::from_secs(60),
            #[cfg(not(feature = "async"))]
            runtime: None,
        }
    }

    #[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
    fn default() -> Self {
        ExporterConfig {
            endpoint: format!("localhost:{}", DEFAULT_OTLP_PORT),
            protocol: Protocol::Grpc,
            credentials: None,
            headers: None,
            compression: None,
            timeout: Duration::from_secs(60),
            completion_queue_count: 2,
        }
    }
}

impl Default for TraceExporter {
    /// Return a Span Exporter with the default configuration
    #[cfg(feature = "tonic")]
    fn default() -> Self {
        let config: ExporterConfig = ExporterConfig::default();

        let endpoint = Channel::from_shared(config.endpoint).unwrap();

        let channel = endpoint.timeout(config.timeout).connect_lazy().unwrap();

        TraceExporter {
            trace_exporter: TraceServiceClient::new(channel),
            timeout: config.timeout,
            metadata: config.metadata,
            #[cfg(not(feature = "async"))]
            runtime: config.runtime.unwrap_or_else(|| {
                tokio::runtime::Builder::new_current_thread()
                    .enable_all()
                    .build()
                    .unwrap()
            }),
        }
    }

    /// Return a Span Exporter with the default configuration
    #[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
    fn default() -> Self {
        let config: ExporterConfig = ExporterConfig::default();

        let channel: Channel =
            ChannelBuilder::new(Arc::new(Environment::new(config.completion_queue_count)))
                .connect(config.endpoint.as_str());

        TraceExporter {
            trace_exporter: TraceServiceClient::new(channel),
            timeout: config.timeout,
            headers: None,
        }
    }
}

impl Debug for TraceExporter {
    #[cfg(feature = "tonic")]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Exporter")
            .field("metadata", &self.metadata)
            .field("timeout", &self.timeout)
            .field("trace_exporter", &"TraceServiceClient")
            .finish()
    }

    #[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Exporter")
            .field("headers", &self.headers)
            .field("timeout", &self.timeout)
            .field("trace_exporter", &"TraceServiceClient")
            .finish()
    }
}

impl TraceExporter {
    /// Builds a new span exporter with the given configuration
    #[cfg(feature = "tonic")]
    pub fn new(config: ExporterConfig) -> Result<Self, crate::Error> {
        let endpoint = Channel::from_shared(config.endpoint)?;

        #[cfg(all(feature = "tonic", feature = "tls"))]
        let channel = match config.tls_config {
            Some(tls_config) => endpoint.tls_config(tls_config)?,
            None => endpoint,
        }
        .timeout(config.timeout)
        .connect_lazy()?;

        #[cfg(not(feature = "tls"))]
        let channel = endpoint.timeout(config.timeout).connect_lazy()?;

        let client = match config.metadata.to_owned() {
            None => TraceServiceClient::new(channel),
            Some(metadata) => {
                TraceServiceClient::with_interceptor(channel, move |mut req: Request<()>| {
                    for key_and_value in metadata.iter() {
                        match key_and_value {
                            KeyAndValueRef::Ascii(key, value) => {
                                req.metadata_mut().append(key, value.to_owned())
                            }
                            KeyAndValueRef::Binary(key, value) => {
                                req.metadata_mut().append_bin(key, value.to_owned())
                            }
                        };
                    }

                    Ok(req)
                })
            }
        };

        Ok(TraceExporter {
            trace_exporter: client,
            timeout: config.timeout,
            metadata: config.metadata,
            #[cfg(not(feature = "async"))]
            runtime: config.runtime.unwrap_or_else(|| {
                tokio::runtime::Builder::new_current_thread()
                    .enable_all()
                    .build()
                    .unwrap()
            }),
        })
    }

    /// Builds a new span exporter with the given configuration
    #[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
    pub fn new(config: ExporterConfig) -> Self {
        let mut builder: ChannelBuilder =
            ChannelBuilder::new(Arc::new(Environment::new(config.completion_queue_count)));

        if let Some(compression) = config.compression {
            builder = builder.default_compression_algorithm(compression.into());
        }

        let channel: Channel = match config.credentials {
            None => builder.connect(config.endpoint.as_str()),
            Some(credentials) => builder.secure_connect(
                config.endpoint.as_str(),
                ChannelCredentialsBuilder::new()
                    .cert(credentials.cert.into(), credentials.key.into())
                    .build(),
            ),
        };

        TraceExporter {
            trace_exporter: TraceServiceClient::new(channel),
            timeout: config.timeout,
            headers: config.headers,
        }
    }
}

#[async_trait]
impl SpanExporter for TraceExporter {
    #[cfg(feature = "tonic")]
    async fn export(&mut self, batch: Vec<SpanData>) -> ExportResult {
        let request = Request::new(ExportTraceServiceRequest {
            resource_spans: batch.into_iter().map(Into::into).collect(),
        });

        #[cfg(feature = "async")]
        self.trace_exporter
            .to_owned()
            .export(request)
            .await
            .map_err::<crate::Error, _>(Into::into)?;

        #[cfg(not(feature = "async"))]
        self.runtime
            .block_on(self.trace_exporter.to_owned().export(request))
            .map_err::<crate::Error, _>(Into::into)?;

        Ok(())
    }

    #[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
    async fn export(&mut self, batch: Vec<SpanData>) -> ExportResult {
        let request = ExportTraceServiceRequest {
            resource_spans: RepeatedField::from_vec(batch.into_iter().map(Into::into).collect()),
            unknown_fields: Default::default(),
            cached_size: Default::default(),
        };

        let mut call_options = CallOption::default().timeout(self.timeout);

        if let Some(headers) = self.headers.clone() {
            let mut metadata_builder: MetadataBuilder = MetadataBuilder::new();

            for (key, value) in headers {
                let _ = metadata_builder.add_str(key.as_str(), value.as_str());
            }

            call_options = call_options.headers(metadata_builder.build());
        }

        let receiver = self
            .trace_exporter
            .export_async_opt(&request, call_options)
            .map_err::<crate::Error, _>(Into::into)?;
        receiver.await.map_err::<crate::Error, _>(Into::into)?;
        Ok(())
    }
}