Skip to main content

Manual Instrumentation

This guide covers advanced manual span management techniques for TypeScript/JavaScript applications when you need fine-grained control over observability beyond the automatic withActiveSpan 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

The withActiveSpan 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:
For consistent attribute naming and TypeScript autocomplete support, use semantic conventions. See our Semantic Conventions guide for best practices.

Conditional Span Creation

Create spans conditionally based on your application logic:

Basic Manual Span Management

When you need fine-grained control over spans beyond what withActiveSpan 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 when withActiveSpan 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:
Manual span management requires careful attention to memory usage and proper cleanup to avoid memory leaks.
  1. Memory Usage: Manually created spans consume memory until explicitly ended
  2. Context Propagation: Manual context management can be error-prone and impact performance
  3. Error Handling: Ensure spans are always ended, even when exceptions occur
  4. Batch Processing: Use batch processors for high-volume applications to reduce overhead
  5. Sampling: Implement sampling to reduce overhead in production environments

Best Practices

Use withActiveSpan

  • Prefer withActiveSpan for 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

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.
For more advanced observability patterns and best practices:
Combine manual instrumentation with Semantic Conventions for consistent, maintainable observability across your application.