Is there a way to have division when writing terraform code for a log alert in Datadog?

178 Views Asked by At

I want have a terraform code to create a Datadog monitor for the percentage of errors in logs compared with all of them.

This is what I've tried

resource "datadog_monitor" "log_errors_count" {
    count                         = local.memory_usage_threshold.critical \> 0 ? 1 : 0
    name                          = "\[${module.label.id}\] ${length(var.description) \> 0 ? var.description : "Log Errors Percentage"}"
    type                          = "log alert"
    query                         = "logs(\"service:api-member status:error\").index(\"*\").rollup(\"count\").by(\"service\").last(\"${var.period}\") / logs(\"service:api-member\").index(\"*\").rollup(\"count\").by(\"service\").last(\"${var.period}\") \> ${local.logged_errors_threshold.critical}"

    monitor_thresholds {
        ok                                  = local.logged_errors_threshold.ok
        warning                             = local.logged_errors_threshold.warning
        critical                            = local.logged_errors_threshold.critical
    }

}

But it returns: 400 Bad Request: {"errors":["The value provided for parameter 'query' is invalid: invalid operator specified: "]}

I have done this kind of division for a metric alert and it worked fine. Using Datadog dashboard I can create a log monitor the way I want, but it looks like I am missing something when I try to do it using terraform.

1

There are 1 best solutions below

1
On

Try to escape the internal quotes with a backslash

query = "logs(/"service:api-member status:error/").index(/"/").rollup/"count/").by(/"service/").last(/"${var.period}/") / logs(/"service:api-member/").index(/"*"/).rollup(/"count/").by(/"service/").last(/"${var.period}/") \> ${local.logged_errors_threshold.critical}"