|
| 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 | + |
0 commit comments