SigNoz / terraform-provider-signoz

Terraform Provider of SigNoz
https://signoz.io
Mozilla Public License 2.0
5 stars 1 forks source link

Inconsistent State in signoz_alert Resource After Modification - Requires Resource Deletion and Re-Creation #24

Open Creator54 opened 1 month ago

Creator54 commented 1 month ago

When modifying the signoz_alert resource in Terraform, the state is not preserved. Any changes require deleting and re-creating the alert resource due to inconsistent provider behavior.

Steps to Reproduce

  1. Create a signoz_alert resource using Terraform (see example main.tf below).
  2. Apply the configuration to create the alert.
  3. Modify any parameters in the signoz_alert resource.
  4. Run terraform apply to apply the updated configuration.

Expected Behavior

Terraform should update the resource in place without requiring deletion and re-creation.

Actual Behavior

Terraform throws an error indicating an inconsistent result after applying changes to the signoz_alert resource. The condition field appears to be the root cause, as seen in the error output below.

Error Output

Plan: 0 to add, 1 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

signoz_alert.high_memory_usage: Modifying... [id=34]
╷
│ Error: Provider produced inconsistent result after apply
│ 
│ When applying changes to signoz_alert.high_memory_usage, provider "provider[\"registry.terraform.io/signoz/signoz\"]" produced an unexpected new value: .condition: was
│ cty.StringVal("{\"absentFor\":10,\"alertOnAbsent\":true,\"compositeQuery\":{\"builderQueries\":{\"A\":{\"aggregateAttribute\":{\"dataType\":\"float64\",\"isColumn\":true,\"isJSON\":false,\"key\":\"system_memory_usage\",\"type\":\"Gauge\"},\"aggregateOperator\":\"avg\",\"dataSource\":\"metrics\",\"disabled\":false,\"expression\":\"A\",\"filters\":{\"items\":[{\"key\":{\"dataType\":\"string\",\"isColumn\":false,\"isJSON\":false,\"key\":\"state\",\"type\":\"tag\"},\"op\":\"!=\",\"value\":\"free\"}],\"op\":\"AND\"},\"functions\":[],\"groupBy\":[{\"dataType\":\"string\",\"isColumn\":false,\"isJSON\":false,\"key\":\"host_name\",\"type\":\"tag\"}],\"having\":[],\"legend\":\"{{host_name}}\",\"limit\":null,\"orderBy\":[],\"queryName\":\"A\",\"reduceTo\":\"avg\",\"spaceAggregation\":\"sum\",\"stepInterval\":60,\"timeAggregation\":\"avg\"}},\"fillGaps\":false,\"panelType\":\"graph\",\"queryType\":\"builder\",\"unit\":\"bytes\"},\"matchType\":\"0\",\"op\":\"\\u003e\",\"selectedQueryName\":\"A\",\"target\":90,\"targetUnit\":\"gibibytes\"}"),
│ but now
│ cty.StringVal("{\"absentFor\":10,\"alertOnAbsent\":true,\"compositeQuery\":{\"builderQueries\":{\"A\":{\"IsAnomaly\":false,\"QueriesUsedInFormula\":null,\"ShiftBy\":0,\"aggregateAttribute\":{\"dataType\":\"float64\",\"isColumn\":true,\"isJSON\":false,\"key\":\"system_memory_usage\",\"type\":\"Gauge\"},\"aggregateOperator\":\"avg\",\"dataSource\":\"metrics\",\"disabled\":false,\"expression\":\"A\",\"filters\":{\"items\":[{\"key\":{\"dataType\":\"string\",\"isColumn\":false,\"isJSON\":false,\"key\":\"state\",\"type\":\"tag\"},\"op\":\"!=\",\"value\":\"free\"}],\"op\":\"AND\"},\"groupBy\":[{\"dataType\":\"string\",\"isColumn\":false,\"isJSON\":false,\"key\":\"host_name\",\"type\":\"tag\"}],\"legend\":\"{{host_name}}\",\"limit\":0,\"offset\":0,\"pageSize\":0,\"queryName\":\"A\",\"reduceTo\":\"avg\",\"spaceAggregation\":\"sum\",\"stepInterval\":60,\"timeAggregation\":\"avg\"}},\"panelType\":\"graph\",\"queryType\":\"builder\",\"unit\":\"bytes\"},\"matchType\":\"0\",\"op\":\"\\u003e\",\"selectedQueryName\":\"A\",\"target\":90,\"targetUnit\":\"gibibytes\"}").
│ 
│ This is a bug in the provider, which should be reported in the provider's own issue tracker.
╵

main.tf

terraform {
  required_providers {
    signoz = {
      source = "registry.terraform.io/signoz/signoz"
    }
  }
}

provider "signoz" {
  http_max_retry = 10
  http_timeout = 35
}

resource "signoz_alert" "high_memory_usage" {
  alert            = "High Memory Usage Alert"
  alert_type       = "METRIC_BASED_ALERT"
  severity         = "critical"
  broadcast_to_all = false
  condition        = jsonencode({
    absentFor     = 10
    alertOnAbsent = true
    compositeQuery = {
      queryType = "builder"
      panelType = "graph"
      unit = "bytes"
      fillGaps = false
      builderQueries = {
        A = {
          dataSource        = "metrics"
          queryName         = "A"
          aggregateOperator = "avg"
          aggregateAttribute = {
            key      = "system_memory_usage"
            dataType = "float64"
            type     = "Gauge"
            isColumn = true
            isJSON   = false
          }
          timeAggregation  = "avg"
          spaceAggregation = "sum"
          functions = []
          filters = {
            items = [
              {
                key = {
                  key      = "state"
                  dataType = "string"
                  type     = "tag"
                  isColumn = false
                  isJSON   = false
                }
                op    = "!="
                value = "free"
              }
            ]
            op = "AND"
          }
          expression    = "A"
          disabled      = false
          stepInterval  = 60
          groupBy = [
            {
              key      = "host_name"
              dataType = "string"
              type     = "tag"
              isColumn = false
              isJSON   = false
            }
          ]
          legend   = "{{host_name}}"
          reduceTo = "avg"
        }
      }
    }
    op                = ">"
    target            = 90
    targetUnit        = "gibibytes"
    matchType         = "0"
    selectedQueryName = "A"
  })
  description = "Triggers when Memory usage exceeds 90%"
  eval_window = "5m0s"
  frequency   = "1m0s"
  labels = {
    "observer" = "terraform-demo"
  }
  preferred_channels = ["terraform alerts"]
  rule_type         = "threshold_rule"
  version           = "v4"
}
grandwizard28 commented 6 days ago

@Creator54 thanks for reporting the bug!

Would you be able to contribute a PR?