Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for promQL condition type in AlertPolicy #8448

Merged
merged 11 commits into from
Jul 26, 2023
72 changes: 72 additions & 0 deletions mmv1/products/monitoring/AlertPolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,78 @@ properties:
a separate rule for the purposes of triggering notifications.
Label keys and corresponding values can be used in notifications
generated by this condition.
- !ruby/object:Api::Type::NestedObject
name: conditionPrometheusQueryLanguage
description: |
A Monitoring Query Language query that outputs a boolean stream

A condition type that allows alert policies to be defined using
Prometheus Query Language (PromQL).

The PrometheusQueryLanguageCondition message contains information
from a Prometheus alerting rule and its associated rule group.
properties:
- !ruby/object:Api::Type::String
name: query
description: |
The PromQL expression to evaluate. Every evaluation cycle this
expression is evaluated at the current time, and all resultant time
series become pending/firing alerts. This field must not be empty.
required: true
- !ruby/object:Api::Type::String
name: duration
description: |
Alerts are considered firing once their PromQL expression evaluated
to be "true" for this long. Alerts whose PromQL expression was not
evaluated to be "true" for long enough are considered pending. The
default value is zero. Must be zero or positive.
- !ruby/object:Api::Type::String
name: evaluationInterval
required: true
description: |
How often this rule should be evaluated. Must be a positive multiple
of 30 seconds or missing. The default value is 30 seconds. If this
PrometheusQueryLanguageCondition was generated from a Prometheus
alerting rule, then this value should be taken from the enclosing
rule group.
- !ruby/object:Api::Type::KeyValuePairs
name: labels
description: |
Labels to add to or overwrite in the PromQL query result. Label names
must be valid.

Label values can be templatized by using variables. The only available
variable names are the names of the labels in the PromQL result, including
"__name__" and "value". "labels" may be empty. This field is intended to be
used for organizing and identifying the AlertPolicy
- !ruby/object:Api::Type::String
name: ruleGroup
description: |
The rule group name of this alert in the corresponding Prometheus
configuration file.

Some external tools may require this field to be populated correctly
in order to refer to the original Prometheus configuration file.
The rule group name and the alert name are necessary to update the
relevant AlertPolicies in case the definition of the rule group changes
in the future.

This field is optional. If this field is not empty, then it must be a
valid Prometheus label name.
- !ruby/object:Api::Type::String
name: alertRule
description: |
The alerting rule name of this alert in the corresponding Prometheus
configuration file.

Some external tools may require this field to be populated correctly
in order to refer to the original Prometheus configuration file.
The rule group name and the alert name are necessary to update the
relevant AlertPolicies in case the definition of the rule group changes
in the future.

This field is optional. If this field is not empty, then it must be a
valid Prometheus label name.
required: true
- !ruby/object:Api::Type::Array
name: 'notificationChannels'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestAccMonitoringAlertPolicy(t *testing.T) {
"mql": testAccMonitoringAlertPolicy_mql,
"log": testAccMonitoringAlertPolicy_log,
"forecast": testAccMonitoringAlertPolicy_forecast,
"promql": testAccMonitoringAlertPolicy_promql,
}

for name, tc := range testCases {
Expand Down Expand Up @@ -210,6 +211,28 @@ func testAccMonitoringAlertPolicy_forecast(t *testing.T) {
})
}

func testAccMonitoringAlertPolicy_promql(t *testing.T) {

alertName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
conditionName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckAlertPolicyDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccMonitoringAlertPolicy_promqlCfg(alertName, conditionName),
},
{
ResourceName: "google_monitoring_alert_policy.promql",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccMonitoringAlertPolicy_basicCfg(alertName, conditionName, aligner, filter string) string {
return fmt.Sprintf(`
resource "google_monitoring_alert_policy" "basic" {
Expand Down Expand Up @@ -393,3 +416,33 @@ resource "google_monitoring_alert_policy" "forecast" {
}
`, alertName, conditionName, aligner, filter)
}

func testAccMonitoringAlertPolicy_promqlCfg(alertName, conditionName string) string {
return fmt.Sprintf(`
resource "google_monitoring_alert_policy" "promql" {
display_name = "%s"
combiner = "OR"
enabled = true

conditions {
display_name = "%s"

condition_prometheus_query_language {
query = "vector(1)"
duration = "60s"
evaluation_interval = "60s"
labels = {
"severity" = "page"
}
alert_rule = "AlwaysOn"
rule_group = "abc"
}
}

documentation {
content = "test content"
mime_type = "text/markdown"
}
}
`, alertName, conditionName)
}