Class: Fluent::Plugin::SystemdEntryMutator

Inherits:
Object
  • Object
show all
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

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(**options)
  @opts = options_from_hash(options)
  validate_options(@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

.default_optsObject



44
45
46
# File 'lib/fluent/plugin/systemd/entry_mutator.rb', line 44

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


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

Returns:

  • (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

#warningsObject



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