astrid_telemetry/lib.rs
1//! Astrid Telemetry - Logging and tracing for the Astrid secure agent runtime.
2//!
3//! This crate provides:
4//! - Configurable logging setup with multiple formats
5//! - Request context for correlation across operations
6//! - Integration with the tracing ecosystem
7//!
8//! # Example
9//!
10//! ```rust,no_run
11//! use astrid_telemetry::{LogConfig, LogFormat, setup_logging, RequestContext};
12//!
13//! # fn main() -> Result<(), astrid_telemetry::TelemetryError> {
14//! // Set up logging
15//! let config = LogConfig::new("debug")
16//! .with_format(LogFormat::Pretty)
17//! .with_directive("astrid_mcp=trace");
18//!
19//! setup_logging(&config)?;
20//!
21//! // Create a request context
22//! let ctx = RequestContext::new("my_component")
23//! .with_operation("process_request");
24//!
25//! // Use the context's span for tracing
26//! let span = ctx.span();
27//! let _guard = span.enter();
28//! tracing::info!("Processing request");
29//! # Ok(())
30//! # }
31//! ```
32
33#![deny(unsafe_code)]
34#![deny(missing_docs)]
35#![deny(clippy::all)]
36#![deny(unreachable_pub)]
37#![deny(clippy::unwrap_used)]
38#![cfg_attr(test, allow(clippy::unwrap_used))]
39
40pub mod prelude;
41
42mod context;
43mod error;
44mod logging;
45
46pub use context::RequestContext;
47pub use error::{TelemetryError, TelemetryResult};
48pub use logging::{LogConfig, LogFormat, LogTarget, setup_logging};
49
50/// Convert an [`astrid_config::Config`] into a [`LogConfig`] for telemetry init.
51///
52/// Available when the `config` feature is enabled.
53#[cfg(feature = "config")]
54#[must_use]
55pub fn log_config_from(cfg: &astrid_config::Config) -> LogConfig {
56 let format = match cfg.logging.format.as_str() {
57 "pretty" => LogFormat::Pretty,
58 "json" => LogFormat::Json,
59 "full" => LogFormat::Full,
60 _ => LogFormat::Compact,
61 };
62 LogConfig {
63 level: cfg.logging.level.clone(),
64 format,
65 directives: cfg.logging.directives.clone(),
66 ..Default::default()
67 }
68}