Skip to content

Commit

Permalink
feat(instrument): add tracing instrumentation for logging
Browse files Browse the repository at this point in the history
Signed-off-by: Riccardo Gallo <[email protected]>
  • Loading branch information
rgallor committed Dec 3, 2024
1 parent 6c21d39 commit 3630da0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/astarte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use serde::Deserialize;
use std::env::VarError;
use std::path::{Path, PathBuf};
use std::{env, io};
use tracing::{debug, error};
use tracing::{debug, error, instrument};
use uuid::{uuid, Uuid};

const DEVICE_DATASTREAM: &str =
Expand Down Expand Up @@ -76,6 +76,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))?
Expand Down Expand Up @@ -129,6 +130,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<Path>) {
match tokio::fs::read_to_string(&path).await {
Ok(file) => {
Expand All @@ -153,6 +155,7 @@ impl ConnectionConfigBuilder {
}

/// Build a complete Astarte configuration or return an error
#[instrument(skip_all)]
pub async fn build(self) -> eyre::Result<(DeviceClient<SqliteStore>, SdkConnection)> {
let astarte_connection = self
.astarte_connection
Expand Down
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<SqliteStore>) -> eyre::Result<()> {
loop {
select! {
Expand Down Expand Up @@ -72,6 +73,7 @@ impl StreamManager {
}

/// Send data to Astarte
#[instrument(skip_all)]
async fn send_data(&mut self, client: &DeviceClient<SqliteStore>) -> eyre::Result<()> {
// Send data to Astarte
let value = self.stream_cfg.next_value();
Expand All @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion src/shutdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<impl std::future::Future<Output = ()>> {
use futures::FutureExt;
use tokio::signal::unix::SignalKind;
Expand Down Expand Up @@ -40,6 +41,7 @@ pub fn shutdown() -> eyre::Result<impl std::future::Future<Output = ()>> {

#[cfg(not(unix))]
/// Shut down the application in case a SIGINT is received.
#[instrument(skip_all)]
pub fn shutdown() -> eyre::Result<impl std::future::Future<Output = ()>> {
use futures::FutureExt;

Expand Down

0 comments on commit 3630da0

Please sign in to comment.