0

Add a alert rule to the system

by
Published Oct 17, 2025

Add a alert rule to the system

Script signoz Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Signoz = {
3
  apiKey: string;
4
  baseUrl: string;
5
};
6
/**
7
 * Add a alert rule to the system
8
 * Add a alert rule to the system
9
 */
10
export async function main(
11
  auth: Signoz,
12
  body: {} & {
13
    alert: string;
14
    alertType:
15
      | "LOGS_BASED_ALERT"
16
      | "METRIC_BASED_ALERT"
17
      | "TRACES_BASED_ALERT"
18
      | "EXCEPTIONS_BASED_ALERT";
19
    description?: string;
20
    ruleType: "threshold_rule" | "promql_rule";
21
    evalWindow?: string;
22
    frequency?: string;
23
    condition: {
24
      target: {};
25
      matchType: "0" | "1" | "2" | "3" | "4" | "5";
26
      op: "0" | "1" | "2" | "3" | "4";
27
      compositeQuery: {
28
        builderQueries?: {};
29
        chQueries?: {};
30
        promQueries?: {};
31
        panelType: "graph" | "table" | "value" | "list" | "trace";
32
        queryType: "builder" | "clickhouse_sql" | "promql";
33
        unit?: string;
34
        fillGaps?: false | true;
35
      };
36
      targetUnit?: string;
37
      selectedQueryName?: string;
38
      alertOnAbsent?: false | true;
39
      absentFor?: number;
40
      requireMinPoints?: false | true;
41
      requiredNumPoints?: number;
42
    };
43
    labels?: { key?: string; value?: string };
44
    annotations?: { key?: string; value?: string };
45
    disabled?: false | true;
46
    source?: string;
47
    preferredChannels?: string[];
48
    version?: string;
49
  },
50
) {
51
  const url = new URL(`https://${auth.baseUrl}/api/v1/rules`);
52

53
  const response = await fetch(url, {
54
    method: "POST",
55
    headers: {
56
      "Content-Type": "application/json",
57
      "SIGNOZ-API-KEY": auth.apiKey,
58
    },
59
    body: JSON.stringify(body),
60
  });
61
  if (!response.ok) {
62
    const text = await response.text();
63
    throw new Error(`${response.status} ${text}`);
64
  }
65
  return await response.text();
66
}
67