Skip to content

Commit

Permalink
add wait time monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
Aohzan committed Aug 23, 2023
1 parent dbc3972 commit 00666d3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
10 changes: 10 additions & 0 deletions database/pgbouncer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Creates DataDog monitors with the following checks:
- PGBouncer database connection limit
- PGBouncer does not respond
- PGBouncer pool connection limit
- PGBouncer wait time

<!-- BEGIN_TF_DOCS -->
## Requirements
Expand Down Expand Up @@ -48,6 +49,7 @@ Creates DataDog monitors with the following checks:
| [datadog_monitor.pgbouncer_availability](https://registry.terraform.io/providers/DataDog/datadog/latest/docs/resources/monitor) | resource |
| [datadog_monitor.pgbouncer_database_connection_limit](https://registry.terraform.io/providers/DataDog/datadog/latest/docs/resources/monitor) | resource |
| [datadog_monitor.pgbouncer_pool_connection_limit](https://registry.terraform.io/providers/DataDog/datadog/latest/docs/resources/monitor) | resource |
| [datadog_monitor.pgbouncer_wait_time](https://registry.terraform.io/providers/DataDog/datadog/latest/docs/resources/monitor) | resource |

## Inputs

Expand Down Expand Up @@ -81,6 +83,13 @@ Creates DataDog monitors with the following checks:
| <a name="input_pgbouncer_pool_connection_limit_threshold_warning"></a> [pgbouncer\_pool\_connection\_limit\_threshold\_warning](#input\_pgbouncer\_pool\_connection\_limit\_threshold\_warning) | Warning number of connections limit monitor | `number` | `70` | no |
| <a name="input_pgbouncer_pool_connection_limit_time_aggregator"></a> [pgbouncer\_pool\_connection\_limit\_time\_aggregator](#input\_pgbouncer\_pool\_connection\_limit\_time\_aggregator) | Monitor time aggregator for PGBouncer connection monitor [available values: min, max or avg] | `string` | `"avg"` | no |
| <a name="input_pgbouncer_pool_connection_limit_timeframe"></a> [pgbouncer\_pool\_connection\_limit\_timeframe](#input\_pgbouncer\_pool\_connection\_limit\_timeframe) | Monitor timeframe for PGBouncer connection monitor [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`] | `string` | `"last_5m"` | no |
| <a name="input_pgbouncer_wait_time_enabled"></a> [pgbouncer\_wait\_time\_enabled](#input\_pgbouncer\_wait\_time\_enabled) | Flag to enable PGBouncer wait time monitor | `bool` | `true` | no |
| <a name="input_pgbouncer_wait_time_extra_tags"></a> [pgbouncer\_wait\_time\_extra\_tags](#input\_pgbouncer\_wait\_time\_extra\_tags) | Extra tags for PGBouncer wait time monitor | `list(string)` | `[]` | no |
| <a name="input_pgbouncer_wait_time_message"></a> [pgbouncer\_wait\_time\_message](#input\_pgbouncer\_wait\_time\_message) | Custom message for PGBouncer connection monitor | `string` | `""` | no |
| <a name="input_pgbouncer_wait_time_threshold_critical"></a> [pgbouncer\_wait\_time\_threshold\_critical](#input\_pgbouncer\_wait\_time\_threshold\_critical) | Critical number in μs of wait time monitor | `number` | `100000` | no |
| <a name="input_pgbouncer_wait_time_threshold_warning"></a> [pgbouncer\_wait\_time\_threshold\_warning](#input\_pgbouncer\_wait\_time\_threshold\_warning) | Warning number in μs of wait time monitor | `number` | `70000` | no |
| <a name="input_pgbouncer_wait_time_time_aggregator"></a> [pgbouncer\_wait\_time\_time\_aggregator](#input\_pgbouncer\_wait\_time\_time\_aggregator) | Monitor time aggregator for PGBouncer connection monitor [available values: min, max or avg] | `string` | `"avg"` | no |
| <a name="input_pgbouncer_wait_time_timeframe"></a> [pgbouncer\_wait\_time\_timeframe](#input\_pgbouncer\_wait\_time\_timeframe) | Monitor timeframe for PGBouncer connection monitor [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`] | `string` | `"last_5m"` | no |
| <a name="input_prefix_slug"></a> [prefix\_slug](#input\_prefix\_slug) | Prefix string to prepend between brackets on every monitors names | `string` | `""` | no |
| <a name="input_timeout_h"></a> [timeout\_h](#input\_timeout\_h) | Default auto-resolving state (in hours) | `number` | `0` | no |

Expand All @@ -91,6 +100,7 @@ Creates DataDog monitors with the following checks:
| <a name="output_pgbouncer_availability_id"></a> [pgbouncer\_availability\_id](#output\_pgbouncer\_availability\_id) | id for monitor pgbouncer\_availability |
| <a name="output_pgbouncer_database_connection_limit_id"></a> [pgbouncer\_database\_connection\_limit\_id](#output\_pgbouncer\_database\_connection\_limit\_id) | id for monitor pgbouncer\_database\_connection\_limit |
| <a name="output_pgbouncer_pool_connection_limit_id"></a> [pgbouncer\_pool\_connection\_limit\_id](#output\_pgbouncer\_pool\_connection\_limit\_id) | id for monitor pgbouncer\_pool\_connection\_limit |
| <a name="output_pgbouncer_wait_time_id"></a> [pgbouncer\_wait\_time\_id](#output\_pgbouncer\_wait\_time\_id) | id for monitor pgbouncer\_wait\_time |
<!-- END_TF_DOCS -->
## Related documentation

Expand Down
44 changes: 44 additions & 0 deletions database/pgbouncer/inputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,47 @@ variable "pgbouncer_database_connection_limit_timeframe" {
type = string
default = "last_5m"
}

###############################
### PGBouncer wait time ###
###############################

variable "pgbouncer_wait_time_threshold_critical" {
default = 100000
description = "Critical number in μs of wait time monitor"
}

variable "pgbouncer_wait_time_threshold_warning" {
default = 70000
description = "Warning number in μs of wait time monitor"
}

variable "pgbouncer_wait_time_enabled" {
description = "Flag to enable PGBouncer wait time monitor"
type = bool
default = true
}

variable "pgbouncer_wait_time_extra_tags" {
description = "Extra tags for PGBouncer wait time monitor"
type = list(string)
default = []
}

variable "pgbouncer_wait_time_message" {
description = "Custom message for PGBouncer connection monitor"
type = string
default = ""
}

variable "pgbouncer_wait_time_time_aggregator" {
description = "Monitor time aggregator for PGBouncer connection monitor [available values: min, max or avg]"
type = string
default = "avg"
}

variable "pgbouncer_wait_time_timeframe" {
description = "Monitor timeframe for PGBouncer connection monitor [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`]"
type = string
default = "last_5m"
}
28 changes: 27 additions & 1 deletion database/pgbouncer/monitors-pgbouncer.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ EOQ
tags = concat(["env:${var.environment}", "type:database", "provider:pgbouncer", "resource:pgbouncer", "team:claranet", "created-by:terraform"], var.pgbouncer_pool_connection_limit_extra_tags)
}


resource "datadog_monitor" "pgbouncer_database_connection_limit" {
count = var.pgbouncer_database_connection_limit_enabled ? 1 : 0
name = "${var.prefix_slug == "" ? "" : "[${var.prefix_slug}]"}[${var.environment}] PGBouncer database connection limit"
Expand Down Expand Up @@ -84,3 +83,30 @@ EOQ
tags = concat(["env:${var.environment}", "type:database", "provider:pgbouncer", "resource:pgbouncer", "team:claranet", "created-by:terraform"], var.pgbouncer_database_connection_limit_extra_tags)
}

resource "datadog_monitor" "pgbouncer_wait_time" {
count = var.pgbouncer_wait_time_enabled ? 1 : 0
name = "${var.prefix_slug == "" ? "" : "[${var.prefix_slug}]"}[${var.environment}] PGBouncer wait time"
message = coalesce(var.pgbouncer_wait_time_message, var.message)
type = "query alert"

query = <<EOQ
${var.pgbouncer_wait_time_time_aggregator}(${var.pgbouncer_wait_time_timeframe}):
avg:pgbouncer.stats.avg_wait_time${module.filter-tags.query_alert} by {host,db}
> ${var.pgbouncer_wait_time_threshold_critical}
EOQ

monitor_thresholds {
warning = var.pgbouncer_wait_time_threshold_warning
critical = var.pgbouncer_wait_time_threshold_critical
}

evaluation_delay = var.evaluation_delay
new_host_delay = var.new_host_delay
notify_no_data = false
renotify_interval = 0
require_full_window = true
timeout_h = 0
include_tags = true

tags = concat(["env:${var.environment}", "type:database", "provider:pgbouncer", "resource:pgbouncer", "team:claranet", "created-by:terraform"], var.pgbouncer_wait_time_extra_tags)
}
5 changes: 5 additions & 0 deletions database/pgbouncer/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ output "pgbouncer_pool_connection_limit_id" {
value = datadog_monitor.pgbouncer_pool_connection_limit.*.id
}

output "pgbouncer_wait_time_id" {
description = "id for monitor pgbouncer_wait_time"
value = datadog_monitor.pgbouncer_wait_time.*.id
}

0 comments on commit 00666d3

Please sign in to comment.