telemetry_rust/lib.rs
1#![warn(missing_docs, clippy::missing_panics_doc)]
2
3//! A comprehensive OpenTelemetry telemetry library for Rust applications.
4//!
5//! This crate provides easy-to-use telemetry integration for Rust applications, with support for
6//! OpenTelemetry tracing, metrics, and logging. It includes middleware for popular frameworks
7//! like Axum and AWS Lambda, along with utilities for context propagation and configuration.
8//!
9//! # Features
10//!
11//! - OpenTelemetry tracing instrumentation
12//! - Formatted logs with tracing metadata
13//! - Context Propagation for incoming and outgoing HTTP requests
14//! - Axum middleware to instrument http services
15//! - AWS Lambda instrumentation layer
16//! - AWS SDK instrumentation with automatic attribute extraction
17//! - Integration testing tools
18//!
19//! # Available Feature Flags
20//!
21//! ## Core Features
22//! - `axum`: Axum web framework middleware support
23//! - `test`: Testing utilities for OpenTelemetry validation
24//! - `zipkin`: Zipkin context propagation support (enabled by default)
25//! - `xray`: AWS X-Ray context propagation support
26//! - `future`: Future instrumentation utilities (mostly used internally)
27//!
28//! ## AWS Features
29//! - `aws-span`: AWS SDK span creation utilities
30//! - `aws-instrumentation`: Lightweight manual instrumentation for AWS SDK operations
31//! - `aws-stream-instrumentation`: Instrumentation for AWS SDK pagination streams
32//! - `aws-fluent-builder-instrumentation`: Core traits for fluent builders instrumentation (see [service-specific features](#aws-service-specific-features))
33//! - `aws-lambda`: AWS Lambda runtime middleware
34//!
35//! ## AWS Service-Specific Features
36//! - `aws-dynamodb`: DynamoDB automatic fluent builders instrumentation
37//! - `aws-firehose`: Firehose automatic fluent builders instrumentation
38//! - `aws-sns`: SNS automatic fluent builders instrumentation
39//! - `aws-sqs`: SQS automatic fluent builders instrumentation
40//!
41//! ## Feature Bundles
42//! - `aws`: All core AWS features (span + instrumentation + stream instrumentation)
43//! - `aws-full`: All AWS features including Lambda, all service-specific instrumentations, and X-Ray propagation
44//! - `full`: All features enabled
45//!
46//! # Quick Start
47//!
48//! ```rust
49//! use telemetry_rust::{init_tracing, shutdown_tracer_provider};
50//! use tracing::Level;
51//!
52//! // Initialize telemetry
53//! let tracer_provider = init_tracing!(Level::INFO);
54//!
55//! // Your application code here...
56//!
57//! // Shutdown telemetry when done
58//! shutdown_tracer_provider(&tracer_provider);
59//! ```
60
61// Initialization logic was retired from https://github.com/davidB/tracing-opentelemetry-instrumentation-sdk/
62// which is licensed under CC0 1.0 Universal
63// https://github.com/davidB/tracing-opentelemetry-instrumentation-sdk/blob/d3609ac2cc699d3a24fbf89754053cc8e938e3bf/LICENSE
64
65use tracing::level_filters::LevelFilter;
66#[cfg(debug_assertions)]
67use tracing_subscriber::fmt::format::FmtSpan;
68use tracing_subscriber::layer::SubscriberExt;
69
70use opentelemetry::trace::TracerProvider as _;
71pub use opentelemetry::{Array, Context, Key, KeyValue, StringValue, Value, global};
72pub use opentelemetry_sdk::{
73 Resource,
74 error::OTelSdkError,
75 resource::{EnvResourceDetector, ResourceDetector, TelemetryResourceDetector},
76 trace::SdkTracerProvider as TracerProvider,
77};
78pub use opentelemetry_semantic_conventions::attribute as semconv;
79pub use tracing_opentelemetry::{OpenTelemetryLayer, OpenTelemetrySpanExt};
80
81pub mod fmt;
82pub mod http;
83pub mod middleware;
84pub mod otlp;
85pub mod propagation;
86
87#[cfg(feature = "axum")]
88pub use tracing_opentelemetry_instrumentation_sdk;
89
90#[cfg(feature = "test")]
91pub mod test;
92
93#[cfg(feature = "future")]
94pub mod future;
95
96mod filter;
97mod util;
98
99/// Resource detection utility for automatically configuring OpenTelemetry service metadata.
100///
101/// This struct helps detect and configure service information from environment variables
102/// with fallback values. It supports the standard OpenTelemetry environment variables
103/// as well as common service naming conventions.
104///
105/// # Environment Variables
106///
107/// The following environment variables are checked in order of priority:
108/// - Service name: `OTEL_SERVICE_NAME`, service.name from `OTEL_RESOURCE_ATTRIBUTES`, `SERVICE_NAME`, `APP_NAME`
109/// - Service version: `OTEL_SERVICE_VERSION`, service.version from `OTEL_RESOURCE_ATTRIBUTES`, `SERVICE_VERSION`, `APP_VERSION`
110///
111/// Note: `OTEL_RESOURCE_ATTRIBUTES` is automatically parsed by the OpenTelemetry SDK's environment resource detector.
112#[derive(Debug, Default)]
113pub struct DetectResource {
114 fallback_service_name: &'static str,
115 fallback_service_version: &'static str,
116}
117
118impl DetectResource {
119 /// Creates a new `DetectResource` with the provided fallback service name and version.
120 ///
121 /// # Arguments
122 ///
123 /// * `fallback_service_name` - The default service name to use if not found in environment variables.
124 /// * `fallback_service_version` - The default service version to use if not found in environment variables.
125 pub fn new(
126 fallback_service_name: &'static str,
127 fallback_service_version: &'static str,
128 ) -> Self {
129 DetectResource {
130 fallback_service_name,
131 fallback_service_version,
132 }
133 }
134
135 /// Builds the OpenTelemetry resource with detected service information.
136 ///
137 /// This method checks environment variables in order of priority and falls back
138 /// to the provided default values if no environment variables are set.
139 ///
140 /// # Returns
141 ///
142 /// A configured [`Resource`] with service name and version attributes.
143 pub fn build(self) -> Resource {
144 let env_detector = EnvResourceDetector::new();
145 let env_resource = env_detector.detect();
146
147 let read_from_env = |key| util::env_var(key).map(Into::into);
148
149 let service_name_key = Key::new(semconv::SERVICE_NAME);
150 let service_name_value = read_from_env("OTEL_SERVICE_NAME")
151 .or_else(|| env_resource.get(&service_name_key))
152 .or_else(|| read_from_env("SERVICE_NAME"))
153 .or_else(|| read_from_env("APP_NAME"))
154 .unwrap_or_else(|| self.fallback_service_name.into());
155
156 let service_version_key = Key::new(semconv::SERVICE_VERSION);
157 let service_version_value = read_from_env("OTEL_SERVICE_VERSION")
158 .or_else(|| env_resource.get(&service_version_key))
159 .or_else(|| read_from_env("SERVICE_VERSION"))
160 .or_else(|| read_from_env("APP_VERSION"))
161 .unwrap_or_else(|| self.fallback_service_version.into());
162
163 let resource = Resource::builder_empty()
164 .with_detectors(&[
165 Box::new(TelemetryResourceDetector),
166 Box::new(env_detector),
167 ])
168 .with_attributes([
169 KeyValue::new(service_name_key, service_name_value),
170 KeyValue::new(service_version_key, service_version_value),
171 ])
172 .build();
173
174 // Debug
175 resource.iter().for_each(
176 |kv| tracing::debug!(target: "otel::setup::resource", key = %kv.0, value = %kv.1),
177 );
178
179 resource
180 }
181}
182
183macro_rules! fmt_layer {
184 () => {{
185 let layer = tracing_subscriber::fmt::layer();
186
187 #[cfg(debug_assertions)]
188 let layer = layer.compact().with_span_events(FmtSpan::CLOSE);
189 #[cfg(not(debug_assertions))]
190 let layer = layer.json().event_format(fmt::JsonFormat);
191
192 layer.with_writer(std::io::stdout)
193 }};
194}
195
196/// Initializes tracing with OpenTelemetry integration and fallback service information.
197///
198/// This function sets up a complete tracing infrastructure including:
199/// - A temporary subscriber for setup logging
200/// - Resource detection from environment variables with fallbacks
201/// - OTLP tracer provider initialization
202/// - Global propagator configuration
203/// - Final subscriber with both console output and OpenTelemetry export
204///
205/// # Arguments
206///
207/// - `log_level`: The minimum log level for events
208/// - `fallback_service_name`: Default service name if not found in environment variables
209/// - `fallback_service_version`: Default service version if not found in environment variables
210///
211/// # Returns
212///
213/// A configured [`TracerProvider`] that should be kept alive for the duration of the application
214/// and passed to [`shutdown_tracer_provider`] on shutdown.
215///
216/// # Examples
217///
218/// ```rust
219/// use telemetry_rust::{init_tracing_with_fallbacks, shutdown_tracer_provider};
220/// use tracing::Level;
221///
222/// let tracer_provider = init_tracing_with_fallbacks(Level::INFO, "my-service", "1.0.0");
223///
224/// // Your application code here...
225///
226/// shutdown_tracer_provider(&tracer_provider);
227/// ```
228///
229/// # Panics
230///
231/// This function will panic if:
232/// - The OTLP tracer provider cannot be initialized
233/// - The text map propagator cannot be configured
234pub fn init_tracing_with_fallbacks(
235 log_level: tracing::Level,
236 fallback_service_name: &'static str,
237 fallback_service_version: &'static str,
238) -> TracerProvider {
239 // set to debug to log detected resources, configuration read and infered
240 let setup_subscriber = tracing_subscriber::registry()
241 .with(Into::<LevelFilter>::into(log_level))
242 .with(fmt_layer!());
243 let _guard = tracing::subscriber::set_default(setup_subscriber);
244 tracing::info!("init logging & tracing");
245
246 let otel_rsrc =
247 DetectResource::new(fallback_service_name, fallback_service_version).build();
248 let tracer_provider =
249 otlp::init_tracer(otel_rsrc, otlp::identity).expect("TracerProvider setup");
250
251 global::set_tracer_provider(tracer_provider.clone());
252 global::set_text_map_propagator(
253 propagation::TextMapSplitPropagator::from_env().expect("TextMapPropagator setup"),
254 );
255
256 let otel_layer =
257 OpenTelemetryLayer::new(tracer_provider.tracer(env!("CARGO_PKG_NAME")));
258 let subscriber = tracing_subscriber::registry()
259 .with(Into::<filter::TracingFilter>::into(log_level))
260 .with(fmt_layer!())
261 .with(otel_layer);
262 tracing::subscriber::set_global_default(subscriber).unwrap();
263
264 tracer_provider
265}
266
267/// Convenience macro for initializing tracing with package name and version as fallbacks.
268///
269/// This macro calls [`init_tracing_with_fallbacks`] using the current package's name and version
270/// from `CARGO_PKG_NAME` and `CARGO_PKG_VERSION` environment variables as fallback values.
271///
272/// # Arguments
273///
274/// - `log_level`: The minimum log level for events (e.g., `Level::INFO`)
275///
276/// # Returns
277///
278/// A configured [`TracerProvider`] that should be kept alive for the duration of the application.
279///
280/// # Examples
281///
282/// ```rust
283/// use telemetry_rust::{init_tracing, shutdown_tracer_provider};
284/// use tracing::Level;
285///
286/// let tracer_provider = init_tracing!(Level::INFO);
287///
288/// // Your application code here...
289///
290/// shutdown_tracer_provider(&tracer_provider);
291/// ```
292#[macro_export]
293macro_rules! init_tracing {
294 ($log_level:expr) => {
295 $crate::init_tracing_with_fallbacks(
296 $log_level,
297 env!("CARGO_PKG_NAME"),
298 env!("CARGO_PKG_VERSION"),
299 )
300 };
301}
302
303/// Properly shuts down a tracer provider, flushing pending spans and cleaning up resources.
304///
305/// This function performs a graceful shutdown of the tracer provider by:
306/// 1. Attempting to flush any pending spans to the exporter
307/// 2. Shutting down the tracer provider and its associated resources
308/// 3. Logging any errors that occur during the shutdown process
309///
310/// # Arguments
311///
312/// - `provider`: Reference to the [`TracerProvider`] to shut down
313///
314/// # Examples
315///
316/// ```rust
317/// use telemetry_rust::{init_tracing, shutdown_tracer_provider};
318/// use tracing::Level;
319///
320/// let tracer_provider = init_tracing!(Level::INFO);
321///
322/// // Your application code here...
323///
324/// shutdown_tracer_provider(&tracer_provider);
325/// ```
326#[inline]
327pub fn shutdown_tracer_provider(provider: &TracerProvider) {
328 if let Err(err) = provider.force_flush() {
329 tracing::warn!(?err, "failed to flush tracer provider");
330 }
331 if let Err(err) = provider.shutdown() {
332 tracing::warn!(?err, "failed to shutdown tracer provider");
333 } else {
334 tracing::info!("tracer provider is shutdown")
335 }
336}