opentelemetry-xray
opentelemetry-xray is a Rust library that provides a flexible interface for AWS X‑Ray distributed tracing using OpenTelemetry. It simplifies the integration of AWS X‑Ray with your applications by setting up a global tracer provider, structured logging via tracing, and graceful shutdown of the tracer.
Features
- AWS X‑Ray Integration:
- Format trace IDs according to AWS X‑Ray standards
- Automatic propagation of X-Ray trace context
- Compatible with AWS X-Ray console and analysis tools
- OpenTelemetry Support:
- Built on top of the robust OpenTelemetry ecosystem
- Standard span attributes and semantic conventions
- Extensible trace context propagation
- Structured Logging:
- Seamless integration with the
tracingecosystem - Enriched, structured logs with trace context
- Configurable log levels and filters
- Seamless integration with the
- Modular Design:
- Flexible initialization options
- Component-level control
- Customizable setup for different environments
Installation
Add opentelemetry-xray to your Cargo.toml:
[]
= "0.1.0"
= { = "0.27", = ["trace"] }
= "0.1"
= { = "0.3", = ["env-filter"] }
Usage
Quick Start
For most applications, use the default initialization:
use ;
let config = TelemetryConfig ;
// Initialize everything with default settings
let telemetry = init?;
// Your application logic here...
// Clean up on shutdown
telemetry.shutdown;
Modular Initialization
For more control, initialize components individually:
use ;
let config = TelemetryConfig ;
// Initialize components as needed
init_propagator; // Set up X-Ray context propagation
let provider = init_provider?; // Create tracer provider
init_subscriber?; // Set up tracing subscriber
// Create telemetry instance
let telemetry = new;
Span Creation
Create spans using the tracing macros for automatic context propagation:
use info_span;
let span = info_span!;
let _guard = span.enter;
// Operation is now traced with X-Ray
perform_work;
Context Propagation
Propagate trace context across service boundaries:
use ;
use HeaderMap;
// Inject trace context into outgoing requests
let mut headers = new;
inject_headers;
// Extract trace context from incoming requests
let parent_context = extract_headers;
let span = info_span!;
let _guard = span.enter;
Configuration Options
TelemetryConfig
let config = TelemetryConfig ;
Best Practices
-
Initialization:
- Use
Telemetry::init()for standard setups - Use modular initialization when you need custom configuration
- Always call
shutdown()during cleanup
- Use
-
Span Creation:
- Use
tracingmacros for automatic context propagation - Keep spans focused and well-named
- Ensure proper span cleanup
- Use
-
Error Handling:
- Handle initialization errors appropriately
- Validate configuration before use
- Use error propagation for debugging
Testing
Run the test suite:
License
This project is licensed under the MIT License - see the LICENSE file for details.
Related Projects
Support
For issues and feature requests, please use the GitHub issue tracker.