Skip to content

Commit 42f7b2d

Browse files
authored
Merge pull request #28 from flashbots/peg/logging
Add logging - trying to give similar logging behaviour / options as `cvm-reverse-proxy`
2 parents 03b9f86 + d6202bc commit 42f7b2d

File tree

4 files changed

+158
-25
lines changed

4 files changed

+158
-25
lines changed

Cargo.lock

Lines changed: 113 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ edition = "2024"
55
license = "MIT"
66

77
[dependencies]
8-
tokio = { version = "1.48.0", features = ["full"]}
9-
tokio-rustls = { version = "0.26.4", default-features = false, features = ["ring"]}
8+
tokio = { version = "1.48.0", features = ["full"] }
9+
tokio-rustls = { version = "0.26.4", default-features = false, features = ["ring"] }
1010
sha2 = "0.10.9"
1111
x509-parser = "0.18.0"
1212
thiserror = "2.0.17"
@@ -27,6 +27,8 @@ bytes = "1.10.1"
2727
http = "1.3.1"
2828
serde_json = "1.0.145"
2929
serde = "1.0.228"
30+
tracing = "0.1.41"
31+
tracing-subscriber = { version = "0.3.20", features = ["env-filter", "json"] }
3032
parity-scale-codec = "3.7.5"
3133

3234
[dev-dependencies]

src/lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use parity_scale_codec::{Decode, Encode};
1111
use thiserror::Error;
1212
use tokio::sync::{mpsc, oneshot};
1313
use tokio_rustls::rustls::server::{VerifierBuilderError, WebPkiClientVerifier};
14+
use tracing::{error, warn};
1415

1516
#[cfg(test)]
1617
mod test_helpers;
@@ -153,7 +154,7 @@ impl ProxyServer {
153154
)
154155
.await
155156
{
156-
eprintln!("Failed to handle connection: {err}");
157+
warn!("Failed to handle connection: {err}");
157158
}
158159
});
159160

