This document describes the comprehensive end-to-end tracing implementation for data packets as they move from external API providers, through the relayers, to on-chain submission.
The tracing system provides full visibility into the data flow through the StellarFlow backend, enabling:
- Request Flow Tracking: Trace data packets from API ingestion to on-chain submission
- Performance Monitoring: Identify bottlenecks and latency issues
- Error Tracking: Pinpoint where failures occur in the pipeline
- Distributed Tracing: Correlate requests across multiple services
-
Tracing Library (
src/lib/tracing.ts)- Custom OpenTelemetry-compatible tracing implementation
- W3C trace context support
- Multiple exporters (Console, Jaeger, Honeycomb)
-
Middleware (
src/middleware/tracingMiddleware.ts)- Express middleware for automatic request tracing
- Axios interceptors for HTTP request propagation
- Child span creation utilities
-
Configuration (
src/config/tracingConfig.ts)- Environment-based configuration
- Exporter setup and validation
- Service initialization
-
Service Integration (
src/services/tracingService.ts)- Business operation tracing helpers
- Decorators for automatic function tracing
- Custom span creation for specific operations
External API → Relayer → Express App → Tracing Middleware → Business Logic
- Traceparent header extraction from incoming requests
- Root span creation with HTTP metadata
- Request context propagation
Business Logic → External API Call → Response Processing → Validation
- Child span creation for API requests
- Automatic traceparent header injection
- Response and error tracking
Validation → Multi-sig Operations → Signature Collection → Submission
- Relayer-specific span creation
- Authorization and validation tracking
- Multi-sig operation tracing
Stellar Network → Transaction Submission → Confirmation → Recording
- Stellar transaction span creation
- Network interaction tracking
- Success/failure monitoring
# Enable/disable tracing
TRACING_ENABLED=true
# Service identification
TRACING_SERVICE_NAME=stellarflow-backend
# Console exporter (development)
TRACING_CONSOLE_EXPORTER=true
# Jaeger exporter
TRACING_JAEGER_ENDPOINT=http://localhost:14268/api/traces
# Honeycomb exporter
TRACING_HONEYCOMB_ENDPOINT=https://api.honeycomb.io/v1/events
TRACING_HONEYCOMB_API_KEY=your_api_key
TRACING_HONEYCOMB_DATASET=stellarflow
# Export configuration
TRACING_EXPORT_INTERVAL_MS=5000
TRACING_SAMPLING_RATE=1.0- Outputs traces to console for local development
- Always enabled by default in development mode
- Sends traces to Jaeger collector
- Requires Jaeger instance running
- Recommended for on-premises deployments
- Sends traces to Honeycomb.io
- Requires API key and dataset configuration
- Recommended for cloud deployments
The system uses W3C trace context headers:
traceparent: 00-traceId-parentSpanId-traceFlags
tracestate: key1=value1,key2=value2
- HTTP Spans: Track incoming/outgoing HTTP requests
- Business Spans: Track application logic operations
- Database Spans: Track database operations
- External API Spans: Track third-party API calls
- Blockchain Spans: Track Stellar network interactions
- Relayer Request Processing: From receipt to validation
- API Provider Requests: External data fetching
- Price Validation: Data quality checks
- Multi-sig Operations: Signature collection and validation
- On-Chain Submission: Stellar transaction submission
- Database Operations: Read/write operations
- Cache Operations: Redis interactions
- Webhook Deliveries: External notifications
import { TracingService } from '../services/tracingService';
// Create child span for custom operation
const span = TracingService.traceRelayerRequest(req, 'relayer-name', 'price-update');
try {
// Business logic here
TracingService.addLog(span, 'info', 'Processing complete');
} catch (error) {
TracingService.finishSpan(span, error);
throw error;
} finally {
TracingService.finishSpan(span);
}import { withTracing } from '../services/tracingService';
class PriceService {
@withTracing('price_validation', { 'operation.type': 'validation' })
async validatePrice(currency: string, rate: number) {
// Function automatically traced
}
}import { executeWithTrace } from '../services/tracingService';
const result = await executeWithTrace(
'api_request.fetch_rates',
async () => {
return await axios.get('https://api.example.com/rates');
},
{ 'api.provider': 'example' }
);- Request Flow: Follow trace ID through the system
- Latency Analysis: Identify slow operations
- Error Correlation: Find root cause of failures
- Service Dependencies: Understand service interactions
- Request duration by endpoint
- API provider response times
- Database operation latency
- Cache hit/miss ratios
- On-chain submission times
- Error rates by operation type
- Configurable sampling rate to reduce overhead
- Default: 100% sampling for development
- Production: Recommended 10-20% sampling
- Traces exported in batches every 5 seconds
- Configurable via
TRACING_EXPORT_INTERVAL_MS - Graceful shutdown ensures all traces exported
- Active spans tracked in memory
- Automatic cleanup on span completion
- Configurable retention policies
- Automatic log masking applied to trace data
- Configurable field redaction
- PII filtering in headers and payloads
- Secure connections to trace collectors
- API key protection for cloud services
- Network access control for Jaeger
- Missing Traces: Check
TRACING_ENABLEDsetting - Export Failures: Verify collector endpoints and credentials
- High Memory: Reduce sampling rate or export interval
- Missing Context: Ensure middleware order is correct
Enable console logging for debugging:
TRACING_CONSOLE_EXPORTER=true
TRACING_ENABLED=trueMonitor tracing system health:
- Check exporter connectivity
- Monitor export queue sizes
- Validate trace context propagation
The current implementation is compatible with OpenTelemetry standards and can be easily migrated:
// Future migration to OpenTelemetry SDK
import { NodeSDK } from '@opentelemetry/sdk-node';
const sdk = new NodeSDK({
serviceName: process.env.TRACING_SERVICE_NAME,
traceExporter: new JaegerExporter({...})
});
sdk.start();The tracing system supports custom extensions:
- Custom Exporters: Implement
TraceExporterinterface - Custom Decorators: Extend tracing decorators
- Custom Middleware: Add domain-specific tracing
- Early Instrumentation: Add tracing at application entry points
- Consistent Naming: Use standardized span names and tags
- Error Handling: Always finish spans, even on errors
- Performance: Monitor tracing overhead and adjust sampling
- Security: Review trace data for sensitive information
- OpenTelemetry SDK Integration: Full OpenTelemetry compatibility
- Automatic Instrumentation: Code-based auto-instrumentation
- Advanced Sampling: Adaptive sampling algorithms
- Trace Aggregation: Real-time trace analytics
- Alerting Integration: Trace-based alerting rules
This implementation provides comprehensive end-to-end tracing for the StellarFlow backend, enabling full visibility into data packet flow from external APIs through relayers to on-chain submission.