Skip to content

Commit

Permalink
Merge pull request #62 from SumoLogic/slo_conditions
Browse files Browse the repository at this point in the history
Added SLOConditions.
  • Loading branch information
arunpatyal authored Nov 28, 2022
2 parents d5bd64b + 2a27eeb commit 727ec9a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 13 deletions.
41 changes: 40 additions & 1 deletion inputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ variable "monitor_monitor_type" {
description = "The type of monitor, Logs or Metrics. Default Logs"
default = "Logs"
}
variable "monitor_slo_type" {
type = string
description = "The type of Slo, Sli or BurnRate. Required if Monitor Type is Slo."
default = null
}
variable "monitor_slo_id" {
type = string
description = "Slo Id. Required if Monitor Type is Slo."
default = null
}
#variable "monitor_evaluation_delay" { . #TODO
# type = string
# description = "Evaluation Delay."
# default = ""
# }
variable "monitor_is_disabled" {
type = bool
description = "Whether or not the monitor is disabled. Default false."
Expand All @@ -30,8 +45,9 @@ variable "group_notifications" {
}

variable "queries" {
type = map
type = map
description = "All queries for the monitor."
default = null
}

variable "triggers" {
Expand All @@ -47,8 +63,31 @@ variable "triggers" {
}
))
description = "Triggers for alerting."
default = null
}

variable "slo_burnrate_triggers" {
type = list(object(
{
threshold = number,
time_range = string,
trigger_type = string,
}
))
description = "Triggers for SLO Burn Rate alerting."
default = null
}

variable "slo_sli_triggers" {
type = list(object(
{
threshold = number,
trigger_type = string,
}
))
description = "Triggers for SLO Sli alerting."
default = null
}

