Skip to content

Commit 511585e

Browse files
feat: add support for Log Analytics in log bucket destination (#179)
Co-authored-by: Imran Nayer <[email protected]>
1 parent 63d105b commit 511585e

File tree

11 files changed

+107
-17
lines changed

11 files changed

+107
-17
lines changed

build/int.cloudbuild.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ steps:
7676
args: ['/bin/bash', '-c', 'cft test run TestLogBucketProjectModule --stage apply --verbose']
7777
- id: go-verify-logbucket-project
7878
waitFor:
79-
- go-apply-logbucket-org
79+
- go-apply-logbucket-project
8080
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
8181
args: ['/bin/bash', '-c', 'cft test run TestLogBucketProjectModule --stage verify --verbose']
8282
- id: go-teardown-logbucket-project

examples/logbucket/project/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Log Export: Log Bucket destination at Project level
22

3-
These examples configures a project-level log sink that feeds a logging log bucket destination with log bucket and log sink in the same project or in separated projects.
3+
These examples configures a project-level log sink that feeds a logging log bucket destination with log bucket and log sink in the same project or in separated projects. This example also configures [Log Analytics](https://cloud.google.com/logging/docs/log-analytics) in the log bucket with a linked BigQuery dataset.
44

55
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
66
## Inputs
@@ -14,8 +14,10 @@ These examples configures a project-level log sink that feeds a logging log buck
1414

1515
| Name | Description |
1616
|------|-------------|
17+
| log\_bkt\_linked\_ds\_name\_same\_proj | The name for the log bucket linked BigQuery dataset name in same project example. |
1718
| log\_bkt\_name\_same\_proj | The name for the log bucket for sink and logbucket in same project example. |
1819
| log\_bkt\_same\_proj | The project where the log bucket is created for sink and logbucket in same project example. |
20+
| log\_bucket\_linked\_ds\_name | The name for the log bucket linked BigQuery dataset name. |
1921
| log\_bucket\_name | The name for the log bucket. |
2022
| log\_bucket\_project | The project where the log bucket is created. |
2123
| log\_sink\_dest\_uri\_same\_proj | A fully qualified URI for the log sink for sink and logbucket in same project example. |

examples/logbucket/project/main.tf

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ module "log_export" {
3131
}
3232

3333
module "destination" {
34-
source = "../../..//modules/logbucket"
35-
project_id = var.project_destination_logbkt_id
36-
name = "logbucket_from_other_project_${random_string.suffix.result}"
37-
location = "global"
38-
log_sink_writer_identity = module.log_export.writer_identity
34+
source = "../../..//modules/logbucket"
35+
project_id = var.project_destination_logbkt_id
36+
name = "logbucket_from_other_project_${random_string.suffix.result}"
37+
location = "global"
38+
enable_analytics = true
39+
linked_dataset_id = "log_analytics_dataset"
40+
linked_dataset_description = "dataset for log bucket"
41+
log_sink_writer_identity = module.log_export.writer_identity
3942
}
4043

4144
#-------------------------------------#
@@ -56,6 +59,9 @@ module "dest_same_proj" {
5659
project_id = var.project_destination_logbkt_id
5760
name = "logbucket_from_same_project_${random_string.suffix.result}"
5861
location = "global"
62+
enable_analytics = true
63+
linked_dataset_id = "log_analytics_dataset_same"
64+
linked_dataset_description = "dataset for log bucket in the same project"
5965
log_sink_writer_identity = module.log_export_same_proj.writer_identity
6066
grant_write_permission_on_bkt = false
6167
}

examples/logbucket/project/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ output "log_bucket_name" {
2424
value = module.destination.resource_name
2525
}
2626

27+
output "log_bucket_linked_ds_name" {
28+
description = "The name for the log bucket linked BigQuery dataset name."
29+
value = module.destination.linked_dataset_name
30+
}
31+
2732
output "log_sink_project_id" {
2833
description = "The project id where the log sink is created."
2934
value = module.log_export.parent_resource_id
@@ -58,6 +63,11 @@ output "log_bkt_name_same_proj" {
5863
value = module.dest_same_proj.resource_name
5964
}
6065

66+
output "log_bkt_linked_ds_name_same_proj" {
67+
description = "The name for the log bucket linked BigQuery dataset name in same project example."
68+
value = module.dest_same_proj.linked_dataset_name
69+
}
70+
6171
output "log_sink_id_same_proj" {
6272
description = "The project id where the log sink is created for sink and logbucket in same project example."
6373
value = module.log_export_same_proj.parent_resource_id

modules/logbucket/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ module "destination" {
3737

3838
| Name | Description | Type | Default | Required |
3939
|------|-------------|------|---------|:--------:|
40+
| enable\_analytics | (Optional) Whether or not Log Analytics is enabled. A Log bucket with Log Analytics enabled can be queried in the Log Analytics page using SQL queries. Cannot be disabled once enabled. | `bool` | `false` | no |
4041
| grant\_write\_permission\_on\_bkt | (Optional) Indicates whether the module is responsible for granting write permission on the logbucket. This permission will be given by default, but if the user wants, this module can skip this step. This is the case when the sink route logs to a log bucket in the same Cloud project, no new service account will be created and this module will need to bypass granting permissions. | `bool` | `true` | no |
42+
| linked\_dataset\_description | A use-friendly description of the linked BigQuery dataset. The maximum length of the description is 8000 characters. | `string` | `null` | no |
43+
| linked\_dataset\_id | The ID of the linked BigQuery dataset. A valid link dataset ID must only have alphanumeric characters and underscores within it and have up to 100 characters. | `string` | `null` | no |
4144
| location | The location of the log bucket. | `string` | `"global"` | no |
4245
| log\_sink\_writer\_identity | The service account that logging uses to write log entries to the destination. (This is available as an output coming from the root module). | `string` | n/a | yes |
4346
| name | The name of the log bucket to be created and used for log entries matching the filter. | `string` | n/a | yes |
@@ -50,6 +53,7 @@ module "destination" {
5053
|------|-------------|
5154
| console\_link | The console link to the destination log buckets |
5255
| destination\_uri | The destination URI for the log bucket. |
56+
| linked\_dataset\_name | The resource name of the linked BigQuery dataset. |
5357
| project | The project in which the log bucket was created. |
5458
| resource\_name | The resource name for the destination log bucket |
5559

modules/logbucket/main.tf

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,25 @@ resource "google_project_service" "enable_destination_api" {
3333
#------------#
3434

3535
resource "google_logging_project_bucket_config" "bucket" {
36-
project = google_project_service.enable_destination_api.project
37-
location = var.location
38-
retention_days = var.retention_days
39-
bucket_id = var.name
36+
project = google_project_service.enable_destination_api.project
37+
location = var.location
38+
retention_days = var.retention_days
39+
enable_analytics = var.enable_analytics
40+
bucket_id = var.name
41+
}
42+
43+
#-------------------------#
44+
# Linked BigQuery dataset #
45+
#-------------------------#
46+
47+
resource "google_logging_linked_dataset" "linked_dataset" {
48+
count = var.linked_dataset_id != null ? 1 : 0
49+
50+
link_id = var.linked_dataset_id
51+
description = var.linked_dataset_description
52+
parent = "projects/${google_project_service.enable_destination_api.project}"
53+
bucket = google_logging_project_bucket_config.bucket.id
54+
location = var.location
4055
}
4156

4257
#--------------------------------#

modules/logbucket/metadata.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,26 @@ spec:
6565
- name: splunk-sink
6666
location: examples/splunk-sink
6767
variables:
68+
- name: enable_analytics
69+
description: (Optional) Whether or not Log Analytics is enabled. A Log bucket with Log Analytics enabled can be queried in the Log Analytics page using SQL queries. Cannot be disabled once enabled.
70+
type: bool
71+
default: false
72+
required: false
6873
- name: grant_write_permission_on_bkt
6974
description: (Optional) Indicates whether the module is responsible for granting write permission on the logbucket. This permission will be given by default, but if the user wants, this module can skip this step. This is the case when the sink route logs to a log bucket in the same Cloud project, no new service account will be created and this module will need to bypass granting permissions.
7075
type: bool
7176
default: true
7277
required: false
78+
- name: linked_dataset_description
79+
description: A use-friendly description of the linked BigQuery dataset. The maximum length of the description is 8000 characters.
80+
type: string
81+
default: Logbucket linked BigQuery dataset
82+
required: false
83+
- name: linked_dataset_id
84+
description: The ID of the linked BigQuery dataset. A valid link dataset ID must only have alphanumeric characters and underscores within it and have up to 100 characters.
85+
type: string
86+
default: null
87+
required: false
7388
- name: location
7489
description: The location of the log bucket.
7590
type: string
@@ -97,6 +112,8 @@ spec:
97112
description: The console link to the destination log buckets
98113
- name: destination_uri
99114
description: The destination URI for the log bucket.
115+
- name: linked_dataset_name
116+
description: The resource name of the linked BigQuery dataset.
100117
- name: project
101118
description: The project in which the log bucket was created.
102119
- name: resource_name

modules/logbucket/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ output "destination_uri" {
3333
description = "The destination URI for the log bucket."
3434
value = local.destination_uri
3535
}
36+
37+
output "linked_dataset_name" {
38+
description = "The resource name of the linked BigQuery dataset."
39+
value = var.linked_dataset_id != null ? google_logging_linked_dataset.linked_dataset[0].name : ""
40+
}

modules/logbucket/variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ variable "grant_write_permission_on_bkt" {
4646
type = bool
4747
default = true
4848
}
49+
50+
variable "enable_analytics" {
51+
description = "(Optional) Whether or not Log Analytics is enabled. A Log bucket with Log Analytics enabled can be queried in the Log Analytics page using SQL queries. Cannot be disabled once enabled."
52+
type = bool
53+
default = false
54+
}
55+
56+
variable "linked_dataset_id" {
57+
description = "The ID of the linked BigQuery dataset. A valid link dataset ID must only have alphanumeric characters and underscores within it and have up to 100 characters."
58+
type = string
59+
default = null
60+
}
61+
62+
variable "linked_dataset_description" {
63+
description = "A use-friendly description of the linked BigQuery dataset. The maximum length of the description is 8000 characters."
64+
type = string
65+
default = null
66+
}

modules/logbucket/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ terraform {
2020

2121
google = {
2222
source = "hashicorp/google"
23-
version = ">= 3.53, < 6"
23+
version = ">= 4.59, < 6"
2424
}
2525
}
2626

0 commit comments

Comments
 (0)