Skip to main content

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.
E2B Trace Visualization
Visualize your E2B operations with detailed span information including code execution, sandbox lifecycle, and performance metrics.

Installation

Peer Dependencies: @opentelemetry/api >= 1.9.0, @e2b/code-interpreter >= 0.1.0

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

Use E2BInstrumentation to add tracing to your E2B code interpreter:
This is the simplest approach—just add the instrumentation and all E2B operations are automatically traced!

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 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 creation and initialization
  • Sandbox status changes
  • Sandbox termination
  • Resource allocation and usage
  • Cell execution start and completion
  • Code content and language
  • Execution results (stdout, stderr, return values)
  • Execution errors and stack traces
  • File reads and writes
  • File paths and sizes
  • File system operations
  • 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:

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

Creating sandboxes is expensive. Reuse them for multiple operations:
Set appropriate timeouts for long-running code:
Always close sandboxes to avoid resource leaks:
Use traces to understand sandbox usage patterns and optimize costs.

Performance Considerations

The instrumentation adds minimal overhead for tracing operations.
Use OpenTelemetry sampling for high-volume applications:

Troubleshooting

Ensure OpenTelemetry is initialized before creating E2B sandboxes:
Check that captureCodeContent is enabled:
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!

License

MIT