Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

fix(js-sdk): pass getMetrics start/end as query params (#1427)

Mish Ushakovcab59caa0b93
diff --git a/.changeset/brave-pandas-listen.md b/.changeset/brave-pandas-listen.md
new file mode 100644
index 000000000..62cf50f63
--- /dev/null
+++ b/.changeset/brave-pandas-listen.md
@@ -0,0 +1,5 @@
+---
+"e2b": patch
+---
+
+Fix `Sandbox.getMetrics()` sending `start` and `end` as path parameters instead of query parameters, which caused the requested time range to be silently ignored
diff --git a/packages/js-sdk/src/sandbox/sandboxApi.ts b/packages/js-sdk/src/sandbox/sandboxApi.ts
index 8529de2ae..007a936cf 100644
--- a/packages/js-sdk/src/sandbox/sandboxApi.ts
+++ b/packages/js-sdk/src/sandbox/sandboxApi.ts
@@ -798,6 +798,8 @@ export class SandboxApi {
params: {
path: {
sandboxID: sandboxId,
+ },
+ query: {
start,
end,
},
diff --git a/packages/js-sdk/tests/sandbox/metrics.test.ts b/packages/js-sdk/tests/sandbox/metrics.test.ts
index e3f125ff2..d7dbca551 100644
--- a/packages/js-sdk/tests/sandbox/metrics.test.ts
+++ b/packages/js-sdk/tests/sandbox/metrics.test.ts
@@ -7,8 +7,8 @@ sandboxTest.skipIf(isDebug)(
'sbx metrics',
{ timeout: 60_000 },
async ({ sandbox }) => {
- const now = new Date()
- let metrics: SandboxMetrics[]
+ // Wait for the sandbox to have some metrics
+ let metrics: SandboxMetrics[] = []
for (let i = 0; i < 60; i++) {
metrics = await sandbox.getMetrics()
if (metrics.length > 0) {
@@ -25,8 +25,45 @@ sandboxTest.skipIf(isDebug)(
expect(metric.memUsed).toBeDefined()
expect(metric.cpuUsedPct).toBeDefined()
expect(metric.cpuCount).toBeDefined()
+ }
+)
+
+sandboxTest.skipIf(isDebug)(
+ 'sbx metrics time range',
+ { timeout: 60_000 },
+ async ({ sandbox }) => {
+ const start = new Date()
+
+ // Wait for the sandbox to have some metrics within the test's time window
+ let metrics: SandboxMetrics[] = []
+ let end = new Date()
+ for (let i = 0; i < 60; i++) {
+ end = new Date()
+ metrics = await sandbox.getMetrics({ start, end })
+ if (metrics.length > 0) {
+ break
+ }
+ await wait(500)
+ }
- metrics = await sandbox.getMetrics({ start: now, end: new Date() })
expect(metrics.length).toBeGreaterThan(0)
+
+ // All returned metrics must fall within the requested time range
+ // (10s slack — metric timestamps are aligned to collection buckets,
+ // currently 5s, and the query params are second-precision)
+ const slackMs = 10_000
+ for (const m of metrics) {
+ expect(m.timestamp.getTime()).toBeGreaterThanOrEqual(
+ start.getTime() - slackMs
+ )
+ expect(m.timestamp.getTime()).toBeLessThanOrEqual(end.getTime() + slackMs)
+ }
+
+ // A time range from before the sandbox existed must return no metrics
+ const noMetrics = await sandbox.getMetrics({
+ start: new Date(start.getTime() - 60 * 60 * 1000),
+ end: new Date(start.getTime() - 30 * 60 * 1000),
+ })
+ expect(noMetrics).toHaveLength(0)
}
)
diff --git a/packages/python-sdk/tests/async/sandbox_async/test_metrics.py b/packages/python-sdk/tests/async/sandbox_async/test_metrics.py
index f3b07a914..d065fa8d5 100644
--- a/packages/python-sdk/tests/async/sandbox_async/test_metrics.py
+++ b/packages/python-sdk/tests/async/sandbox_async/test_metrics.py
@@ -7,7 +7,6 @@ import pytest
@pytest.mark.skip_debug()
@pytest.mark.timeout(60)
async def test_sbx_metrics(async_sandbox_factory):
- start_time = datetime.datetime.now()
sbx = await async_sandbox_factory(timeout=60)
# Wait for the sandbox to have some metrics
@@ -28,5 +27,36 @@ async def test_sbx_metrics(async_sandbox_factory):
assert metric.disk_used is not None
assert metric.disk_total is not None
- metrics = await sbx.get_metrics(start=start_time, end=datetime.datetime.now())
+
+async def test_sbx_metrics_time_range(async_sandbox_factory):
+ start_time = datetime.datetime.now(datetime.timezone.utc)
+ sbx = await async_sandbox_factory(timeout=60)
+
+ # Wait for the sandbox to have some metrics within the test's time window
+ metrics = []
+ end_time = start_time
+ for _ in range(60):
+ end_time = datetime.datetime.now(datetime.timezone.utc)
+ metrics = await sbx.get_metrics(start=start_time, end=end_time)
+ if len(metrics) > 0:
+ break
+ await asyncio.sleep(0.5)
+
assert len(metrics) > 0
+
+ # All returned metrics must fall within the requested time range
+ # (10s slack - metric timestamps are aligned to collection buckets,
+ # currently 5s, and the query params are second-precision)
+ slack = 10
+ for metric in metrics:
+ assert metric.timestamp.timestamp() >= start_time.timestamp() - slack
+ assert metric.timestamp.timestamp() <= end_time.timestamp() + slack
+
+ # A time range from before the sandbox existed must return no metrics
+ metrics = await sbx.get_metrics(
+ start=start_time - datetime.timedelta(hours=1),
+ end=start_time - datetime.timedelta(minutes=30),
+ )
+ assert len(metrics) == 0
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/test_metrics.py b/packages/python-sdk/tests/sync/sandbox_sync/test_metrics.py
index e6cb55faa..3846ecf15 100644
--- a/packages/python-sdk/tests/sync/sandbox_sync/test_metrics.py
+++ b/packages/python-sdk/tests/sync/sandbox_sync/test_metrics.py
@@ -7,8 +7,8 @@ import pytest
@pytest.mark.skip_debug()
@pytest.mark.timeout(60)
def test_sbx_metrics(sandbox_factory) -> None:
- start_time = datetime.datetime.now()
sbx = sandbox_factory(timeout=60)
+
# Wait for the sandbox to have some metrics
metrics = []
for _ in range(60):
@@ -27,5 +27,36 @@ def test_sbx_metrics(sandbox_factory) -> None:
assert metric.disk_used is not None
assert metric.disk_total is not None
- metrics = sbx.get_metrics(start=start_time, end=datetime.datetime.now())
+
+def test_sbx_metrics_time_range(sandbox_factory) -> None:
+ start_time = datetime.datetime.now(datetime.timezone.utc)
+ sbx = sandbox_factory(timeout=60)
+
+ # Wait for the sandbox to have some metrics within the test's time window
+ metrics = []
+ end_time = start_time
+ for _ in range(60):
+ end_time = datetime.datetime.now(datetime.timezone.utc)
+ metrics = sbx.get_metrics(start=start_time, end=end_time)
+ if len(metrics) > 0:
+ break
+ time.sleep(0.5)
+
assert len(metrics) > 0
+
+ # All returned metrics must fall within the requested time range
+ # (10s slack - metric timestamps are aligned to collection buckets,
+ # currently 5s, and the query params are second-precision)
+ slack = 10
+ for metric in metrics:
+ assert metric.timestamp.timestamp() >= start_time.timestamp() - slack
+ assert metric.timestamp.timestamp() <= end_time.timestamp() + slack
+
+ # A time range from before the sandbox existed must return no metrics
+ metrics = sbx.get_metrics(
+ start=start_time - datetime.timedelta(hours=1),
+ end=start_time - datetime.timedelta(minutes=30),
+ )
+ assert len(metrics) == 0