Skip to content

Commit b9087eb

Browse files
authored
Merge pull request #2 from data-platform-hq/add-log-analytics-link
fix: add log analytics link
2 parents 8523d19 + f587928 commit b9087eb

File tree

3 files changed

+128
-17
lines changed

3 files changed

+128
-17
lines changed

README.md

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,37 @@ No modules.
2323

2424
## Resources
2525

26-
| Name | Type |
27-
| ----------------------------------------------------------------------------------------------------------------------------- | -------- |
28-
| [azurerm_mssql_database.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mssql_database) | resource |
26+
| Name | Type |
27+
|---------------------------------------------------------------------------------------------------------------------------------------------------------------| -------- |
28+
| [azurerm_mssql_database.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mssql_database) | resource |
29+
| [azurerm_monitor_diagnostic_setting.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_diagnostic_setting) | resource |
2930

3031
## Inputs
3132

3233
| Name | Description | Type | Default | Required |
33-
| --------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------ | ---------------------------- | :------: |
34-
| <a name="input_server_id"></a> [server\_id](#input\_server\_id) | Id of SQL server | `string` | n/a | yes |
35-
| <a name="input_server_fqdn"></a> [server\_fqdn](#input\_server\_fqdn) | FQDN of Azure SQL Server | `string` | n/a | yes |
36-
| <a name="input_tags"></a> [tags](#input\_tags) | tags for resources | `map(string)` | {} | no |
37-
| <a name="input_default_collation"></a> [default\_collation](#input\_default\_collation) | Specifies the collation of the database | `string` | SQL_Latin1_General_CP1_CI_AS | no |
38-
| <a name="input_default_sku"></a> [default\_sku](#input\_default\_sku) | Specifies the SKU of the database | `string` | GP_S_Gen5_1 | no |
39-
| <a name="input_default_max_size"></a> [default\_max_size](#input\_default\_max\_size) | The max size of the database in gigabytes | `string` | 20 | no |
40-
| <a name="input_default_min_capacity"></a> [default\_min\_capacity](#input\_default\_min\_capacity) | The min size of the database in gigabytes | `string` | 0.5 | no |
41-
| <a name="input_default_autopause_delay"></a> [default\_autopause\_delay](#input\_default\_autopause\_delay) | Time in minutes after which database is automatically paused. A value of -1 means that automatic pause is disabled | `string` | 60 | no |
42-
| <a name="input_default_retention_days"></a> [default\_retention\_days](#input\_default\_retention\_days) | Specifies the number of days to keep in the Threat Detection audit logs | `string` | 3 | no |
43-
| <a name="input_default_create_mode"></a> [default\_create\_mode](#input\_default\_create\_mode) | Type of create mode selected in database config object | `string` | Default | no |
44-
| <a name="input_default_creation_source_database_id"></a> [default\_creation\_source\_database\_id](#input\_default\_creation\_source\_database\_id) | This variable is used in case 'create_mode'='Copy' | `string` | null | no |
45-
| <a name="input_storage_account_type"></a> [storage\_account\_type](#input\_storage\_account\_type) | Specifies the storage account type used to store backups for this database | `string` | ZRS | no |
46-
| <a name="input_databases"></a> [databases](#input\_databases) | "Map of databases | `map(map(string))` | {} | no |
34+
|-----------------------------------------------------------------------------------------------------------------------------------------------------| ------------------------------------------------------------------------------------------------------------------ | ------------------ | ---------------------------- | :------: |
35+
| <a name="input_project"></a> [project](#input\_project) | Project name | `string` | n/a | yes |
36+
| <a name="input_env"></a> [env](#input\_env) | Environment name | `string` | n/a | yes |
37+
| <a name="input_location"></a> [location](#input\_location) | Azure location | `string` | n/a | yes |
38+
| <a name="input_server_id"></a> [server\_id](#input\_server\_id) | Id of SQL server | `string` | n/a | yes |
39+
| <a name="input_server_fqdn"></a> [server\_fqdn](#input\_server\_fqdn) | FQDN of Azure SQL Server | `string` | n/a | yes |
40+
| <a name="input_tags"></a> [tags](#input\_tags) | tags for resources | `map(string)` | {} | no |
41+
| <a name="input_default_collation"></a> [default\_collation](#input\_default\_collation) | Specifies the collation of the database | `string` | SQL_Latin1_General_CP1_CI_AS | no |
42+
| <a name="input_default_sku"></a> [default\_sku](#input\_default\_sku) | Specifies the SKU of the database | `string` | GP_S_Gen5_1 | no |
43+
| <a name="input_default_max_size"></a> [default\_max_size](#input\_default\_max\_size) | The max size of the database in gigabytes | `string` | 20 | no |
44+
| <a name="input_default_min_capacity"></a> [default\_min\_capacity](#input\_default\_min\_capacity) | The min size of the database in gigabytes | `string` | 0.5 | no |
45+
| <a name="input_default_autopause_delay"></a> [default\_autopause\_delay](#input\_default\_autopause\_delay) | Time in minutes after which database is automatically paused. A value of -1 means that automatic pause is disabled | `string` | 60 | no |
46+
| <a name="input_default_retention_days"></a> [default\_retention\_days](#input\_default\_retention\_days) | Specifies the number of days to keep in the Threat Detection audit logs | `string` | 3 | no |
47+
| <a name="input_default_create_mode"></a> [default\_create\_mode](#input\_default\_create\_mode) | Type of create mode selected in database config object | `string` | Default | no |
48+
| <a name="input_default_creation_source_database_id"></a> [default\_creation\_source\_database\_id](#input\_default\_creation\_source\_database\_id) | This variable is used in case 'create_mode'='Copy' | `string` | null | no |
49+
| <a name="input_storage_account_type"></a> [storage\_account\_type](#input\_storage\_account\_type) | Specifies the storage account type used to store backups for this database | `string` | ZRS | no |
50+
| <a name="input_databases"></a> [databases](#input\_databases) | "Map of databases | `map(map(string))` | {} | no |
51+
| <a name="input_log_analytics_workspace_id"></a> [log\_analytics\_workspace\_id](#input\_log\_analytics\_workspace\_id) | Log Analytics Workspace ID | `string` | n/a | no |
52+
| <a name="input_log_category_list"></a> [log\_category\_list](#input\_log\_category\_list) | Category list log | `list(any)` | <pre>[<br> "QueryStoreRuntimeStatistics", <br> "QueryStoreWaitStatistics", <br> "QueryStoreWaitStatistics", <br> "Errors", <br> "DatabaseWaitStatistics", <br> "Timeouts", <br> "Blocks", <br> "Deadlocks" <br>]</pre> | no |
53+
| <a name="input_category_list_metrics"></a> [category\_list\_metrics](#input\_category\_list\_metrics) | Category list metrics | `list(any)` | <pre>[<br> "Basic", <br> "WorkloadManagement" <br>]</pre> | no |
54+
| <a name="input_log_retention_days"></a> [log\_retention\_days](#input\_log\_retention\_days) | Retention log policy days | `number` | 7 | no |
55+
| <a name="input_metric_retention_days"></a> [metric\_retention\_days](#input\_metric\_retention\_days) | Retention metric policy days | `number` | 7 | no |
56+
| <a name="input_destination_type"></a> [destination\_type](#input\_destination\_type) | Log analytics destination type | `string` | Dedicated | no |
4757

4858
## Outputs
4959

monitoring.tf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
locals {
2+
dbname_lists = [for n in azurerm_mssql_database.this : n.name]
3+
}
4+
5+
resource "azurerm_monitor_diagnostic_setting" "this" {
6+
for_each = length(var.log_analytics_workspace_id) == 0 ? [] : toset(local.dbname_lists)
7+
8+
name = "monitoring-${var.project}-${var.env}-${var.location}"
9+
target_resource_id = azurerm_mssql_database.this[each.key].id
10+
log_analytics_workspace_id = var.log_analytics_workspace_id
11+
log_analytics_destination_type = var.destination_type
12+
13+
dynamic "log" {
14+
for_each = var.log_category_list
15+
content {
16+
category = log.value
17+
enabled = true
18+
19+
retention_policy {
20+
days = var.log_retention_days
21+
enabled = true
22+
}
23+
}
24+
}
25+
26+
dynamic "metric" {
27+
for_each = var.category_list_metrics
28+
content {
29+
category = metric.value
30+
enabled = true
31+
32+
retention_policy {
33+
days = var.metric_retention_days
34+
enabled = true
35+
}
36+
}
37+
}
38+
depends_on = [azurerm_mssql_database.this]
39+
}

variables.tf

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
variable "project" {
2+
type = string
3+
description = "Project name"
4+
}
5+
6+
variable "env" {
7+
type = string
8+
description = "Environment name"
9+
}
10+
11+
variable "location" {
12+
type = string
13+
description = "Azure location"
14+
}
15+
116
variable "server_id" {
217
type = string
318
description = "Id of SQL server"
@@ -73,3 +88,50 @@ variable "databases" {
7388
description = "Map of databases"
7489
default = {}
7590
}
91+
92+
variable "log_analytics_workspace_id" {
93+
type = string
94+
description = "Log Analytics Workspace ID"
95+
default = ""
96+
}
97+
98+
variable "log_category_list" {
99+
type = list(any)
100+
default = [
101+
"QueryStoreRuntimeStatistics",
102+
"QueryStoreWaitStatistics",
103+
"Errors",
104+
"DatabaseWaitStatistics",
105+
"Timeouts",
106+
"Blocks",
107+
"Deadlocks"
108+
]
109+
description = "Category list log"
110+
}
111+
112+
variable "category_list_metrics" {
113+
type = list(any)
114+
default = [
115+
"Basic",
116+
"WorkloadManagement"
117+
]
118+
description = "Category list metrics"
119+
}
120+
121+
variable "log_retention_days" {
122+
default = 7
123+
type = number
124+
description = "Retention log policy days"
125+
}
126+
127+
variable "metric_retention_days" {
128+
default = 7
129+
type = number
130+
description = "Retention metric policy days"
131+
}
132+
133+
variable "destination_type" {
134+
type = string
135+
default = "Dedicated"
136+
description = "Log analytics destination type"
137+
}

0 commit comments

Comments
 (0)