LoginSignup
4
0

More than 1 year has passed since last update.

Terraformを使ったNewRelicのアラート設定

Last updated at Posted at 2020-12-19

やること

今回は、newrelic_nrql_alert_conditionを設定していきます。
特に詰まりそうな、部分を重点的に説明できるといいかなと思います。

ソースコード

簡単にterraform module化しているので、必要なかたはソースからダウンロードしてみてください。

terraform provider について

参考URL

NewRelicのprodiverでは、以下が必須の設定項目になっています。

  • account_id (アカウントID)
    • NEW_RELIC_ACCOUNT_ID の環境変数で代用可能
  • api_key (APIキー)
    • NEW_RELIC_API_KEY の環境変数で代用可能
    • NRAKから始まるキー
  • region (リージョン)
    • US もしくは EU

provider "newrelic" {
  account_id = "<Your Account ID>"
  api_key = "<Your Personal API Key>"    # usually prefixed with 'NRAK'
  region = "US"                          # Valid regions are US and EU
}

newrelic_insights_event 以外は、基本的に api_key の設定でOKです。

newrelic_nrql_alert_condition について

これを使うと、NRQLを使って確認できるメトリクスであれば、ほとんどのアラートを設定できます。
ちなみに、 newrelic_alert_policy (policy) に対して、複数のアラートを設定可能です。

これの何が嬉しいかというと、
今までAPMとInfrastructureのアラートを設定するときは、 newrelic_alert_conditionnewrelic_infra_alert_condition 2つのresourceを使い分けないといけなかったのですが、これがあれば 1つのresouceで設定可能 になります。
さらに、NRQLをそのまま使用できるので、既存のダッシュボードの設定から、View queryでNRQLをコピーしてそのまま貼り付けるだけでアラートをコード化できます。

スクリーンショット 2020-12-19 21.53.38.png

参考URL

APMのSlow Queryアラートを設定してみる

where句に設定可能な、演算子(NRQL)はこちらを参照ください

newrelic_nrql_alert_condition の設定項目で難しいところ

  • type
  • value_function
    • single_value と sum が設定可能、たぶんメトリクスの集計方法
      • single_value だと合計値じゃないので、メトリクスがないところはデータなしとなる
  • violation_time_limit
    • この時間を設定していると、アラート発生から時間経過後になにかしらの自動設定が可能
    • ちなみに、TerraformのドキュメントにはDEPRECATEDと書いてあったけど、violation_time_limit_secondsのほうが、terraform plan時にDEPRECATEDになっていた、ドキュメントのミス??
  • nrql -> evaluation_offset
    • よくわからないけど、3分にしておく
    • グラフを表示するときに使う設定?
    • NRQLで設定したクエリの開始時間??
  • critical/warning -> threshold_occurrences
    • 閾値を超えた値の評価方法
      • all: すべての値が閾値を超えている
      • at_least_once: 1つ以上の値が閾値を超えている
    • value_functionでsumを選んだ場合は、 at_least_onceのみ設定可能

moduleのサンプル

modules/nrql_alert/main.tf

resource "newrelic_alert_policy" "alert" {
  name = var.name
}

resource "newrelic_nrql_alert_condition" "alert" {
  policy_id            = newrelic_alert_policy.alert.id
  type                 = var.type
  name                 = var.name
  value_function       = var.value_function
  violation_time_limit = var.violation_time_limit

  nrql {
    query             = var.query
    evaluation_offset = var.evaluation_offset
  }

  dynamic "critical" {
    for_each = var.critical
    content {
      operator              = critical.value.operator
      threshold             = critical.value.threshold
      threshold_duration    = critical.value.threshold_duration
      threshold_occurrences = critical.value.threshold_occurrences
    }
  }

  dynamic "warning" {
    for_each = var.warning
    content {
      operator              = warning.value.operator
      threshold             = warning.value.threshold
      threshold_duration    = warning.value.threshold_duration
      threshold_occurrences = warning.value.threshold_occurrences
    }
  }
}
modules/nrql_alert/variables.tf

variable "name" {
  type = string
}

variable "type" {
  type = string
}

variable "value_function" {
  type = string
}

variable "violation_time_limit" {
  type    = string
  default = "ONE_HOUR"
}

variable "query" {
  type = string
}

variable "evaluation_offset" {
  type    = number
  default = 3
}

variable "critical" {
  type = list(any)
}

variable "warning" {
  type    = list(any)
  default = []
}

module呼び出し側のサンプル

newrelic_alert/nrql_alert_mysql_slow_query.tf

module "nrql_alert_mysql_slow_query" {
  source         = "../modules/newrelic/nrql_alert"
  name           = "${local.name} slow query"
  type           = "static"
  value_function = "sum"

# query
## ヒアドキュメントを使うとわかりやすいので、おすすめ
## apm_app_name はAPMの設定で付けている名前に変更する

  query = <<EOF
   SELECT average(apm.service.datastore.operation.duration) * 1000 
   FROM Metric 
   WHERE  (appName = '${var.apm_app_name}') 
   AND ((metricTimesliceName = 'Datastore/operation/MySQL/show' AND datastoreType = 'MySQL')) 
   FACET `metricTimesliceName`
EOF

# critical
## operator は閾値の下か上かどちらでアラートを出すか
## threshold_duration は秒単位なので、 300secで5分

  critical = [
    {
      operator              = "above"
      threshold             = 10
      threshold_duration    = 300
      threshold_occurrences = "at_least_once"
    }
  ]

  warning = [
    {
      operator              = "above"
      threshold             = 5
      threshold_duration    = 300
      threshold_occurrences = "at_least_once"
    }
  ]
}

アラートの確認方法

Alerts & AI -> Polices -> newrelic-sample-default slow query

スクリーンショット 2020-12-19 22.02.11.png

ちゃんとPreview chartが表示されていることを確認

スクリーンショット 2020-12-19 21.45.49.png

Thresholdsの設定値も確認

スクリーンショット 2020-12-19 22.32.41.png

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0