Skip to main content
Monitor CPU, memory, and disk usage of your sandboxes using the metrics API.

Getting Metrics

from moru import Sandbox

sandbox = Sandbox.create()

# Run some work
sandbox.commands.run("python3 -c 'x = list(range(10000000))'")

# Get current metrics
metrics = sandbox.get_metrics()

for m in metrics:
    print(f"Time: {m.timestamp}")
    print(f"CPU: {m.cpu_used_pct:.1f}% of {m.cpu_count} cores")
    print(f"Memory: {m.mem_used / 1024 / 1024:.1f} MB / {m.mem_total / 1024 / 1024:.1f} MB")
    print(f"Disk: {m.disk_used / 1024 / 1024:.1f} MB / {m.disk_total / 1024 / 1024:.1f} MB")

Metrics Fields

FieldTypeDescription
timestampdatetimeWhen metrics were collected
cpu_count / cpuCountnumberNumber of vCPUs
cpu_used_pct / cpuUsedPctnumberCPU usage percentage
mem_total / memTotalnumberTotal memory in bytes
mem_used / memUsednumberUsed memory in bytes
disk_total / diskTotalnumberTotal disk space in bytes
disk_used / diskUsednumberUsed disk space in bytes

Time Range Queries

Query metrics for a specific time range:
from datetime import datetime, timedelta

sandbox = Sandbox.create()

# Get metrics from last 5 minutes
end = datetime.now()
start = end - timedelta(minutes=5)

metrics = sandbox.get_metrics(start=start, end=end)

for m in metrics:
    print(f"{m.timestamp}: CPU {m.cpu_used_pct:.1f}%")

CLI Metrics

View metrics using the CLI:
# Show current metrics
moru sandbox metrics sbx_abc123

# Stream metrics in real-time
moru sandbox metrics sbx_abc123 --follow

# Output as JSON
moru sandbox metrics sbx_abc123 --format json
Example output:
CPU:    15.2% (2 cores)
Memory: 256 MB / 512 MB
Disk:   1.2 GB / 10 GB

Dashboard Visualization

The Moru Dashboard provides graphical visualization of metrics:
  1. Navigate to moru.io/dashboard
  2. Go to Sandboxes
  3. Click on your sandbox
  4. View the Metrics tab
The dashboard shows:
  • CPU usage over time
  • Memory usage trends
  • Disk usage history
  • Resource limit warnings

Monitoring Example

Monitor a sandbox and alert on high usage:
import time
from moru import Sandbox

sandbox = Sandbox.create()

# Start a workload
sandbox.commands.run("stress --cpu 2 --timeout 30", background=True)

# Monitor metrics
for _ in range(6):
    metrics = sandbox.get_metrics()
    if metrics:
        m = metrics[-1]  # Latest metrics
        cpu_pct = m.cpu_used_pct
        mem_pct = (m.mem_used / m.mem_total) * 100

        print(f"CPU: {cpu_pct:.1f}%, Memory: {mem_pct:.1f}%")

        if cpu_pct > 80:
            print("WARNING: High CPU usage!")
        if mem_pct > 90:
            print("WARNING: High memory usage!")

    time.sleep(5)

sandbox.kill()

Metrics Retention

Metrics are collected every 30 seconds and retained for 24 hours. Need longer retention? Contact us at hi@moru.io.

Next Steps