Skip to main content

libdd_trace_utils/otlp_encoder/
mod.rs

1// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/
2// SPDX-License-Identifier: Apache-2.0
3
4//! OTLP encoder: maps Datadog spans to the prost OTLP types (the IR), then to the HTTP/protobuf
5//! or HTTP/JSON wire format.
6
7pub(crate) mod json_serializer;
8pub mod mapper;
9
10pub use mapper::map_traces_to_otlp;
11
12pub use libdd_trace_protobuf::opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest as ProtoExportTraceServiceRequest;
13use prost::Message;
14
15/// Serialize the prost OTLP request to the HTTP/protobuf wire format.
16pub fn encode_otlp_protobuf(req: &ProtoExportTraceServiceRequest) -> Vec<u8> {
17    req.encode_to_vec()
18}
19
20/// Serialize the prost OTLP request to the HTTP/JSON wire format (OTLP/JSON spec).
21pub fn encode_otlp_json(req: &ProtoExportTraceServiceRequest) -> serde_json::Result<Vec<u8>> {
22    json_serializer::to_otlp_json_vec(req)
23}
24
25/// Tracer-level attributes used to populate the OTLP Resource on export.
26///
27/// These are the fields from the tracer's configuration that map to OTLP Resource attributes
28/// (service.name, deployment.environment.name, service.version, telemetry.sdk.*, runtime-id).
29/// Callers should build this from their own tracer metadata struct.
30#[derive(Clone, Debug, Default)]
31#[non_exhaustive]
32pub struct OtlpResourceInfo {
33    pub service: String,
34    pub env: String,
35    pub app_version: String,
36    pub language: String,
37    pub tracer_version: String,
38    pub runtime_id: String,
39    pub hostname: String,
40    pub process_tags: String,
41    /// When true, emits `_dd.stats_computed: "true"` on the OTLP resource to prevent
42    /// double-counted APM metrics in Datadog Agent OTLP receivers (backwards compatible).
43    pub client_computed_stats: bool,
44}
45
46#[cfg(test)]
47mod encode_tests {
48    use super::*;
49    use crate::span::v04::Span;
50    use crate::span::BytesData;
51    use libdd_trace_protobuf::opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest as ProtoReq;
52    use libdd_trace_protobuf::opentelemetry::proto::common::v1::any_value::Value as ProtoValue;
53    use prost::Message;
54
55    fn sample_native() -> (Vec<Vec<Span<BytesData>>>, OtlpResourceInfo) {
56        let resource_info = OtlpResourceInfo {
57            service: "svc".to_string(),
58            ..Default::default()
59        };
60        let mut span: Span<BytesData> = Span {
61            trace_id: 0x5b8efff798038103_d269b633813fc60c_u128,
62            span_id: 0xEEE19B7EC3C1B174,
63            name: libdd_tinybytes::BytesString::from_static("op"),
64            resource: libdd_tinybytes::BytesString::from_static("res"),
65            start: 1,
66            duration: 2,
67            error: 1,
68            ..Default::default()
69        };
70        span.meta.insert(
71            "error.msg".into(),
72            libdd_tinybytes::BytesString::from_static("boom"),
73        );
74        span.meta.insert(
75            "http.method".into(),
76            libdd_tinybytes::BytesString::from_static("GET"),
77        );
78        (vec![vec![span]], resource_info)
79    }
80
81    #[test]
82    fn json_and_protobuf_carry_same_span() {
83        // Decisive guard: JSON and protobuf are encoded from the *same* prost IR, so the two
84        // wire formats cannot drift.
85        let (chunks, info) = sample_native();
86        let req = map_traces_to_otlp(chunks, &info, false);
87        let json = encode_otlp_json(&req).unwrap();
88        let pb = encode_otlp_protobuf(&req);
89
90        let jv: serde_json::Value = serde_json::from_slice(&json).unwrap();
91        let jspan = &jv["resourceSpans"][0]["scopeSpans"][0]["spans"][0];
92        let proto = ProtoReq::decode(pb.as_slice()).unwrap();
93        let pspan = &proto.resource_spans[0].scope_spans[0].spans[0];
94
95        assert_eq!(jspan["name"].as_str().unwrap(), pspan.name);
96        assert_eq!(
97            jspan["spanId"].as_str().unwrap(),
98            hex::encode(&pspan.span_id)
99        );
100        assert_eq!(
101            jspan["traceId"].as_str().unwrap(),
102            hex::encode(&pspan.trace_id)
103        );
104        let pst = pspan.status.as_ref().unwrap();
105        assert_eq!(jspan["status"]["code"].as_i64().unwrap() as i32, pst.code);
106        assert_eq!(jspan["status"]["message"].as_str().unwrap(), pst.message);
107        let jattr = jspan["attributes"]
108            .as_array()
109            .unwrap()
110            .iter()
111            .find(|a| a["key"] == "http.method")
112            .unwrap();
113        let pattr = pspan
114            .attributes
115            .iter()
116            .find(|a| a.key == "http.method")
117            .unwrap();
118        let pval = match pattr.value.as_ref().unwrap().value.as_ref().unwrap() {
119            ProtoValue::StringValue(v) => v.as_str(),
120            other => panic!("expected string, got {other:?}"),
121        };
122        assert_eq!(jattr["value"]["stringValue"].as_str().unwrap(), pval);
123        assert_eq!(jattr["value"]["stringValue"].as_str().unwrap(), "GET");
124    }
125
126    #[test]
127    fn protobuf_round_trips_through_prost() {
128        // Round-trip the IR through the protobuf wire format: decoding the encoded bytes
129        // reproduces the original prost request, i.e. the encoding is lossless. (A JSON round-trip
130        // would need a deserializer mirroring `json_serializer`, which this crate doesn't ship;
131        // `json_and_protobuf_carry_same_span` guards that the JSON matches this same IR.)
132        let (chunks, info) = sample_native();
133        let req = map_traces_to_otlp(chunks, &info, false);
134        let decoded = ProtoReq::decode(encode_otlp_protobuf(&req).as_slice()).unwrap();
135        assert_eq!(decoded, req);
136    }
137}