opentelemetry_configuration/
lib.rs

1//! Opinionated OpenTelemetry SDK configuration and lifecycle management.
2//!
3//! Wires together the OpenTelemetry SDK, OTLP exporters, and `tracing` into a
4//! cohesive configuration system with automatic lifecycle management.
5//!
6//! # Example
7//!
8//! ```no_run
9//! use opentelemetry_configuration::{OtelSdkBuilder, SdkError};
10//!
11//! fn main() -> Result<(), SdkError> {
12//!     let _guard = OtelSdkBuilder::new()
13//!         .service_name("my-service")
14//!         .build()?;
15//!
16//!     tracing::info!("Application running");
17//!     Ok(())
18//! }
19//! ```
20
21#![forbid(unsafe_code)]
22#![warn(missing_docs)]
23
24mod builder;
25mod config;
26mod error;
27mod guard;
28mod rust_detector;
29
30pub use builder::{OtelSdkBuilder, ResourceConfigBuilder};
31pub use config::{
32    BatchConfig, ComputeEnvironment, EndpointConfig, OtelSdkConfig, Protocol, ResourceConfig,
33    SignalConfig,
34};
35pub use error::SdkError;
36pub use guard::OtelGuard;
37pub use rust_detector::{RustBuildInfo, RustResourceDetector, emit_rustc_env};
38
39/// Re-exported for version compatibility with this crate's dependencies.
40pub use opentelemetry;
41/// Re-exported for version compatibility with this crate's dependencies.
42pub use opentelemetry_sdk;
43/// Re-exported for version compatibility with this crate's dependencies.
44pub use tracing;
45/// Re-exported for trace context propagation (e.g., `OpenTelemetrySpanExt::set_parent`).
46pub use tracing_opentelemetry;
47
48/// Re-exported for users who want to construct custom configuration providers.
49pub use figment;
50
51/// Captures Rust build-time information as resource attributes.
52///
53/// This macro reads environment variables set by [`emit_rustc_env`] in build.rs.
54/// Use it with [`OtelSdkBuilder::with_rust_build_info`] to add rustc version
55/// and channel information to your telemetry resource attributes.
56///
57/// # Example
58///
59/// In build.rs:
60///
61/// ```
62/// opentelemetry_configuration::emit_rustc_env();
63/// ```
64///
65/// In main.rs:
66///
67/// ```no_run
68/// # fn main() -> Result<(), opentelemetry_configuration::SdkError> {
69/// use opentelemetry_configuration::{OtelSdkBuilder, capture_rust_build_info};
70///
71/// let _guard = OtelSdkBuilder::new()
72///     .service_name("my-service")
73///     .with_rust_build_info(capture_rust_build_info!())
74///     .build()?;
75/// # Ok(())
76/// # }
77/// ```
78///
79/// # Attributes Added
80///
81/// When combined with [`emit_rustc_env`] in build.rs, this adds:
82/// - `process.runtime.version` - rustc version (e.g., "1.84.0")
83/// - `process.runtime.description` - full version string
84/// - `rust.channel` - release channel ("stable", "beta", or "nightly")
85#[macro_export]
86macro_rules! capture_rust_build_info {
87    () => {
88        $crate::RustBuildInfo {
89            rustc_version: option_env!("RUSTC_VERSION"),
90            rust_channel: option_env!("RUST_CHANNEL"),
91            rustc_version_full: option_env!("RUSTC_VERSION_FULL"),
92        }
93    };
94}