Get prometheus metrics
One script reply has been approved by the moderators Verified

Returns prometheus metrics for a service.

Created by hugo697 141 days ago Picked 1 time
Submitted by hugo697 Bun
Verified 141 days ago
1
//native
2
type Clickhouse = {
3
  username: string;
4
  password: string;
5
  host: string;
6
};
7
/**
8
 * Get prometheus metrics
9
 * Returns prometheus metrics for a service.
10
 */
11
export async function main(
12
  auth: Clickhouse,
13
  organizationId: string,
14
  serviceId: string,
15
  filtered_metrics: string | undefined
16
) {
17
  const url = new URL(
18
    `https://api.clickhouse.cloud/v1/organizations/${organizationId}/services/${serviceId}/prometheus`
19
  );
20
  for (const [k, v] of [["filtered_metrics", filtered_metrics]]) {
21
    if (v !== undefined && v !== "" && k !== undefined) {
22
      url.searchParams.append(k, v);
23
    }
24
  }
25
  const response = await fetch(url, {
26
    method: "GET",
27
    headers: {
28
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
29
    },
30
    body: undefined,
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.text();
37
}
38