Skip to content

Commit c1574ad

Browse files
authored
feat: Added support for Log filtering (using new input log_filters) and support for enabling annotation filtering (using new input enable_annotations) (#152)
* feat: added option to apply filters on logs * Update variables.tf * Update variables.tf * Update variables.tf * fixed precommit * added documentation for log_filters variable * added variable in DA * precommit fix * exposed enable_annotations variable in DA * Update variables.tf * Update variables.tf * resolved comments * fixed precommit error
1 parent b5cf554 commit c1574ad

File tree

8 files changed

+118
-0
lines changed

8 files changed

+118
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ No modules.
117117
| <a name="input_cluster_config_endpoint_type"></a> [cluster\_config\_endpoint\_type](#input\_cluster\_config\_endpoint\_type) | The type of endpoint to use for the cluster config access: `default`, `private`, `vpe`, or `link`. The `default` value uses the default endpoint of the cluster. | `string` | `"default"` | no |
118118
| <a name="input_cluster_id"></a> [cluster\_id](#input\_cluster\_id) | The ID of the cluster to deploy the agent. | `string` | n/a | yes |
119119
| <a name="input_cluster_resource_group_id"></a> [cluster\_resource\_group\_id](#input\_cluster\_resource\_group\_id) | The resource group ID of the cluster. | `string` | n/a | yes |
120+
| <a name="input_enable_annotations"></a> [enable\_annotations](#input\_enable\_annotations) | Set to true to include pod annotations in log records. Default annotations such as pod IP address and container ID, along with any custom annotations on the pod, will be included. This can help filter logs based on pod annotations in Cloud Logs. | `bool` | `false` | no |
120121
| <a name="input_enable_multiline"></a> [enable\_multiline](#input\_enable\_multiline) | Enable or disable multiline log support. [Learn more](https://cloud.ibm.com/docs/cloud-logs?topic=cloud-logs-agent-multiline) | `bool` | `false` | no |
121122
| <a name="input_is_vpc_cluster"></a> [is\_vpc\_cluster](#input\_is\_vpc\_cluster) | Specify true if the target cluster for the agent is a VPC cluster, false if it is a classic cluster. | `bool` | `true` | no |
123+
| <a name="input_log_filters"></a> [log\_filters](#input\_log\_filters) | List of additional filters to be applied on logs. [Learn more](https://github.com/terraform-ibm-modules/terraform-ibm-logs-agent/blob/main/solutions/fully-configurable/DA-types.md#configuring-log-filters). | `any` | `[]` | no |
122124
| <a name="input_logs_agent_additional_metadata"></a> [logs\_agent\_additional\_metadata](#input\_logs\_agent\_additional\_metadata) | The list of additional metadata fields to add to the routed logs. | <pre>list(object({<br/> key = optional(string)<br/> value = optional(string)<br/> }))</pre> | `[]` | no |
123125
| <a name="input_logs_agent_chart"></a> [logs\_agent\_chart](#input\_logs\_agent\_chart) | The name of the Helm chart to deploy. | `string` | `"logs-agent-helm"` | no |
124126
| <a name="input_logs_agent_chart_location"></a> [logs\_agent\_chart\_location](#input\_logs\_agent\_chart\_location) | The location of the Logs agent helm chart. | `string` | `"oci://icr.io/ibm/observe"` | no |

examples/logs-agent-iks/main.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,27 @@ module "logs_agent" {
153153
cloud_logs_ingress_endpoint = module.cloud_logs.ingress_private_endpoint
154154
cloud_logs_ingress_port = 3443
155155
logs_agent_enable_scc = false # only true for Openshift
156+
log_filters = [
157+
{
158+
name = "lua"
159+
match = "*"
160+
call = "add_crn_field"
161+
code = <<EOL
162+
-- Enrich records with CRN field
163+
-- This is just a sample code for showing usage of lua filter
164+
function add_crn_field(tag, timestamp, record)
165+
166+
record["logSourceCRN"] = "crn:v1:bluemix:public:postgres:us-south:a/123456::"
167+
record["saveServiceCopy"] = "true"
168+
169+
return 2, 0, record
170+
end
171+
EOL
172+
},
173+
{
174+
name = "grep"
175+
match = "*"
176+
exclude = ["message.level debug"]
177+
}
178+
]
156179
}

ibm_catalog.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@
225225
}
226226
}
227227
},
228+
{
229+
"key": "enable_annotations"
230+
},
231+
{
232+
"key": "log_filters"
233+
},
228234
{
229235
"key": "logs_agent_selected_log_source_paths"
230236
},

main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,16 @@ resource "helm_release" "logs_agent" {
115115
value = var.enable_multiline
116116
}
117117

118+
set {
119+
name = "includeAnnotations"
120+
value = var.enable_annotations
121+
}
122+
118123
# dummy value hack to force update https://github.com/hashicorp/terraform-provider-helm/issues/515#issuecomment-813088122
119124
values = [
120125
yamlencode({
121126
tolerations = var.logs_agent_tolerations
127+
additionalFilters = var.log_filters
122128
resources = var.logs_agent_resources
123129
additionalMetadata = local.logs_agent_additional_metadata
124130
dummy = uuid()

solutions/fully-configurable/DA-types.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,47 @@ In this example:
108108
### What It Does
109109

110110
The `logs_agent_resources` variable is used to configure the resource requests and limits for the logs agent pods. This ensures that the logs agent has sufficient resources to operate efficiently while preventing it from consuming excessive resources on the node. The configuration is passed to the Helm chart during deployment, ensuring that the specified resource constraints are applied.
111+
112+
===================================================================
113+
114+
# Configuring Log filters
115+
116+
:exclamation: **Important:** `log_filters` variable should be passed as a list to the input even if there is only one filter which you want to apply.
117+
118+
`log_filters` can be used to apply some filters on the logs which you want to send to Cloud Logs instance such as modifying some fields, dropping info/debug logs to just keep error logs, or running some custom functions with the help of lua filter. For more information please refer [this](https://docs.fluentbit.io/manual/data-pipeline/filters)
119+
120+
### Example `log_filters` Usage
121+
122+
To configure additional filters on logs, you can set the `log_filters` variable in below format.
123+
124+
```hcl
125+
[
126+
{
127+
name = "lua"
128+
match = "*"
129+
call = "add_crn_field"
130+
code = <<EOL
131+
-- Enrich records with CRN field
132+
-- This is just a sample code for showing usage of lua filter
133+
function add_crn_field(tag, timestamp, record)
134+
135+
record["logSourceCRN"] = "crn:v1:bluemix:public:postgres:us-south:a/123456::"
136+
record["saveServiceCopy"] = "true"
137+
138+
return 2, 0, record
139+
end
140+
EOL
141+
},
142+
{
143+
name = "grep"
144+
match = "*"
145+
exclude = ["message.level debug"]
146+
}
147+
]
148+
```
149+
150+
In this example:
151+
- First filter in the list is a lua filter with which you can run a custom code on your logs. Here an extra logSourceCRN field is getting appended in the log record.
152+
- Second filter in the list is grep filter which is dropping all the records which have message.level field set to debug in the log record.
153+
154+
We can use other filters such as modify, nest also.

solutions/fully-configurable/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@ module "logs_agent" {
4444
wait_till = var.wait_till
4545
wait_till_timeout = var.wait_till_timeout
4646
enable_multiline = var.enable_multiline
47+
enable_annotations = var.enable_annotations
48+
log_filters = var.log_filters
4749
}

solutions/fully-configurable/variables.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,23 @@ variable "logs_agent_log_source_namespaces" {
165165
nullable = false
166166
}
167167

168+
variable "enable_annotations" {
169+
description = "Set to true to include pod annotations in log records. Default annotations such as pod IP address and container ID, along with any custom annotations on the pod, will be included. This can help filter logs based on pod annotations in Cloud Logs."
170+
type = bool
171+
default = false
172+
}
173+
174+
variable "log_filters" {
175+
176+
# variable type is any because filters schema is not fixed and there are many filters each having its unique fields.
177+
# logs-agent helm chart expects this variable to be provided in list format even if a single filter is passed.
178+
179+
description = "List of additional filters to be applied on logs. [Learn more](https://github.com/terraform-ibm-modules/terraform-ibm-logs-agent/blob/main/solutions/fully-configurable/DA-types.md#configuring-log-filters)."
180+
type = any
181+
default = []
182+
}
183+
184+
168185
variable "logs_agent_iam_mode" {
169186
type = string
170187
default = "TrustedProfile"

variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,26 @@ variable "cloud_logs_ingress_port" {
233233
condition = contains([3443, 443], var.cloud_logs_ingress_port)
234234
}
235235
}
236+
236237
variable "enable_multiline" {
237238
description = "Enable or disable multiline log support. [Learn more](https://cloud.ibm.com/docs/cloud-logs?topic=cloud-logs-agent-multiline)"
238239
type = bool
239240
default = false
240241
}
242+
243+
variable "enable_annotations" {
244+
description = "Set to true to include pod annotations in log records. Default annotations such as pod IP address and container ID, along with any custom annotations on the pod, will be included. This can help filter logs based on pod annotations in Cloud Logs."
245+
type = bool
246+
default = false
247+
}
248+
249+
250+
variable "log_filters" {
251+
252+
# variable type is any because filters schema is not fixed and there are many filters each having its unique fields.
253+
# logs-agent helm chart expects this variable to be provided in list format even if a single filter is passed.
254+
255+
description = "List of additional filters to be applied on logs. [Learn more](https://github.com/terraform-ibm-modules/terraform-ibm-logs-agent/blob/main/solutions/fully-configurable/DA-types.md#configuring-log-filters)."
256+
type = any
257+
default = []
258+
}

0 commit comments

Comments
 (0)