@@ -244,7 +245,7 @@ impl ProxyServer {
244245
Err(e) => {
245246
// This error is highly unlikely - that the measurement values fail to
246247
// encode to JSON or fit in an HTTP header
247-
eprintln!("Failed to encode measurement values: {e}");
248+
error!("Failed to encode measurement values: {e}");
248249
}
249250
}
250251
headers.insert(
@@ -260,7 +261,7 @@ impl ProxyServer {
260261
Ok::<Response<BoxBody<bytes::Bytes, hyper::Error>>, hyper::Error>(res)
261262
}
262263
Err(e) => {
263-
eprintln!("Failed to handle a request from a proxy-client: {e}");
264+
warn!("Failed to handle a request from a proxy-client: {e}");
264265
let mut resp = Response::new(full(format!("Request failed: {e}")));
265266
*resp.status_mut() = hyper::StatusCode::BAD_GATEWAY;
266267
Ok(resp)
@@ -291,15 +292,15 @@ impl ProxyServer {
291292
// Drive the connection
292293
tokio::spawn(async move {
293294
if let Err(e) = conn.await {
294-
eprintln!("Client connection error: {e}");
295+
warn!("Client connection error: {e}");
295296
}
296297
});
297298

298299
// Forward the request from the proxy-client to the target server
299300
match sender.send_request(req).await {
300301
Ok(resp) => Ok(resp.map(|b| b.boxed())),
301302
Err(e) => {
302-
eprintln!("send_request error: {e}");
303+
warn!("send_request error: {e}");
303304
let mut resp = Response::new(full(format!("Request failed: {e}")));
304305
*resp.status_mut() = hyper::StatusCode::BAD_GATEWAY;
305306
Ok(resp)
@@ -428,7 +429,7 @@ impl ProxyClient {
428429
Err(e) => {
429430
// This error is highly unlikely - that the measurement values fail to
430431
// encode to JSON or fit in an HTTP header
431-
eprintln!("Failed to encode measurement values: {e}");
432+
error!("Failed to encode measurement values: {e}");
432433
}
433434
}
434435
headers.insert(
@@ -440,7 +441,7 @@ impl ProxyClient {
440441
(Ok(resp.map(|b| b.boxed())), false)
441442
}
442443
Err(e) => {
443-
eprintln!("Failed to send request to proxy-server: {e}");
444+
warn!("Failed to send request to proxy-server: {e}");
444445
let mut resp = Response::new(full(format!("Request failed: {e}")));
445446
*resp.status_mut() = hyper::StatusCode::BAD_GATEWAY;
446447

@@ -450,7 +451,7 @@ impl ProxyClient {
450451

451452
// Send the response back to the source client
452453
if response_tx.send(response).is_err() {
453-
eprintln!("Failed to forward response to source client, probably they dropped the connection");
454+
warn!("Failed to forward response to source client, probably they dropped the connection");
454455
}
455456

456457
// If the connection to the proxy server failed, reconnect
@@ -488,7 +489,7 @@ impl ProxyClient {
488489

489490
tokio::spawn(async move {
490491
if let Err(err) = Self::handle_connection(inbound, requests_tx).await {
491-
eprintln!("Failed to handle connection from source client: {err}");
492+
warn!("Failed to handle connection from source client: {err}");
492493
}
493494
});
494495

@@ -510,7 +511,7 @@ impl ProxyClient {
510511
Ok::<Response<BoxBody<bytes::Bytes, hyper::Error>>, hyper::Error>(res)
511512
}
512513
Err(e) => {
513-
eprintln!("send_request error: {e}");
514+
warn!("send_request error: {e}");
514515
let mut resp = Response::new(full(format!("Request failed: {e}")));
515516
*resp.status_mut() = hyper::StatusCode::BAD_GATEWAY;
516517
Ok(resp)
@@ -551,7 +552,7 @@ impl ProxyClient {
551552
return output;
552553
}
553554
Err(e) => {
554-
eprintln!("Reconnect failed: {e}. Retrying in {:#?}...", delay);
555+
warn!("Reconnect failed: {e}. Retrying in {:#?}...", delay);
555556
tokio::time::sleep(delay).await;
556557

557558
// increase delay for next time (exponential), but clamp to max_delay
@@ -634,7 +635,7 @@ impl ProxyClient {
634635
// Drive the connection
635636
tokio::spawn(async move {
636637
if let Err(e) = conn.await {
637-
eprintln!("Client connection error: {e}");
638+
warn!("Client connection error: {e}");
638639
}
639640
});
640641

src/main.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anyhow::{anyhow, ensure};
22
use clap::{Parser, Subcommand};
33
use std::{fs::File, net::SocketAddr, path::PathBuf};
44
use tokio_rustls::rustls::pki_types::{CertificateDer, PrivateKeyDer};
5+
use tracing::level_filters::LevelFilter;
56

67
use attested_tls_proxy::{
78
attestation::{measurements::get_measurements_from_file, AttestationType, AttestationVerifier},
@@ -13,15 +14,13 @@ use attested_tls_proxy::{
1314
struct Cli {
1415
#[clap(subcommand)]
1516
command: CliCommand,
16-
// TODO missing:
17-
// Name: "log-json",
18-
// Value: false,
19-
// Usage: "log in JSON format",
20-
//
21-
// Name: "log-debug",
22-
// Value: true,
23-
// Usage: "log debug messages",
24-
//
17+
/// Log debug messages
18+
#[arg(long, global = true)]
19+
log_debug: bool,
20+
/// Log in JSON format
21+
#[arg(long, global = true)]
22+
log_json: bool,
23+
// TODO still missing
2524
// Name: "log-dcap-quote",
2625
// EnvVars: []string{"LOG_DCAP_QUOTE"},
2726
// Value: false,
@@ -106,6 +105,24 @@ enum CliCommand {
106105
async fn main() -> anyhow::Result<()> {
107106
let cli = Cli::parse();
108107

108+
let level_filter = if cli.log_debug {
109+
LevelFilter::DEBUG
110+
} else {
111+
LevelFilter::WARN
112+
};
113+
114+
let env_filter = tracing_subscriber::EnvFilter::builder()
115+
.with_default_directive(level_filter.into())
116+
.from_env_lossy();
117+
118+
let subscriber = tracing_subscriber::fmt::Subscriber::builder().with_env_filter(env_filter);
119+
120+
if cli.log_json {
121+
subscriber.json().init();
122+
} else {
123+
subscriber.pretty().init();
124+
}
125+
109126
match cli.command {
110127
CliCommand::Client {
111128
listen_addr,
@@ -170,7 +187,7 @@ async fn main() -> anyhow::Result<()> {
170187

171188
loop {
172189
if let Err(err) = client.accept().await {
173-
eprintln!("Failed to handle connection: {err}");
190+
tracing::error!("Failed to handle connection: {err}");
174191
}
175192
}
176193
}
@@ -211,7 +228,7 @@ async fn main() -> anyhow::Result<()> {
211228

212229
loop {
213230
if let Err(err) = server.accept().await {
214-
eprintln!("Failed to handle connection: {err}");
231+
tracing::error!("Failed to handle connection: {err}");
215232
}
216233
}
217234
}

0 commit comments

Comments
 (0)