Manual Instrumentation
This guide covers advanced manual span management techniques for TypeScript/JavaScript applications when you need fine-grained control over observability beyond the automaticwithActiveSpan method.
withActiveSpan Method
The recommended approach for most use cases with automatic context management and error handling.
Manual Span Control
Complete manual control over span lifecycle, attributes, and context propagation.
withActiveSpan Method
ThewithActiveSpan method is the recommended approach for most manual instrumentation needs. It automatically handles context propagation, error handling, and span cleanup, making it both safer and easier to use than manual span management. For consistent attribute naming, combine this with Semantic Conventions.
Basic Usage
Error Handling
withActiveSpan automatically handles errors and ensures proper span cleanup:
Context Propagation
withActiveSpan automatically propagates span context to child operations:
Custom Attributes and Events
Add rich metadata to your spans:Conditional Span Creation
Create spans conditionally based on your application logic:Basic Manual Span Management
When you need fine-grained control over spans beyond whatwithActiveSpan provides, you can manually manage span lifecycle, attributes, and context propagation.
Using startActiveSpan
startActiveSpan provides automatic context management but requires manual error handling:
Using startSpan (Complete Manual Control)
startSpan gives you complete control but requires manual context management:
Span Context Propagation
Manually propagate span context across async boundaries and service boundaries whenwithActiveSpan isn’t sufficient:
Error Handling Patterns
Implement robust error handling for manual span management:Custom Span Processors
Create custom span processors for specialized processing needs, filtering, and multiple export destinations.Custom Exporters
Configure custom exporters alongside LangWatch:Span Filtering
Implement span filtering to control which spans are processed:Multiple Exporters
Configure multiple exporters for different destinations:Batch Processing Configuration
Optimize batch processing for high-volume applications:Performance Considerations
When using manual span management, consider these performance implications:- Memory Usage: Manually created spans consume memory until explicitly ended
- Context Propagation: Manual context management can be error-prone and impact performance
- Error Handling: Ensure spans are always ended, even when exceptions occur
- Batch Processing: Use batch processors for high-volume applications to reduce overhead
- Sampling: Implement sampling to reduce overhead in production environments
Best Practices
Use withActiveSpan
- Prefer
withActiveSpanfor most use cases - Automatic context propagation and error handling
- Guaranteed span cleanup
Manual Control
- Use manual span management only when needed
- Always end spans in finally blocks
- Use try-catch-finally patterns consistently
Context Management
- Propagate span context across async boundaries
- Use context.with() for async operations
- Maintain span hierarchy properly
Attributes and Events
- Add meaningful custom attributes for filtering
- Use consistent attribute naming conventions
- Include relevant business context
Performance
- Implement appropriate sampling strategies
- Use batch processors for high volume
- Monitor observability overhead
Error Handling
- Set appropriate status codes and error messages
- Record exceptions with context
- Maintain error flow in your application
When to Use Each Approach
- withActiveSpan (Recommended)
- startActiveSpan
- startSpan (Manual)
Use
withActiveSpan for:- Most application logic
- Operations that need automatic context propagation
- When you want automatic error handling and cleanup
- Simple to moderate complexity operations
For most use cases, the
withActiveSpan method provides the best balance of ease of use, safety, and functionality. Only use manual span management when you need specific control over span lifecycle or context propagation that withActiveSpan cannot provide.Related Documentation
For more advanced observability patterns and best practices:- Integration Guide - Basic setup and core concepts
- API Reference - Complete API documentation
- Semantic Conventions - Standardized attribute naming guidelines
- Debugging and Troubleshooting - Debug manual instrumentation issues
- Framework Integrations - Framework-specific instrumentation approaches