Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion opentelemetry-otlp/src/exporter/http/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::Protocol;
use opentelemetry::{otel_debug, otel_warn};
use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult};
use opentelemetry_sdk::logs::{LogBatch, LogExporter};
#[cfg(feature = "http-proto")]
use prost::Message;
use std::time;

Expand Down Expand Up @@ -51,13 +52,18 @@ fn handle_partial_success(response_body: &[u8], protocol: Protocol) {
return;
}
},
_ => match Message::decode(response_body) {
#[cfg(feature = "http-proto")]
Protocol::HttpBinary => match Message::decode(response_body) {
Ok(r) => r,
Err(e) => {
otel_debug!(name: "HttpLogsClient.ResponseParseError", error = e.to_string());
return;
}
},
#[cfg(feature = "grpc-tonic")]
Protocol::Grpc => {
unreachable!("HTTP client should not receive Grpc protocol")
}
};

if let Some(partial_success) = response.partial_success {
Expand Down
8 changes: 7 additions & 1 deletion opentelemetry-otlp/src/exporter/http/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::Protocol;
use opentelemetry::{otel_debug, otel_warn};
use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult};
use opentelemetry_sdk::metrics::data::ResourceMetrics;
#[cfg(feature = "http-proto")]
use prost::Message;

use super::OtlpHttpClient;
Expand Down Expand Up @@ -47,13 +48,18 @@ fn handle_partial_success(response_body: &[u8], protocol: Protocol) {
return;
}
},
_ => match Message::decode(response_body) {
#[cfg(feature = "http-proto")]
Protocol::HttpBinary => match Message::decode(response_body) {
Ok(r) => r,
Err(e) => {
otel_debug!(name: "HttpMetricsClient.ResponseParseError", error = e.to_string());
return;
}
},
#[cfg(feature = "grpc-tonic")]
Protocol::Grpc => {
unreachable!("HTTP client should not receive Grpc protocol")
}
};

if let Some(partial_success) = response.partial_success {
Expand Down
22 changes: 19 additions & 3 deletions opentelemetry-otlp/src/exporter/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use opentelemetry_proto::transform::trace::tonic::group_spans_by_resource_and_sc
use opentelemetry_sdk::logs::LogBatch;
#[cfg(feature = "trace")]
use opentelemetry_sdk::trace::SpanData;
#[cfg(feature = "http-proto")]
use prost::Message;
use std::collections::HashMap;
use std::env;
Expand Down Expand Up @@ -595,7 +596,12 @@ impl OtlpHttpClient {
Ok(json) => (json.into_bytes(), "application/json"),
Err(e) => return Err(e.to_string()),
},
_ => (req.encode_to_vec(), "application/x-protobuf"),
#[cfg(feature = "http-proto")]
Protocol::HttpBinary => (req.encode_to_vec(), "application/x-protobuf"),
#[cfg(feature = "grpc-tonic")]
Protocol::Grpc => {
unreachable!("HTTP client should not receive Grpc protocol")
}
};

let (processed_body, content_encoding) = self.process_body(body)?;
Expand All @@ -617,7 +623,12 @@ impl OtlpHttpClient {
Ok(json) => (json.into_bytes(), "application/json"),
Err(e) => return Err(e.to_string()),
},
_ => (req.encode_to_vec(), "application/x-protobuf"),
#[cfg(feature = "http-proto")]
Protocol::HttpBinary => (req.encode_to_vec(), "application/x-protobuf"),
#[cfg(feature = "grpc-tonic")]
Protocol::Grpc => {
unreachable!("HTTP client should not receive Grpc protocol")
}
};

let (processed_body, content_encoding) = self.process_body(body)?;
Expand All @@ -642,7 +653,12 @@ impl OtlpHttpClient {
return None;
}
},
_ => (req.encode_to_vec(), "application/x-protobuf"),
#[cfg(feature = "http-proto")]
Protocol::HttpBinary => (req.encode_to_vec(), "application/x-protobuf"),
#[cfg(feature = "grpc-tonic")]
Protocol::Grpc => {
unreachable!("HTTP client should not receive Grpc protocol")
}
};

