@@ -30,6 +30,9 @@ locals {
30
30
minute = 60 * local. second
31
31
hour = 60 * local. minute
32
32
day = 24 * local. hour
33
+
34
+
35
+ create_json_logging_alert_policy = length (keys (var. log_based_json_indicators )) > 0 && var. enable_log_based_json_indicators
33
36
}
34
37
35
38
# Common forward progress #
@@ -296,6 +299,7 @@ resource "google_logging_metric" "json_payload_logging_metric" {
296
299
metric_descriptor {
297
300
metric_kind = " DELTA"
298
301
value_type = " INT64"
302
+
299
303
labels {
300
304
key = " location"
301
305
value_type = " STRING"
@@ -306,44 +310,58 @@ resource "google_logging_metric" "json_payload_logging_metric" {
306
310
value_type = " STRING"
307
311
description = " name of service"
308
312
}
309
- }
310
313
311
- label_extractors = {
312
- " location" = " EXTRACT(resource.labels.location)"
313
- " service_name" = " EXTRACT(resource.labels.service_name)"
314
+ dynamic "labels" {
315
+ for_each = each. value . label_extractors
316
+ content {
317
+ key = labels. key
318
+ value_type = " STRING"
319
+ description = " Custom extracted label: ${ labels . key } "
320
+ }
321
+ }
314
322
}
323
+
324
+ label_extractors = merge (
325
+ {
326
+ " location" = " EXTRACT(resource.labels.location)"
327
+ " service_name" = " EXTRACT(resource.labels.service_name)"
328
+ },
329
+ each. value . label_extractors
330
+ )
315
331
}
316
332
317
333
resource "google_monitoring_alert_policy" "json_payload_logging_alert_policy" {
318
- count = length ( keys (var . log_based_json_indicators )) > 0 && var. enable_log_based_json_indicators ? 1 : 0
334
+ for_each = local . create_json_logging_alert_policy ? var. log_based_json_alert_policies : {}
319
335
320
- project = var. project_id
321
-
322
- display_name = " LogBasedJSON-${ local . resource_value } "
336
+ project = var. project_id
337
+ display_name = " ${ each . key } -${ local . resource_value } "
323
338
severity = " ERROR"
324
339
combiner = " OR"
325
340
326
341
dynamic "conditions" {
327
- for_each = var . log_based_json_indicators
342
+ for_each = each . value . metric_keys
328
343
329
344
content {
330
- display_name = " ${ conditions . key } logging high"
345
+ display_name = " ${ conditions . value } - logging- high"
331
346
332
347
condition_threshold {
333
348
filter = <<- EOT
334
- metric.type= "${ local . user_metric_root_prefix } /${ local . resource_value } -${ conditions . key } "
335
- resource.type="${ local . resource_type } "
336
- EOT
349
+ metric.type = "${ local . user_metric_root_prefix } /${ local . resource_value } -${ conditions . value } "
350
+ resource.type="${ local . resource_type } "
351
+ EOT
337
352
338
- duration = " ${ conditions . value . condition_threshold . window } s"
353
+ duration = " ${ each . value . condition_threshold . window } s"
339
354
comparison = " COMPARISON_GT"
340
- threshold_value = conditions . value . condition_threshold . threshold
355
+ threshold_value = each . value . condition_threshold . threshold
341
356
342
357
aggregations {
343
358
alignment_period = " 60s"
344
359
per_series_aligner = " ALIGN_SUM"
345
360
cross_series_reducer = " REDUCE_SUM"
346
- group_by_fields = distinct (concat (local. default_group_by_fields , conditions. value . additional_group_by_fields != null ? conditions. value . additional_group_by_fields : []))
361
+ group_by_fields = distinct (concat (
362
+ local. default_group_by_fields ,
363
+ conditions. value . additional_group_by_fields != null ? conditions. value . additional_group_by_fields : []
364
+ ))
347
365
}
348
366
349
367
trigger {
@@ -355,28 +373,25 @@ resource "google_monitoring_alert_policy" "json_payload_logging_alert_policy" {
355
373
356
374
alert_strategy {
357
375
auto_close = " ${ local . day } s"
358
-
359
376
notification_channel_strategy {
360
377
renotify_interval = " ${ local . day } s"
361
378
}
362
379
}
363
380
364
381
dynamic "documentation" {
365
- for_each = var . runbook_urls . json_based_logs != null ? [1 ] : []
382
+ for_each = each . value . runbook_url != null ? [1 ] : []
366
383
content {
367
- content = var . runbook_urls . json_based_logs
384
+ content = each . value . runbook_url
368
385
mime_type = " text/markdown"
369
386
}
370
387
}
371
388
372
389
notification_channels = var. notification_channels_non_paging
373
390
374
- depends_on = [
375
- google_logging_metric . json_payload_logging_metric
376
- ]
391
+ depends_on = [google_logging_metric . json_payload_logging_metric ]
377
392
}
378
393
379
- # CR service specific #
394
+ # CR service specific #
380
395
381
396
resource "google_monitoring_alert_policy" "service_4xx_alert_policy" {
382
397
count = ! local. is_job && var. service_4xx_configuration . enabled ? 1 : 0
@@ -565,7 +580,7 @@ resource "google_monitoring_alert_policy" "service_max_conns_alert_policy" {
565
580
metric.type="${ local . metric_root_prefix } /container/max_request_concurrencies"
566
581
resource.type="${ local . resource_type } "
567
582
resource.label.${ local . resource_label } ="${ local . resource_value } "
568
- ${ var . service_max_conns_configuration . additional_filters != null ? var . service_max_conns_configuration . additional_filters : " " }
583
+ ${ var . service_max_conns_configuration . additional_filters != null ? var . service_max_conns_configuration . additional_filters : " " }
569
584
EOT
570
585
571
586
duration = " ${ var . service_max_conns_configuration . window } s"
@@ -624,7 +639,7 @@ resource "google_monitoring_alert_policy" "job_failure_alert_policy" {
624
639
metric.label.result="failed"
625
640
resource.type="${ local . resource_type } "
626
641
resource.label.${ local . resource_label } ="${ local . resource_value } "
627
- ${ var . job_failure_configuration . additional_filters != null ? var . job_failure_configuration . additional_filters : " " }
642
+ ${ var . job_failure_configuration . additional_filters != null ? var . job_failure_configuration . additional_filters : " " }
628
643
EOT
629
644
630
645
duration = " ${ var . job_failure_configuration . window } s"
0 commit comments