継続は力なり

タイトル通り定期的な更新を心掛けるブログです。

Datadog の Watchdog モニターを Terraform で作る

タダです.

前回の記事で Datadog の Watchdog モニター作る話を書きました.この記事はその続きで,Watchdog のモニターを Terraform で作っていきます.

sadayoshi-tada.hatenablog.com

Terraform で Watchdog モニター作成のためのコード

今回は Watchdog の APM/Infrastructure/Logs のモニターを作るコードを書いてみました.

resource "datadog_monitor" "watchdog_apm_monitor" {
  name    = "APM Watchdog Anomary Detect"
  type    = "event-v2 alert"
  query   = "events(\"source:watchdog tags:\\\"story_category:apm\\\" tags:\\\"env:${var.env}\\\"\").rollup(\"count\").by(\"story_key,service,resource_name\").last(\"30m\") > 0"
  message = <<-EOT
      {{#is_alert}}
      @slack-xxx
      {{value}} > {{threshold}}
      Watchdog が ${var.env} 環境で Datadog APM の異常を検知しました
      {{/is_alert}}
  EOT
  monitor_thresholds {
    critical = 0
  }
  include_tags        = true
  tags = ["foo:bar", "team:fooBar"]
}
resource "datadog_monitor" "watchdog_infra_monitor" {
  name    = "Infrastructure Watchdog Anomary Detect"
  type    = "event-v2 alert"
  query   = "events(\"source:watchdog tags:\\\"story_category:infrastructure\\\"\").rollup(\"count\").by(\"story_key\").last(\"30m\") > 0"
  message = <<-EOT
      {{#is_alert}}
      @slack-xxx
      {{value}} > {{threshold}}
       Watchdog が ${var.env} 環境で Datadog Infrastructureの異常を検知しました
      {{/is_alert}}
  EOT
  monitor_thresholds {
    critical = 0
  }
  include_tags = true
  tags = ["foo:bar", "team:fooBar"]
}
resource "datadog_monitor" "watchdog_logs_monitor" {
  name    = "Logs Watchdog Anomary Detect"
  type    = "event-v2 alert"
  query   = "events(\"source:watchdog tags:\\\"story_category:logs\\\" tags:\\\"story_type:logs_pattern_anomaly\\\" tags:\\\"env:${var.env}\\\"\").rollup(\"count\").by(\"story_key,env,service,log_source\").last(\"30m\") > 0"
  message = <<-EOT
      {{#is_alert}}
      @slack-xxx
      {{value}} > {{threshold}}
      Watchdog が ${var.env} 環境で Datadog Logs の異常を検知しました
      {{/is_alert}}
  EOT
  monitor_thresholds {
    critical = 0
  }
  include_tags        = true
  tags = ["foo:bar", "team:fooBar"]
}

関連情報

registry.terraform.io

まとめ

Datadog の Watchdog モニターを Terraform で作ってみた時の学びを備忘録としてまとめました.