use crate::{ExportConfig, Protocol};
use opentelemetry_http::HttpClient;
use std::collections::HashMap;
use std::sync::Arc;
use super::default_headers;
#[cfg(feature = "http-proto")]
#[derive(Debug)]
#[cfg_attr(
all(
not(feature = "reqwest-client"),
not(feature = "surf-client"),
not(feature = "reqwest-blocking-client")
),
derive(Default)
)]
pub struct HttpConfig {
pub client: Option<Arc<dyn HttpClient>>,
pub headers: Option<HashMap<String, String>>,
}
#[cfg(any(
feature = "reqwest-blocking-client",
feature = "reqwest-client",
feature = "surf-client"
))]
impl Default for HttpConfig {
fn default() -> Self {
HttpConfig {
#[cfg(feature = "reqwest-blocking-client")]
client: Some(Arc::new(reqwest::blocking::Client::new())),
#[cfg(all(
not(feature = "reqwest-blocking-client"),
not(feature = "surf-client"),
feature = "reqwest-client"
))]
client: Some(Arc::new(reqwest::Client::new())),
#[cfg(all(
not(feature = "reqwest-client"),
not(feature = "reqwest-blocking-client"),
feature = "surf-client"
))]
client: Some(Arc::new(surf::Client::new())),
#[cfg(all(
not(feature = "reqwest-client"),
not(feature = "surf-client"),
not(feature = "reqwest-blocking-client")
))]
client: None,
headers: None,
}
}
}
#[derive(Debug)]
pub struct HttpExporterBuilder {
pub(crate) exporter_config: ExportConfig,
pub(crate) http_config: HttpConfig,
}
impl Default for HttpExporterBuilder {
fn default() -> Self {
HttpExporterBuilder {
exporter_config: ExportConfig {
protocol: Protocol::HttpBinary,
..ExportConfig::default()
},
http_config: HttpConfig {
headers: Some(default_headers()),
..HttpConfig::default()
},
}
}
}
impl HttpExporterBuilder {
pub fn with_http_client<T: HttpClient + 'static>(mut self, client: T) -> Self {
self.http_config.client = Some(Arc::new(client));
self
}
pub fn with_headers(mut self, headers: HashMap<String, String>) -> Self {
let mut inst_headers = self.http_config.headers.unwrap_or_default();
inst_headers.extend(headers.into_iter());
self.http_config.headers = Some(inst_headers);
self
}
}