Skip to content

Commit

Permalink
feat: Prefer DD_PROXY_HTTPS over HTTPS_PROXY (#673)
Browse files Browse the repository at this point in the history
* feat: Prefer DD_PROXY_HTTPS over HTTPS_PROXY

* fix: no proxy on ints

* fix: clippy thx
  • Loading branch information
astuyve authored Oct 16, 2024
1 parent 9b01c90 commit 92272e9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
21 changes: 18 additions & 3 deletions dogstatsd/src/datadog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use protobuf::Message;
use reqwest;
use serde::{Serialize, Serializer};
use serde_json;
use tracing::debug;
use tracing::{debug, error};

/// Interface for the `DogStatsD` metrics intake API.
#[derive(Debug)]
Expand All @@ -20,11 +20,18 @@ pub struct DdApi {

impl DdApi {
#[must_use]
pub fn new(api_key: String, site: String) -> Self {
pub fn new(api_key: String, site: String, https_proxy: Option<String>) -> Self {
let client = match Self::build_client(https_proxy) {
Ok(client) => client,
Err(e) => {
error!("Unable to parse proxy URL, no proxy will be used. {:?}", e);
reqwest::Client::new()
}
};
DdApi {
api_key,
fqdn_site: site,
client: reqwest::Client::new(),
client,
}
}

Expand Down Expand Up @@ -96,6 +103,14 @@ impl DdApi {
}
};
}

fn build_client(https_proxy: Option<String>) -> Result<reqwest::Client, reqwest::Error> {
let mut builder = reqwest::Client::builder();
if let Some(proxy) = https_proxy {
builder = builder.proxy(reqwest::Proxy::https(proxy)?);
}
builder.build()
}
}

#[derive(Debug, Serialize, Clone, Copy)]
Expand Down
9 changes: 7 additions & 2 deletions dogstatsd/src/flusher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ pub fn build_fqdn_metrics(site: String) -> String {

#[allow(clippy::await_holding_lock)]
impl Flusher {
pub fn new(api_key: String, aggregator: Arc<Mutex<Aggregator>>, site: String) -> Self {
let dd_api = datadog::DdApi::new(api_key, site);
pub fn new(
api_key: String,
aggregator: Arc<Mutex<Aggregator>>,
site: String,
https_proxy: Option<String>,
) -> Self {
let dd_api = datadog::DdApi::new(api_key, site, https_proxy);
Flusher { dd_api, aggregator }
}

Expand Down
1 change: 1 addition & 0 deletions dogstatsd/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async fn dogstatsd_server_ships_series() {
"mock-api-key".to_string(),
Arc::clone(&metrics_aggr),
mock_server.url(),
None,
);

let server_address = "127.0.0.1:18125";
Expand Down
8 changes: 7 additions & 1 deletion serverless/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub async fn main() {
.map(|val| val.to_lowercase() != "false")
.unwrap_or(true);

let https_proxy = env::var("DD_PROXY_HTTPS")
.or_else(|_| env::var("HTTPS_PROXY"))
.ok();
debug!("Starting serverless trace mini agent");

let mini_agent_version = env!("CARGO_PKG_VERSION").to_string();
Expand Down Expand Up @@ -79,7 +82,8 @@ pub async fn main() {

let mut metrics_flusher = if dd_use_dogstatsd {
debug!("Starting dogstatsd");
let (_, metrics_flusher) = start_dogstatsd(dd_dogstatsd_port, dd_api_key, dd_site).await;
let (_, metrics_flusher) =
start_dogstatsd(dd_dogstatsd_port, dd_api_key, dd_site, https_proxy).await;
info!("dogstatsd-udp: starting to listen on port {dd_dogstatsd_port}");
metrics_flusher
} else {
Expand All @@ -104,6 +108,7 @@ async fn start_dogstatsd(
port: u16,
dd_api_key: Option<String>,
dd_site: String,
https_proxy: Option<String>,
) -> (CancellationToken, Option<Flusher>) {
let metrics_aggr = Arc::new(Mutex::new(
MetricsAggregator::new(EMPTY_TAGS, CONTEXTS).expect("Failed to create metrics aggregator"),
Expand Down Expand Up @@ -131,6 +136,7 @@ async fn start_dogstatsd(
dd_api_key,
Arc::clone(&metrics_aggr),
build_fqdn_metrics(dd_site),
https_proxy,
);
Some(metrics_flusher)
}
Expand Down
4 changes: 3 additions & 1 deletion trace-mini-agent/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ impl Config {
..Default::default()
},
obfuscation_config,
proxy_url: env::var("HTTPS_PROXY").ok(),
proxy_url: env::var("DD_PROXY_HTTPS")
.or_else(|_| env::var("HTTPS_PROXY"))
.ok(),
})
}
}
Expand Down

0 comments on commit 92272e9

Please sign in to comment.