diff --git a/datadog-trace-utils/src/send_data/mod.rs b/datadog-trace-utils/src/send_data/mod.rs index 4d70c4486d..acc4fb2ff7 100644 --- a/datadog-trace-utils/src/send_data/mod.rs +++ b/datadog-trace-utils/src/send_data/mod.rs @@ -205,13 +205,16 @@ impl SendData { self.retry_strategy = retry_strategy; } - /// Overrides the set target Endpoint. + /// Returns a clone of the SendData with the user-defined endpoint. /// /// # Arguments /// /// * `endpoint`: The new endpoint to be used. - pub fn set_target(&mut self, endpoint: Endpoint) { - self.target = endpoint; + pub fn with_endpoint(&self, endpoint: Endpoint) -> SendData { + SendData { + target: endpoint, + ..self.clone() + } } /// Sends the data to the target endpoint. @@ -977,4 +980,54 @@ mod tests { assert_eq!(res.bytes_sent, 0); assert_eq!(res.responses_count_per_code.len(), 0); } + + #[test] + fn test_with_endpoint() { + let header_tags = HEADER_TAGS; + let payload = setup_payload(&header_tags); + let original_endpoint = Endpoint { + api_key: Some(std::borrow::Cow::Borrowed("original-key")), + url: "http://originalexample.com/".parse::().unwrap(), + timeout_ms: 1000, + ..Endpoint::default() + }; + + let original_data = SendData::new( + 100, + TracerPayloadCollection::V07(vec![payload]), + header_tags, + &original_endpoint, + ); + + let new_endpoint = Endpoint { + api_key: Some(std::borrow::Cow::Borrowed("new-key")), + url: "http://newexample.com/".parse::().unwrap(), + timeout_ms: 2000, + ..Endpoint::default() + }; + + let new_data = original_data.with_endpoint(new_endpoint.clone()); + + assert_eq!(new_data.target.api_key, new_endpoint.api_key); + assert_eq!(new_data.target.url, new_endpoint.url); + assert_eq!(new_data.target.timeout_ms, new_endpoint.timeout_ms); + + assert_eq!(new_data.size, original_data.size); + assert_eq!(new_data.headers, original_data.headers); + assert_eq!(new_data.retry_strategy, original_data.retry_strategy); + assert_eq!( + new_data.tracer_payloads.size(), + original_data.tracer_payloads.size() + ); + + assert_eq!(original_data.target.api_key, original_endpoint.api_key); + assert_eq!(original_data.target.url, original_endpoint.url); + assert_eq!( + original_data.target.timeout_ms, + original_endpoint.timeout_ms + ); + + #[cfg(feature = "compression")] + assert!(matches!(new_data.compression, Compression::None)); + } } diff --git a/datadog-trace-utils/src/send_with_retry/retry_strategy.rs b/datadog-trace-utils/src/send_with_retry/retry_strategy.rs index 4111cea94f..bdd3f074ff 100644 --- a/datadog-trace-utils/src/send_with_retry/retry_strategy.rs +++ b/datadog-trace-utils/src/send_with_retry/retry_strategy.rs @@ -9,6 +9,7 @@ use tokio::time::sleep; /// Enum representing the type of backoff to use for the delay between retries. /// ``` #[derive(Debug, Clone)] +#[cfg_attr(test, derive(PartialEq))] pub enum RetryBackoffType { /// Increases the delay by a fixed increment each attempt. Linear, @@ -24,6 +25,7 @@ pub enum RetryBackoffType { /// It includes the maximum number of retries, the delay between retries, the type of backoff to /// use, and an optional jitter to add randomness to the delay. #[derive(Debug, Clone)] +#[cfg_attr(test, derive(PartialEq))] pub struct RetryStrategy { /// The maximum number of retries to attempt. max_retries: u32,