Overview
@kubiks/otel-e2b provides comprehensive OpenTelemetry instrumentation for E2B (Code Interpreter). Add distributed tracing to your AI-powered code execution, sandbox operations, and code interpreter workflows with automatic instrumentation.

Visualize your E2B operations with detailed span information including code execution, sandbox lifecycle, and performance metrics.
Installation
Supported Frameworks
Works with any TypeScript framework and Node.js runtime:Next.js
App Router & Pages Router
Fastify
High-performance server
NestJS
Enterprise framework
Express
Classic Node.js server
Remix
Full-stack framework
SvelteKit
Modern web framework
Supported Platforms
Works with any observability platform that supports OpenTelemetry:Quick Start
UseE2BInstrumentation to add tracing to your E2B code interpreter:
Configuration Options
By default, code and output are captured in spans. You can disable this by setting the respective options to
false for sensitive environments.What You Get
Each E2B operation automatically creates a span with rich telemetry data:Span Information
Span Information
- Span name:
e2b.sandbox.create,e2b.notebook.execCell,e2b.filesystem.write, etc. - Operation type: Type of E2B operation (create, execute, read, write, etc.)
- Code content: The code being executed (configurable)
- Execution output: Results from code execution (configurable)
- Sandbox ID: Unique identifier for the sandbox
- Execution time: Duration of operations
Sandbox Lifecycle
Sandbox Lifecycle
- Sandbox creation and initialization
- Sandbox status changes
- Sandbox termination
- Resource allocation and usage
Code Execution
Code Execution
- Cell execution start and completion
- Code content and language
- Execution results (stdout, stderr, return values)
- Execution errors and stack traces
File Operations
File Operations
- File reads and writes
- File paths and sizes
- File system operations
Error Tracking
Error Tracking
- Exceptions are recorded with stack traces
- Proper span status (OK, ERROR)
- Error messages and codes
Span Attributes
The instrumentation adds the following attributes to each span:| Attribute | Description | Example |
|---|---|---|
e2b.operation | Type of operation | notebook.execCell |
e2b.sandbox.id | Sandbox identifier | sandbox-abc123 |
e2b.code | Code being executed | print("Hello") |
e2b.language | Programming language | python |
e2b.output | Execution output | Hello\n |
e2b.execution.status | Execution status | success |
Usage Examples
Basic Code Execution
File Operations
Streaming Execution
Complete Integration Example
Here’s a complete example of E2B with OpenTelemetry in a Next.js application:lib/e2b.ts
instrumentation.ts
app/api/execute/route.ts
Best Practices
Reuse Sandboxes When Possible
Reuse Sandboxes When Possible
Creating sandboxes is expensive. Reuse them for multiple operations:
Handle Timeouts
Handle Timeouts
Set appropriate timeouts for long-running code:
Clean Up Resources
Clean Up Resources
Always close sandboxes to avoid resource leaks:
Monitor Execution Costs
Monitor Execution Costs
Use traces to understand sandbox usage patterns and optimize costs.
Performance Considerations
Minimal Overhead
Minimal Overhead
The instrumentation adds minimal overhead for tracing operations.
Sampling
Sampling
Use OpenTelemetry sampling for high-volume applications:
Troubleshooting
Spans Not Appearing
Spans Not Appearing
Ensure OpenTelemetry is initialized before creating E2B sandboxes:
Code Content Not Captured
Code Content Not Captured
Check that
captureCodeContent is enabled:Sandbox Creation Failures
Sandbox Creation Failures
Verify your E2B API key is set correctly:
Resources
E2B Documentation
Learn more about E2B
GitHub Repository
View source code and examples
npm Package
View package on npm
Report Issues
Found a bug? Let us know!