1

Find Monitors

by
Published Jul 18, 2022
Script datadog Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 days ago
1
//native
2

3
type Datadog = {
4
  apiKey: string;
5
  appKey: string;
6
  apiBase: string;
7
};
8

9
/**
10
 * Find Monitors
11
 * Search for monitors and return every match (following pagination). Leave
12
 * `query` empty to return all monitors, or pass a Datadog monitor search
13
 * query such as `type:metric` or `env:production`.
14
 */
15
export async function main(dd_auth: Datadog, query: string = "") {
16
  const monitors = [];
17
  for await (const monitor of searchToEnd(dd_auth, query)) {
18
    monitors.push(monitor);
19
  }
20
  return monitors;
21
}
22

23
async function* searchToEnd(dd_auth: Datadog, query: string) {
24
  let page = 0;
25
  let pageCount = 1;
26
  while (page < pageCount) {
27
    const url = new URL(`${dd_auth.apiBase}/api/v1/monitor/search`);
28
    if (query !== "") {
29
      url.searchParams.append("query", query);
30
    }
31
    url.searchParams.append("page", String(page));
32

33
    const response = await fetch(url, {
34
      method: "GET",
35
      headers: {
36
        "DD-API-KEY": dd_auth.apiKey,
37
        "DD-APPLICATION-KEY": dd_auth.appKey,
38
        Accept: "application/json",
39
      },
40
    });
41

42
    if (!response.ok) {
43
      throw new Error(`${response.status} ${await response.text()}`);
44
    }
45

46
    const result = await response.json();
47
    for (const monitor of result.monitors ?? []) {
48
      yield monitor;
49
    }
50
    pageCount = result.metadata?.page_count ?? 1;
51
    page++;
52
  }
53
}
54

Other submissions
  • Submitted by rossmccrann Deno
    Created 398 days ago
    1
    import ApiClient from "https://deno.land/x/datadog_api/client.ts";
    2
    import V1MonitorsApi from "https://deno.land/x/datadog_api/v1/monitors.ts";
    3
    
    
    4
    type Datadog = {
    5
      apiKey: string;
    6
      appKey: string;
    7
      apiBase: string;
    8
    };
    9
    export async function main(dd_auth: Datadog) {
    10
      let count = 0;
    11
      // Set up a Monitors API client
    12
      const datadog = new ApiClient(dd_auth);
    13
      const monitorsApi = new V1MonitorsApi(datadog);
    14
    
    
    15
      let monitor_urls = [];
    16
      // Search for relevant monitors via a metric filter
    17
      for await (const monitor of monitorsApi.searchToEnd(`metric:trace*`)) {
    18
        // Skip monitors that have a scoped environment set
    19
        if (!monitor.query.includes("env:production")) continue;
    20
        if (!monitor.query.includes("env:sandbox")) continue;
    21
    
    
    22
        // return the monitor URL for further manual inspection
    23
        monitor_urls.push(`https://app.datadoghq.eu/monitors/${monitor.id}`);
    24
        count++;
    25
      }
    26
    
    
    27
      // Return number of matched monitors as a summary
    28
      return { count: count, monitor_urls: monitor_urls };
    29
    }
    30