Skip to content

Commit f2375d1

Browse files
committed
fix: tracing
1 parent b171b0f commit f2375d1

File tree

5 files changed

+185
-42
lines changed

5 files changed

+185
-42
lines changed

Cargo.lock

Lines changed: 83 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ description = "Acme DNS implementation written in Rust"
1111

1212
[dependencies]
1313
tracing = "0.1"
14+
tracing-subscriber = "0.2"
15+
tracing-futures = { version = "0.2.4", features = ["futures-03"]}
1416
sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio", "macros", "migrate"] }
1517
tokio = { version = "0.2", features = ["dns", "tcp", "udp"] }
1618
uuid = { version = "0.8", features = ["v4"] }

src/api.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use std::sync::Arc;
1111
use tokio::io::{AsyncRead, AsyncWrite};
1212
use tokio::net::{TcpListener, ToSocketAddrs};
1313
use tokio_rustls::TlsAcceptor;
14-
use tracing::error;
14+
use tracing::{error, info};
15+
use tracing_futures::Instrument;
1516
use warp::{http::Response, reply, serve, Filter, Rejection, Reply};
1617

1718
use crate::cert::{Cert, CertFacade};
@@ -102,18 +103,24 @@ pub struct Api {
102103
pool: PgPool,
103104
}
104105

106+
#[tracing::instrument(skip(pool))]
105107
async fn register(pool: PgPool, domain: Domain) -> Result<reply::Response, Rejection> {
106-
let _domain = match DomainFacade::create_domain(&pool, &domain).await {
108+
let _domain = match DomainFacade::create_domain(&pool, &domain)
109+
.in_current_span()
110+
.await
111+
{
107112
Err(e) => {
113+
error!("{}", e);
108114
return Ok(Response::builder()
109115
.status(500)
110116
.body(e.to_string())
111117
.unwrap()
112-
.into_response())
118+
.into_response());
113119
}
114120
Ok(domain) => domain,
115121
};
116122

123+
info!("Success for call");
117124
Ok(Response::new("no error").into_response())
118125
}
119126

@@ -131,7 +138,10 @@ impl Api {
131138
Ok(Api { http, https, pool })
132139
}
133140

