tracing_otel_extra/
lib.rs

1//! # Tracing Extra
2//!
3//! This crate provides common utilities for initializing tracing and OpenTelemetry.
4//!
5//! ## Features
6//!
7//! The crate is organized into several feature flags:
8//!
9//! - `otel`: OpenTelemetry integration for distributed tracing
10//! - `logger`: Basic logging functionality with configurable formats
11//! - `env`: Environment-based logging configuration
12//! - `context`: Trace context utilities
13//! - `fields`: Common tracing fields and attributes
14//! - `http`: HTTP request/response tracing
15//! - `span`: Span creation and management utilities
16//!
17//! ## Examples
18//!
19//! Basic usage with configuration builder:
20//! ```rust,no_run,ignore
21//! use tracing_otel_extra::{Logger, LogFormat};
22//! use opentelemetry::KeyValue;
23//!
24//! #[tokio::main]
25//! async fn main() {
26//!     let _guard = Logger::new("my-service")
27//!         .with_format(LogFormat::Json)
28//!         .with_ansi(false)
29//!         .with_sample_ratio(0.1)
30//!         .with_attributes(vec![
31//!             KeyValue::new("environment", "production"),
32//!             KeyValue::new("version", "1.0.0"),
33//!         ])
34//!         .init()
35//!         .expect("Failed to initialize tracing");
36//!
37//!     // Your application code here
38//!
39//!     // Cleanup is handled automatically when the guard is dropped
40//! }
41//! ```
42//!
43//! Using environment-based configuration:
44//! ```rust,no_run,ignore
45//! use tracing_otel_extra::init_logging_from_env;
46//!
47//! #[tokio::main]
48//! async fn main() {
49//!     // Configure through environment variables:
50//!     // LOG_SERVICE_NAME=my-service
51//!     // LOG_FORMAT=json
52//!     // LOG_SAMPLE_RATIO=0.1
53//!     let _guard = init_logging_from_env(None)
54//!         .expect("Failed to initialize tracing from environment");
55//!
56//!     // Your application code here
57//! }
58//! ```
59//!
60
61// HTTP tracing modules
62#[cfg(any(
63    feature = "context",
64    feature = "fields",
65    feature = "http",
66    feature = "span",
67))]
68pub mod http;
69
70// OpenTelemetry integration
71#[cfg(feature = "otel")]
72pub mod otel {
73    pub use tracing_opentelemetry_extra::*;
74}
75
76// Logging functionality
77#[cfg(any(feature = "logger", feature = "env"))]
78pub mod logger;
79
80// Re-exports
81#[cfg(feature = "otel")]
82pub use otel::*;
83
84// Logger module exports
85#[cfg(feature = "logger")]
86pub use logger::{
87    FmtSpan, LogFormat, LogRollingRotation, Logger, LoggerFileAppender, init_logging,
88};
89
90// Logger module exports
91#[cfg(feature = "env")]
92pub use logger::{init_logger_from_env, init_logging_from_env};
93
94// Macros module exports
95#[cfg(feature = "macros")]
96pub mod macros;
97
98// Extra module exports (backward compatibility)
99pub mod extract {
100    #[cfg(feature = "context")]
101    pub use crate::http::context;
102
103    #[cfg(feature = "fields")]
104    pub use crate::http::fields;
105
106    #[cfg(feature = "http")]
107    pub use crate::http::propagation as http;
108
109    #[cfg(feature = "span")]
110    pub use crate::http::span;
111}