Skip to content

Commit

Permalink
Merge pull request #3 from a5chin/feature/monitoring-tools
Browse files Browse the repository at this point in the history
監視用のモジュールを作成
  • Loading branch information
a5chin authored Nov 13, 2024
2 parents 0c241e3 + b8a05db commit d04afe3
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ repos:
"./modules/gcs2spanner/",
]

- repo: https://github.com/terraform-docs/terraform-docs
rev: "v0.18.0"
hooks:
- id: terraform-docs-go
name: terraform-docs-monitoring-tools
args:
[
"markdown",
"table",
"--output-file",
"./README.md",
"./modules/monitoring-tools/",
]

- repo: https://github.com/terraform-docs/terraform-docs
rev: "v0.18.0"
hooks:
Expand Down
45 changes: 45 additions & 0 deletions modules/monitoring-tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >=1.7 |
| <a name="requirement_google"></a> [google](#requirement\_google) | >= 5.22.0 |
| <a name="requirement_google-beta"></a> [google-beta](#requirement\_google-beta) | >= 5.22.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_google"></a> [google](#provider\_google) | >= 5.22.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [google_monitoring_alert_policy.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/monitoring_alert_policy) | resource |
| [google_monitoring_notification_channel.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/monitoring_notification_channel) | resource |
| [google_project_service.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_service) | resource |
| [google_project.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/project) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_channels"></a> [channels](#input\_channels) | Channel variable that contains `error` and `warn` as keys | `map(string)` | n/a | yes |
| <a name="input_location"></a> [location](#input\_location) | The location of the resource. | `string` | n/a | yes |
| <a name="input_project_id"></a> [project\_id](#input\_project\_id) | The ID of Google Cloud Platform. | `string` | n/a | yes |
| <a name="input_secrets"></a> [secrets](#input\_secrets) | The token required for notifications to Slack.<br> The variable is required for monitoring. | `string` | n/a | yes |
| <a name="input_target"></a> [target](#input\_target) | The target information for monitoring.<br> `base_value` is used to calculate a numeric value based on the target resource.<br> Specifically, it is used to obtain a percentage, as in `floor(base_value * threshold.value)`. | <pre>object({<br> title = string<br> metric = string<br> resource_type = string<br> label = string<br> name = string<br> filter = string<br> reducer = string<br> aligner = string<br> base_value = optional(number, 1)<br> threshold = map(<br> object({<br> value = number<br> window = string<br> })<br> )<br> })</pre> | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_enabled_apis"></a> [enabled\_apis](#output\_enabled\_apis) | Already enabled APIs list. |
| <a name="output_policies"></a> [policies](#output\_policies) | Alert policies name object. |
<!-- END_TF_DOCS -->
45 changes: 45 additions & 0 deletions modules/monitoring-tools/alert.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
resource "google_monitoring_alert_policy" "main" {
for_each = local.levels

display_name = "[${upper(each.value)}] ${var.target.name} ${var.target.title}"

conditions {
display_name = "[${upper(each.value)}] ${var.target.name} - ${var.target.title}"

condition_threshold {
filter = <<EOF
metric.type = "${var.target.metric}"
AND resource.type = "${var.target.resource_type}"
AND ${var.target.label} = ${var.target.name}
AND ${var.target.filter}
EOF

aggregations {
alignment_period = var.target.threshold[each.value].window
cross_series_reducer = var.target.reducer
per_series_aligner = var.target.aligner
}

trigger {
count = 1
}

threshold_value = (
var.target.base_value == 1 ?
var.target.threshold[each.value].value :
floor(var.target.base_value * var.target.threshold[each.value].value)
)

comparison = "COMPARISON_GT"
duration = "0s"
}
}

alert_strategy {
auto_close = "604800s"
}

combiner = "OR"
enabled = "true"
notification_channels = []
}
20 changes: 20 additions & 0 deletions modules/monitoring-tools/channel.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resource "google_monitoring_notification_channel" "main" {
for_each = local.levels

display_name = upper(each.key)
type = "slack"

labels = {
channel_name = each.value
}

sensitive_labels {
auth_token = yamldecode(var.secrets)["auth_token"]
}

lifecycle {
ignore_changes = [
labels["team"]
]
}
}
16 changes: 16 additions & 0 deletions modules/monitoring-tools/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
locals {
apis = toset([
"monitoring.googleapis.com",
])
levels = toset(keys(var.target.threshold))
}

data "google_project" "main" {}

resource "google_project_service" "main" {
for_each = local.apis

project = data.google_project.main.project_id
service = each.value
disable_on_destroy = false
}
12 changes: 12 additions & 0 deletions modules/monitoring-tools/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
output "enabled_apis" {
description = "Already enabled APIs list."
value = [for api in google_project_service.main : api.service]
}

output "policies" {
description = "Alert policies name object."
value = {
error = google_monitoring_alert_policy.main["error"].name
warn = google_monitoring_alert_policy.main["warn"].name
}
}
14 changes: 14 additions & 0 deletions modules/monitoring-tools/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_version = ">=1.7"

required_providers {
google = {
source = "hashicorp/google"
version = ">= 5.22.0"
}
google-beta = {
source = "hashicorp/google"
version = ">= 5.22.0"
}
}
}
47 changes: 47 additions & 0 deletions modules/monitoring-tools/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
variable "project_id" {
description = "The ID of Google Cloud Platform."
type = string
}

variable "location" {
description = "The location of the resource."
type = string
}

variable "target" {
description = <<EOF
The target information for monitoring.
`base_value` is used to calculate a numeric value based on the target resource.
Specifically, it is used to obtain a percentage, as in `floor(base_value * threshold.value)`.
EOF
type = object({
title = string
metric = string
resource_type = string
label = string
name = string
filter = string
reducer = string
aligner = string
base_value = optional(number, 1)
threshold = map(
object({
value = number
window = string
})
)
})
}

variable "channels" {
description = "Channel variable that contains `error` and `warn` as keys"
type = map(string)
}

variable "secrets" {
description = <<EOF
The token required for notifications to Slack.
The variable is required for monitoring.
EOF
type = string
}

0 comments on commit d04afe3

Please sign in to comment.