Skip to content

Commit 59919ba

Browse files
author
Thomas Luijken
committed
Added logging and loglevels
1 parent b1e1d72 commit 59919ba

File tree

6 files changed

+21
-18
lines changed

6 files changed

+21
-18
lines changed

oxybox/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
CONFIG_FILE="example-config.yml"
22
DNS_HOSTS="1.1.1.1, 8.8.8.8"
33
MIMIR_ENDPOINT="http://localhost:9009"
4+
RUST_LOG=info

oxybox/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "oxybox"
3-
version = "1.0.1"
3+
version = "1.0.2"
44
authors = ["Thomas Luijken"]
55
categories = ["command-line-utilities"]
66
description = "A drop in replacement for blackbox exporter for Prometheus, with support for TLS and HTTP/3."
@@ -17,7 +17,7 @@ tokio = { version = "1", features = ["full"] }
1717
reqwest = { version = "0.12", features = ["json", "rustls-tls", "http3"] }
1818
native-tls = "0.2"
1919
tokio-native-tls = "0.3"
20-
x509-parser = "0.17"
20+
x509-parser = "0.18"
2121
url = "2"
2222
prost = "0.14"
2323
snap = "1.1"
@@ -32,6 +32,8 @@ unicode-truncate = "2.0"
3232
openssl = { version = "0.10", features = ["vendored"] }
3333
serde_yaml = "0.9"
3434
dotenvy = "0.15"
35+
env_logger = "0.11"
36+
log = "0.4"
3537

3638
[build-dependencies]
3739
prost-build = "0.14" # Protobuf code generator for Prost

oxybox/src/config/app_config.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{net::IpAddr, time::Duration};
22
use std::env;
33

