rollbar / terraform-provider-rollbar

Terraform provider for Rollbar
https://rollbar.com
MIT License
28 stars 13 forks source link

Pagerduty occurrence_rate notification always shows up as changed #269

Closed phillipuniverse closed 4 months ago

phillipuniverse commented 2 years ago

Here is my terraform configuration:

provider "rollbar" {
  api_key = var.rollbar_terraform_api_key
}

provider "rollbar" {
  alias           = "notifications_provider"
  project_api_key = rollbar_project_access_token.notifications.access_token
}

# Create a project and assign the team
resource "rollbar_project" "settlements" {
  name     = "settlements"
  team_ids = ["256208"]
  lifecycle {
    ignore_changes = [team_ids]
  }
}

resource "rollbar_project_access_token" "post_server_item" {
  name       = "Service post item access token"
  project_id = rollbar_project.settlements.id
  scopes     = ["post_server_item"]
}

resource "rollbar_project_access_token" "notifications" {
  name       = "Access token for reading/writing notification rules"
  project_id = rollbar_project.settlements.id
  scopes     = ["read", "write"]
}

# Generally speaking these notifications map onto the Rollbar API. Better docs for these properties can be found
# at the Rollbar API docs at https://explorer.docs.rollbar.com/#tag/PagerDuty-Notification-Rules/paths/~1api~11~1notifications~1pagerduty/post
resource "rollbar_notification" "pd_high_occurrence" {
  provider = rollbar.notifications_provider
  channel  = "pagerduty"

  rule {
    trigger = "occurrence_rate"
    filters {
      type      = "level"
      operation = "gte"
      value     = "error"
    }
    filters {
      type   = "rate"
      period = 300 # value in seconds, 5m
      count  = 75
    }
  }
  config {
    service_key = var.pd_service_key
  }
}

resource "rollbar_notification" "pd_resolve_item" {
  provider = rollbar.notifications_provider
  channel  = "pagerduty"

  rule {
    trigger = "resolved_item"
  }
  config {
    service_key = var.pd_service_key
  }
}

resource "rollbar_notification" "pd_reactivate_item" {
  provider = rollbar.notifications_provider
  channel  = "pagerduty"

  rule {
    trigger = "reactivated_item"
  }
  config {
    service_key = var.pd_service_key
  }
}

It seems like every time I run a terraform plan this results in changes that are not actually changes, but only with the pd_high_occurrence resource.

On the first apply, I see the notifications created in the Rollbar project UI. When I re-run it, it detects the following difference:

# rollbar_notification.pd_high_occurrence has changed
~ resource "rollbar_notification" "pd_high_occurrence" {
      id      = "5502736"
      # (1 unchanged attribute hidden)

    - rule {
        - trigger = "occurrence_rate" -> null

        - filters {
            - count     = 0 -> null
            - operation = "gte" -> null
            - period    = 0 -> null
            - type      = "level" -> null
            - value     = "error" -> null
          }
        - filters {
            - count  = 75 -> null
            - period = 300 -> null
            - type   = "rate" -> null
          }
      }
    + rule {
        + trigger = "occurrence_rate"

        + filters {
            + count  = 75
            + period = 300
            + type   = "rate"
          }
        + filters {
            + count     = 0
            + operation = "gte"
            + period    = 0
            + type      = "level"
            + value     = "error"
          }
      }
      # (1 unchanged block hidden)
  }

I don't really understand what's going on here or what the change it's detecting, it looks the same?

phillipuniverse commented 2 years ago

This might be a problem with multiple filters? I modified my config to remove the level >= error filter and now I correctly get no changes:

resource "rollbar_notification" "pd_high_occurrence" {
  provider = rollbar.notifications_provider
  channel  = "pagerduty"

  rule {
    trigger = "occurrence_rate"
    filters {
      type   = "rate"
      period = 300 # value in seconds, 5m
      count  = 75
    }
  }
  config {
    service_key = var.pd_service_key
  }
}
alinazabara commented 2 years ago

Same here (for Slack notifications for occurrence_rate with multiple triggers). Adding count = 0 and period = 0 to additional filters doesn't help.

zdavis-rollbar commented 4 months ago

@phillipuniverse and @alinazabara. I know this is an old issue and a difficult one to solve. The issue is indeed with the ordering of the filters; since this is the list, the order matters. The problem is documented here

While its not perfect, our API returns type=level as the last filter, so if you switch the order in the terraform file then it will mitigate the problem - you won't see any differences

Here is an example:

resource "rollbar_notification" "pd_high_occurrence" {
  channel  = "pagerduty"

  rule {
    trigger = "occurrence_rate"
    filters {
      type   = "rate"
      period = 300 # value in seconds, 5m
      count  = 75
    }
    filters {
      type      = "level"
      operation = "gte"
      value     = "error"
    }
  }
  config {
    service_key = "some_key"
  }
}