use crate::InitError;
use crate::config::exporters::{NewRelicGrpcExporterConfig, NewRelicHttpExporterConfig};
use crate::error::ExporterKind;
pub(crate) fn build_http_span_exporter(
config: &NewRelicHttpExporterConfig,
) -> Result<opentelemetry_otlp::SpanExporter, InitError> {
use opentelemetry_otlp::{SpanExporter, WithExportConfig, WithHttpConfig};
let mut headers = std::collections::HashMap::new();
headers.insert(
"Api-Key".to_string(),
config
.license_key
.unredact()
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::NewRelicHttp,
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::NewRelicHttp,
reason: e.to_string(),
})
}
pub(crate) fn build_grpc_span_exporter(
config: &NewRelicGrpcExporterConfig,
) -> Result<opentelemetry_otlp::SpanExporter, InitError> {
use opentelemetry_otlp::{SpanExporter, WithExportConfig, WithTonicConfig};
let mut metadata = tonic::metadata::MetadataMap::new();
metadata.insert(
"api-key",
config
.license_key
.unredact()
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::NewRelicGrpc,
reason: e.to_string(),
})?
.parse()
.map_err(
|e: tonic::metadata::errors::InvalidMetadataValue| InitError::Exporter {
exporter: ExporterKind::NewRelicGrpc,
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::NewRelicGrpc,
reason: e.to_string(),
})
}
pub(crate) fn build_http_log_exporter(
config: &NewRelicHttpExporterConfig,
) -> Result<opentelemetry_otlp::LogExporter, InitError> {
use opentelemetry_otlp::{LogExporter, WithExportConfig, WithHttpConfig};
let mut headers = std::collections::HashMap::new();
headers.insert(
"Api-Key".to_string(),
config
.license_key
.unredact()
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::NewRelicHttp,
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::NewRelicHttp,
reason: e.to_string(),
})
}
pub(crate) fn build_grpc_log_exporter(
config: &NewRelicGrpcExporterConfig,
) -> Result<opentelemetry_otlp::LogExporter, InitError> {
use opentelemetry_otlp::{LogExporter, WithExportConfig, WithTonicConfig};
let mut metadata = tonic::metadata::MetadataMap::new();
metadata.insert(
"api-key",
config
.license_key
.unredact()
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::NewRelicGrpc,
reason: e.to_string(),
})?
.parse()
.map_err(
|e: tonic::metadata::errors::InvalidMetadataValue| InitError::Exporter {
exporter: ExporterKind::NewRelicGrpc,
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::NewRelicGrpc,
reason: e.to_string(),
})
}
pub(crate) fn build_http_metric_exporter(
config: &NewRelicHttpExporterConfig,
) -> Result<opentelemetry_otlp::MetricExporter, InitError> {
use opentelemetry_otlp::{MetricExporter, WithExportConfig, WithHttpConfig};
let mut headers = std::collections::HashMap::new();
headers.insert(
"Api-Key".to_string(),
config
.license_key
.unredact()
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::NewRelicHttp,
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::NewRelicHttp,
reason: e.to_string(),
})
}
pub(crate) fn build_grpc_metric_exporter(
config: &NewRelicGrpcExporterConfig,
) -> Result<opentelemetry_otlp::MetricExporter, InitError> {
use opentelemetry_otlp::{MetricExporter, WithExportConfig, WithTonicConfig};
let mut metadata = tonic::metadata::MetadataMap::new();
metadata.insert(
"api-key",
config
.license_key
.unredact()
.to_str()
.map_err(|e| InitError::Exporter {
exporter: ExporterKind::NewRelicGrpc,
reason: e.to_string(),
})?
.parse()
.map_err(
|e: tonic::metadata::errors::InvalidMetadataValue| InitError::Exporter {
exporter: ExporterKind::NewRelicGrpc,
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::NewRelicGrpc,
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_new_relic_http_config(config: &OpenTelemetryConfig) -> &NewRelicHttpExporterConfig {
match &config.tracer_provider.processors[0] {
SpanProcessor::Batch(batch) => match &batch.exporter {
SpanExporter::NewRelicHttp(nr) => nr,
_ => panic!("Expected NewRelicHttp exporter"),
},
_ => panic!("Expected Batch processor"),
}
}
fn get_new_relic_grpc_config(config: &OpenTelemetryConfig) -> &NewRelicGrpcExporterConfig {
match &config.tracer_provider.processors[0] {
SpanProcessor::Batch(batch) => match &batch.exporter {
SpanExporter::NewRelicGrpc(nr) => nr,
_ => panic!("Expected NewRelicGrpc 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:
new_relic_http:
license_key: test-license-key
endpoint: https://otlp.nr-data.net:4318
"},
&Default::default(),
)
.unwrap();
let nr = get_new_relic_http_config(&config);
let result = build_http_span_exporter(nr);
assert!(result.is_ok());
}
#[test]
fn build_http_span_exporter_eu_endpoint() {
let config: OpenTelemetryConfig = parse_yaml(
indoc::indoc! {"
tracer_provider:
processors:
- batch:
exporter:
new_relic_http:
license_key: test-license-key
endpoint: https://otlp.eu01.nr-data.net:4318
"},
&Default::default(),
)
.unwrap();
let nr = get_new_relic_http_config(&config);
let result = build_http_span_exporter(nr);
assert!(result.is_ok());
}
#[test]
fn build_http_log_exporter_from_config() {
let config: OpenTelemetryConfig = parse_yaml(
indoc::indoc! {"
tracer_provider:
processors:
- batch:
exporter:
new_relic_http:
license_key: test-license-key
endpoint: https://otlp.nr-data.net:4318
"},
&Default::default(),
)
.unwrap();
let nr = get_new_relic_http_config(&config);
let result = build_http_log_exporter(nr);
assert!(result.is_ok());
}
#[test]
fn build_http_metric_exporter_from_config() {
let config: OpenTelemetryConfig = parse_yaml(
indoc::indoc! {"
tracer_provider:
processors:
- batch:
exporter:
new_relic_http:
license_key: test-license-key
endpoint: https://otlp.nr-data.net:4318
"},
&Default::default(),
)
.unwrap();
let nr = get_new_relic_http_config(&config);
let result = build_http_metric_exporter(nr);
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:
new_relic_grpc:
license_key: test-license-key
endpoint: https://otlp.nr-data.net:4317
"},
&Default::default(),
)
.unwrap();
let nr = get_new_relic_grpc_config(&config);
let result = build_grpc_span_exporter(nr);
assert!(result.is_ok());
}
#[tokio::test]
async fn build_grpc_span_exporter_eu_endpoint() {
let config: OpenTelemetryConfig = parse_yaml(
indoc::indoc! {"
tracer_provider:
processors:
- batch:
exporter:
new_relic_grpc:
license_key: test-license-key
endpoint: https://otlp.eu01.nr-data.net:4317
"},
&Default::default(),
)
.unwrap();
let nr = get_new_relic_grpc_config(&config);
let result = build_grpc_span_exporter(nr);
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:
new_relic_grpc:
license_key: test-license-key
endpoint: https://otlp.nr-data.net:4317
"},
&Default::default(),
)
.unwrap();
let nr = get_new_relic_grpc_config(&config);
let result = build_grpc_log_exporter(nr);
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:
new_relic_grpc:
license_key: test-license-key
endpoint: https://otlp.nr-data.net:4317
"},
&Default::default(),
)
.unwrap();
let nr = get_new_relic_grpc_config(&config);
let result = build_grpc_metric_exporter(nr);
assert!(result.is_ok());
}
}