From 657811ec549cd4c16a5cdb096c07118aff45244e Mon Sep 17 00:00:00 2001 From: Daniel Lamando Date: Thu, 25 Nov 2021 19:28:06 +0100 Subject: [PATCH] Add an example using the Monitors API --- examples/emit-metrics.ts | 0 examples/find-monitors.ts | 22 ++++++++++++++++++++++ v1/monitors.ts | 6 ++++++ 3 files changed, 28 insertions(+) mode change 100644 => 100755 examples/emit-metrics.ts create mode 100755 examples/find-monitors.ts diff --git a/examples/emit-metrics.ts b/examples/emit-metrics.ts old mode 100644 new mode 100755 diff --git a/examples/find-monitors.ts b/examples/find-monitors.ts new file mode 100755 index 0000000..4ba40ef --- /dev/null +++ b/examples/find-monitors.ts @@ -0,0 +1,22 @@ +#!/usr/bin/env -S deno run --allow-env --allow-net + +import DatadogApi from "../mod.ts"; +const datadogApi = DatadogApi.fromEnvironment(Deno.env); + +// This example looks at all monitors using any APM trace metrics, +// and prints links to those which are not scoped to an APM environment. + +let count = 0; +// Search for relevant monitors via a metric filter +for await (const monitor of datadogApi.v1Monitors.searchToEnd("metric:trace*")) { + + // Skip monitors that have a scoped environment set + if (!monitor.query.includes('env:production')) continue; + if (!monitor.query.includes('env:sandbox')) continue; + + // Print the monitor URL for further manual inspection + console.log(`https://app.datadoghq.eu/monitors/${monitor.id}`); + count++; +} +// Print number of matched monitors as a summary +console.log({count}) diff --git a/v1/monitors.ts b/v1/monitors.ts index c38e0ed..b5f7ffc 100644 --- a/v1/monitors.ts +++ b/v1/monitors.ts @@ -8,6 +8,12 @@ interface ApiClient { }): Promise; } +/** + * Monitors allow you to watch a metric or check that you care about, + * notifying your team when some defined threshold is exceeded. + * + * Official API docs: https://docs.datadoghq.com/api/latest/monitors/ + */ export default class DatadogMonitorsApi { #api: ApiClient; constructor(api: ApiClient) {