Class: LogStash::Filters::Prune

Inherits:
Base show all
Defined in:
lib/logstash/filters/prune.rb

Overview

The prune filter is for pruning event data from @fileds based on whitelist/blacklist of field names or their values (names and values can also be regular expressions).

Constant Summary

Constants inherited from Base

Base::RESERVED

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#execute, #initialize, #threadsafe?

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Filters::Base

Instance Method Details

#filter(event) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/logstash/filters/prune.rb', line 80

def filter(event)
  return unless filter?(event)

  hash = event.to_hash

  # We need to collect fields which needs to be remove ,and only in the end
  # actually remove it since then interpolation mode you can get unexpected
  # results as fields with dynamic values will not match since the fields to
  # which they refer have already been removed.
  fields_to_remove = []

  unless @whitelist_names.empty?
    @whitelist_names_regexp = Regexp.union(@whitelist_names.map {|x| Regexp.new(event.sprintf(x))}) if @interpolate
    hash.each_key do |field|
      fields_to_remove << field unless field.match(@whitelist_names_regexp)
    end
  end

  unless @blacklist_names.empty?
    @blacklist_names_regexp = Regexp.union(@blacklist_names.map {|x| Regexp.new(event.sprintf(x))}) if @interpolate
    hash.each_key do |field|
      fields_to_remove << field if field.match(@blacklist_names_regexp)
    end
  end

  @whitelist_values.each do |key, value|
    if @interpolate
      key = event.sprintf(key)
      value = Regexp.new(event.sprintf(value))
    end
    if hash[key]
      if hash[key].is_a?(Array)
        subvalues_to_remove = hash[key].find_all{|x| not x.match(value)}
        unless subvalues_to_remove.empty?
          fields_to_remove << (subvalues_to_remove.length == hash[key].length ? key : { :key => key, :values => subvalues_to_remove })
        end
      else
        fields_to_remove << key if not hash[key].match(value)
      end
    end
  end

  @blacklist_values.each do |key, value|
    if @interpolate
      key = event.sprintf(key)
      value = Regexp.new(event.sprintf(value))
    end
    if hash[key]
      if hash[key].is_a?(Array)
        subvalues_to_remove = hash[key].find_all{|x| x.match(value)}
        unless subvalues_to_remove.empty?
          fields_to_remove << (subvalues_to_remove.length == hash[key].length ? key : { :key => key, :values => subvalues_to_remove })
        end
      else
        fields_to_remove << key if hash[key].match(value)
      end
    end
  end

  fields_to_remove.each do |field|
    if field.is_a?(Hash)
      hash[field[:key]] = hash[field[:key]] - field[:values]
    else
      hash.delete(field)
    end
  end

  filter_matched(event)
end

#registerObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/logstash/filters/prune.rb', line 66

def register
  unless @interpolate
    @whitelist_names_regexp = Regexp.union(@whitelist_names.map {|x| Regexp.new(x)})
    @blacklist_names_regexp = Regexp.union(@blacklist_names.map {|x| Regexp.new(x)})
    @whitelist_values.each do |key, value|
      @whitelist_values[key] = Regexp.new(value)
    end
    @blacklist_values.each do |key, value|
      @blacklist_values[key] = Regexp.new(value)
    end
  end
end