0

Get App CPU Percentage Metrics

by
Published Dec 20, 2024

To retrieve cpu percentage metrics for a given app, send a GET request to `/v2/monitoring/metrics/apps/cpu_percentage`.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Get App CPU Percentage Metrics
7
 * To retrieve cpu percentage metrics for a given app, send a GET request to `/v2/monitoring/metrics/apps/cpu_percentage`.
8
 */
9
export async function main(
10
  auth: Digitalocean,
11
  app_id: string | undefined,
12
  app_component: string | undefined,
13
  start: string | undefined,
14
  end: string | undefined,
15
) {
16
  const url = new URL(
17
    `https://api.digitalocean.com/v2/monitoring/metrics/apps/cpu_percentage`,
18
  );
19
  for (const [k, v] of [
20
    ["app_id", app_id],
21
    ["app_component", app_component],
22
    ["start", start],
23
    ["end", end],
24
  ]) {
25
    if (v !== undefined && v !== "" && k !== undefined) {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42