Skip to content

Commit 4f02fa9

Browse files
committed
add tracing instruments
1 parent aaeaaf9 commit 4f02fa9

File tree

5 files changed

+125
-5
lines changed

5 files changed

+125
-5
lines changed

src/logging.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use crate::error::Error;
22
use std::time::SystemTime;
3-
use tracing_subscriber::fmt::{format::Writer, time::FormatTime};
3+
use tracing::level_filters::LevelFilter;
4+
use tracing_subscriber::fmt::{
5+
format::{FmtSpan, Writer},
6+
time::FormatTime,
7+
};
48

59
struct DateTimeFormatter;
610

@@ -12,11 +16,19 @@ impl FormatTime for DateTimeFormatter {
1216
}
1317
}
1418

15-
pub fn setup() -> Result<(), Error> {
19+
pub fn setup(debug: bool) -> Result<(), Error> {
20+
let level = if cfg!(debug_assertions) | debug {
21+
LevelFilter::DEBUG
22+
} else {
23+
LevelFilter::INFO
24+
};
25+
1626
let subscriber = tracing_subscriber::fmt()
1727
.compact()
1828
.with_timer(DateTimeFormatter)
1929
.with_target(false)
30+
.with_max_level(level)
31+
.with_span_events(FmtSpan::CLOSE)
2032
.finish();
2133
tracing::subscriber::set_global_default(subscriber)?;
2234

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub static MIGRATOR: Migrator = sqlx::migrate!();
1919
async fn main() -> Result<(), error::Error> {
2020
let args = cli::Cli::parse();
2121

22-
logging::setup()?;
22+
logging::setup(args.debug)?;
2323

2424
info!("PixelWeather Server v{}", env!("CARGO_PKG_VERSION"));
2525
debug!("Arguments: {args:?}");

src/server/client.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ impl<S> Client<S> {
6464
message.take_request().ok_or(Error::NotRequest)
6565
}
6666

67+
#[tracing::instrument(name = "Client::send_message()", skip(self), level = "debug")]
6768
async fn send_message(&mut self, msg: Message) -> Result<()> {
6869
// Serialize the message.
6970
let raw = msg.serialize();
@@ -87,6 +88,13 @@ impl<S> Client<S> {
8788
Ok(())
8889
}
8990

91+
#[tracing::instrument(
92+
name = "Client::receive_message()",
93+
skip(self),
94+
level = "debug",
95+
err,
96+
ret
97+
)]
9098
async fn receive_message(&mut self) -> Result<Message> {
9199
// First read the message size.
92100
self.stream
@@ -183,6 +191,13 @@ impl Client<Unathenticated> {
183191
}
184192
}
185193

194+
#[tracing::instrument(
195+
name = "Client<Unathenticated>::receive_handshake()",
196+
skip(self),
197+
level = "debug",
198+
err,
199+
ret
200+
)]
186201
async fn receive_handshake(&mut self) -> Result<Mac> {
187202
let req = self.receive_request().await?;
188203
let Request::Handshake { mac } = req else {
@@ -230,6 +245,11 @@ impl Client<Authenticated> {
230245
self.state.update_state = UpdateState::UpToDate;
231246
}
232247

248+
#[tracing::instrument(
249+
name = "Client<Authenticated>::store_update_check_result()",
250+
skip(self, blob),
251+
level = "debug"
252+
)]
233253
pub fn store_update_check_result(
234254
&mut self,
235255
current_version: Version,

src/server/client_handle.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use super::{
55
rate_limit::RateLimiter,
66
};
77
use crate::error::Error;
8-
use tracing::{debug, error, warn};
98
use pwmp_client::pwmp_msg::{request::Request, response::Response};
109
use std::{io::Read, net::SocketAddr, sync::Arc, time::Duration};
1110
use tokio::{net::TcpStream, time::timeout};
11+
use tracing::{debug, error, warn};
1212

1313
#[allow(clippy::needless_pass_by_value)]
1414
pub async fn handle_client(
@@ -78,6 +78,7 @@ pub async fn handle_client(
7878
Ok(())
7979
}
8080

81+
#[tracing::instrument(name = "handle_request()", skip_all, level = "debug", err, ret)]
8182
async fn handle_request(
8283
req: Request,
8384
client: &mut Client<Authenticated>,

src/server/db.rs

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use sqlx::{
1010
Pool, Postgres,
1111
postgres::{PgConnectOptions, PgPoolOptions, PgSslMode},
1212
};
13+
use std::fmt::Debug;
1314

1415
pub type NodeId = i32;
1516
pub type MeasurementId = i32;
@@ -25,6 +26,7 @@ macro_rules! query {
2526
}
2627

2728
impl DatabaseClient {
29+
#[tracing::instrument(name = "DatabaseClient::init()", level = "debug", err, skip_all)]
2830
pub async fn new(config: &Config) -> Result<Self, Error> {
2931
let mut opts = PgConnectOptions::new()
3032
.host(&config.database.host)
@@ -45,6 +47,12 @@ impl DatabaseClient {
4547
Ok(Self(pool))
4648
}
4749

50+
#[tracing::instrument(
51+
name = "DatabaseClient::setup_timezone()",
52+
level = "debug",
53+
skip(self),
54+
err
55+
)]
4856
pub async fn setup_timezone(&self, tz: &str) -> Result<(), Error> {
4957
if !self.validate_timezone(tz).await? {
5058
return Err(Error::InvalidTimeZone(tz.into()));
@@ -57,6 +65,13 @@ impl DatabaseClient {
5765
Ok(())
5866
}
5967

68+
#[tracing::instrument(
69+
name = "DatabaseClient::authorize_device()",
70+
level = "debug",
71+
skip(self),
72+
err,
73+
ret
74+
)]
6075
pub async fn authorize_device(&self, mac: &Mac) -> Result<Option<NodeId>, Error> {
6176
let mac = mac.to_string();
6277

@@ -68,6 +83,12 @@ impl DatabaseClient {
6883
}
6984
}
7085

86+
#[tracing::instrument(
87+
name = "DatabaseClient::create_notification()",
88+
level = "debug",
89+
skip(self),
90+
err
91+
)]
7192
pub async fn create_notification(&self, node_id: NodeId, content: &str) -> Result<(), Error> {
7293
query!(
7394
self.0,
@@ -80,6 +101,13 @@ impl DatabaseClient {
80101
Ok(())
81102
}
82103

104+
#[tracing::instrument(
105+
name = "DatabaseClient::get_settings()",
106+
level = "debug",
107+
skip(self),
108+
err,
109+
ret
110+
)]
83111
pub async fn get_settings(&self, node_id: NodeId) -> Result<Option<NodeSettings>, Error> {
84112
let result = query!(
85113
self.0,
@@ -101,6 +129,13 @@ impl DatabaseClient {
101129
}
102130
}
103131

132+
#[tracing::instrument(
133+
name = "DatabaseClient::post_results()",
134+
level = "debug",
135+
skip(self),
136+
err,
137+
ret
138+
)]
104139
pub async fn post_results(
105140
&self,
106141
node: NodeId,
@@ -126,6 +161,12 @@ impl DatabaseClient {
126161
Ok(result.id)
127162
}
128163

164+
#[tracing::instrument(
165+
name = "DatabaseClient::post_stats()",
166+
level = "debug",
167+
skip(self),
168+
err
169+
)]
129170
pub async fn post_stats(
130171
&self,
131172
measurement: MeasurementId,
@@ -146,12 +187,24 @@ impl DatabaseClient {
146187
Ok(())
147188
}
148189

190+
#[tracing::instrument(
191+
name = "DatabaseClient::run_migrations()",
192+
level = "debug",
193+
skip(self),
194+
err
195+
)]
149196
pub async fn run_migrations(&self) -> Result<(), Error> {
150197
crate::MIGRATOR.run(&self.0).await?;
151198

152199
Ok(())
153200
}
154201