4-
use dotenvy::dotenv;
54
use tokio_native_tls::TlsConnector as TokioTlsConnector;
65
use trust_dns_resolver::{
76
TokioAsyncResolver,
@@ -23,8 +22,6 @@ pub struct AppConfig {
2322
/// It also sets up the DNS hosts and Mimir endpoint.
2423
pub fn load_config() -> AppConfig {
2524

26-
dotenv().ok();
27-
2825
let config_file_location =
2926
env::var("CONFIG_FILE").unwrap_or_else(|_| "config.yml".to_string());
3027
let config_str = std::fs::read_to_string(&config_file_location)
@@ -38,7 +35,7 @@ pub fn load_config() -> AppConfig {
3835
.map(|s| s.trim().to_string())
3936
.collect();
4037

41-
println!("Using DNS hosts: {:?}", dns_hosts);
38+
log::info!("Using DNS hosts: {:?}", dns_hosts);
4239

4340
let mimir_endpoint =
4441
env::var("MIMIR_ENDPOINT").unwrap_or_else(|_| "http://localhost:9009".to_string());

oxybox/src/http_probe/probe.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ async fn get_connect_timings(
116116
(tls_time, cert_validity_seconds)
117117
}
118118
Err(e) => {
119-
eprintln!("Failed to establish TLS connection for host {host}: {e}");
119+
log::error!("Failed to establish TLS connection for host {host}: {e}");
120120
return Err(format!(
121121
"Failed to establish TLS connection for host {host}: {e}"
122122
));
@@ -201,7 +201,7 @@ async fn probe_url(
201201
)
202202
}
203203
Err(e) => {
204-
eprintln!("HTTP request failed for URL {url}: {e}");
204+
log::error!("HTTP request failed for URL {url}: {e}");
205205
return Err(format!("HTTP request failed for URL {url}: {e}"));
206206
}
207207
};
@@ -323,7 +323,7 @@ async fn handle_target_probe(
323323
.unwrap_or(false);
324324

325325
if accepted {
326-
println!(
326+
log::debug!(
327327
"[{padded_tenant}] ✅ URL: {}, Status: {:?}, Elapsed: {:.2}ms, Cert: {}",
328328
url,
329329
probe.http_status,
@@ -334,20 +334,20 @@ async fn handle_target_probe(
334334
.unwrap_or_else(|| "N/A".to_string())
335335
);
336336
} else {
337-
println!(
337+
log::error!(
338338
"[{padded_tenant}] ❌ Unexpected status for {url}: {:?} (accepted: {:?})",
339339
probe.http_status, target.accepted_status_codes
340340
);
341341
}
342342

343343
let metrics = create_probe_metrics(&probe, accepted);
344344
if let Err(e) = send_to_mimir(mimir_target, Some(org_id), metrics).await {
345-
println!("[{padded_tenant}] Failed to send metrics for {url}: {e}");
345+
log::error!("[{padded_tenant}] Failed to send metrics for {url}: {e}");
346346
}
347347
}
348348
Err(e) => {
349349
// in case we cannot probe the url, send a failed probe with zeroed metrics
350-
println!("[{padded_tenant}] ❌ Probe error for {url}: {e}");
350+
log::error!("[{padded_tenant}] ❌ Probe error for {url}: {e}");
351351
let probe = ProbeResult {
352352
url: url.to_string(),
353353
dns_time: None,
@@ -362,7 +362,7 @@ async fn handle_target_probe(
362362
};
363363
let metrics = create_probe_metrics(&probe, false);
364364
if let Err(e) = send_to_mimir(mimir_target, Some(org_id), metrics).await {
365-
println!("[{padded_tenant}] Failed to send error metrics for {url}: {e}");
365+
log::error!("[{padded_tenant}] Failed to send error metrics for {url}: {e}");
366366
}
367367
}
368368
}

oxybox/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::time::Duration;
22
use config::app_config::{load_config, setup_resolver, setup_tls_connector};
3+
use dotenvy::dotenv;
34
use tokio::time::sleep;
45
pub mod http_probe;
56
use http_probe::probe::run_probe_loop;
@@ -8,11 +9,13 @@ pub mod mimir;
89

910
#[tokio::main]
1011
async fn main() {
12+
dotenv().ok();
13+
env_logger::init();
1114
let app_config = load_config();
1215
let resolver = setup_resolver(&app_config.dns_hosts).expect("Failed to init resolver");
1316
let tls_connector = setup_tls_connector().expect("Failed to build TLS connector");
1417

15-
println!("Using Mimir endpoint: {}", app_config.mimir_endpoint);
18+
log::info!("Using Mimir endpoint: {}", app_config.mimir_endpoint);
1619

1720
for (key, org_config) in app_config.config {
1821
let resolver = resolver.clone();

oxybox/src/mimir/client.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub async fn send_to_mimir(
2626
metrics: Vec<TimeSeries>,
2727
) -> Result<(), Box<dyn std::error::Error>> {
2828
if metrics.is_empty() {
29-
println!("No metrics to send.");
29+
log::warn!("No metrics to send.");
3030
return Ok(());
3131
}
3232

@@ -66,7 +66,7 @@ pub async fn send_to_mimir(
6666
if !response.status().is_success() {
6767
let status = response.status();
6868
let body = response.text().await.unwrap_or_default();
69-
eprintln!("Failed to push to Mimir: {} - {}", status, body);
69+
log::error!("Failed to push to Mimir: {} - {}", status, body);
7070
return Err(format!("Failed to push to Mimir: {status} - {body}").into());
7171
}
7272
Ok(())
@@ -155,8 +155,8 @@ mod tests {
155155

156156
// Attempt to send
157157
match send_to_mimir(mimir_url, tenant_id, metrics_to_send).await {
158-
Ok(_) => println!("Test metrics sent successfully."),
159-
Err(e) => eprintln!("Failed to send test metrics: {}", e),
158+
Ok(_) => log::debug!("Test metrics sent successfully."),
159+
Err(e) => log::debug!("Failed to send test metrics: {}", e),
160160
}
161161

162162
assert!(true);

0 commit comments

Comments
 (0)