Skip to content

Commit 3ea5aca

Browse files
[SVLS-8150] fix(config): ensure logs intake URL is correctly prefixed (#1021)
## Overview Ensures `DD_LOGS_CONFIG_LOGS_DD_URL` is correctly prefixed with `https://` ## Testing Manually tested that logs get sent to alternate logs intake
1 parent 2049503 commit 3ea5aca

1 file changed

Lines changed: 55 additions & 2 deletions

File tree

mod.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,11 @@ impl ConfigBuilder {
217217
}
218218

219219
// If Logs URL is not set, set it to the default
220-
if self.config.logs_config_logs_dd_url.is_empty() {
220+
if self.config.logs_config_logs_dd_url.trim().is_empty() {
221221
self.config.logs_config_logs_dd_url = build_fqdn_logs(self.config.site.clone());
222+
} else {
223+
self.config.logs_config_logs_dd_url =
224+
logs_intake_url(self.config.logs_config_logs_dd_url.as_str());
222225
}
223226

224227
// If APM URL is not set, set it to the default
@@ -481,6 +484,19 @@ fn build_fqdn_logs(site: String) -> String {
481484
format!("https://http-intake.logs.{site}")
482485
}
483486

487+
#[inline]
488+
#[must_use]
489+
fn logs_intake_url(url: &str) -> String {
490+
let url = url.trim();
491+
if url.is_empty() {
492+
return url.to_string();
493+
}
494+
if url.starts_with("https://") || url.starts_with("http://") {
495+
return url.to_string();
496+
}
497+
format!("https://{url}")
498+
}
499+
484500
pub fn deserialize_optional_string<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
485501
where
486502
D: Deserializer<'de>,
@@ -811,7 +827,44 @@ pub mod tests {
811827
let config = get_config(Path::new(""));
812828
assert_eq!(
813829
config.logs_config_logs_dd_url,
814-
"agent-http-intake-pci.logs.datadoghq.com:443".to_string()
830+
"https://agent-http-intake-pci.logs.datadoghq.com:443".to_string()
831+
);
832+
Ok(())
833+
});
834+
}
835+
836+
#[test]
837+
fn test_logs_intake_url_adds_prefix() {
838+
figment::Jail::expect_with(|jail| {
839+
jail.clear_env();
840+
jail.set_env(
841+
"DD_LOGS_CONFIG_LOGS_DD_URL",
842+
"dr-test-failover-http-intake.logs.datadoghq.com:443",
843+
);
844+
845+
let config = get_config(Path::new(""));
846+
// ensure host:port URL is prefixed with https://
847+
assert_eq!(
848+
config.logs_config_logs_dd_url,
849+
"https://dr-test-failover-http-intake.logs.datadoghq.com:443".to_string()
850+
);
851+
Ok(())
852+
});
853+
}
854+
855+
#[test]
856+
fn test_prefixed_logs_intake_url() {
857+
figment::Jail::expect_with(|jail| {
858+
jail.clear_env();
859+
jail.set_env(
860+
"DD_LOGS_CONFIG_LOGS_DD_URL",
861+
"https://custom-intake.logs.datadoghq.com:443",
862+
);
863+
864+
let config = get_config(Path::new(""));
865+
assert_eq!(
866+
config.logs_config_logs_dd_url,
867+
"https://custom-intake.logs.datadoghq.com:443".to_string()
815868
);
816869
Ok(())
817870
});

0 commit comments

Comments
 (0)