Skip to content

Commit 10576c0

Browse files
haustenirvingniharika-98
authored andcommitted
Updated AlertPolicy resource to include sql condition types. (GoogleCloudPlatform#12971)
1 parent 37b3cff commit 10576c0

File tree

2 files changed

+371
-0
lines changed

2 files changed

+371
-0
lines changed

mmv1/products/monitoring/AlertPolicy.yaml

+144
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,150 @@ properties:
925925
926926
Users with the `monitoring.alertPolicyViewer` role are able to see the
927927
name of the non-existent metric in the alerting policy condition.
928+
- name: 'conditionSql'
929+
type: NestedObject
930+
description: |
931+
A condition that allows alerting policies to be defined using GoogleSQL.
932+
SQL conditions examine a sliding window of logs using GoogleSQL.
933+
Alert policies with SQL conditions may incur additional billing.
934+
properties:
935+
- name: 'query'
936+
type: String
937+
description: |
938+
The Log Analytics SQL query to run, as a string. The query must
939+
conform to the required shape. Specifically, the query must not try to
940+
filter the input by time. A filter will automatically be applied
941+
to filter the input so that the query receives all rows received
942+
since the last time the query was run.
943+
required: true
944+
- name: 'minutes'
945+
type: NestedObject
946+
description: |
947+
Used to schedule the query to run every so many minutes.
948+
properties:
949+
- name: 'periodicity'
950+
type: Integer
951+
description: |
952+
Number of minutes between runs. The interval must be greater than or
953+
equal to 5 minutes and less than or equal to 1440 minutes.
954+
required: true
955+
exactly_one_of:
956+
- 'minutes'
957+
- 'hourly'
958+
- 'daily'
959+
- name: 'hourly'
960+
type: NestedObject
961+
description: |
962+
Used to schedule the query to run every so many hours.
963+
properties:
964+
- name: 'periodicity'
965+
type: Integer
966+
description: |
967+
Number of hours between runs. The interval must be greater than or
968+
equal to 1 hour and less than or equal to 48 hours.
969+
required: true
970+
- name: 'minuteOffset'
971+
type: Integer
972+
description: |
973+
The number of minutes after the hour (in UTC) to run the query.
974+
Must be greater than or equal to 0 minutes and less than or equal to
975+
59 minutes. If left unspecified, then an arbitrary offset is used.
976+
exactly_one_of:
977+
- 'minutes'
978+
- 'hourly'
979+
- 'daily'
980+
- name: 'daily'
981+
type: NestedObject
982+
description: |
983+
Used to schedule the query to run every so many days.
984+
properties:
985+
- name: periodicity
986+
type: Integer
987+
description: |
988+
The number of days between runs. Must be greater than or equal
989+
to 1 day and less than or equal to 30 days.
990+
required: true
991+
- name: 'executionTime'
992+
type: NestedObject
993+
description: |
994+
The time of day (in UTC) at which the query should run. If left
995+
unspecified, the server picks an arbitrary time of day and runs
996+
the query at the same time each day.
997+
properties:
998+
- name: 'hours'
999+
type: Integer
1000+
description: |
1001+
Hours of a day in 24 hour format. Must be greater than or equal
1002+
to 0 and typically must be less than or equal to 23. An API may
1003+
choose to allow the value "24:00:00" for scenarios like business
1004+
closing time.
1005+
- name: 'minutes'
1006+
type: Integer
1007+
description: |
1008+
Minutes of an hour. Must be greater than or equal to 0 and
1009+
less than or equal to 59.
1010+
- name: 'seconds'
1011+
type: Integer
1012+
description: |
1013+
Seconds of a minute. Must be greater than or equal to 0 and
1014+
typically must be less than or equal to 59. An API may allow the
1015+
value 60 if it allows leap-seconds.
1016+
- name: 'nanos'
1017+
type: Integer
1018+
description: |
1019+
Fractions of seconds, in nanoseconds. Must be greater than or
1020+
equal to 0 and less than or equal to 999,999,999.
1021+
exactly_one_of:
1022+
- 'minutes'
1023+
- 'hourly'
1024+
- 'daily'
1025+
- name: 'rowCountTest'
1026+
type: NestedObject
1027+
description: |
1028+
Test the row count against a threshold.
1029+
properties:
1030+
- name: 'comparison'
1031+
type: Enum
1032+
description: |
1033+
The comparison to apply between the time
1034+
series (indicated by filter and aggregation)
1035+
and the threshold (indicated by
1036+
threshold_value). The comparison is applied
1037+
on each time series, with the time series on
1038+
the left-hand side and the threshold on the
1039+
right-hand side. Only COMPARISON_LT and
1040+
COMPARISON_GT are supported currently.
1041+
enum_values:
1042+
- 'COMPARISON_GT'
1043+
- 'COMPARISON_GE'
1044+
- 'COMPARISON_LT'
1045+
- 'COMPARISON_LE'
1046+
- 'COMPARISON_EQ'
1047+
- 'COMPARISON_NE'
1048+
required: true
1049+
- name: 'threshold'
1050+
type: Integer
1051+
description: |
1052+
Test the boolean value in the indicated column.
1053+
required: true
1054+
exactly_one_of:
1055+
- 'rowCountTest'
1056+
- 'booleanTest'
1057+
- name: 'booleanTest'
1058+
type: NestedObject
1059+
description: |
1060+
The start date and time of the query. If left unspecified, then the
1061+
query will start immediately.
1062+
properties:
1063+
- name: 'column'
1064+
type: String
1065+
description: |
1066+
The name of the column containing the boolean value. If the value
1067+
in a row is NULL, that row is ignored.
1068+
required: true
1069+
exactly_one_of:
1070+
- 'rowCountTest'
1071+
- 'booleanTest'
9281072
- name: 'notificationChannels'
9291073
type: Array
9301074
# TODO chrisst - turn this into a resource ref

mmv1/third_party/terraform/services/monitoring/resource_monitoring_alert_policy_test.go

+227
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestAccMonitoringAlertPolicy(t *testing.T) {
2222
"log": testAccMonitoringAlertPolicy_log,
2323
"forecast": testAccMonitoringAlertPolicy_forecast,
2424
"promql": testAccMonitoringAlertPolicy_promql,
25+
"sql": testAccMonitoringAlertPolicy_sql,
2526
}
2627

2728
for name, tc := range testCases {
@@ -235,6 +236,24 @@ func testAccMonitoringAlertPolicy_promql(t *testing.T) {
235236
})
236237
}
237238

