Skip to content

Commit a123001

Browse files
author
Thomas Luijken
committed
Use client within the probe thread
1 parent 8feb140 commit a123001

6 files changed

Lines changed: 22 additions & 33 deletions

File tree

oxybox/src/config/app_config.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::{net::IpAddr, time::Duration};
21
use std::env;
2+
use std::{net::IpAddr, time::Duration};
33

44
use tokio_native_tls::TlsConnector as TokioTlsConnector;
55
use trust_dns_resolver::{
@@ -21,11 +21,9 @@ pub struct AppConfig {
2121
/// parses it into a `Config` struct, and overrides certain values with environment variables.
2222
/// It also sets up the DNS hosts and Mimir endpoint.
2323
pub fn load_config() -> AppConfig {
24-
25-
let config_file_location =
26-
env::var("CONFIG_FILE").unwrap_or_else(|_| "config.yml".to_string());
27-
let config_str = std::fs::read_to_string(&config_file_location)
28-
.expect("Failed to read config.yaml");
24+
let config_file_location = env::var("CONFIG_FILE").unwrap_or_else(|_| "config.yml".to_string());
25+
let config_str =
26+
std::fs::read_to_string(&config_file_location).expect("Failed to read config.yaml");
2927

3028
let config: Config = serde_yaml::from_str(&config_str).expect("Invalid YAML");
3129

@@ -65,7 +63,9 @@ pub fn setup_tls_connector() -> Result<TokioTlsConnector, native_tls::Error> {
6563
/// * `dns_hosts` - A slice of strings representing DNS host IPs (e.g., "
6664
/// # Returns
6765
/// A `Result` containing a `TokioAsyncResolver` if successful, or an error if the setup fails.
68-
pub fn setup_resolver(dns_hosts: &[String]) -> Result<TokioAsyncResolver, Box<dyn std::error::Error>> {
66+
pub fn setup_resolver(
67+
dns_hosts: &[String],
68+
) -> Result<TokioAsyncResolver, Box<dyn std::error::Error>> {
6969
let mut opts = ResolverOpts::default();
7070
opts.attempts = 2;
7171
opts.timeout = Duration::from_millis(100);

oxybox/src/config/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
pub mod probe_config;
21
pub mod app_config;
2+
pub mod probe_config;

oxybox/src/config/probe_config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use serde::Deserialize;
44
/// Contains the organisation ID, the polling interval in seconds, and a list of target configurations.
55
#[derive(Debug, Clone, Deserialize)]
66
pub struct OrganisationConfig {
7-
87
/// The organisation ID for which this configuration applies.
98
/// This translates to the 'Org-Id' header in the Mimir requests.
109
pub organisation_id: String,
@@ -20,7 +19,6 @@ pub struct OrganisationConfig {
2019
/// Contains the target URL and a list of accepted HTTP status codes.
2120
#[derive(Debug, Clone, Deserialize)]
2221
pub struct TargetConfig {
23-
2422
/// The URL of the target service to be monitored.
2523
pub url: String,
2624

@@ -74,7 +72,9 @@ pub mod test {
7472
assert_eq!(demo_config.targets[0].url, "https://www.google.com");
7573
assert_eq!(demo_config.targets[1].url, "https://www.github.com");
7674
assert_eq!(demo_config.targets[1].accepted_status_codes, vec![200, 301]);
77-
let org_x_config = config.get("organisationX").expect("OrganisationX config not found");
75+
let org_x_config = config
76+
.get("organisationX")
77+
.expect("OrganisationX config not found");
7878
assert_eq!(org_x_config.organisation_id, "1");
7979
assert_eq!(org_x_config.polling_interval_seconds, 20);
8080
assert_eq!(org_x_config.targets.len(), 1);

oxybox/src/http_probe/probe.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use trust_dns_resolver::{AsyncResolver, TokioAsyncResolver};
1818

1919
use super::result::ProbeResult;
2020

21-
2221
/// Struct to hold the results of an HTTP probe.
2322
/// This struct contains various metrics related to the HTTP request, such as DNS resolution time, connection time, TLS handshake time, HTTP status code, and more.
2423
/// # Fields
@@ -46,7 +45,6 @@ async fn get_connect_timings(
4645
>,
4746
with_tls: bool,
4847
) -> Result<HttpProbeResult, String> {
49-
5048
// step one: DNS resolution
5149
let dns_start = Instant::now();
5250
if host.is_empty() {
@@ -87,7 +85,7 @@ async fn get_connect_timings(
8785
tls_time: None,
8886
});
8987
}
90-
88+
9189
// step four: TLS handshake
9290
let tls_start = Instant::now();
9391
let tls_stream = connector.connect(host, stream).await;
@@ -166,7 +164,6 @@ async fn probe_url(
166164
>,
167165
url: &str,
168166
) -> Result<ProbeResult, String> {
169-
170167
let probe_start = Instant::now();
171168
let url = url.to_string();
172169

@@ -225,7 +222,6 @@ async fn probe_url(
225222
})
226223
}
227224

228-
229225
pub async fn run_probe_loop(
230226
tenant_name: String,
231227
org_config: OrganisationConfig,
@@ -234,18 +230,10 @@ pub async fn run_probe_loop(
234230
mimir_endpoint: String,
235231
max_org_width: usize,
236232
) {
237-
let client = Client::builder()
238-
.timeout(Duration::from_secs(5))
239-
.danger_accept_invalid_certs(true)
240-
.user_agent("reqwest-h2-h3-probe/1.0")
241-
.build()
242-
.expect("Failed to create client");
243-
244233
loop {
245234
let mut handles = vec![];
246235

247236
for target in &org_config.targets {
248-
let client = client.clone();
249237
let connector = tls_connector.clone();
250238
let resolver = resolver.clone();
251239
let target = target.clone();
@@ -258,7 +246,6 @@ pub async fn run_probe_loop(
258246
tenant_name,
259247
&org_id,
260248
&target,
261-
&client,
262249
&connector,
263250
&resolver,
264251
&mimir_endpoint,
@@ -287,7 +274,6 @@ fn to_fixed_width(input: &str, width: usize) -> String {
287274
format!("{:<width$}", truncated, width = width)
288275
}
289276

290-
291277
/// Handles probing a target URL and sending the results to Mimir.
292278
/// # Arguments
293279
/// * `tenant` - The tenant name for logging and metrics.
@@ -302,12 +288,18 @@ async fn handle_target_probe(
302288
tenant: String,
303289
org_id: &str,
304290
target: &TargetConfig,
305-
client: &Client,
306291
tls_connector: &TokioTlsConnector,
307292
resolver: &TokioAsyncResolver,
308293
mimir_target: &str,
309294
max_width: usize,
310295
) {
296+
let client = Client::builder()
297+
.timeout(Duration::from_secs(5))
298+
.danger_accept_invalid_certs(true)
299+
.user_agent("reqwest-h2-h3-probe/1.0")
300+
.build()
301+
.expect("Failed to create client");
302+
311303
let url = &target.url;
312304
let result = probe_url(client.clone(), tls_connector, resolver, url).await;
313305

@@ -338,7 +330,8 @@ async fn handle_target_probe(
338330
} else {
339331
log::error!(
340332
"[{padded_tenant}] ❌ Unexpected status for {url}: {:?} (accepted: {:?})",
341-
probe.http_status, target.accepted_status_codes
333+
probe.http_status,
334+
target.accepted_status_codes
342335
);
343336
}
344337

oxybox/src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::time::Duration;
21
use config::app_config::{load_config, setup_resolver, setup_tls_connector};
32
use dotenvy::dotenv;
3+
use std::time::Duration;
44
use tokio::time::sleep;
55
pub mod http_probe;
66
use http_probe::probe::run_probe_loop;
@@ -37,6 +37,3 @@ async fn main() {
3737
sleep(Duration::from_secs(60)).await;
3838
}
3939
}
40-
41-
42-

oxybox/src/mimir/client.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,5 @@ mod tests {
160160
}
161161

162162
assert!(true);
163-
164163
}
165164
}

0 commit comments

Comments
 (0)