#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OtlpTransport {
Grpc,
HttpProtobuf,
HttpJson,
}
impl OtlpTransport {
fn default_for_compiled_features(signal: &str) -> Self {
if cfg!(feature = "grpc-tonic") {
return Self::Grpc;
}
if cfg!(feature = "http-proto") {
return Self::HttpProtobuf;
}
if cfg!(feature = "http-json") {
return Self::HttpJson;
}
panic!("signal-kit was compiled without any OTLP transport feature; enable one of grpc-tonic, http-proto or http-json to export signal '{signal}'");
}
fn protocol_token(self) -> &'static str {
match self {
Self::Grpc => "grpc",
Self::HttpProtobuf => "http/protobuf",
Self::HttpJson => "http/json",
}
}
}
pub fn resolve_otlp_transport(signal: &str) -> OtlpTransport {
let signal_var = format!("OTEL_EXPORTER_OTLP_{}_PROTOCOL", signal.to_uppercase());
let configured = std::env::var(&signal_var)
.or_else(|_| std::env::var("OTEL_EXPORTER_OTLP_PROTOCOL"))
.unwrap_or_default();
let trimmed = configured.trim();
if trimmed.is_empty() {
return OtlpTransport::default_for_compiled_features(signal);
}
match trimmed {
"grpc" => OtlpTransport::Grpc,
"http/protobuf" => OtlpTransport::HttpProtobuf,
"http/json" => OtlpTransport::HttpJson,
other => panic!(
"unsupported OTLP protocol '{other}' for signal '{signal}' (set via {signal_var} or OTEL_EXPORTER_OTLP_PROTOCOL); expected one of: grpc, http/protobuf, http/json"
),
}
}
pub(crate) fn assert_transport_supported(transport: OtlpTransport, signal: &str) {
let missing_feature = match transport {
OtlpTransport::Grpc => {
if cfg!(feature = "grpc-tonic") {
None
} else {
Some("grpc-tonic")
}
}
OtlpTransport::HttpProtobuf => {
if cfg!(feature = "http-proto") {
None
} else {
Some("http-proto")
}
}
OtlpTransport::HttpJson => {
if cfg!(feature = "http-json") {
None
} else {
Some("http-json")
}
}
};
if let Some(feature) = missing_feature {
panic!(
"OTLP protocol '{}' selected for signal '{signal}' but signal-kit was compiled without the '{feature}' cargo feature; enable it (or choose a compiled protocol)",
transport.protocol_token()
);
}
if matches!(transport, OtlpTransport::HttpProtobuf | OtlpTransport::HttpJson)
&& !cfg!(any(
feature = "reqwest-client",
feature = "reqwest-blocking-client",
feature = "reqwest-rustls-webpki-roots",
feature = "hyper-client"
))
{
panic!(
"OTLP protocol '{}' selected for signal '{signal}' but no HTTP client is compiled; enable one of signal-kit's client features: reqwest-client, reqwest-blocking-client, reqwest-rustls-webpki-roots",
transport.protocol_token()
);
}
}
pub(crate) fn build_failure(transport: OtlpTransport, signal: &str, error: String) -> String {
format!(
"failed to build OTLP {} exporter for protocol '{}': {error}",
signal,
transport.protocol_token()
)
}
use opentelemetry_otlp::Protocol;
fn http_protocol(transport: OtlpTransport) -> Protocol {
match transport {
OtlpTransport::HttpJson => Protocol::HttpJson,
OtlpTransport::HttpProtobuf => Protocol::HttpBinary,
OtlpTransport::Grpc => unreachable!("gRPC transport does not use an HTTP protocol"),
}
}
#[cfg(all(
feature = "traces",
any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json")
))]
pub(crate) mod span_exporter {
use super::{OtlpTransport, Protocol, assert_transport_supported, build_failure, http_protocol};
pub(crate) fn build(transport: OtlpTransport) -> opentelemetry_otlp::SpanExporter {
assert_transport_supported(transport, "traces");
let result = build_inner(transport);
result.unwrap_or_else(|error| panic!("{}", build_failure(transport, "trace", error)))
}
fn build_inner(
transport: OtlpTransport,
) -> Result<opentelemetry_otlp::SpanExporter, String> {
match transport {
OtlpTransport::Grpc => grpc(transport),
OtlpTransport::HttpProtobuf | OtlpTransport::HttpJson => http(transport, http_protocol(transport)),
}
}
#[cfg(feature = "grpc-tonic")]
fn grpc(_transport: OtlpTransport) -> Result<opentelemetry_otlp::SpanExporter, String> {
opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.build()
.map_err(|error| error.to_string())
}
#[cfg(not(feature = "grpc-tonic"))]
fn grpc(_transport: OtlpTransport) -> Result<opentelemetry_otlp::SpanExporter, String> {
Err("grpc transport requires the grpc-tonic feature".to_string())
}
#[cfg(any(feature = "http-proto", feature = "http-json"))]
fn http(
_transport: OtlpTransport,
protocol: Protocol,
) -> Result<opentelemetry_otlp::SpanExporter, String> {
use opentelemetry_otlp::WithExportConfig;
opentelemetry_otlp::SpanExporter::builder()
.with_http()
.with_protocol(protocol)
.build()
.map_err(|error| error.to_string())
}
#[cfg(not(any(feature = "http-proto", feature = "http-json")))]
fn http(
_transport: OtlpTransport,
_protocol: Protocol,
) -> Result<opentelemetry_otlp::SpanExporter, String> {
Err("HTTP transport requires the http-proto or http-json feature".to_string())
}
}
#[cfg(all(
feature = "logs-otlp",
any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json")
))]
pub(crate) mod log_exporter {
use super::{OtlpTransport, Protocol, assert_transport_supported, build_failure, http_protocol};
pub(crate) fn build(transport: OtlpTransport) -> opentelemetry_otlp::LogExporter {
assert_transport_supported(transport, "logs");
let result = build_inner(transport);
result.unwrap_or_else(|error| panic!("{}", build_failure(transport, "log", error)))
}
fn build_inner(transport: OtlpTransport) -> Result<opentelemetry_otlp::LogExporter, String> {
match transport {
OtlpTransport::Grpc => grpc(transport),
OtlpTransport::HttpProtobuf | OtlpTransport::HttpJson => http(transport, http_protocol(transport)),
}
}
#[cfg(feature = "grpc-tonic")]
fn grpc(_transport: OtlpTransport) -> Result<opentelemetry_otlp::LogExporter, String> {
opentelemetry_otlp::LogExporter::builder()
.with_tonic()
.build()
.map_err(|error| error.to_string())
}
#[cfg(not(feature = "grpc-tonic"))]
fn grpc(_transport: OtlpTransport) -> Result<opentelemetry_otlp::LogExporter, String> {
Err("grpc transport requires the grpc-tonic feature".to_string())
}
#[cfg(any(feature = "http-proto", feature = "http-json"))]
fn http(
_transport: OtlpTransport,
protocol: Protocol,
) -> Result<opentelemetry_otlp::LogExporter, String> {
use opentelemetry_otlp::WithExportConfig;
opentelemetry_otlp::LogExporter::builder()
.with_http()
.with_protocol(protocol)
.build()
.map_err(|error| error.to_string())
}
#[cfg(not(any(feature = "http-proto", feature = "http-json")))]
fn http(
_transport: OtlpTransport,
_protocol: Protocol,
) -> Result<opentelemetry_otlp::LogExporter, String> {
Err("HTTP transport requires the http-proto or http-json feature".to_string())
}
}
#[cfg(all(
feature = "metrics",
any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json")
))]
pub(crate) mod metric_exporter {
use super::{OtlpTransport, Protocol, assert_transport_supported, build_failure, http_protocol};
pub(crate) fn build(transport: OtlpTransport) -> opentelemetry_otlp::MetricExporter {
assert_transport_supported(transport, "metrics");
let result = build_inner(transport);
result.unwrap_or_else(|error| panic!("{}", build_failure(transport, "metric", error)))
}
fn build_inner(transport: OtlpTransport) -> Result<opentelemetry_otlp::MetricExporter, String> {
match transport {
OtlpTransport::Grpc => grpc(transport),
OtlpTransport::HttpProtobuf | OtlpTransport::HttpJson => http(transport, http_protocol(transport)),
}
}
#[cfg(feature = "grpc-tonic")]
fn grpc(_transport: OtlpTransport) -> Result<opentelemetry_otlp::MetricExporter, String> {
opentelemetry_otlp::MetricExporter::builder()
.with_tonic()
.with_temporality(opentelemetry_sdk::metrics::Temporality::default())
.build()
.map_err(|error| error.to_string())
}
#[cfg(not(feature = "grpc-tonic"))]
fn grpc(_transport: OtlpTransport) -> Result<opentelemetry_otlp::MetricExporter, String> {
Err("grpc transport requires the grpc-tonic feature".to_string())
}
#[cfg(any(feature = "http-proto", feature = "http-json"))]
fn http(
_transport: OtlpTransport,
protocol: Protocol,
) -> Result<opentelemetry_otlp::MetricExporter, String> {
use opentelemetry_otlp::WithExportConfig;
opentelemetry_otlp::MetricExporter::builder()
.with_http()
.with_protocol(protocol)
.with_temporality(opentelemetry_sdk::metrics::Temporality::default())
.build()
.map_err(|error| error.to_string())
}
#[cfg(not(any(feature = "http-proto", feature = "http-json")))]
fn http(
_transport: OtlpTransport,
_protocol: Protocol,
) -> Result<opentelemetry_otlp::MetricExporter, String> {
Err("HTTP transport requires the http-proto or http-json feature".to_string())
}
}