variable "connection_notifications" {
type = list(object(
Expand Down
74 changes: 62 additions & 12 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
locals {
# Logs Monitor
LogsCriticalAlert = [for d in var.triggers: d if d.trigger_type =="Critical" && var.monitor_monitor_type == "Logs"]
LogsResolveCriticalAlert = [for d in var.triggers: d if d.trigger_type =="ResolvedCritical" && var.monitor_monitor_type == "Logs"]
LogsWarningAlert = [for d in var.triggers: d if d.trigger_type =="Warning" && var.monitor_monitor_type == "Logs"]
LogsResolveWarningAlert = [for d in var.triggers: d if d.trigger_type =="ResolvedWarning" && var.monitor_monitor_type == "Logs"]
LogsMissingData = [for d in var.triggers: d if d.trigger_type =="MissingData" && var.monitor_monitor_type == "Logs"]
LogsCriticalAlert = var.triggers != null ? [for d in var.triggers: d if d.trigger_type =="Critical" && var.monitor_monitor_type == "Logs"] : []
LogsResolveCriticalAlert = var.triggers != null ? [for d in var.triggers: d if d.trigger_type =="ResolvedCritical" && var.monitor_monitor_type == "Logs"] : []
LogsWarningAlert = var.triggers != null ? [for d in var.triggers: d if d.trigger_type =="Warning" && var.monitor_monitor_type == "Logs"] : []
LogsResolveWarningAlert = var.triggers != null ? [for d in var.triggers: d if d.trigger_type =="ResolvedWarning" && var.monitor_monitor_type == "Logs"] : []
LogsMissingData = var.triggers != null ? [for d in var.triggers: d if d.trigger_type =="MissingData" && var.monitor_monitor_type == "Logs"] : []

# Metrics Monitor
MetricsCriticalAlert = [for d in var.triggers: d if d.trigger_type =="Critical" && var.monitor_monitor_type == "Metrics"]
MetricsResolveCriticalAlert = [for d in var.triggers: d if d.trigger_type =="ResolvedCritical" && var.monitor_monitor_type == "Metrics"]
MetricsWarningAlert = [for d in var.triggers: d if d.trigger_type =="Warning" && var.monitor_monitor_type == "Metrics"]
MetricsResolveWarningAlert = [for d in var.triggers: d if d.trigger_type =="ResolvedWarning" && var.monitor_monitor_type == "Metrics"]
MetricsMissingData = [for d in var.triggers: d if d.trigger_type =="MissingData" && var.monitor_monitor_type == "Metrics"]
MetricsCriticalAlert = var.triggers != null ? [for d in var.triggers: d if d.trigger_type =="Critical" && var.monitor_monitor_type == "Metrics"] : []
MetricsResolveCriticalAlert = var.triggers != null ? [for d in var.triggers: d if d.trigger_type =="ResolvedCritical" && var.monitor_monitor_type == "Metrics"] : []
MetricsWarningAlert = var.triggers != null ? [for d in var.triggers: d if d.trigger_type =="Warning" && var.monitor_monitor_type == "Metrics"] : []
MetricsResolveWarningAlert = var.triggers != null ? [for d in var.triggers: d if d.trigger_type =="ResolvedWarning" && var.monitor_monitor_type == "Metrics"] : []
MetricsMissingData = var.triggers != null ? [for d in var.triggers: d if d.trigger_type =="MissingData" && var.monitor_monitor_type == "Metrics"] : []

# SLO SLI Monitor
SLOSLICriticalAlert = var.slo_sli_triggers != null ? [for d in var.slo_sli_triggers: d if d.trigger_type =="Critical" && var.monitor_monitor_type == "Slo"] : []
SLOSLIWarningAlert = var.slo_sli_triggers != null ? [for d in var.slo_sli_triggers: d if d.trigger_type =="Warning" && var.monitor_monitor_type == "Slo"] : []

# SLO Burn Rate Monitor
SLOBurnRateCriticalAlert = var.slo_burnrate_triggers != null ? [for d in var.slo_burnrate_triggers: d if d.trigger_type =="Critical" && var.monitor_monitor_type == "Slo"] : []
SLOBurnRateWarningAlert = var.slo_burnrate_triggers != null ? [for d in var.slo_burnrate_triggers: d if d.trigger_type =="Warning" && var.monitor_monitor_type == "Slo"] : []

hasLogsCriticalAlert = (length(local.LogsCriticalAlert) + length(local.LogsResolveCriticalAlert)) == 2
hasLogsWarningAlert = (length(local.LogsWarningAlert) + length(local.LogsResolveWarningAlert)) == 2
hasMetricsCriticalAlert = (length(local.MetricsCriticalAlert) + length(local.MetricsResolveCriticalAlert)) == 2
hasMetricsWarningAlert = (length(local.MetricsWarningAlert)+ length(local.MetricsResolveWarningAlert)) == 2
hasLogsMissingData = length(local.LogsMissingData) == 1
hasMetricsMissingData = length(local.MetricsMissingData) == 1
hasSLOSLICriticalAlert = length(local.SLOSLICriticalAlert) == 1
hasSLOSLIWarningAlert = length(local.SLOSLIWarningAlert) == 1
hasSLOBurnRateCriticalAlert = length(local.SLOBurnRateCriticalAlert) == 1
hasSLOBurnRateWarningAlert = length(local.SLOBurnRateWarningAlert) == 1
}

resource "sumologic_monitor" "tf_monitor" {
Expand All @@ -30,9 +42,11 @@ resource "sumologic_monitor" "tf_monitor" {
content_type = "Monitor"
monitor_type = var.monitor_monitor_type
group_notifications = var.group_notifications
slo_id = var.monitor_slo_id


dynamic "queries" {
for_each = var.queries
for_each = var.queries == null ? {} : var.queries
content {
row_id = queries.key
query = queries.value
Expand Down Expand Up @@ -101,7 +115,7 @@ resource "sumologic_monitor" "tf_monitor" {
for_each = local.hasMetricsWarningAlert ? ["1"] : []
content {
time_range = local.MetricsWarningAlert[0].time_range
occurrence_type = local.MetricsCriticalAlert[0].occurrence_type
occurrence_type = local.MetricsWarningAlert[0].occurrence_type
alert {
threshold = local.MetricsWarningAlert[0].threshold
threshold_type = local.MetricsWarningAlert[0].threshold_type
Expand All @@ -121,6 +135,42 @@ resource "sumologic_monitor" "tf_monitor" {
trigger_source = local.MetricsMissingData[0].trigger_source
}
}
dynamic "slo_sli_condition" {
for_each = toset(var.monitor_monitor_type == "Slo" && var.monitor_slo_type == "Sli" ? ["1"] : [])
content {
dynamic "critical" {
for_each = local.hasSLOSLICriticalAlert ? ["1"] : []
content {
sli_threshold = local.SLOSLICriticalAlert[0].threshold
}
}
dynamic "warning" {
for_each = local.hasSLOSLIWarningAlert ? ["1"] : []
content {
sli_threshold = local.SLOSLIWarningAlert[0].threshold
}
}
}
}
dynamic "slo_burn_rate_condition" {
for_each = toset(var.monitor_monitor_type == "Slo" && var.monitor_slo_type == "BurnRate" ? ["1"] : [])
content {
dynamic "critical" {
for_each = local.hasSLOBurnRateCriticalAlert ? ["1"] : []
content {
time_range = local.SLOBurnRateCriticalAlert[0].time_range
burn_rate_threshold = local.SLOBurnRateCriticalAlert[0].threshold
}
}
dynamic "warning" {
for_each = local.hasSLOBurnRateWarningAlert ? ["1"] : []
content {
time_range = local.SLOBurnRateWarningAlert[0].time_range
burn_rate_threshold = local.SLOBurnRateWarningAlert[0].threshold
}
}
}
}
}

dynamic "notifications" {
Expand Down

0 comments on commit 727ec9a

Please sign in to comment.