Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 1 addition & 5 deletions libdd-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ pub mod header {
#![allow(clippy::declare_interior_mutable_const)]
use hyper::{header::HeaderName, http::HeaderValue};

// These strings are defined separately to be used in context where &str are used to represent
// headers (e.g. SendData) while keeping a single source of truth.
pub const DATADOG_SEND_REAL_HTTP_STATUS_STR: &str = "datadog-send-real-http-status";
pub const DATADOG_TRACE_COUNT_STR: &str = "x-datadog-trace-count";
pub const APPLICATION_MSGPACK_STR: &str = "application/msgpack";
pub const APPLICATION_PROTOBUF_STR: &str = "application/x-protobuf";

Expand All @@ -101,7 +97,7 @@ pub mod header {
/// If this is not set then the agent will always return a 200 regardless if the payload is
/// dropped.
pub const DATADOG_SEND_REAL_HTTP_STATUS: HeaderName =
HeaderName::from_static(DATADOG_SEND_REAL_HTTP_STATUS_STR);
HeaderName::from_static("datadog-send-real-http-status");
pub const DATADOG_API_KEY: HeaderName = HeaderName::from_static("dd-api-key");
pub const APPLICATION_JSON: HeaderValue = HeaderValue::from_static("application/json");
pub const APPLICATION_MSGPACK: HeaderValue = HeaderValue::from_static(APPLICATION_MSGPACK_STR);
Expand Down
7 changes: 3 additions & 4 deletions libdd-data-pipeline/src/stats_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use std::{
borrow::Borrow,
collections::HashMap,
sync::{
atomic::{AtomicU64, Ordering},
Arc, Mutex,
Expand Down Expand Up @@ -84,11 +83,11 @@ impl StatsExporter {
}
let body = rmp_serde::encode::to_vec_named(&payload)?;

let mut headers: HashMap<&'static str, String> = self.meta.borrow().into();
let mut headers: http::HeaderMap = self.meta.borrow().into();

headers.insert(
http::header::CONTENT_TYPE.as_str(),
libdd_common::header::APPLICATION_MSGPACK_STR.to_string(),
http::header::CONTENT_TYPE,
libdd_common::header::APPLICATION_MSGPACK,
);

let result = send_with_retry(
Expand Down
4 changes: 3 additions & 1 deletion libdd-data-pipeline/src/trace_exporter/agent_response.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

use http::HeaderName;
use std::sync::Arc;

use arc_swap::ArcSwap;

pub const DATADOG_RATES_PAYLOAD_VERSION_HEADER: &str = "datadog-rates-payload-version";
pub const DATADOG_RATES_PAYLOAD_VERSION: HeaderName =
HeaderName::from_static("datadog-rates-payload-version");

/// `AgentResponse` structure holds agent response information upon successful request.
#[derive(Debug, PartialEq)]
Expand Down
15 changes: 7 additions & 8 deletions libdd-data-pipeline/src/trace_exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::pausable_worker::PausableWorker;
use crate::stats_exporter::StatsExporter;
use crate::telemetry::{SendPayloadTelemetry, TelemetryClient};
use crate::trace_exporter::agent_response::{
AgentResponsePayloadVersion, DATADOG_RATES_PAYLOAD_VERSION_HEADER,
AgentResponsePayloadVersion, DATADOG_RATES_PAYLOAD_VERSION,
};
use crate::trace_exporter::error::{InternalErrorKind, RequestError, TraceExporterError};
use crate::{
Expand All @@ -45,7 +45,7 @@ use libdd_trace_utils::trace_utils::TracerHeaderTags;
use std::io;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::{borrow::Borrow, collections::HashMap, str::FromStr};
use std::{borrow::Borrow, str::FromStr};
use tokio::runtime::Runtime;
use tracing::{debug, error, warn};

Expand Down Expand Up @@ -148,8 +148,8 @@ impl<'a> From<&'a TracerMetadata> for TracerHeaderTags<'a> {
}
}

impl<'a> From<&'a TracerMetadata> for HashMap<&'static str, String> {
fn from(tags: &'a TracerMetadata) -> HashMap<&'static str, String> {
impl<'a> From<&'a TracerMetadata> for http::HeaderMap {
fn from(tags: &'a TracerMetadata) -> http::HeaderMap {
TracerHeaderTags::from(tags).into()
}
}
Expand Down Expand Up @@ -563,7 +563,7 @@ impl TraceExporter {
&self,
endpoint: &Endpoint,
mp_payload: Vec<u8>,
headers: HashMap<&'static str, String>,
headers: http::HeaderMap,
chunks: usize,
chunks_dropped_p0: usize,
) -> Result<AgentResponse, TraceExporterError> {
Expand Down Expand Up @@ -738,7 +738,7 @@ impl TraceExporter {
match (
status.is_success(),
self.agent_payload_response_version.as_ref(),
response.headers().get(DATADOG_RATES_PAYLOAD_VERSION_HEADER),
response.headers().get(DATADOG_RATES_PAYLOAD_VERSION),
) {
(false, _, _) => {
// If the status is not success, the rates are considered unchanged
Expand Down Expand Up @@ -876,7 +876,6 @@ mod tests {
use libdd_trace_utils::msgpack_encoder;
use libdd_trace_utils::span::v04::SpanBytes;
use libdd_trace_utils::span::v05;
use std::collections::HashMap;
use std::net;
use std::time::Duration;
use tokio::time::sleep;
Expand Down Expand Up @@ -920,7 +919,7 @@ mod tests {
..Default::default()
};

let hashmap: HashMap<&'static str, String> = (&tracer_tags).into();
let hashmap: http::HeaderMap = (&tracer_tags).into();

assert_eq!(hashmap.get("datadog-meta-tracer-version").unwrap(), "v0.1");
assert_eq!(hashmap.get("datadog-meta-lang").unwrap(), "rust");
Expand Down
56 changes: 22 additions & 34 deletions libdd-data-pipeline/src/trace_exporter/trace_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@
// SPDX-License-Identifier: Apache-2.0

use crate::trace_exporter::agent_response::{
AgentResponsePayloadVersion, DATADOG_RATES_PAYLOAD_VERSION_HEADER,
AgentResponsePayloadVersion, DATADOG_RATES_PAYLOAD_VERSION,
};
use crate::trace_exporter::error::TraceExporterError;
use crate::trace_exporter::TraceExporterOutputFormat;
use http::header::CONTENT_TYPE;
use http::{header::CONTENT_TYPE, HeaderMap, HeaderValue};
use libdd_common::header::{
APPLICATION_MSGPACK_STR, DATADOG_SEND_REAL_HTTP_STATUS_STR, DATADOG_TRACE_COUNT_STR,
APPLICATION_MSGPACK, DATADOG_SEND_REAL_HTTP_STATUS, DATADOG_TRACE_COUNT,
};
use libdd_trace_utils::msgpack_decoder::decode::error::DecodeError;
use libdd_trace_utils::msgpack_encoder;
use libdd_trace_utils::span::{v04::Span, TraceData};
use libdd_trace_utils::trace_utils::{self, TracerHeaderTags};
use libdd_trace_utils::tracer_payload;
use std::collections::HashMap;

/// Prepared traces payload ready for sending to the agent
pub(super) struct PreparedTracesPayload {
/// Serialized msgpack payload
pub data: Vec<u8>,
/// HTTP headers for the request
pub headers: HashMap<&'static str, String>,
pub headers: HeaderMap,
/// Number of trace chunks
pub chunk_count: usize,
}
Expand Down Expand Up @@ -78,20 +77,16 @@ impl<'a> TraceSerializer<'a> {
}

/// Build HTTP headers for traces request
fn build_traces_headers(
&self,
header_tags: TracerHeaderTags,
chunk_count: usize,
) -> HashMap<&'static str, String> {
let mut headers: HashMap<&'static str, String> = header_tags.into();
headers.insert(DATADOG_SEND_REAL_HTTP_STATUS_STR, "1".to_string());
headers.insert(DATADOG_TRACE_COUNT_STR, chunk_count.to_string());
headers.insert(CONTENT_TYPE.as_str(), APPLICATION_MSGPACK_STR.to_string());
fn build_traces_headers(&self, header_tags: TracerHeaderTags, chunk_count: usize) -> HeaderMap {
let mut headers: HeaderMap = header_tags.into();
headers.reserve(4);
headers.insert(DATADOG_SEND_REAL_HTTP_STATUS, HeaderValue::from_static("1"));
headers.insert(DATADOG_TRACE_COUNT, chunk_count.into());
headers.insert(CONTENT_TYPE, APPLICATION_MSGPACK);
if let Some(agent_payload_response_version) = &self.agent_payload_response_version {
headers.insert(
DATADOG_RATES_PAYLOAD_VERSION_HEADER,
agent_payload_response_version.header_value(),
);
// should never fail, as the version should only contain visible ascii chars
let _ = HeaderValue::try_from(agent_payload_response_version.header_value())
.map(|v| headers.insert(DATADOG_RATES_PAYLOAD_VERSION, v));
}
headers
}
Expand All @@ -115,9 +110,7 @@ mod tests {
use super::*;
use crate::trace_exporter::agent_response::AgentResponsePayloadVersion;
use http::header::CONTENT_TYPE;
use libdd_common::header::{
APPLICATION_MSGPACK_STR, DATADOG_SEND_REAL_HTTP_STATUS_STR, DATADOG_TRACE_COUNT_STR,
};
use libdd_common::header::APPLICATION_MSGPACK_STR;
use libdd_tinybytes::BytesString;
use libdd_trace_utils::span::v04::SpanBytes;
use libdd_trace_utils::trace_utils::TracerHeaderTags;
Expand Down Expand Up @@ -179,12 +172,9 @@ mod tests {
let headers = serializer.build_traces_headers(header_tags, 3);

// Check basic headers are present
assert_eq!(headers.get(DATADOG_SEND_REAL_HTTP_STATUS_STR).unwrap(), "1");
assert_eq!(headers.get(DATADOG_TRACE_COUNT_STR).unwrap(), "3");
assert_eq!(
headers.get(CONTENT_TYPE.as_str()).unwrap(),
APPLICATION_MSGPACK_STR
);
assert_eq!(headers.get(DATADOG_SEND_REAL_HTTP_STATUS).unwrap(), "1");
assert_eq!(headers.get(DATADOG_TRACE_COUNT).unwrap(), "3");
assert_eq!(headers.get(CONTENT_TYPE).unwrap(), APPLICATION_MSGPACK_STR);

// Check tracer metadata headers are present
assert_eq!(headers.get("datadog-meta-lang").unwrap(), "rust");
Expand Down Expand Up @@ -212,8 +202,8 @@ mod tests {
let headers = serializer.build_traces_headers(header_tags, 2);

// Check that agent payload version header is included
assert!(headers.contains_key(DATADOG_RATES_PAYLOAD_VERSION_HEADER));
assert_eq!(headers.get(DATADOG_TRACE_COUNT_STR).unwrap(), "2");
assert!(headers.contains_key(DATADOG_RATES_PAYLOAD_VERSION));
assert_eq!(headers.get(DATADOG_TRACE_COUNT).unwrap(), "2");
}

#[test]
Expand Down Expand Up @@ -346,7 +336,7 @@ mod tests {
assert!(!prepared.headers.is_empty());

// Check headers
assert_eq!(prepared.headers.get(DATADOG_TRACE_COUNT_STR).unwrap(), "2");
assert_eq!(prepared.headers.get(DATADOG_TRACE_COUNT).unwrap(), "2");
assert_eq!(prepared.headers.get("datadog-meta-lang").unwrap(), "rust");
}

Expand Down Expand Up @@ -377,9 +367,7 @@ mod tests {

let prepared = result.unwrap();
assert_eq!(prepared.chunk_count, 1);
assert!(prepared
.headers
.contains_key(DATADOG_RATES_PAYLOAD_VERSION_HEADER));
assert!(prepared.headers.contains_key(DATADOG_RATES_PAYLOAD_VERSION));
}

#[test]
Expand All @@ -394,7 +382,7 @@ mod tests {
let prepared = result.unwrap();
assert_eq!(prepared.chunk_count, 0);
assert!(!prepared.data.is_empty()); // Even empty traces result in some serialized data
assert_eq!(prepared.headers.get(DATADOG_TRACE_COUNT_STR).unwrap(), "0");
assert_eq!(prepared.headers.get(DATADOG_TRACE_COUNT).unwrap(), "0");
}

#[test]
Expand Down
Loading
Loading