@@ -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,61 @@ 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 . json_payload_labels
316
+ content {
317
+ key = labels. value
318
+ value_type = " STRING"
319
+ description = " Extracted from jsonPayload: ${ labels . value } "
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
+ {
330
+ for label in each . value . json_payload_labels :
331
+ label = > " EXTRACT(jsonPayload.${ label } )"
332
+ }
333
+ )
315
334
}
316
335
317
336
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
337
+ for_each = local . create_json_logging_alert_policy ? var. log_based_json_alert_policies : {}
319
338
320
- project = var. project_id
321
-
322
- display_name = " LogBasedJSON-${ local . resource_value } "
339
+ project = var. project_id
340
+ display_name = " ${ each . key } -${ local . resource_value } "
323
341
severity = " ERROR"
324
342
combiner = " OR"
325
343
326
344
dynamic "conditions" {
327
- for_each = var . log_based_json_indicators
345
+ for_each = each . value . metric_keys
328
346
329
347
content {
330
- display_name = " ${ conditions . key } logging high"
348
+ display_name = " ${ conditions . value } - logging- high"
331
349
332
350
condition_threshold {
333
351
filter = <<- EOT
334
- metric.type="${ local . user_metric_root_prefix } / ${ local . resource_value } -${ conditions . key } "
335
- resource.type="${ local . resource_type } "
336
- EOT
352
+ metric.type="logging.googleapis.com/user/ ${ local . resource_value } -${ conditions . value } "
353
+ resource.type="${ local . resource_type } "
354
+ EOT
337
355
338
- duration = " ${ conditions . value . condition_threshold . window } s"
356
+ duration = " ${ each . value . condition_threshold . window } s"
339
357
comparison = " COMPARISON_GT"
340
- threshold_value = conditions . value . condition_threshold . threshold
358
+ threshold_value = each . value . condition_threshold . threshold
341
359
342
360
aggregations {
343
361
alignment_period = " 60s"
344
362
per_series_aligner = " ALIGN_SUM"
345
363
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 : []))
364
+ group_by_fields = distinct (concat (
365
+ [" resource.label.location" ],
366
+ each. value . additional_group_by_fields != null ? each. value . additional_group_by_fields : []
367
+ ))
347
368
}
348
369
349
370
trigger {
@@ -355,28 +376,25 @@ resource "google_monitoring_alert_policy" "json_payload_logging_alert_policy" {
355
376
356
377
alert_strategy {
357
378
auto_close = " ${ local . day } s"
358
-
359
379
notification_channel_strategy {
360
380
renotify_interval = " ${ local . day } s"
361
381
}
362
382
}
363
383
364
384
dynamic "documentation" {
365
- for_each = var . runbook_urls . json_based_logs != null ? [1 ] : []
385
+ for_each = each . value . runbook_url != null ? [1 ] : []
366
386
content {
367
- content = var . runbook_urls . json_based_logs
387
+ content = each . value . runbook_url
368
388
mime_type = " text/markdown"
369
389
}
370
390
}
371
391
372
392
notification_channels = var. notification_channels_non_paging
373
393
374
- depends_on = [
375
- google_logging_metric . json_payload_logging_metric
376
- ]
394
+ depends_on = [google_logging_metric . json_payload_logging_metric ]
377
395
}
378
396
379
- # CR service specific #
397
+ # CR service specific #
380
398
381
399
resource "google_monitoring_alert_policy" "service_4xx_alert_policy" {
382
400
count = ! local. is_job && var. service_4xx_configuration . enabled ? 1 : 0
@@ -565,7 +583,7 @@ resource "google_monitoring_alert_policy" "service_max_conns_alert_policy" {
565
583
metric.type="${ local . metric_root_prefix } /container/max_request_concurrencies"
566
584
resource.type="${ local . resource_type } "
567
585
resource.label.${ local . resource_label } ="${ local . resource_value } "
568
- ${ var . service_max_conns_configuration . additional_filters != null ? var . service_max_conns_configuration . additional_filters : " " }
586
+ ${ var . service_max_conns_configuration . additional_filters != null ? var . service_max_conns_configuration . additional_filters : " " }
569
587
EOT
570
588
571
589
duration = " ${ var . service_max_conns_configuration . window } s"
@@ -624,7 +642,7 @@ resource "google_monitoring_alert_policy" "job_failure_alert_policy" {
624
642
metric.label.result="failed"
625
643
resource.type="${ local . resource_type } "
626
644
resource.label.${ local . resource_label } ="${ local . resource_value } "
627
- ${ var . job_failure_configuration . additional_filters != null ? var . job_failure_configuration . additional_filters : " " }
645
+ ${ var . job_failure_configuration . additional_filters != null ? var . job_failure_configuration . additional_filters : " " }
628
646
EOT
629
647
630
648
duration = " ${ var . job_failure_configuration . window } s"
0 commit comments