Skip to content

Commit ef3ee94

Browse files
committed
use json names
1 parent 6328d6d commit ef3ee94

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

lib/codecs/src/encoding/format/otlp.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use crate::encoding::ProtobufSerializer;
22
use bytes::BytesMut;
33
use opentelemetry_proto::proto::{
44
DESCRIPTOR_BYTES, LOGS_REQUEST_MESSAGE_TYPE, METRICS_REQUEST_MESSAGE_TYPE,
5-
RESOURCE_LOGS_ROOT_FIELD, RESOURCE_METRICS_ROOT_FIELD, RESOURCE_SPANS_ROOT_FIELD,
5+
RESOURCE_LOGS_JSON_FIELD, RESOURCE_LOGS_ROOT_FIELD, RESOURCE_METRICS_JSON_FIELD,
6+
RESOURCE_METRICS_ROOT_FIELD, RESOURCE_SPANS_JSON_FIELD, RESOURCE_SPANS_ROOT_FIELD,
67
TRACES_REQUEST_MESSAGE_TYPE,
78
};
89
use tokio_util::codec::Encoder;
@@ -98,26 +99,28 @@ impl Encoder<Event> for OtlpSerializer {
9899
fn encode(&mut self, event: Event, buffer: &mut BytesMut) -> Result<(), Self::Error> {
99100
// Determine which descriptor to use based on top-level OTLP fields
100101
// This handles events that were decoded with use_otlp_decoding enabled
102+
// We check both protobuf field names (snake_case) and JSON field names (camelCase)
103+
// because the deserializer may use either depending on the use_json_names option
101104
match &event {
102105
Event::Log(log) => {
103-
if log.contains(RESOURCE_LOGS_ROOT_FIELD) {
106+
if log.contains(RESOURCE_LOGS_ROOT_FIELD) || log.contains(RESOURCE_LOGS_JSON_FIELD) {
104107
self.logs_descriptor.encode(event, buffer)
105-
} else if log.contains(RESOURCE_METRICS_ROOT_FIELD) {
108+
} else if log.contains(RESOURCE_METRICS_ROOT_FIELD) || log.contains(RESOURCE_METRICS_JSON_FIELD) {
106109
// Currently the OTLP metrics are Vector logs (not metrics).
107110
self.metrics_descriptor.encode(event, buffer)
108111
} else {
109112
Err(format!(
110-
"Log event does not contain OTLP top-level fields ({RESOURCE_LOGS_ROOT_FIELD} or {RESOURCE_METRICS_ROOT_FIELD})",
113+
"Log event does not contain OTLP top-level fields ({RESOURCE_LOGS_ROOT_FIELD}/{RESOURCE_LOGS_JSON_FIELD} or {RESOURCE_METRICS_ROOT_FIELD}/{RESOURCE_METRICS_JSON_FIELD})",
111114
)
112115
.into())
113116
}
114117
}
115118
Event::Trace(trace) => {
116-
if trace.contains(RESOURCE_SPANS_ROOT_FIELD) {
119+
if trace.contains(RESOURCE_SPANS_ROOT_FIELD) || trace.contains(RESOURCE_SPANS_JSON_FIELD) {
117120
self.traces_descriptor.encode(event, buffer)
118121
} else {
119122
Err(format!(
120-
"Trace event does not contain OTLP top-level field ({RESOURCE_SPANS_ROOT_FIELD})",
123+
"Trace event does not contain OTLP top-level field ({RESOURCE_SPANS_ROOT_FIELD}/{RESOURCE_SPANS_JSON_FIELD})",
121124
)
122125
.into())
123126
}

lib/opentelemetry-proto/src/proto.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ pub const RESOURCE_LOGS_ROOT_FIELD: &str = "resource_logs";
99
pub const RESOURCE_METRICS_ROOT_FIELD: &str = "resource_metrics";
1010
pub const RESOURCE_SPANS_ROOT_FIELD: &str = "resource_spans";
1111

12+
// JSON names (camelCase) for the same fields, used when use_json_names is enabled
13+
pub const RESOURCE_LOGS_JSON_FIELD: &str = "resourceLogs";
14+
pub const RESOURCE_METRICS_JSON_FIELD: &str = "resourceMetrics";
15+
pub const RESOURCE_SPANS_JSON_FIELD: &str = "resourceSpans";
16+
1217
/// Service stub and clients.
1318
pub mod collector {
1419
pub mod trace {

0 commit comments

Comments
 (0)