match self.process_body(body) {
Expand Down
8 changes: 7 additions & 1 deletion opentelemetry-otlp/src/exporter/http/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use opentelemetry_sdk::{
error::{OTelSdkError, OTelSdkResult},
trace::{SpanData, SpanExporter},
};
#[cfg(feature = "http-proto")]
use prost::Message;

impl SpanExporter for OtlpHttpClient {
Expand Down Expand Up @@ -52,13 +53,18 @@ fn handle_partial_success(response_body: &[u8], protocol: Protocol) {
return;
}
},
_ => match Message::decode(response_body) {
#[cfg(feature = "http-proto")]
Protocol::HttpBinary => match Message::decode(response_body) {
Ok(r) => r,
Err(e) => {
otel_debug!(name: "HttpTraceClient.ResponseParseError", error = e.to_string());
return;
}
},
#[cfg(feature = "grpc-tonic")]
Protocol::Grpc => {
unreachable!("HTTP client should not receive Grpc protocol")
}
};

if let Some(partial_success) = response.partial_success {
Expand Down
47 changes: 19 additions & 28 deletions opentelemetry-otlp/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,6 @@ pub const OTEL_EXPORTER_OTLP_PROTOCOL: &str = "OTEL_EXPORTER_OTLP_PROTOCOL";
/// Compression algorithm to use, defaults to none.
pub const OTEL_EXPORTER_OTLP_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_COMPRESSION";

#[cfg(feature = "http-json")]
/// Default protocol, using http-json.
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON;
#[cfg(all(feature = "http-proto", not(feature = "http-json")))]
/// Default protocol, using http-proto.
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF;
#[cfg(all(
feature = "grpc-tonic",
not(any(feature = "http-proto", feature = "http-json"))
))]
/// Default protocol, using grpc
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = OTEL_EXPORTER_OTLP_PROTOCOL_GRPC;

#[cfg(not(any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json")))]
/// Default protocol if no features are enabled.
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = "";

const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF: &str = "http/protobuf";
const OTEL_EXPORTER_OTLP_PROTOCOL_GRPC: &str = "grpc";
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON: &str = "http/json";

/// Max waiting time for the backend to process each signal batch, defaults to 10 seconds.
pub const OTEL_EXPORTER_OTLP_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_TIMEOUT";
/// Default max waiting time for the backend to process each signal batch.
Expand Down Expand Up @@ -84,6 +63,7 @@ pub struct ExportConfig {
pub timeout: Option<Duration>,
}

#[cfg(any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json"))]
impl Default for ExportConfig {
fn default() -> Self {
let protocol = default_protocol();
Expand Down Expand Up @@ -190,14 +170,25 @@ fn resolve_compression_from_env(
}
}

/// default protocol based on enabled features
/// Returns the default protocol based on enabled features.
///
/// Priority order (first available wins):
/// 1. http-json (if enabled)
/// 2. http-proto (if enabled)
/// 3. grpc-tonic (if enabled)
#[cfg(any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json"))]
fn default_protocol() -> Protocol {
match OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT {
OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF => Protocol::HttpBinary,
OTEL_EXPORTER_OTLP_PROTOCOL_GRPC => Protocol::Grpc,
OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON => Protocol::HttpJson,
_ => Protocol::HttpBinary,
}
#[cfg(feature = "http-json")]
return Protocol::HttpJson;

#[cfg(all(feature = "http-proto", not(feature = "http-json")))]
return Protocol::HttpBinary;

#[cfg(all(
feature = "grpc-tonic",
not(any(feature = "http-proto", feature = "http-json"))
))]
return Protocol::Grpc;
}

/// default user-agent headers
Expand Down
6 changes: 4 additions & 2 deletions opentelemetry-otlp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ pub use crate::exporter::tonic::{HasTonicConfig, WithTonicConfig};
pub use crate::exporter::{
HasExportConfig, WithExportConfig, OTEL_EXPORTER_OTLP_COMPRESSION, OTEL_EXPORTER_OTLP_ENDPOINT,
OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT, OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_PROTOCOL,
OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT, OTEL_EXPORTER_OTLP_TIMEOUT,
OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT,
OTEL_EXPORTER_OTLP_TIMEOUT, OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT,
};

#[cfg(feature = "experimental-http-retry")]
Expand Down Expand Up @@ -450,10 +449,13 @@ use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Protocol {
/// GRPC protocol
#[cfg(feature = "grpc-tonic")]
Grpc,
/// HTTP protocol with binary protobuf
#[cfg(feature = "http-proto")]
HttpBinary,
/// HTTP protocol with JSON payload
#[cfg(feature = "http-json")]
HttpJson,
}

Expand Down