DataDog / terraform-provider-datadog

Terraform Datadog provider
https://www.terraform.io/docs/providers/datadog/
Mozilla Public License 2.0
402 stars 377 forks source link

Validation error on ci-pipelines monitor that uses a formula #1846

Closed jfirebaugh closed 1 year ago

jfirebaugh commented 1 year ago

Steps to Reproduce

Create a CI pipeline monitor in the Datadog UI that uses the following configuration:

Screen Shot 2023-04-03 at 1 07 22 PM

terraform import to import it into a terraform configuration. Set the query attribute to the value shown in the Datadog UI:

ci-pipelines("(count[ci_level:job @ci.job.retry_count:>=1 @buildkite.queue:production-*] / count[ci_level:job @buildkite.queue:production-*]) * 100").last("5m") > 5

Run terraform apply.

Expected Behavior

Apply succeeds.

Actual Behavior

Validation error.

Terraform Version

Terraform v1.1.3 on darwin_arm64

Affected Resource(s)

Terraform Configuration Files

resource "datadog_monitor" "monitor" {
  name = "test"
  type = "ci-pipelines alert"
  query = <<-EOF
    ci-pipelines("(count[ci_level:job @ci.job.retry_count:>=1 @buildkite.queue:production-*] / count[ci_level:job @buildkite.queue:production-*]) * 100").last("5m") > 5
  EOF
}

Debug Output

Error: error validating monitor from https://api.datadoghq.com/api/v1/monitor/115420349/validate: 400 Bad Request: {"errors":["The value provided for parameter 'query' is invalid: invalid operator specified: "]}

therve commented 1 year ago

Hi,

I believe you need to use the formula support to express that monitor. If you click on Export in the UI it should give you the proper format. It should look something like that:

resource "datadog_monitor" "monitor" {
  name    = "test"
  message = "message"
  type    = "ci-pipelines alert"
  query   = "formula(\"(query1 / query2) * 100\").last(\"5m\") > 5"

  variables {       
    event_query {
      data_source = "ci_pipelines"
      name        = "query1"
      indexes     = ["*"]   
      compute {
        aggregation = "count"
      }         
      search {
        query = "ci_level:job @ci.job.retry_count:>=1 @buildkite.queue:production-*"
      }
    }
    event_query {
      data_source = "ci_pipelines"
      name        = "query2"
      indexes     = ["*"]   
      compute {
        aggregation = "count"
      }
      search {
        query = "ci_level:job @buildkite.queue:production-*"
      }         
    }   
  }
}

Let us know if that works for you, thanks.

jfirebaugh commented 1 year ago

Yes, that works, thanks!