Skip to content

Commit dd69455

Browse files
committed
chore(otlp): prevent invalid combinations of exporter and protocol
1 parent 5c8dbc8 commit dd69455

File tree

6 files changed

+60
-36
lines changed

6 files changed

+60
-36
lines changed

opentelemetry-otlp/src/exporter/http/logs.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::Protocol;
33
use opentelemetry::{otel_debug, otel_warn};
44
use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult};
55
use opentelemetry_sdk::logs::{LogBatch, LogExporter};
6+
#[cfg(feature = "http-proto")]
67
use prost::Message;
78
use std::time;
89

@@ -51,13 +52,18 @@ fn handle_partial_success(response_body: &[u8], protocol: Protocol) {
5152
return;
5253
}
5354
},
54-
_ => match Message::decode(response_body) {
55+
#[cfg(feature = "http-proto")]
56+
Protocol::HttpBinary => match Message::decode(response_body) {
5557
Ok(r) => r,
5658
Err(e) => {
5759
otel_debug!(name: "HttpLogsClient.ResponseParseError", error = e.to_string());
5860
return;
5961
}
6062
},
63+
#[cfg(feature = "grpc-tonic")]
64+
Protocol::Grpc => {
65+
unreachable!("HTTP client should not receive Grpc protocol")
66+
}
6167
};
6268

6369
if let Some(partial_success) = response.partial_success {

opentelemetry-otlp/src/exporter/http/metrics.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::Protocol;
33
use opentelemetry::{otel_debug, otel_warn};
44
use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult};
55
use opentelemetry_sdk::metrics::data::ResourceMetrics;
6+
#[cfg(feature = "http-proto")]
67
use prost::Message;
78

89
use super::OtlpHttpClient;
@@ -47,13 +48,18 @@ fn handle_partial_success(response_body: &[u8], protocol: Protocol) {
4748
return;
4849
}
4950
},
50-
_ => match Message::decode(response_body) {
51+
#[cfg(feature = "http-proto")]
52+
Protocol::HttpBinary => match Message::decode(response_body) {
5153
Ok(r) => r,
5254
Err(e) => {
5355
otel_debug!(name: "HttpMetricsClient.ResponseParseError", error = e.to_string());
5456
return;
5557
}
5658
},
59+
#[cfg(feature = "grpc-tonic")]
60+
Protocol::Grpc => {
61+
unreachable!("HTTP client should not receive Grpc protocol")
62+
}
5763
};
5864

5965
if let Some(partial_success) = response.partial_success {

opentelemetry-otlp/src/exporter/http/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use opentelemetry_proto::transform::trace::tonic::group_spans_by_resource_and_sc
1515
use opentelemetry_sdk::logs::LogBatch;
1616
#[cfg(feature = "trace")]
1717
use opentelemetry_sdk::trace::SpanData;
18+
#[cfg(feature = "http-proto")]
1819
use prost::Message;
1920
use std::collections::HashMap;
2021
use std::env;
@@ -595,7 +596,12 @@ impl OtlpHttpClient {
595596
Ok(json) => (json.into_bytes(), "application/json"),
596597
Err(e) => return Err(e.to_string()),
597598
},
598-
_ => (req.encode_to_vec(), "application/x-protobuf"),
599+
#[cfg(feature = "http-proto")]
600+
Protocol::HttpBinary => (req.encode_to_vec(), "application/x-protobuf"),
601+
#[cfg(feature = "grpc-tonic")]
602+
Protocol::Grpc => {
603+
unreachable!("HTTP client should not receive Grpc protocol")
604+
}
599605
};
600606

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

623634
let (processed_body, content_encoding) = self.process_body(body)?;
@@ -642,7 +653,12 @@ impl OtlpHttpClient {
642653
return None;
643654
}
644655
},
645-
_ => (req.encode_to_vec(), "application/x-protobuf"),
656+
#[cfg(feature = "http-proto")]
657+
Protocol::HttpBinary => (req.encode_to_vec(), "application/x-protobuf"),
658+
#[cfg(feature = "grpc-tonic")]
659+
Protocol::Grpc => {
660+
unreachable!("HTTP client should not receive Grpc protocol")
661+
}
646662
};
647663

