Enhances observability with tracing and Debug derives

Applies `#[derive(Debug)]` to key structs (`Brain`, `LedPump`, `OrganSocket`) to facilitate easier state inspection during development and debugging.

Integrates `#[instrument]` attributes from the `tracing` crate on critical methods (`Brain::rest`, `LedPump::do_work`, `Organ::run_organ_message_loop`) to provide detailed span information in logs.

Makes `id` and `parent_id` fields of `ChildIdentity` public, enabling direct access.

Refactors `Organ::start` to delegate its message loop to a dedicated private method, improving code clarity.
This commit is contained in:
Russell Gilbert 2026-03-27 08:30:12 +00:00
parent 09f75f97eb
commit 9e8b3eed5b
5 changed files with 15 additions and 15 deletions

View file

@ -1,4 +1,4 @@
use tracing::{debug, info}; use tracing::{debug, info, instrument};
use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc::{Receiver, Sender};
use crate::lifecycle::{LifeState, LifecycleCommand, LifecycleCommandResponse, LifecycleReceipt}; use crate::lifecycle::{LifeState, LifecycleCommand, LifecycleCommandResponse, LifecycleReceipt};
use crate::lifecycle::LifeState::{Dying, Buried, Genisys, Dead}; use crate::lifecycle::LifeState::{Dying, Buried, Genisys, Dead};
@ -7,6 +7,7 @@ use crate::organs::organ_socket::OrganSocket;
use crate::protocols::{BrainMessage, OrganCommand, OrganCommandEnvelope, OrganResponse}; use crate::protocols::{BrainMessage, OrganCommand, OrganCommandEnvelope, OrganResponse};
use crate::system::time::Time; use crate::system::time::Time;
#[derive(Debug)]
pub struct Brain { pub struct Brain {
state:LifeState, state:LifeState,
divine_rx : Receiver<LifecycleCommand>, divine_rx : Receiver<LifecycleCommand>,
@ -59,6 +60,7 @@ impl Brain {
} }
} }
#[instrument(skip(self), fields(state=?self.state))]
fn rest(&self) { fn rest(&self) {
debug!("Brain is {} and resting.", self.state); debug!("Brain is {} and resting.", self.state);
std::thread::sleep(std::time::Duration::from_millis(200)) std::thread::sleep(std::time::Duration::from_millis(200))

View file

@ -1,7 +1,7 @@
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ChildIdentity { pub struct ChildIdentity {
id: u32, pub id: u32,
parent_id: u32, pub parent_id: u32,
} }
impl ChildIdentity { impl ChildIdentity {

View file

@ -3,13 +3,16 @@ use crate::organs::parenchyma::Parenchyma;
use crate::protocols::{OrganCommand, OrganCommandEnvelope, OrganResponse}; use crate::protocols::{OrganCommand, OrganCommandEnvelope, OrganResponse};
use crate::system::time::Time; use crate::system::time::Time;
use crate::{child_identity, impl_identifiable}; use crate::{child_identity, impl_identifiable};
use tracing::{info}; use tracing::{info, instrument};
#[derive(Debug)]
pub struct LedPump { pub struct LedPump {
identity: child_identity::ChildIdentity, identity: child_identity::ChildIdentity,
last_beat: u64, last_beat: u64,
} }
impl_identifiable!(LedPump, { impl_identifiable!(LedPump, {
#[instrument(skip(self), fields(led_pump_id = self.identity.id))]
fn do_work(&mut self, _envelope: OrganCommandEnvelope) -> OrganResponse { fn do_work(&mut self, _envelope: OrganCommandEnvelope) -> OrganResponse {
// Your logic here // Your logic here
let now = Time::time_stamp_millis(); let now = Time::time_stamp_millis();

View file

@ -6,16 +6,6 @@ use crate::organs::parenchyma::Parenchyma;
use crate::protocols::{BrainMessage, OrganCommand, OrganCommandEnvelope, OrganResponse}; use crate::protocols::{BrainMessage, OrganCommand, OrganCommandEnvelope, OrganResponse};
use crate::system::time::Time; use crate::system::time::Time;
// pub trait Organ: Send {
// /// Returns the immutable U32 ID assigned by the factory.
// fn id(&self) -> u32;
//
// fn add_parenchyma(&mut self, parenchyma: Box<dyn Parenchyma>);
// }
pub struct Organ { pub struct Organ {
id: u32, id: u32,
brain_command_rx: mpsc::Receiver<OrganCommandEnvelope>, brain_command_rx: mpsc::Receiver<OrganCommandEnvelope>,
@ -45,8 +35,12 @@ impl Organ {
} }
} }
#[instrument(skip(self), fields(heart_id = self.id))]
pub fn start(&mut self) { pub fn start(&mut self) {
self.run_organ_message_loop();
}
#[instrument(skip(self), fields(heart_id = self.id))]
fn run_organ_message_loop(&mut self) {
info!("Heart listener active"); info!("Heart listener active");
while let Ok(envelope) = self.brain_command_rx.recv() { while let Ok(envelope) = self.brain_command_rx.recv() {

View file

@ -3,6 +3,7 @@ use std::sync::mpsc::Sender;
use crate::lifecycle::LifeState; use crate::lifecycle::LifeState;
use crate::protocols::{OrganCommand, OrganCommandEnvelope}; use crate::protocols::{OrganCommand, OrganCommandEnvelope};
#[derive(Debug)]
pub struct OrganSocket { pub struct OrganSocket {
pub id: u32, pub id: u32,
pub last_reported_state : LifeState, pub last_reported_state : LifeState,