From 3d8bc6a69b7ea3b77d2220b7ef636eb50d6f82ce Mon Sep 17 00:00:00 2001 From: ArtiomMatiom Date: Mon, 8 Jul 2024 09:57:20 +0200 Subject: [PATCH] service plan diagnostic settings --- modules/azure/service_plan/main.tf | 32 +++++++++++++++++++++++++ modules/azure/service_plan/variables.tf | 11 +++++++++ 2 files changed, 43 insertions(+) diff --git a/modules/azure/service_plan/main.tf b/modules/azure/service_plan/main.tf index 4b8e58a8..654f3750 100644 --- a/modules/azure/service_plan/main.tf +++ b/modules/azure/service_plan/main.tf @@ -84,3 +84,35 @@ resource "azurerm_monitor_autoscale_setting" "autoscale_setting" { } } } + + +data "azurerm_monitor_diagnostic_categories" "diagnostic_categories" { + count = var.loganalytics_diagnostic_setting == null ? 0 : 1 + resource_id = azurerm_service_plan.sp.id +} + +resource "azurerm_monitor_diagnostic_setting" "diagnostic_setting" { + count = var.loganalytics_diagnostic_setting == null ? 0 : 1 + name = "diag-${var.name}" + target_resource_id = azurerm_service_plan.sp.id + log_analytics_workspace_id = var.loganalytics_diagnostic_setting.workspace_id + + + dynamic "enabled_log" { + for_each = var.loganalytics_diagnostic_setting.categories == null ? data.azurerm_monitor_diagnostic_categories.diagnostic_categories[0].log_category_types : var.loganalytics_diagnostic_setting.categories + + content { + category = enabled_log.value + } + } + + dynamic "metric" { + for_each = var.loganalytics_diagnostic_setting.metrics == null ? data.azurerm_monitor_diagnostic_categories.diagnostic_categories[0].metrics : var.loganalytics_diagnostic_setting.metrics + + content { + category = metric.value + enabled = true + } + } +} + diff --git a/modules/azure/service_plan/variables.tf b/modules/azure/service_plan/variables.tf index 1c5f1ccd..e214f19c 100644 --- a/modules/azure/service_plan/variables.tf +++ b/modules/azure/service_plan/variables.tf @@ -65,3 +65,14 @@ variable "scaling_rules" { } ] } + + +variable "loganalytics_diagnostic_setting" { + type = object({ + workspace_id = string, // log analytics workspace ID + categories = optional(list(string)), // null list,means send all categories + metrics = optional(list(string)), // null list means send all metrics + }) + description = "Specifies the log categories that have to be sent to Log analytics." + default = null +}