648664
match self.process_body(body) {

opentelemetry-otlp/src/exporter/http/trace.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use opentelemetry_sdk::{
55
error::{OTelSdkError, OTelSdkResult},
66
trace::{SpanData, SpanExporter},
77
};
8+
#[cfg(feature = "http-proto")]
89
use prost::Message;
910

1011
impl SpanExporter for OtlpHttpClient {
@@ -52,13 +53,18 @@ fn handle_partial_success(response_body: &[u8], protocol: Protocol) {
5253
return;
5354
}
5455
},
55-
_ => match Message::decode(response_body) {
56+
#[cfg(feature = "http-proto")]
57+
Protocol::HttpBinary => match Message::decode(response_body) {
5658
Ok(r) => r,
5759
Err(e) => {
5860
otel_debug!(name: "HttpTraceClient.ResponseParseError", error = e.to_string());
5961
return;
6062
}
6163
},
64+
#[cfg(feature = "grpc-tonic")]
65+
Protocol::Grpc => {
66+
unreachable!("HTTP client should not receive Grpc protocol")
67+
}
6268
};
6369

6470
if let Some(partial_success) = response.partial_success {

opentelemetry-otlp/src/exporter/mod.rs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,6 @@ pub const OTEL_EXPORTER_OTLP_PROTOCOL: &str = "OTEL_EXPORTER_OTLP_PROTOCOL";
2929
/// Compression algorithm to use, defaults to none.
3030
pub const OTEL_EXPORTER_OTLP_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_COMPRESSION";
3131

32-
#[cfg(feature = "http-json")]
33-
/// Default protocol, using http-json.
34-
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON;
35-
#[cfg(all(feature = "http-proto", not(feature = "http-json")))]
36-
/// Default protocol, using http-proto.
37-
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF;
38-
#[cfg(all(
39-
feature = "grpc-tonic",
40-
not(any(feature = "http-proto", feature = "http-json"))
41-
))]
42-
/// Default protocol, using grpc
43-
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = OTEL_EXPORTER_OTLP_PROTOCOL_GRPC;
44-
45-
#[cfg(not(any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json")))]
46-
/// Default protocol if no features are enabled.
47-
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = "";
48-
49-
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF: &str = "http/protobuf";
50-
const OTEL_EXPORTER_OTLP_PROTOCOL_GRPC: &str = "grpc";
51-
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON: &str = "http/json";
52-
5332
/// Max waiting time for the backend to process each signal batch, defaults to 10 seconds.
5433
pub const OTEL_EXPORTER_OTLP_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_TIMEOUT";
5534
/// Default max waiting time for the backend to process each signal batch.
@@ -84,6 +63,7 @@ pub struct ExportConfig {
8463
pub timeout: Option<Duration>,
8564
}
8665

66+
#[cfg(any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json"))]
8767
impl Default for ExportConfig {
8868
fn default() -> Self {
8969
let protocol = default_protocol();
@@ -190,14 +170,22 @@ fn resolve_compression_from_env(
190170
}
191171
}
192172

193-
/// default protocol based on enabled features
173+
/// Returns the default protocol based on enabled features.
174+
///
175+
/// Priority order (first available wins):
176+
/// 1. http-json (if enabled)
177+
/// 2. http-proto (if enabled)
178+
/// 3. grpc-tonic (if enabled)
179+
#[cfg(any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json"))]
194180
fn default_protocol() -> Protocol {
195-
match OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT {
196-
OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF => Protocol::HttpBinary,
197-
OTEL_EXPORTER_OTLP_PROTOCOL_GRPC => Protocol::Grpc,
198-
OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON => Protocol::HttpJson,
199-
_ => Protocol::HttpBinary,
200-
}
181+
#[cfg(feature = "http-json")]
182+
return Protocol::HttpJson;
183+
184+
#[cfg(all(feature = "http-proto", not(feature = "http-json")))]
185+
return Protocol::HttpBinary;
186+
187+
#[cfg(all(feature = "grpc-tonic", not(any(feature = "http-proto", feature = "http-json"))))]
188+
return Protocol::Grpc;
201189
}
202190

203191
/// default user-agent headers

opentelemetry-otlp/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,7 @@ pub use crate::exporter::tonic::{HasTonicConfig, WithTonicConfig};
409409
pub use crate::exporter::{
410410
HasExportConfig, WithExportConfig, OTEL_EXPORTER_OTLP_COMPRESSION, OTEL_EXPORTER_OTLP_ENDPOINT,
411411
OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT, OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_PROTOCOL,
412-
OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT, OTEL_EXPORTER_OTLP_TIMEOUT,
413-
OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT,
412+
OTEL_EXPORTER_OTLP_TIMEOUT, OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT,
414413
};
415414

416415
#[cfg(feature = "experimental-http-retry")]
@@ -450,10 +449,13 @@ use serde::{Deserialize, Serialize};
450449
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
451450
pub enum Protocol {
452451
/// GRPC protocol
452+
#[cfg(feature = "grpc-tonic")]
453453
Grpc,
454454
/// HTTP protocol with binary protobuf
455+
#[cfg(feature = "http-proto")]
455456
HttpBinary,
456457
/// HTTP protocol with JSON payload
458+
#[cfg(feature = "http-json")]
457459
HttpJson,
458460
}
459461

0 commit comments

Comments
 (0)