prima_tracing/lib.rs
1//! `prima_tracing` provide an handy way for configuring the [`tracing`] crate with
2//! support for JSON output formatter and integration with opentelemetry
3//! # Usage
4//!
5//! ```rust
6//!
7//! use prima_tracing::{builder, configure_subscriber, Country, Environment, init_subscriber};
8//! # #[cfg(not(feature = "traces"))]
9//! # {
10//! let subscriber = configure_subscriber(
11//! builder("ping")
12//! .with_country(Country::Common)
13//! .with_env(Environment::Dev)
14//! .build()
15//! );
16//!
17//! let _guard = init_subscriber(subscriber);
18//! # }
19//! ```
20
21mod config;
22
23mod subscriber;
24
25#[cfg(feature = "json-logger")]
26pub mod json;
27#[cfg(feature = "traces")]
28pub mod layer;
29#[cfg(feature = "traces")]
30pub mod resources;
31#[cfg(feature = "traces")]
32pub mod telemetry;
33
34pub use crate::config::{
35 builder, Country, Environment, EnvironmentParseError, SubscriberConfig, SubscriberConfigBuilder,
36};
37pub use crate::subscriber::{
38 configure_subscriber, init_subscriber, ContextInfo, EventFormatter, Tracing, Uninstall,
39};
40pub use tracing;
41
42/// Create a tracing error event, casting the error to &dyn [std::error::Error] for [layer::ErrorLayer],
43/// and adding the type name as error.kind.
44///
45/// Usage:
46/// ```
47/// use prima_tracing::report_error;
48///
49/// let error = "not a number".parse::<usize>().unwrap_err();
50/// report_error!(error, "Parsing error!");
51/// ```
52///
53/// You can also add use add attributes and do things, just like with a regular [tracing::error]
54/// macro call
55/// ```
56/// use prima_tracing::report_error;
57/// # let input = "not a number";
58/// # let uid = "1223";
59///
60/// let error = input.parse::<usize>().unwrap_err();
61/// report_error!(error, input, user=uid, "Parsing error: {}", error);
62/// ```
63#[macro_export]
64macro_rules! report_error {
65 ($error:expr, $($args:tt)*) => {
66 {
67 let kind = ::std::any::type_name_of_val(&$error);
68 $crate::tracing::error!(error.kind = kind, error = &$error as &dyn ::std::error::Error, $($args)+)
69 }
70 };
71}