From 0bb15d6738b80b3b3f6170bae5114f4843e3bf9a Mon Sep 17 00:00:00 2001 From: Riccardo Gallo Date: Fri, 29 Nov 2024 16:10:25 +0100 Subject: [PATCH] feat(instrument): add tracing instrumentation for logging Signed-off-by: Riccardo Gallo --- src/astarte.rs | 5 ++++- src/config.rs | 3 ++- src/lib.rs | 6 +++++- src/shutdown.rs | 4 +++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/astarte.rs b/src/astarte.rs index 0146cac..0140d1c 100644 --- a/src/astarte.rs +++ b/src/astarte.rs @@ -17,7 +17,7 @@ use color_eyre::eyre::{eyre, OptionExt, WrapErr}; use serde::Deserialize; use std::path::{Path, PathBuf}; use std::{env, io}; -use tracing::{debug, error}; +use tracing::{debug, error, instrument}; /// Stream Rust test node identifier const STREAM_RUST_TEST_NODE_UUID: uuid::Uuid = uuid::uuid!("d72a6187-7cf1-44cc-87e8-e991936166dc"); @@ -70,6 +70,7 @@ impl ConnectionConfigBuilder { /// Init astarte config from env var if they have been set /// /// If an error is returned, it means that one or more environment variables have not been set + #[instrument(skip_all)] pub fn try_from_env(&mut self) -> eyre::Result<()> { let con = env::var("ASTARTE_CONNECTION") .map(|s| AstarteConnection::from_str(&s, true))? @@ -113,6 +114,7 @@ impl ConnectionConfigBuilder { } /// Update the missing config values taking them from a config.toml file + #[instrument(skip_all)] pub async fn from_toml(&mut self, path: impl AsRef) { match tokio::fs::read_to_string(&path).await { Ok(file) => { @@ -137,6 +139,7 @@ impl ConnectionConfigBuilder { } /// Build a complete Astarte configuration or return an error + #[instrument(skip_all)] pub async fn build(self) -> eyre::Result<(DeviceClient, SdkConnection)> { let astarte_connection = self .astarte_connection diff --git a/src/config.rs b/src/config.rs index 77815e8..3081621 100644 --- a/src/config.rs +++ b/src/config.rs @@ -11,7 +11,7 @@ use crate::math::MathFunction; use color_eyre::eyre; use std::f64::consts::PI; use std::time::SystemTime; -use tracing::debug; +use tracing::{debug, instrument}; /// Stream configuration /// @@ -49,6 +49,7 @@ impl StreamConfig { } /// Update the stream internal configuration + #[instrument(skip_all)] pub(crate) async fn update_cfg(&mut self, update: StreamConfigUpdate) { let StreamConfigUpdate { sensor_id, update } = update; diff --git a/src/lib.rs b/src/lib.rs index 0be7127..1cd2d3f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ use color_eyre::eyre; use color_eyre::eyre::OptionExt; use std::time::SystemTime; use tokio::select; -use tracing::{debug, error}; +use tracing::{debug, error, instrument}; use crate::cli::Config; use crate::config::{StreamConfig, StreamConfigUpdate}; @@ -45,6 +45,7 @@ impl StreamManager { } /// Handle sending data to Astarte and the reception of new stream configuration from Astarte + #[instrument(skip_all)] pub async fn handle(mut self, client: DeviceClient) -> eyre::Result<()> { loop { select! { @@ -72,6 +73,7 @@ impl StreamManager { } /// Send data to Astarte + #[instrument(skip_all)] async fn send_data(&mut self, client: &DeviceClient) -> eyre::Result<()> { // Send data to Astarte let value = self.stream_cfg.next_value(); @@ -89,6 +91,8 @@ impl StreamManager { Ok(()) } + /// Receive new stream configuration from Astarte. + #[instrument(skip_all)] async fn receive_data(&mut self, event: DeviceEvent) -> eyre::Result<()> { if let astarte_device_sdk::Value::Individual(var) = event.data { // split the mapping path, which looks like "/foo/bar" diff --git a/src/shutdown.rs b/src/shutdown.rs index 82517f6..93fd15e 100644 --- a/src/shutdown.rs +++ b/src/shutdown.rs @@ -8,10 +8,11 @@ use color_eyre::eyre; use color_eyre::eyre::WrapErr; -use tracing::error; +use tracing::{error, instrument}; #[cfg(unix)] /// Shut down the application in case a SIGTERM or SIGINT is received. +#[instrument(skip_all)] pub fn shutdown() -> eyre::Result> { use futures::FutureExt; use tokio::signal::unix::SignalKind; @@ -40,6 +41,7 @@ pub fn shutdown() -> eyre::Result> { #[cfg(not(unix))] /// Shut down the application in case a SIGINT is received. +#[instrument(skip_all)] pub fn shutdown() -> eyre::Result> { use futures::FutureExt;