revoke-trace
Distributed tracing module for the Revoke microservices framework, providing OpenTelemetry-based observability.
Features
- OpenTelemetry Native: Built on OpenTelemetry standards
- Multiple Exporters: Support for OTLP, Jaeger, Zipkin, and Prometheus
- Automatic Propagation: W3C Trace Context propagation
- Span Enrichment: Automatic span attributes and events
- Performance: Minimal overhead with sampling support
- Integration: Easy integration with popular frameworks
Installation
Add to your Cargo.toml:
[]
= { = "0.1.0" }
Quick Start
use ;
use ;
async
Configuration
Basic Configuration
use ;
let config = builder
.service_name
.service_version
.otlp_endpoint
.sample_rate // Sample 10% of traces
.build;
let tracer = config.init.await?;
Advanced Configuration
use ;
use KeyValue;
let config = builder
.service_name
.with_resource
.with_batch_config
.with_sampler
.build;
Usage Patterns
Manual Instrumentation
use global;
use ;
let tracer = tracer;
// Create span with options
let span = tracer
.span_builder
.with_kind
.with_attributes
.start;
// Set span as active
let _guard = span.enter;
// Add events
span.add_event;
// Set status
span.set_status;
Async Context Propagation
use ;
// Automatically propagate context
async
Error Handling
use SpanExt;
let span = tracer.start;
match do_operation.await
Framework Integration
Axum Integration
use ;
use ;
let app = new
.route
.layer;
async
Tonic (gRPC) Integration
use ;
use Server;
let service = default;
let traced_service = new;
builder
.add_service
.serve
.await?;
Database Tracing
use ;
// Trace individual queries
let result = trace_query.await?;
// Trace connection pool
let pool = new
.after_connect
.connect
.await?;
Sampling
Sampling Strategies
use ;
// Always sample
let sampler = AlwaysOn;
// Never sample
let sampler = AlwaysOff;
// Sample 10% of traces
let sampler = TraceIdRatio;
// Parent-based sampling
let sampler = ParentBased;
// Custom sampler
;
Span Enrichment
Automatic Enrichment
use ;
let enricher = new
.with_process_info // PID, executable name
.with_host_info // Hostname, IP
.with_runtime_info // Rust version, OS
.with_environment_info; // Environment variables
tracer.set_span_enricher;
Custom Enrichment
use SpanEnricher;
Metrics Integration
use ;
// Enable trace metrics
let metrics = new
.with_span_duration_histogram
.with_span_count_counter
.with_error_rate_gauge;
// Export to Prometheus
let exporter = prometheus
.with_endpoint
.build;
metrics.set_exporter;
Performance Optimization
Batch Processing
use ;
let batch_config = BatchConfig ;
let processor = new;
Span Limits
use SpanLimits;
let limits = builder
.with_max_attributes_per_span
.with_max_events_per_span
.with_max_links_per_span
.with_max_attribute_length
.build;
tracer.set_span_limits;
Context Propagation
HTTP Headers
use ;
use HeaderMap;
let propagator = new;
// Inject context into headers
let mut headers = new;
propagator.inject;
// Extract context from headers
let context = propagator.extract;
Cross-Service Propagation
use global;
// Set global propagator
set_text_map_propagator;
// In HTTP client
let client = new;
let mut request = client.get;
// Inject current context
get_text_map_propagator;
let response = request.send.await?;
Debugging and Testing
Trace Debugging
use ;
// Console exporter for development
let tracer = new
.with_console_exporter
.with_pretty_print
.build;
// In-memory exporter for testing
let = new
.with_memory_exporter
.build;
// Check captured spans
let spans = receiver.try_recv?;
assert_eq!;
assert_eq!;
Testing Utilities
use ;
Best Practices
- Span Naming: Use descriptive, consistent names (e.g.,
service.operation) - Attributes: Add relevant attributes but avoid sensitive data
- Sampling: Use appropriate sampling rates for production
- Context: Always propagate context across service boundaries
- Errors: Record errors with stack traces when available
- Performance: Use batch processing and sampling to minimize overhead
- Security: Never include passwords, tokens, or PII in traces
Examples
See the examples directory:
basic_tracing.rs- Simple tracing setuphttp_server.rs- HTTP server with tracinggrpc_service.rs- gRPC service with tracingcontext_propagation.rs- Cross-service context propagationcustom_exporter.rs- Custom trace exporter implementation