Skip to content

Commit

Permalink
update openapi and implement getMetrics method
Browse files Browse the repository at this point in the history
  • Loading branch information
0div committed Dec 26, 2024
1 parent eb5522f commit f0601e5
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/js-sdk/src/api/schema.gen.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions packages/js-sdk/src/sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createRpcLogger } from '../logs'
import { Commands, Pty } from './commands'
import { Filesystem } from './filesystem'
import { SandboxApi } from './sandboxApi'
import { components } from '../api/schema.gen'

/**
* Options for creating a new Sandbox.
Expand Down Expand Up @@ -278,6 +279,23 @@ export class Sandbox extends SandboxApi {
return true
}

/**
* Get the metrics of the sandbox.
*
* @param timeoutMs timeout in **milliseconds**.
* @param opts connection options.
*
* @returns metrics of the sandbox.
*/
async getMetrics(
opts?: Pick<SandboxOpts, 'requestTimeoutMs'>
): Promise<components['schemas']['SandboxMetric'][]> {
return await Sandbox.getMetrics(this.sandboxId, {
...this.connectionConfig,
...opts,
})
}

/**
* Set the timeout of the sandbox.
* After the timeout expires the sandbox will be automatically killed.
Expand Down
38 changes: 38 additions & 0 deletions packages/js-sdk/src/sandbox/sandboxApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,44 @@ export class SandboxApi {
)
}

/**
* Get the metrics of the sandbox.
*
* @param sandboxId sandbox ID.
* @param timeoutMs timeout in **milliseconds**.
* @param opts connection options.
*
* @returns metrics of the sandbox.
*/
static async getMetrics(
sandboxId: string,
opts?: SandboxApiOpts
): Promise<components['schemas']['SandboxMetric'][]> {
const config = new ConnectionConfig(opts)
const client = new ApiClient(config)

const res = await client.api.GET('/sandboxes/{sandboxID}/metrics', {
params: {
path: {
sandboxID: sandboxId,
},
},
signal: config.getSignal(opts?.requestTimeoutMs),
})

const err = handleApiError(res)
if (err) {
throw err
}

return (
res.data?.map((metric: components['schemas']['SandboxMetric']) => ({
...metric,
timestamp: new Date(metric.timestamp).toISOString(),
})) ?? []
)
}

/**
* Set the timeout of the specified sandbox.
* After the timeout expires the sandbox will be automatically killed.
Expand Down
12 changes: 12 additions & 0 deletions packages/js-sdk/tests/sandbox/metrics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { assert } from 'vitest'

import { isDebug, sandboxTest, wait } from '../setup.js'

sandboxTest('get sandbox metrics', async ({ sandbox }) => {
const metrics = await sandbox.getMetrics()

assert.isAtLeast(metrics.length, 1)
assert.isAtLeast(metrics[0]?.cpuPct, 0)
assert.isAtLeast(metrics[0]?.memMiBTotal, 0)
assert.isAtLeast(metrics[0]?.memMiBUsed, 0)
})
51 changes: 51 additions & 0 deletions spec/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,31 @@ components:
- ready
- error

SandboxMetric:
description: Metric entry with timestamp and line
required:
- timestamp
- cpuPct
- memMiBUsed
- memMiBTotal
properties:
timestamp:
type: string
format: date-time
description: Timestamp of the log entry
cpuPct:
type: number
format: float
description: CPU usage percentage
memMiBUsed:
type: integer
format: int64
description: Memory used in MiB
memMiBTotal:
type: integer
format: int64
description: Total memory in MiB

Error:
required:
- code
Expand Down Expand Up @@ -516,6 +541,32 @@ paths:
$ref: "#/components/responses/404"
"500":
$ref: "#/components/responses/500"

/sandboxes/{sandboxID}/metrics:
get:
description: Get sandbox metrics
tags: [sandboxes]
security:
- ApiKeyAuth: []
parameters:
- $ref: "#/components/parameters/sandboxID"
responses:
"200":
description: Successfully returned the sandbox metrics
content:
application/json:
schema:
type: array
items:
type: object
$ref: "#/components/schemas/SandboxMetric"
"404":
$ref: "#/components/responses/404"
"401":
$ref: "#/components/responses/401"
"500":
$ref: "#/components/responses/500"


/sandboxes/{sandboxID}/refreshes:
post:
Expand Down

0 comments on commit f0601e5

Please sign in to comment.