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
21#[cfg(feature = "traces")]
22#[macro_use]
23pub mod macros;
24
25mod config;
26mod subscriber;
27
28#[cfg(feature = "async-graphql")]
29pub mod async_graphql;
30#[cfg(feature = "json-logger")]
31pub mod json;
32#[cfg(feature = "traces")]
33pub mod layer;
34#[cfg(feature = "traces")]
35pub mod resources;
36#[cfg(feature = "traces")]
37pub mod telemetry;
38
39pub use crate::config::{
40 builder, Country, Environment, EnvironmentParseError, SubscriberConfig, SubscriberConfigBuilder,
41};
42pub use crate::subscriber::{
43 configure_subscriber, init_subscriber, ContextInfo, EventFormatter, Tracing, Uninstall,
44};
45pub use tracing;
46
47/// Create a tracing error event, casting the error to &dyn [std::error::Error] for [layer::ErrorLayer],
48/// and adding the type name as error.kind.
49///
50/// Usage:
51/// ```
52/// use prima_tracing::report_error;
53///
54/// let error = "not a number".parse::<usize>().unwrap_err();
55/// report_error!(error, "Parsing error!");
56/// ```
57///
58/// You can also add use add attributes and do things, just like with a regular [tracing::error]
59/// macro call
60/// ```
61/// use prima_tracing::report_error;
62/// # let input = "not a number";
63/// # let uid = "1223";
64///
65/// let error = input.parse::<usize>().unwrap_err();
66/// report_error!(error, input, user=uid, "Parsing error: {}", error);
67/// ```
68#[macro_export]
69macro_rules! report_error {
70 ($error:expr, $($args:tt)*) => {
71 {
72 let kind = ::std::any::type_name_of_val(&$error);
73 $crate::tracing::error!(error.kind = kind, error = &$error as &dyn ::std::error::Error, $($args)+)
74 }
75 };
76}