202+
#[tracing::instrument(
203+
name = "DatabaseClient::check_os_update()",
204+
level = "debug",
205+
skip(self),
206+
err
207+
)]
155208
pub async fn check_os_update(
156209
&self,
157210
node: NodeId,
@@ -183,6 +236,13 @@ impl DatabaseClient {
183236
}
184237
}
185238

239+
#[tracing::instrument(
240+
name = "DatabaseClient::send_os_update_stat()",
241+
level = "debug",
242+
skip(self),
243+
err,
244+
ret
245+
)]
186246
pub async fn send_os_update_stat(
187247
&self,
188248
node_id: NodeId,
@@ -208,6 +268,12 @@ impl DatabaseClient {
208268
Ok(result.id)
209269
}
210270

271+
#[tracing::instrument(
272+
name = "DatabaseClient::mark_os_update_stat()",
273+
level = "debug",
274+
skip(self),
275+
err
276+
)]
211277
pub async fn mark_os_update_stat(&self, node_id: NodeId, success: bool) -> Result<(), Error> {
212278
let last_update_id = self.get_last_os_update_stat(node_id).await?;
213279
query!(
@@ -221,6 +287,7 @@ impl DatabaseClient {
221287
Ok(())
222288
}
223289

290+
#[tracing::instrument(name = "DatabaseClient::erase()", level = "debug", skip(self), err)]
224291
pub async fn erase(&self, content_only: bool, keep_devices: bool) -> Result<(), Error> {
225292
if content_only {
226293
if keep_devices {
@@ -247,6 +314,13 @@ impl DatabaseClient {
247314
Ok(())
248315
}
249316

317+
#[tracing::instrument(
318+
name = "DatabaseClient::get_last_os_update_stat()",
319+
level = "debug",
320+
skip(self),
321+
err,
322+
ret
323+
)]
250324
async fn get_last_os_update_stat(&self, node_id: NodeId) -> Result<UpdateStatId, Error> {
251325
Ok(query!(
252326
self.0,
@@ -257,6 +331,12 @@ impl DatabaseClient {
257331
.id)
258332
}
259333

334+
#[tracing::instrument(
335+
name = "DatabaseClient::get_supported_time_zones()",
336+
level = "debug",
337+
skip(self),
338+
err
339+
)]
260340
async fn get_supported_time_zones(&self) -> Result<Vec<String>, Error> {
261341
let results = query!(self.0, "queries/get_tz_names.sql", fetch_all,)?;
262342
let names: Vec<String> = results
@@ -267,7 +347,14 @@ impl DatabaseClient {
267347
Ok(names)
268348
}
269349

270-
async fn validate_timezone<S: PartialEq<String>>(&self, tz: S) -> Result<bool, Error> {
350+
#[tracing::instrument(
351+
name = "DatabaseClient::validate_timezone()",
352+
level = "debug",
353+
skip(self, tz),
354+
err,
355+
ret /* this will print `tz` too */
356+
)]
357+
async fn validate_timezone<S: PartialEq<String> + Debug>(&self, tz: S) -> Result<bool, Error> {
271358
let supported = self.get_supported_time_zones().await?;
272359

273360
Ok(supported.iter().any(|candidate| tz.eq(candidate)))

0 commit comments

Comments
 (0)