Skip to content

Commit 2a689a7

Browse files
committed
add filter docs
1 parent 1380ab1 commit 2a689a7

3 files changed

Lines changed: 187 additions & 0 deletions

File tree

docs/filter-output-sql.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# SQL output filter
2+
Filter and aggregate parsed logs with SQL.
3+
4+
This filter function applies SQL queries on parsed log events. The result of the query is emitted as new event, while the original events are omitted.
5+
6+
Using SQL it is very easy to aggregate values, e.g. group HTTP requests by status codes. The SQL WHERE statement is useful to filter events, before they get shipped to Elasticsearch or [Logsene](https://sematext.com/logsene).
7+
8+
The supported SQL syntax for SELECT statements are described in the [alasql documentation](https://github.com/agershun/alasql/wiki/Select).
9+
10+
# Configuration
11+
12+
Add following section 'outputFilter' to @sematext/logagent configuration file. Please note you could use the plugin with multiple configurations for different event sources.
13+
14+
```
15+
input:
16+
files:
17+
- './access.log'
18+
19+
outputFilter:
20+
- module: sql
21+
config:
22+
source: !!js/regexp /access.log|httpd/
23+
interval: 1 # every second
24+
queries:
25+
- # calculate average page size for different HTTP methods
26+
SELECT 'apache_stats' AS _type,
27+
AVG(size) AS size_avg,
28+
COUNT(method) AS method_count,
29+
method as http_method
30+
FROM ?
31+
GROUP BY method
32+
- # log each request to the login page
33+
SELECT *
34+
FROM ?
35+
WHERE path like "/wp-login%"
36+
output:
37+
elasticsearch:
38+
url: http://localhost:9200
39+
index: mylogs
40+
```
41+
42+
Run logagent with your config:
43+
```
44+
logagent --config logagent-example-config.yml
45+
```

docs/filters.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Filters
2+
3+
Filters can drop, transform or aggregate log events and hook into the processing chain.
4+
5+
There are two types of filters:
6+
- Input filters - process raw input from input plugins before log events get parsed
7+
- Output filters - process parsed log events before they are passed to output plugins.
8+
9+
10+
__Input Plugins -> **Input Filters** -> Parser -> **Output Filter** -> Output Plugins__
11+
12+
13+
Example:
14+
```
15+
1. Input: Tail Web Server Log -g '/var/log/httpd/access.log'
16+
2. Input-Filter: Grep URL's of interest 'login|register|upgrade'
17+
3. Parser: Parse Log and generate fileds like URL, status code, size, referer, country etc.
18+
5. Output Filter: Drop non-relevant log events like redirects (status=302)
19+
6. Output Plugin: Store filtered log events in Elasticsearch
20+
```
21+
22+
Filters can be declared inline as JavaScript in function or as reference to a npm modules in Logagent config file.
23+
24+
## Input filter
25+
26+
Function parameters for input filters:
27+
28+
- sourceName - the name of the log source e.g. '/var/log/httpd/access.log'
29+
- config - the configuration options from the config file
30+
- data - the raw (input filter) or parsed data (output filter)
31+
- callback - MUST be called.
32+
- callback() without parameters drops the event.
33+
- callback (null,data) will pass the log event to the next filter or output plugin.
34+
- callback(error) will report an error and drops the event
35+
36+
Node.js modules can be loaded as filter function with the ```module``` keyword.
37+
A module can be declared inline as JavaScript function using ```!!js/function >>``` in the module property. Properties in the config section are passed to the filter function as "config" object.
38+
39+
Example, using npm modules:
40+
```
41+
inputFilter:
42+
- module: logagent-filter-input-grep
43+
config:
44+
matchSource: !!js/regexp /myapp.log/
45+
include: !!js/regexp /info|error/i
46+
exclude: !!js/regexp /test/i
47+
```
48+
49+
Example, inline JavaScript function:
50+
51+
```
52+
inputFilter:
53+
- module: logagent-filter-input-grep
54+
config:
55+
matchSource: !!js/regexp /myapp.log/
56+
include: !!js/regexp /info|error/i
57+
exclude: !!js/regexp /test/i
58+
module: !!js/function >>
59+
function (sourceName, config, data, callback) {
60+
try {
61+
var drop = false
62+
if (config.matchSource) {
63+
if (!config.matchSource.test(sourceName)) {
64+
// pass data for unmatched source names
65+
return callback(null, data)
66+
}
67+
}
68+
// filter data for matched source names
69+
if (config.include) {
70+
drop = !config.include.test(data)
71+
}
72+
if (config.exclude) {
73+
drop = config.exclude.test(data) || drop
74+
}
75+
drop ? callback() : callback(null, data)
76+
} catch (err) {
77+
return callback(null, data)
78+
}
79+
}
80+
```
81+
82+
# Output filter
83+
84+
Function parameters for output filters:
85+
86+
- context - an object providing information about the log source, e.g. context.source
87+
- config - the configuration options from the config file
88+
- eventEmitter - the eventEmitter send new events to logagent plugins emit('data.parsed', context, data). Required for aggregation plugins, which typicall drop all events and generate new events with aggregated stats.
89+
- data - the raw (input filter) or parsed data (output filter)
90+
- callback - MUST be called.
91+
- callback() without parameters drops the event.
92+
- callback (null,data) will pass the log event to the next filter or output plugin.
93+
- callback(error) will report an error and drops the event
94+
95+
Node.js modules can be loaded as filter function with the ```module``` keyword.
96+
A module can be declared inline as JavaScript function using ```!!js/function >>``` in the module property. Properties in the config section are passed to the filter function as "config" object.
97+
98+
Example, inline declaration to implement the grep filter from above applied to data.message field.
99+
100+
```
101+
outputFilter:
102+
- config:
103+
matchSource: !!js/regexp /myapp.log/
104+
include: !!js/regexp /info|error/i
105+
exclude: !!js/regexp /test/i
106+
module: !!js/function >>
107+
function (context, config, eventEmitter, data, callback) {
108+
try {
109+
var sourceName = context.source
110+
var drop = false
111+
if (config.matchSource) {
112+
if (!config.matchSource.test(sourceName)) {
113+
// pass data for unmatched source names
114+
return callback(null, data)
115+
}
116+
}
117+
// filter data for matched source names
118+
if (config.include) {
119+
drop = !config.include.test(data.message)
120+
}
121+
if (config.exclude) {
122+
drop = config.exclude.test(data) || drop
123+
}
124+
drop ? callback() : callback(null, data)
125+
} catch (err) {
126+
// pass all events to next filter
127+
return callback(null, data)
128+
}
129+
}
130+
```
131+
132+
# List of available filters
133+
134+
- Grep input filter - module alias "grep"
135+
- SQL output filter - module alias "sql"
136+
137+
138+

mkdocs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ pages:
88
- Configuration file: config-file.md
99
- Log Parser and pattern definitions: parser.md
1010
- Plugins: plugins.md
11+
- Filter:
12+
- About filters: filters.md
13+
- SQL output filter: filter-output-sql.md
14+

0 commit comments

Comments
 (0)