Edge Delta Filter with JavaScript
2 minute read
The log-filter-javascript filter can filter logs based on a condition defined with a JavaScript function. To use it, the raw log variable must be named “log” in the script.
Filtering with a Script
The Boolean to decide whether to drop or pass the log must be named “pass” in the script. If “pass” is true, then log is passed through, otherwise it is dropped at this filter
JavaScript Filter Example
In the following configuration, the filter will return a pass value of true only if the service object is prod. If pass is true, it the log will pass the filter while false logs will be dropped. With keep_log_if_failed set to true, logs that cause an error while running the script will not be dropped.
- name: log_filter_script
type: log-filter-javascript
script: |
var obj = JSON.parse(log);
var pass = false;
if (obj.service === "prod") {
pass = true;
}
keep_log_if_failed: true
Example Inputs
The script will return pass = true with following log:
Log: "{"app": "alpine", "service": "prod"}"
The script will return pass = false with following log:
Log: "{"app": "alpine", "service": "nonprod"}"
Required Parameters
name (Required)
The name
parameter specifies the name for the filter. You refer to this name in other places, for example to refer to a specific filter in a workflow or processor. Names must be unique within the filters:
section. It is a YAML list element so it begins with a -
and a space followed by the string. A name is a required parameter for a filter
filters:
- name: <filter-name>
type: log-filter-javascript (Required)
The type
parameter in the filter context specifies the type of filter to apply. A type is a required parameter for a filter.
filters:
- name: <filter-name>
type: <filter-type>
script (Required)
The script
parameter defines the JavaScript function used to apply to matching logs. The script is written in Goja - a type of script in JavaScript. It is a required field for log-transformer-javascript
, enrichment-using-javascript
, and log-filter-javascript
type filters.
filters:
- name: <filter-name>
type: <log-transformer-javascript|enrichment-using-javascript|log-filter-javascript>
script: |
<Goja-JavaScript-Function>
Optional Parameters
keep_log_if_failed
The keep_log_if_failed
parameter defines how to handle an error when processing the filter. It is defined with a Boolean value true
or false
. The default value is false
indicating that the filter will drop or ignore logs that caused an error. The keep_log_if_failed
parameter is optional.
filters:
- name: <filter-name>
type: <filter-type>
keep_log_if_failed: <true|false>