use crate::InitError;
use crate::config::exporters::{HoneycombGrpcExporterConfig, HoneycombHttpExporterConfig};
use crate::error::ExporterKind;
pub(crate) fn build_http_span_exporter(
config: &HoneycombHttpExporterConfig,
) -> Result<opentelemetry_otlp::SpanExporter, InitError> {
use opentelemetry_otlp::{SpanExporter, WithExportConfig, WithHttpConfig};
let mut headers = std::collections::HashMap::new();
headers.insert(
"x-honeycomb-team".to_string(),
config
.api_key
.unredact()
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombHttp,
reason: e.to_string(),
})?
.to_string(),
);
if let Some(dataset) = &config.dataset {
headers.insert(
"x-honeycomb-dataset".to_string(),
dataset
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombHttp,
reason: e.to_string(),
})?
.to_string(),
);
}
SpanExporter::builder()
.with_http()
.with_endpoint(config.endpoint.as_str())
.with_headers(headers)
.build()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombHttp,
reason: e.to_string(),
})
}
pub(crate) fn build_grpc_span_exporter(
config: &HoneycombGrpcExporterConfig,
) -> Result<opentelemetry_otlp::SpanExporter, InitError> {
use opentelemetry_otlp::{SpanExporter, WithExportConfig, WithTonicConfig};
let mut metadata = tonic::metadata::MetadataMap::new();
metadata.insert(
"x-honeycomb-team",
config
.api_key
.unredact()
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
})?
.parse()
.map_err(
|e: tonic::metadata::errors::InvalidMetadataValue| InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
},
)?,
);
if let Some(dataset) = &config.dataset {
metadata.insert(
"x-honeycomb-dataset",
dataset
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
})?
.parse()
.map_err(|e: tonic::metadata::errors::InvalidMetadataValue| {
InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
}
})?,
);
}
SpanExporter::builder()
.with_tonic()
.with_endpoint(config.endpoint.as_str())
.with_metadata(metadata)
.build()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
})
}
pub(crate) fn build_http_log_exporter(
config: &HoneycombHttpExporterConfig,
) -> Result<opentelemetry_otlp::LogExporter, InitError> {
use opentelemetry_otlp::{LogExporter, WithExportConfig, WithHttpConfig};
let mut headers = std::collections::HashMap::new();
headers.insert(
"x-honeycomb-team".to_string(),
config
.api_key
.unredact()
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombHttp,
reason: e.to_string(),
})?
.to_string(),
);
if let Some(dataset) = &config.dataset {
headers.insert(
"x-honeycomb-dataset".to_string(),
dataset
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombHttp,
reason: e.to_string(),
})?
.to_string(),
);
}
LogExporter::builder()
.with_http()
.with_endpoint(config.endpoint.as_str())
.with_headers(headers)
.build()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombHttp,
reason: e.to_string(),
})
}
pub(crate) fn build_grpc_log_exporter(
config: &HoneycombGrpcExporterConfig,
) -> Result<opentelemetry_otlp::LogExporter, InitError> {
use opentelemetry_otlp::{LogExporter, WithExportConfig, WithTonicConfig};
let mut metadata = tonic::metadata::MetadataMap::new();
metadata.insert(
"x-honeycomb-team",
config
.api_key
.unredact()
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
})?
.parse()
.map_err(
|e: tonic::metadata::errors::InvalidMetadataValue| InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
},
)?,
);
if let Some(dataset) = &config.dataset {
metadata.insert(
"x-honeycomb-dataset",
dataset
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
})?
.parse()
.map_err(|e: tonic::metadata::errors::InvalidMetadataValue| {
InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
}
})?,
);
}
LogExporter::builder()
.with_tonic()
.with_endpoint(config.endpoint.as_str())
.with_metadata(metadata)
.build()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
})
}
pub(crate) fn build_http_metric_exporter(
config: &HoneycombHttpExporterConfig,
) -> Result<opentelemetry_otlp::MetricExporter, InitError> {
use opentelemetry_otlp::{MetricExporter, WithExportConfig, WithHttpConfig};
let mut headers = std::collections::HashMap::new();
headers.insert(
"x-honeycomb-team".to_string(),
config
.api_key
.unredact()
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombHttp,
reason: e.to_string(),
})?
.to_string(),
);
if let Some(dataset) = &config.dataset {
headers.insert(
"x-honeycomb-dataset".to_string(),
dataset
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombHttp,
reason: e.to_string(),
})?
.to_string(),
);
}
MetricExporter::builder()
.with_http()
.with_endpoint(config.endpoint.as_str())
.with_headers(headers)
.build()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombHttp,
reason: e.to_string(),
})
}
pub(crate) fn build_grpc_metric_exporter(
config: &HoneycombGrpcExporterConfig,
) -> Result<opentelemetry_otlp::MetricExporter, InitError> {
use opentelemetry_otlp::{MetricExporter, WithExportConfig, WithTonicConfig};
let mut metadata = tonic::metadata::MetadataMap::new();
metadata.insert(
"x-honeycomb-team",
config
.api_key
.unredact()
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
})?
.parse()
.map_err(
|e: tonic::metadata::errors::InvalidMetadataValue| InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
},
)?,
);
if let Some(dataset) = &config.dataset {
metadata.insert(
"x-honeycomb-dataset",
dataset
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
})?
.parse()
.map_err(|e: tonic::metadata::errors::InvalidMetadataValue| {
InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
}
})?,
);
}
MetricExporter::builder()
.with_tonic()
.with_endpoint(config.endpoint.as_str())
.with_metadata(metadata)
.build()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::HoneycombGrpc,
reason: e.to_string(),
})
}
#[cfg(test)]
mod tests {
use apollo_configuration::parse_yaml;
use super::*;
use crate::config::OpenTelemetryConfig;
use crate::config::SpanExporter;
use crate::config::traces::SpanProcessor;
fn get_honeycomb_http_config(config: &OpenTelemetryConfig) -> &HoneycombHttpExporterConfig {
match &config.tracer_provider.processors[0] {
SpanProcessor::Batch(batch) => match &batch.exporter {
SpanExporter::HoneycombHttp(hc) => hc,
_ => panic!("Expected HoneycombHttp exporter"),
},
_ => panic!("Expected Batch processor"),
}
}
fn get_honeycomb_grpc_config(config: &OpenTelemetryConfig) -> &HoneycombGrpcExporterConfig {
match &config.tracer_provider.processors[0] {
SpanProcessor::Batch(batch) => match &batch.exporter {
SpanExporter::HoneycombGrpc(hc) => hc,
_ => panic!("Expected HoneycombGrpc exporter"),
},
_ => panic!("Expected Batch processor"),
}
}
#[test]
fn build_http_span_exporter_from_config() {
let config: OpenTelemetryConfig = parse_yaml(
indoc::indoc! {"
tracer_provider:
processors:
- batch:
exporter:
honeycomb_http:
api_key: test-api-key
endpoint: https://api.honeycomb.io
"},
&Default::default(),
)
.unwrap();
let hc = get_honeycomb_http_config(&config);
let result = build_http_span_exporter(hc);
assert!(result.is_ok());
}
#[test]
fn build_http_span_exporter_with_dataset() {
let config: OpenTelemetryConfig = parse_yaml(
indoc::indoc! {"
tracer_provider:
processors:
- batch:
exporter:
honeycomb_http:
api_key: test-api-key
endpoint: https://api.eu1.honeycomb.io
dataset: my-dataset
"},
&Default::default(),
)
.unwrap();
let hc = get_honeycomb_http_config(&config);
let result = build_http_span_exporter(hc);
assert!(result.is_ok());
}
#[test]
fn build_http_log_exporter_from_config() {
let config: OpenTelemetryConfig = parse_yaml(
indoc::indoc! {"
tracer_provider:
processors:
- batch:
exporter:
honeycomb_http:
api_key: test-api-key
endpoint: https://api.honeycomb.io
"},
&Default::default(),
)
.unwrap();
let hc = get_honeycomb_http_config(&config);
let result = build_http_log_exporter(hc);
assert!(result.is_ok());
}
#[test]
fn build_http_metric_exporter_from_config() {
let config: OpenTelemetryConfig = parse_yaml(
indoc::indoc! {"
tracer_provider:
processors:
- batch:
exporter:
honeycomb_http:
api_key: test-api-key
endpoint: https://api.honeycomb.io
"},
&Default::default(),
)
.unwrap();
let hc = get_honeycomb_http_config(&config);
let result = build_http_metric_exporter(hc);
assert!(result.is_ok());
}
#[tokio::test]
async fn build_grpc_span_exporter_from_config() {
let config: OpenTelemetryConfig = parse_yaml(
indoc::indoc! {"
tracer_provider:
processors:
- batch:
exporter:
honeycomb_grpc:
api_key: test-api-key
endpoint: https://api.honeycomb.io:443
"},
&Default::default(),
)
.unwrap();
let hc = get_honeycomb_grpc_config(&config);
let result = build_grpc_span_exporter(hc);
assert!(result.is_ok());
}
#[tokio::test]
async fn build_grpc_span_exporter_with_dataset() {
let config: OpenTelemetryConfig = parse_yaml(
indoc::indoc! {"
tracer_provider:
processors:
- batch:
exporter:
honeycomb_grpc:
api_key: test-api-key
endpoint: https://api.eu1.honeycomb.io:443
dataset: my-dataset
"},
&Default::default(),
)
.unwrap();
let hc = get_honeycomb_grpc_config(&config);
let result = build_grpc_span_exporter(hc);
assert!(result.is_ok());
}
#[tokio::test]
async fn build_grpc_log_exporter_from_config() {
let config: OpenTelemetryConfig = parse_yaml(
indoc::indoc! {"
tracer_provider:
processors:
- batch:
exporter:
honeycomb_grpc:
api_key: test-api-key
endpoint: https://api.honeycomb.io:443
"},
&Default::default(),
)
.unwrap();
let hc = get_honeycomb_grpc_config(&config);
let result = build_grpc_log_exporter(hc);
assert!(result.is_ok());
}
#[tokio::test]
async fn build_grpc_metric_exporter_from_config() {
let config: OpenTelemetryConfig = parse_yaml(
indoc::indoc! {"
tracer_provider:
processors:
- batch:
exporter:
honeycomb_grpc:
api_key: test-api-key
endpoint: https://api.honeycomb.io:443
"},
&Default::default(),
)
.unwrap();
let hc = get_honeycomb_grpc_config(&config);
let result = build_grpc_metric_exporter(hc);
assert!(result.is_ok());
}
}