Class: Fluent::Plugin::SystemdEntryMutator
- Inherits:
-
Object
- Object
- Fluent::Plugin::SystemdEntryMutator
- Defined in:
- lib/fluent/plugin/systemd/entry_mutator.rb
Overview
A simple stand-alone configurable mutator for systemd journal entries.
Note regarding field mapping: The input field_map option is meant to have a structure that is intuative or logical for humans when declaring a field map.
"<source_field1>" => "<new_field1>",
"<source_field2>" => ["<new_field1>", "<new_field2>"]
Internally the inverse of the human-friendly field_map is computed (and cached) upon object creation and used as a “mapped model”
"<new_field1>" => ["<source_field1>", "<source_field2>"],
"<new_field2>" => ["<source_field2>"]
Defined Under Namespace
Classes: Options
Class Method Summary collapse
Instance Method Summary collapse
-
#format_fields(entry, mapped = nil) ⇒ Object
Run field formatting (mutations applied to all non-mapped fields) against a single journal entry.
-
#initialize(**options) ⇒ SystemdEntryMutator
constructor
Constructor keyword options (all other kwargs are ignored): field_map - hash describing the desired field mapping in the form: => “<new_field>”, … where
new_fieldis a string or array of strings field_map_strict - boolean if true will only include new fields defined infield_mapfields_strip_underscores - boolean if true will strip all leading underscores from non-mapped fields fields_lowercase - boolean if true lowercase all non-mapped fields. -
#map_fields(entry) ⇒ Object
Run field mapping against a single journal entry.
-
#method_missing(sym, *args) ⇒ Object
Expose config state as read-only instance properties of the mutator.
-
#run(entry) ⇒ Object
The main run method that performs all configured mutations, if any, against a single journal entry.
Constructor Details
#initialize(**options) ⇒ SystemdEntryMutator
Constructor keyword options (all other kwargs are ignored): field_map - hash describing the desired field mapping in the form:
{"<source_field>" => "<new_field>", ...}
where `new_field` is a string or array of strings
field_map_strict - boolean if true will only include new fields
defined in `field_map`
fields_strip_underscores - boolean if true will strip all leading
underscores from non-mapped fields
fields_lowercase - boolean if true lowercase all non-mapped fields
raises Fluent::ConfigError for invalid options
45 46 47 48 49 50 51 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 45 def initialize(**) @opts = () (@opts) @map = invert_field_map(@opts.field_map) @map_src_fields = @opts.field_map.keys @no_transform = @opts == self.class.default_opts end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
Expose config state as read-only instance properties of the mutator.
54 55 56 57 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 54 def method_missing(sym, *args) return @opts[sym] if @opts.members.include?(sym) super end |
Class Method Details
.default_opts ⇒ Object
30 31 32 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 30 def self.default_opts Options.new({}, false, false, false) end |
Instance Method Details
#format_fields(entry, mapped = nil) ⇒ Object
Run field formatting (mutations applied to all non-mapped fields) against a single journal entry. Returns the mutated entry hash. entry - hash or Systemd::Journal:Entry mapped - Optional hash that represents a previously mapped entry to
which the formatted fields will be added
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 86 def format_fields(entry, mapped = nil) mapped ||= {} entry.each do |fld, val| # don't mess with explicitly mapped fields next if @map_src_fields.include?(fld) fld = fld.gsub(/\A_+/, "") if @opts.fields_strip_underscores fld = fld.downcase if @opts.fields_lowercase # account for mapping (appending) to an existing systemd field mapped[fld] = mapped.key?(fld) ? [val, mapped[fld]].join(" ") : val end mapped end |
#map_fields(entry) ⇒ Object
Run field mapping against a single journal entry. Returns the mutated entry hash. entry - hash or Systemd::Journal:Entry
71 72 73 74 75 76 77 78 79 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 71 def map_fields(entry) mapped = {} @map.each do |cstm, sysds| vals = sysds.collect { |fld| entry[fld] }.compact next if vals.empty? # systemd field does not exist in source entry mapped[cstm] = vals.length == 1 ? vals[0] : vals.join(" ") end mapped end |
#run(entry) ⇒ Object
The main run method that performs all configured mutations, if any, against a single journal entry. Returns the mutated entry hash. entry - hash or Systemd::Journal:Entry
62 63 64 65 66 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 62 def run(entry) return entry.to_h if @no_transform return map_fields(entry) if @opts.field_map_strict format_fields(entry, map_fields(entry)) end |