141+
#[tracing::instrument(skip(self))]
134142
pub async fn spawn(self) -> Result<()> {
143+
info!("Starting API spawn");
144+
135145
let pool = self.pool.clone();
136146
let routes = warp::path("register")
137147
.and(warp::post())
@@ -141,13 +151,21 @@ impl Api {
141151

142152
let http = self
143153
.http
154+
.map(|http| {
155+
info!(?http, "Starting http");
156+
http.in_current_span()
157+
})
144158
.map(|http| serve(routes.clone()).serve_incoming(http))
145159
.map(tokio::spawn);
146160

147161
let pool = self.pool.clone();
148162
let https = self
149163
.https
150-
.map(|https| serve(routes).serve_incoming(stream(https, pool)))
164+
.map(|https| {
165+
info!(?https, "Starting https");
166+
stream(https, pool).into_stream().in_current_span()
167+
})
168+
.map(|https| serve(routes).serve_incoming(https))
151169
.map(tokio::spawn);
152170

153171
match (https, http) {

src/config.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::Deserialize;
33
use std::collections::HashMap;
44
use std::fs::File;
55
use std::io::Read;
6-
use tracing::info;
6+
use tracing::{debug, info, info_span};
77

88
#[derive(Deserialize, Debug)]
99
pub struct Api {
@@ -35,17 +35,23 @@ pub struct Config {
3535
const DEFAULT_CONFIG_PATH: &str = "config.toml";
3636

3737
// is not async so we can use it to load settings for tokio runtime
38-
pub fn config(config_path: Option<String>) -> Result<Config> {
38+
pub fn load_config(config_path: Option<String>) -> Result<Config> {
3939
let config_path = config_path.as_deref().unwrap_or(DEFAULT_CONFIG_PATH);
40+
41+
let span = info_span!("load_config", config_path);
42+
let _enter = span.enter();
43+
4044
let mut file = File::open(config_path)?;
45+
debug!(?file, "Opened file");
46+
4147
let mut bytes = vec![];
4248
file.read_to_end(&mut bytes)?;
49+
debug!(file_length = bytes.len(), "Read file");
4350

4451
let config = toml::de::from_slice::<Config>(&bytes)?;
45-
4652
// redact db information
4753
let config_str = format!("{:?}", config).replace(&config.general.db, "******");
48-
info!("Loaded {}", config_str);
54+
info!(config = %config_str, "Deserialized config");
4955

5056
Ok(config)
5157
}

src/main.rs

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use anyhow::Result;
22
use futures_util::TryFutureExt;
3-
use simplelog::{Config, LevelFilter, SimpleLogger};
43
use sqlx::migrate::Migrator;
54
use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
65
use sqlx::PgPool;
76
use std::env;
87
use std::str::FromStr;
98
use tokio::runtime::Runtime;
10-
use tracing::error;
9+
use tracing::{debug, error, info};
1110

1211
use crate::acme::DatabasePersist;
1312
use crate::api::Api;
1413
use crate::cert::CertManager;
1514
use crate::dns::{DatabaseAuthority, DNS};
15+
use tracing_futures::Instrument;
1616

1717
mod acme;
1818
mod api;
@@ -25,51 +25,85 @@ mod util;
2525
static MIGRATOR: Migrator = sqlx::migrate!("migrations/postgres");
2626

2727
fn main() {
28-
SimpleLogger::init(LevelFilter::Debug, Config::default()).unwrap();
28+
tracing_subscriber::fmt::init();
2929

30-
if let Err(e) = run() {
31-
error!("{:?}", e);
30+
if run().is_err() {
3231
std::process::exit(1);
3332
}
3433
}
3534

35+
#[tracing::instrument]
3636
fn run() -> Result<()> {
3737
let config_path = env::args().nth(1);
38-
let config = config::config(config_path)?;
38+
let config = config::load_config(config_path)?;
39+
40+
let runtime = match Runtime::new() {
41+
Ok(runtime) => runtime,
42+
Err(e) => {
43+
error!("{}", e);
44+
return Err(e.into());
45+
}
46+
};
47+
debug!("Created runtime");
3948

40-
let runtime = Runtime::new()?;
4149
// Async closure cannot be move, if runtime gets moved into it
4250
// it gets dropped inside an async call
43-
runtime.handle().block_on(async {
44-
let pool = setup_database(&config.general.db).await?;
45-
let authority = DatabaseAuthority::new(pool.clone(), &config.general.name, config.records);
46-
let dns = DNS::new(&config.general.dns, &runtime, authority);
47-
48-
let api = Api::new(
49-
config.api.http.as_deref(),
50-
config.api.https.as_deref(),
51-
pool.clone(),
52-
)
53-
.and_then(Api::spawn);
54-
55-
let persist = DatabasePersist::new(pool.clone(), runtime.handle());
56-
let cert_manager =
57-
CertManager::new(pool, persist, config.general.acme).and_then(CertManager::spawn);
58-
59-
tokio::try_join!(api, cert_manager, dns.spawn())?;
60-
61-
Ok(())
62-
})
51+
let res: Result<()> = runtime.handle().block_on(
52+
async {
53+
let pool = setup_database(&config.general.db).in_current_span().await?;
54+
let authority =
55+
DatabaseAuthority::new(pool.clone(), &config.general.name, config.records);
56+
let dns = DNS::new(&config.general.dns, &runtime, authority);
57+
58+
let api = Api::new(
59+
config.api.http.as_deref(),
60+
config.api.https.as_deref(),
61+
pool.clone(),
62+
)
63+
.and_then(Api::spawn);
64+
65+
let persist = DatabasePersist::new(pool.clone(), runtime.handle());
66+
let cert_manager =
67+
CertManager::new(pool, persist, config.general.acme).and_then(CertManager::spawn);
68+
69+
info!("Starting API Cert Manager and DNS");
70+
tokio::try_join!(api, cert_manager, dns.spawn())?;
71+
72+
Ok(())
73+
}
74+
.in_current_span(),
75+
);
76+
77+
if let Err(e) = res {
78+
error!("{}", e);
79+
return Err(e.into());
80+
};
81+
82+
Ok(())
6383
}
6484

85+
#[tracing::instrument(skip(db))]
6586
async fn setup_database(db: &str) -> Result<PgPool, sqlx::Error> {
66-
let options = PgConnectOptions::from_str(db)?;
67-
let pool = PgPoolOptions::new()
68-
.max_connections(5)
69-
.connect_with(options)
70-
.await?;
87+
let pool = async {
88+
let options = PgConnectOptions::from_str(db)?;
89+
let pool = PgPoolOptions::new()
90+
.max_connections(5)
91+
.connect_with(options)
92+
.await?;
93+
debug!("Created DB pool");
7194

72-
MIGRATOR.run(&pool).await?;
95+
MIGRATOR.run(&pool).await?;
96+
info!("Ran migration");
97+
Ok(pool)
98+
}
99+
.in_current_span()
100+
.await;
73101

74-
Ok(pool)
102+
match pool {
103+
Ok(pool) => Ok(pool),
104+
Err(e) => {
105+
error!("{}", e);
106+
Err(e)
107+
}
108+
}
75109
}

0 commit comments

Comments
 (0)