Skip to main content

miden_node_tracing/
lib.rs

1//! Miden node tracing conventions and OpenTelemetry integration.
2
3extern crate self as miden_node_tracing;
4
5crate::declare_targets! {
6    default: "miden::process",
7    "miden_node_tracing::admin" => "miden::admin",
8    "miden_node_tracing::filter_control" => "miden::admin",
9    "miden_node_tracing::grpc" => "miden::rpc::server",
10}
11
12pub mod admin;
13mod attribute;
14mod filter_control;
15pub mod grpc;
16mod instrument;
17mod logging;
18pub mod panic;
19mod span_ext;
20pub mod spawn;
21mod targets;
22
23#[doc(hidden)]
24pub use attribute::field_name_allowed;
25pub use attribute::{RecordAttribute, record_attribute};
26#[cfg(feature = "testing")]
27pub use logging::setup_test_tracing;
28pub use logging::{
29    OpenTelemetry,
30    OtelGuard,
31    ResourceConfig,
32    TracingConfig,
33    setup_tracing,
34    setup_tracing_with_config,
35};
36#[doc(hidden)]
37pub use miden_node_tracing_macro::__with_tracing_target;
38pub use miden_node_tracing_macro::{
39    debug,
40    debug_span,
41    enabled,
42    error,
43    error_span,
44    info,
45    info_span,
46    instrument,
47    miden_span_record,
48    trace,
49    trace_span,
50    warn,
51    warn_span,
52};
53pub use span_ext::ErrorSpanExt;
54pub use targets::{TargetMap, declared_targets};
55// Used directly by applications and by expansions of `tracing::instrument`.
56pub use tracing::{Instrument, Level, Span, Value, field};
57/// Upstream `tracing` exports required by generated macro code.
58#[doc(hidden)]
59pub use tracing::{event, if_log_enabled, level_enabled, span};
60
61/// Upstream attribute and event macros used by this crate's proc-macro expansions.
62#[doc(hidden)]
63pub mod __private {
64    pub use inventory;
65    pub use tracing::{
66        debug,
67        debug_span,
68        enabled,
69        error,
70        error_span,
71        info,
72        info_span,
73        instrument,
74        trace,
75        trace_span,
76        warn,
77        warn_span,
78    };
79
80    pub use crate::instrument::AsDynError;
81    pub use crate::targets::DeclaredTargetMap;
82}
83
84/// Extends errors with a stable string representation of their source chain.
85pub trait ErrorReport: std::error::Error {
86    /// Returns a string representation of the error and its source chain.
87    fn as_report(&self) -> String {
88        use std::fmt::Write;
89        let mut report = self.to_string();
90
91        std::iter::successors(self.source(), |child| child.source())
92            .for_each(|source| write!(report, "\ncaused by: {source}").unwrap());
93
94        report
95    }
96
97    /// Creates a new root in the error chain and returns the complete error report.
98    fn as_report_context(&self, context: &'static str) -> String {
99        format!("{context}: \ncaused by: {}", self.as_report())
100    }
101}
102
103impl<T: std::error::Error + ?Sized> ErrorReport for T {}
104
105#[cfg(test)]
106mod tests {
107    use super::ErrorReport;
108
109    #[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
110    enum TestSourceError {
111        #[error("source error")]
112        Source,
113    }
114
115    #[derive(thiserror::Error, Debug)]
116    enum TestError {
117        #[error("parent error")]
118        Parent(#[from] TestSourceError),
119    }
120
121    #[test]
122    fn as_report() {
123        let error = TestError::Parent(TestSourceError::Source);
124        assert_eq!("parent error\ncaused by: source error", error.as_report());
125    }
126
127    #[test]
128    fn as_report_context() {
129        let error = TestError::Parent(TestSourceError::Source);
130        assert_eq!(
131            "final error: \ncaused by: parent error\ncaused by: source error",
132            error.as_report_context("final error")
133        );
134    }
135}