Skip to content

Commit 32a21a4

Browse files
committed
Add sbx metrics to SDKs
1 parent 3b26f71 commit 32a21a4

20 files changed

Lines changed: 1126 additions & 9 deletions

File tree

packages/js-sdk/src/api/schema.gen.ts

Lines changed: 210 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/js-sdk/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type {
4141
} from './sandbox/commands'
4242

4343
export type { SandboxOpts } from './sandbox'
44-
export type { SandboxInfo } from './sandbox/sandboxApi'
44+
export type { SandboxInfo, SandboxMetrics } from './sandbox/sandboxApi'
4545
export { Sandbox }
4646
import { Sandbox } from './sandbox'
4747
export default Sandbox

packages/js-sdk/src/sandbox/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,21 @@ export class Sandbox extends SandboxApi {
520520
})
521521
}
522522

523+
524+
/**
525+
* Get the metrics of the sandbox.
526+
*
527+
* @param opts connection options.
528+
*
529+
* @returns metrics of the sandbox.
530+
*/
531+
async getMetrics(opts?: Pick<SandboxOpts, 'requestTimeoutMs'>) {
532+
return await Sandbox.getMetrics(this.sandboxId, {
533+
...this.connectionConfig,
534+
...opts,
535+
})
536+
}
537+
523538
private fileUrl(path?: string, username?: string) {
524539
const url = new URL('/files', this.envdApiUrl)
525540

packages/js-sdk/src/sandbox/sandboxApi.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,47 @@ export interface ListedSandbox {
125125
}
126126

127127

128+
/**
129+
* Sandbox resource usage metrics.
130+
*/
131+
export interface SandboxMetrics {
132+
/**
133+
* Timestamp of the metrics.
134+
*/
135+
timestamp: Date
136+
137+
/**
138+
* CPU usage in percentage.
139+
*/
140+
cpuUsedPct: number
141+
142+
/**
143+
* Number of CPU cores.
144+
*/
145+
cpuCount: number
146+
147+
/**
148+
* Memory usage in bytes.
149+
*/
150+
memUsed: number
151+
152+
/**
153+
* Total memory available in bytes.
154+
*/
155+
memTotal: number
156+
157+
/**
158+
* Used disk space in bytes.
159+
*/
160+
diskUsed: number
161+
162+
/**
163+
* Total disk space available in bytes.
164+
*/
165+
diskTotal: number
166+
}
167+
168+
128169
export class SandboxApi {
129170
protected constructor() {}
130171

@@ -262,6 +303,49 @@ export class SandboxApi {
262303
}
263304
}
264305

306+
307+
/**
308+
* Get the metrics of the sandbox.
309+
*
310+
* @param sandboxId sandbox ID.
311+
* @param opts connection options.
312+
*
313+
* @returns metrics of the sandbox.
314+
*/
315+
static async getMetrics(
316+
sandboxId: string,
317+
opts?: SandboxApiOpts
318+
): Promise<SandboxMetrics[]> {
319+
const config = new ConnectionConfig(opts)
320+
const client = new ApiClient(config)
321+
322+
const res = await client.api.GET('/sandboxes/{sandboxID}/metrics', {
323+
params: {
324+
path: {
325+
sandboxID: sandboxId,
326+
},
327+
},
328+
signal: config.getSignal(opts?.requestTimeoutMs),
329+
})
330+
331+
const err = handleApiError(res)
332+
if (err) {
333+
throw err
334+
}
335+
336+
return (
337+
res.data?.map((metric: components['schemas']['SandboxMetric']) => ({
338+
timestamp: new Date(metric.timestamp),
339+
cpuUsedPct: metric.cpuUsedPct,
340+
cpuCount: metric.cpuCount,
341+
memUsed: metric.memUsed,
342+
memTotal: metric.memTotal,
343+
diskUsed: metric.diskUsed,
344+
diskTotal: metric.diskTotal,
345+
})) ?? []
346+
)
347+
}
348+
265349
/**
266350
* Set the timeout of the specified sandbox.
267351
* After the timeout expires the sandbox will be automatically killed.

0 commit comments

Comments
 (0)