terraform-google-modules / terraform-google-slo

Creates SLOs on Google Cloud from custom Stackdriver metrics capability to export SLOs to Google Cloud services and other systems
https://registry.terraform.io/modules/terraform-google-modules/slo/google
Apache License 2.0
63 stars 28 forks source link

Add name output to slo-native module #100

Closed andrewmackett closed 2 years ago

andrewmackett commented 2 years ago

Summary

At the moment the slo-native module doesn't output the information required to create an alerting policy for the SLO. This PR is adding a new output name to the slo-native module to make creating an alerting policy for the SLO easier.

Fixes #101.

Details

The output uses the google_monitoring_slo resource's name attribute that has the syntax:

projects/[PROJECT_ID_OR_NUMBER]/services/[SERVICE_ID]/serviceLevelObjectives/[SLO_NAME]

Example Usage

The new output allows the google_monitoring_alert_policy resource to easily be configured with a filter as described in the documentation:

# Example alert policy using the slo-native output
resource "google_monitoring_alert_policy" "alert_policy" {
  project      = var.project_id
  display_name = "Slow-Burn Alert Ploicy"
  combiner     = "OR"
  conditions {
    display_name = "Error Budget Burn"
    condition_threshold {
      filter          = "select_slo_burn_rate(\"${module.slo_request_based.name}\", \"24h\")"
      comparison      = "COMPARISON_GT"
      duration        = "0s"
      threshold_value = 2
    }
  }
  notification_channels = [
    google_monitoring_notification_channel.basic.name
  ]
  documentation {
    content = "SLO burn for the past 24h exceeded twice the acceptable budget burn rate."
  }
}

# Custom service for the SLO
resource "google_monitoring_custom_service" "customsrv" {
  project      = var.project_id
  service_id   = "custom-srv-request-slos"
  display_name = "My Custom Service"
}

# Example SLO
module "slo_request_based" {
  source = "../../../modules/slo-native"
  config = {
    project_id          = var.project_id
    service             = google_monitoring_custom_service.customsrv.service_id
    slo_id              = "gcp-latency400ms"
    display_name        = "90% of Google APIs HTTP response latencies < 400ms over a rolling 30-days period"
    goal                = 0.9
    rolling_period_days = 30
    type                = "request_based_sli"
    method              = "distribution_cut"
    metric_filter       = <<EOF
metric.type="serviceruntime.googleapis.com/api/request_latencies"
resource.type="consumed_api"
resource.label."project_id"="${var.project_id}"
EOF
    range_min           = 0
    range_max           = 400
  }
}

# Example notification channel
resource "google_monitoring_notification_channel" "basic" {
  project      = var.project_id
  display_name = "Test Notification Channel"
  type         = "email"
  labels = {
    email_address = "person@example.com"
  }
}
comment-bot-dev commented 2 years ago

Thanks for the PR! šŸš€
āœ… Lint checks have passed.