> ## Documentation Index
> Fetch the complete documentation index at: https://ppio.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 监控指标

> 查阅 PPIO Agent 沙箱监控指标的归档说明，了解该版本运行数据的使用方式，为观察既有任务与资源情况提供参考。

沙箱 “监控指标” 功能可以允许您获取有关沙箱 **vCPU 和内存使用情况** 的信息。

## 获取监控指标信息

指标信息以一个包含时间戳的数组形式提供（参考下方示例），包含 vCPU 和内存使用信息。指标在沙箱启动时开始采集，之后每 2 秒采集一次，在沙箱被删除前会采集最后一次。

> [!NOTE]
>
> 在沙箱创建后，可能需要等待几秒后才能获取到指标信息，在这之前，将会得到一个空数组。

### 使用 SDK 获取指标信息

```js JavaScript & TypeScript icon="js" theme={null}
import { Sandbox } from 'ppio-sandbox/code-interpreter'

const sandbox = await Sandbox.create()
console.log('Sandbox created', sandbox.sandboxId)

let metrics = await sandbox.getMetrics()

// 您也可以通过指定沙箱 ID 来获取指标信息
// const metrics = await Sandbox.getMetrics(sandboxId)

while (metrics && metrics.length <= 1) {
    console.log('Waiting for metrics...')
    await new Promise(resolve => setTimeout(resolve, 1000))
    metrics = await sandbox.getMetrics()
}

console.log('Sandbox metrics:', metrics)

await sandbox.kill()

// 输出示例：
// Sandbox metrics: [
//     {
//       cpuCount: 2,
//       cpuUsedPct: 15.92,
//       memTotalMiB: 987,
//       memUsedMiB: 245,
//       timestamp: '2025-06-22T06:54:28.234Z'
//     },
//     {
//       cpuCount: 2,
//       cpuUsedPct: 0.8,
//       memTotalMiB: 987,
//       memUsedMiB: 246,
//       timestamp: '2025-06-22T06:54:33.232Z'
//     }
//   ]
```

```python Python icon="python" theme={null}
from ppio_sandbox.code_interpreter import Sandbox
import time

sandbox = Sandbox.create()
print('Sandbox created', sandbox.sandbox_id)

metrics = sandbox.get_metrics()

# 您也可以通过指定沙箱 ID 来获取指标信息
# metrics = Sandbox.get_metrics(sbx.sandbox_id)

while len(metrics) <= 1:
    print('Waiting for metrics...')
    time.sleep(1)
    metrics = sandbox.get_metrics()

print('Sandbox metrics', metrics)

# 输出示例：
# Sandbox metrics [SandboxMetrics(timestamp=datetime.datetime(2025, 6, 22, 6, 59, 28, 595373, tzinfo=tzutc()), cpu_used_pct=15.94, cpu_count=2, mem_used_mib=245, mem_total_mib=987), SandboxMetrics(timestamp=datetime.datetime(2025, 6, 22, 6, 59, 33, 591684, tzinfo=tzutc()), cpu_used_pct=1.1, cpu_count=2, mem_used_mib=246, mem_total_mib=987)]

sandbox.kill()
```

### 使用 CLI 获取指标信息

```bash Bash icon="terminal" theme={null}
ppio-sandbox-cli sandbox metrics <sandbox_id>
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.