use crate::exporter::Compression;
use crate::ExportConfig;
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use super::default_headers;
#[derive(Debug)]
#[non_exhaustive]
pub struct GrpcioConfig {
pub credentials: Option<Credentials>,
pub headers: Option<HashMap<String, String>>,
pub compression: Option<Compression>,
pub use_tls: Option<bool>,
pub completion_queue_count: usize,
}
impl Default for GrpcioConfig {
fn default() -> Self {
GrpcioConfig {
credentials: None,
headers: Some(default_headers()),
compression: None,
use_tls: None,
completion_queue_count: 2,
}
}
}
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[derive(Clone, Debug)]
pub struct Credentials {
pub cert: String,
pub key: String,
}
impl From<Compression> for grpcio::CompressionAlgorithms {
fn from(compression: Compression) -> Self {
match compression {
Compression::Gzip => grpcio::CompressionAlgorithms::GRPC_COMPRESS_GZIP,
}
}
}
#[derive(Default, Debug)]
pub struct GrpcioExporterBuilder {
pub(crate) exporter_config: ExportConfig,
pub(crate) grpcio_config: GrpcioConfig,
}
impl GrpcioExporterBuilder {
pub fn with_credentials(mut self, credentials: Credentials) -> Self {
self.grpcio_config.credentials = Some(credentials);
self
}
pub fn with_headers(mut self, headers: HashMap<String, String>) -> Self {
let mut inst_headers = self.grpcio_config.headers.unwrap_or_default();
inst_headers.extend(headers.into_iter());
self.grpcio_config.headers = Some(inst_headers);
self
}
pub fn with_compression(mut self, compression: Compression) -> Self {
self.grpcio_config.compression = Some(compression);
self
}
pub fn with_tls(mut self, use_tls: bool) -> Self {
self.grpcio_config.use_tls = Some(use_tls);
self
}
pub fn with_completion_queue_count(mut self, count: usize) -> Self {
self.grpcio_config.completion_queue_count = count;
self
}
}
#[cfg(test)]
mod tests {
use crate::GrpcioExporterBuilder;
use std::collections::HashMap;
#[test]
fn test_with_headers() {
let mut headers = HashMap::new();
headers.insert("key".to_string(), "value".to_string());
let builder = GrpcioExporterBuilder::default().with_headers(headers);
let result = builder.grpcio_config.headers.unwrap();
assert_eq!(result.get("key").unwrap(), "value");
assert!(result.get("User-Agent").is_some());
let mut headers = HashMap::new();
headers.insert("User-Agent".to_string(), "baz".to_string());
let builder = GrpcioExporterBuilder::default().with_headers(headers);
let result = builder.grpcio_config.headers.unwrap();
assert_eq!(result.get("User-Agent").unwrap(), "baz");
assert_eq!(
result.len(),
GrpcioExporterBuilder::default()
.grpcio_config
.headers
.unwrap()
.len()
);
}
}