239+
func testAccMonitoringAlertPolicy_sql(t *testing.T) {
240+
241+
acctest.VcrTest(t, resource.TestCase{
242+
PreCheck: func() { acctest.AccTestPreCheck(t) },
243+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
244+
CheckDestroy: testAccCheckAlertPolicyDestroyProducer(t),
245+
Steps: []resource.TestStep{
246+
{
247+
Config: testAccMonitoringAlertPolicy_sqlCfg(),
248+
// SQL alerts require additional GCP resources to be created and billed,
249+
// so we only run the plan test for now.
250+
PlanOnly: true,
251+
ExpectNonEmptyPlan: true,
252+
},
253+
},
254+
})
255+
}
256+
238257
func testAccMonitoringAlertPolicy_basicCfg(alertName, conditionName, aligner, filter, severity string) string {
239258
return fmt.Sprintf(`
240259
resource "google_monitoring_alert_policy" "basic" {
@@ -479,3 +498,211 @@ resource "google_monitoring_alert_policy" "promql" {
479498
}
480499
`, alertName, conditionName)
481500
}
501+
502+
func testAccMonitoringAlertPolicy_sqlCfg() string {
503+
return fmt.Sprintf(`
504+
resource "google_monitoring_alert_policy" "sql_minutes_row_count" {
505+
display_name = "minutes_row_count"
506+
combiner = "OR"
507+
enabled = true
508+
509+
conditions {
510+
display_name = "minutes_row_count"
511+
512+
condition_sql {
513+
query = "SELECT severity, resource FROM project.global._Default._AllLogs WHERE severity IS NOT NULL"
514+
minutes {
515+
periodicity = 30
516+
}
517+
row_count_test {
518+
comparison = "COMPARISON_GT"
519+
threshold = "0"
520+
}
521+
}
522+
}
523+
524+
severity = "WARNING"
525+
526+
documentation {
527+
content = "test content"
528+
mime_type = "text/markdown"
529+
subject = "test subject"
530+
links {
531+
display_name = "link display name"
532+
url = "http://mydomain.com"
533+
}
534+
}
535+
}
536+
resource "google_monitoring_alert_policy" "sql_minutes_boolean" {
537+
display_name = "minutes_boolean"
538+
combiner = "OR"
539+
enabled = true
540+
541+
conditions {
542+
display_name = "minutes_boolean"
543+
544+
condition_sql {
545+
query = "SELECT severity, resource FROM project.global._Default._AllLogs WHERE severity IS NOT NULL"
546+
minutes {
547+
periodicity = 30
548+
}
549+
boolean_test {
550+
column = "resource"
551+
}
552+
}
553+
}
554+
555+
severity = "WARNING"
556+
557+
documentation {
558+
content = "test content"
559+
mime_type = "text/markdown"
560+
subject = "test subject"
561+
links {
562+
display_name = "link display name"
563+
url = "http://mydomain.com"
564+
}
565+
}
566+
}
567+
resource "google_monitoring_alert_policy" "sql_hourly_row_count" {
568+
display_name = "hourly_row_count"
569+
combiner = "OR"
570+
enabled = true
571+
572+
conditions {
573+
display_name = "hourly_row_count"
574+
575+
condition_sql {
576+
query = "SELECT severity, resource FROM project.global._Default._AllLogs WHERE severity IS NOT NULL"
577+
hourly {
578+
periodicity = 3
579+
minute_offset = 10
580+
}
581+
row_count_test {
582+
comparison = "COMPARISON_GT"
583+
threshold = "0"
584+
}
585+
}
586+
}
587+
588+
severity = "WARNING"
589+
590+
documentation {
591+
content = "test content"
592+
mime_type = "text/markdown"
593+
subject = "test subject"
594+
links {
595+
display_name = "link display name"
596+
url = "http://mydomain.com"
597+
}
598+
}
599+
}
600+
resource "google_monitoring_alert_policy" "sql_hourly_boolean" {
601+
display_name = "hourly_boolean"
602+
combiner = "OR"
603+
enabled = true
604+
605+
conditions {
606+
display_name = "hourly_boolean"
607+
608+
condition_sql {
609+
query = "SELECT severity, resource FROM project.global._Default._AllLogs WHERE severity IS NOT NULL"
610+
hourly {
611+
periodicity = 3
612+
minute_offset = 10
613+
}
614+
boolean_test {
615+
column = "resource"
616+
}
617+
}
618+
}
619+
620+
severity = "WARNING"
621+
622+
documentation {
623+
content = "test content"
624+
mime_type = "text/markdown"
625+
subject = "test subject"
626+
links {
627+
display_name = "link display name"
628+
url = "http://mydomain.com"
629+
}
630+
}
631+
}
632+
resource "google_monitoring_alert_policy" "sql_daily_row_count" {
633+
display_name = "daily_row_count"
634+
combiner = "OR"
635+
enabled = true
636+
637+
conditions {
638+
display_name = "daily_row_count"
639+
640+
condition_sql {
641+
query = "SELECT severity, resource FROM project.global._Default._AllLogs WHERE severity IS NOT NULL"
642+
daily {
643+
periodicity = 3
644+
execution_time {
645+
hours = 10
646+
minutes = 10
647+
seconds = 10
648+
nanos = 10
649+
}
650+
}
651+
row_count_test {
652+
comparison = "COMPARISON_GT"
653+
threshold = "0"
654+
}
655+
}
656+
}
657+
658+
severity = "WARNING"
659+
660+
documentation {
661+
content = "test content"
662+
mime_type = "text/markdown"
663+
subject = "test subject"
664+
links {
665+
display_name = "link display name"
666+
url = "http://mydomain.com"
667+
}
668+
}
669+
}
670+
resource "google_monitoring_alert_policy" "sql_daily_boolean" {
671+
display_name = "daily_boolean"
672+
combiner = "OR"
673+
enabled = true
674+
675+
conditions {
676+
display_name = "daily_boolean"
677+
678+
condition_sql {
679+
query = "SELECT severity, resource FROM project.global._Default._AllLogs WHERE severity IS NOT NULL"
680+
daily {
681+
periodicity = 3
682+
execution_time {
683+
hours = 10
684+
minutes = 10
685+
seconds = 10
686+
nanos = 10
687+
}
688+
}
689+
boolean_test {
690+
column = "resource"
691+
}
692+
}
693+
}
694+
695+
severity = "WARNING"
696+
697+
documentation {
698+
content = "test content"
699+
mime_type = "text/markdown"
700+
subject = "test subject"
701+
links {
702+
display_name = "link display name"
703+
url = "http://mydomain.com"
704+
}
705+
}
706+
}
707+
`)
708+
}

0 commit comments

Comments
 (0)