1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//! A comprehensive OpenTelemetry telemetry library for Rust applications.
//!
//! This crate provides easy-to-use telemetry integration for Rust applications, with support for
//! OpenTelemetry tracing, metrics, and logging. It includes middleware for popular frameworks
//! like Axum and AWS Lambda, along with instrumentation helpers for outbound clients and
//! utilities for context propagation and configuration.
//!
//! # Features
//!
//! - OpenTelemetry tracing instrumentation
//! - Formatted logs with tracing metadata
//! - Context Propagation for incoming and outgoing HTTP requests
//! - Axum middleware to instrument http services
//! - Hyper connection instrumentation for outbound HTTP requests
//! - Legacy hyper client instrumentation for outbound HTTP requests
//! - Reqwest instrumentation for outbound HTTP requests
//! - AWS Lambda instrumentation layer
//! - AWS SDK instrumentation with automatic attribute extraction
//! - Integration testing tools
//!
//! # Available Feature Flags
//!
//! ## Core Features
//! - `axum`: Axum web framework middleware support
//! - `hyper`: Hyper connection instrumentation for outbound HTTP clients
//! - `hyper-http1`: Hyper HTTP/1 connection instrumentation
//! - `hyper-http2`: Hyper HTTP/2 connection instrumentation
//! - `hyper-client-legacy`: Hyper-util legacy client instrumentation
//! - `reqwest`: Reqwest instrumentation for outbound HTTP clients
//! - `rustls`: Enables rustls TLS backend for HTTP exporters
//! - `test`: Testing utilities for OpenTelemetry validation
//! - `zipkin`: Zipkin context propagation support (enabled by default)
//! - `xray`: AWS X-Ray context propagation support
//! - `future`: Future instrumentation utilities (mostly used internally)
//!
//! ## AWS Features
//! - `aws-span`: AWS SDK span creation utilities
//! - `aws-instrumentation`: Lightweight manual instrumentation for AWS SDK operations
//! - `aws-stream-instrumentation`: Instrumentation for AWS SDK pagination streams
//! - `aws-fluent-builder-instrumentation`: Core traits for fluent builders instrumentation (see [service-specific features](#aws-service-specific-features))
//! - `aws-lambda`: AWS Lambda runtime middleware
//!
//! ## AWS Service-Specific Features
//! - `aws-dynamodb`: DynamoDB automatic fluent builders instrumentation
//! - `aws-firehose`: Firehose automatic fluent builders instrumentation
//! - `aws-s3`: S3 automatic fluent builders instrumentation
//! - `aws-sns`: SNS automatic fluent builders instrumentation
//! - `aws-sqs`: SQS automatic fluent builders instrumentation
//! - `aws-sagemaker-runtime`: SageMaker Runtime automatic fluent builders instrumentation
//! - `aws-secretsmanager`: Secrets Manager automatic fluent builders instrumentation
//! - `aws-ssm`: SSM Parameter Store automatic fluent builders instrumentation
//! - `aws-appconfigdata`: AppConfig Data automatic fluent builders instrumentation
//!
//! ## Feature Bundles
//! - `aws`: All core AWS features (span + instrumentation + stream instrumentation)
//! - `aws-full`: All AWS features including Lambda, all service-specific instrumentations, and X-Ray propagation
//! - `full`: All features enabled
//!
//! # Quick Start
//!
//! ```rust
//! use telemetry_rust::{init_tracing, shutdown_tracer_provider};
//! use tracing::Level;
//!
//! // Initialize telemetry
//! let tracer_provider = init_tracing!(Level::INFO);
//!
//! // Your application code here...
//!
//! // Shutdown telemetry when done
//! shutdown_tracer_provider(&tracer_provider);
//! ```
// Initialization logic was retired from https://github.com/davidB/tracing-opentelemetry-instrumentation-sdk/
// which is licensed under CC0 1.0 Universal
// https://github.com/davidB/tracing-opentelemetry-instrumentation-sdk/blob/d3609ac2cc699d3a24fbf89754053cc8e938e3bf/LICENSE
use LevelFilter;
use FmtSpan;
use SubscriberExt;
use TracerProvider as _;
pub use ;
pub use ;
pub use attribute as semconv;
pub use ;
pub use tracing_opentelemetry_instrumentation_sdk;
/// Resource detection utility for automatically configuring OpenTelemetry service metadata.
///
/// This struct helps detect and configure service information from environment variables
/// with fallback values. It supports the standard OpenTelemetry environment variables
/// as well as common service naming conventions.
///
/// # Environment Variables
///
/// The following environment variables are checked in order of priority:
/// - Service name: `OTEL_SERVICE_NAME`, service.name from `OTEL_RESOURCE_ATTRIBUTES`, `SERVICE_NAME`, `APP_NAME`
/// - Service version: `OTEL_SERVICE_VERSION`, service.version from `OTEL_RESOURCE_ATTRIBUTES`, `SERVICE_VERSION`, `APP_VERSION`
///
/// Note: `OTEL_RESOURCE_ATTRIBUTES` is automatically parsed by the OpenTelemetry SDK's environment resource detector.
/// Initializes tracing with OpenTelemetry integration and fallback service information.
///
/// This function sets up a complete tracing infrastructure including:
/// - A temporary subscriber for setup logging
/// - Resource detection from environment variables with fallbacks
/// - OTLP tracer provider initialization
/// - Global propagator configuration
/// - Final subscriber with both console output and OpenTelemetry export
///
/// # Arguments
///
/// - `log_level`: The minimum log level for events
/// - `fallback_service_name`: Default service name if not found in environment variables
/// - `fallback_service_version`: Default service version if not found in environment variables
///
/// # Returns
///
/// A configured [`TracerProvider`] that should be kept alive for the duration of the application
/// and passed to [`shutdown_tracer_provider`] on shutdown.
///
/// # Examples
///
/// ```rust
/// use telemetry_rust::{init_tracing_with_fallbacks, shutdown_tracer_provider};
/// use tracing::Level;
///
/// let tracer_provider = init_tracing_with_fallbacks(Level::INFO, "my-service", "1.0.0");
///
/// // Your application code here...
///
/// shutdown_tracer_provider(&tracer_provider);
/// ```
///
/// # Panics
///
/// This function will panic if:
/// - The OTLP tracer provider cannot be initialized
/// - The text map propagator cannot be configured
/// Convenience macro for initializing tracing with package name and version as fallbacks.
///
/// This macro calls [`init_tracing_with_fallbacks`] using the current package's name and version
/// from `CARGO_PKG_NAME` and `CARGO_PKG_VERSION` environment variables as fallback values.
///
/// # Arguments
///
/// - `log_level`: The minimum log level for events (e.g., `Level::INFO`)
///
/// # Returns
///
/// A configured [`TracerProvider`] that should be kept alive for the duration of the application.
///
/// # Examples
///
/// ```rust
/// use telemetry_rust::{init_tracing, shutdown_tracer_provider};
/// use tracing::Level;
///
/// let tracer_provider = init_tracing!(Level::INFO);
///
/// // Your application code here...
///
/// shutdown_tracer_provider(&tracer_provider);
/// ```
/// Properly shuts down a tracer provider, flushing pending spans and cleaning up resources.
///
/// This function performs a graceful shutdown of the tracer provider by:
/// 1. Attempting to flush any pending spans to the exporter
/// 2. Shutting down the tracer provider and its associated resources
/// 3. Logging any errors that occur during the shutdown process
///
/// # Arguments
///
/// - `provider`: Reference to the [`TracerProvider`] to shut down
///
/// # Examples
///
/// ```rust
/// use telemetry_rust::{init_tracing, shutdown_tracer_provider};
/// use tracing::Level;
///
/// let tracer_provider = init_tracing!(Level::INFO);
///
/// // Your application code here...
///
/// shutdown_tracer_provider(&tracer_provider);
/// ```