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_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.
-
#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.
- #respond_to_missing?(sym, include_private = false) ⇒ Boolean
-
#run(entry) ⇒ Object
The main run method that performs all configured mutations, if any, against a single journal entry.
- #warnings ⇒ Object
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
59 60 61 62 63 64 65 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 59 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.
68 69 70 71 72 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 68 def method_missing(sym, *args) return @opts[sym] if @opts.members.include?(sym) super end |
Class Method Details
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
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 105 def format_fields(entry, mapped = nil) entry.each_with_object(mapped || {}) do |(fld, val), formatted_entry| # don't mess with explicitly mapped fields next if @map_src_fields.include?(fld) fld = format_field_name(fld) # account for mapping (appending) to an existing systemd field formatted_entry[fld] = join_if_needed([val, mapped[fld]]) end end |
#map_fields(entry) ⇒ Object
Run field mapping against a single journal entry. Returns the mutated entry hash. entry - hash or ‘Systemd::Journal:Entry`
91 92 93 94 95 96 97 98 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 91 def map_fields(entry) @map.each_with_object({}) do |(cstm, sysds), mapped| vals = sysds.collect { |fld| entry[fld] }.compact next if vals.empty? # systemd field does not exist in source entry mapped[cstm] = join_if_needed(vals) end end |
#respond_to_missing?(sym, include_private = false) ⇒ Boolean
74 75 76 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 74 def respond_to_missing?(sym, include_private = false) @opts.members.include?(sym) || super 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`
81 82 83 84 85 86 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 81 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 |
#warnings ⇒ Object
116 117 118 119 120 |
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 116 def warnings return [] unless field_map_strict && field_map.empty? '`field_map_strict` set to true with empty `field_map`, expect no fields' end |