Class: Fluent::Plugin::IPinfoFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_ipinfo.rb

Instance Method Summary collapse

Constructor Details

#initializeIPinfoFilter

Returns a new instance of IPinfoFilter.



36
37
38
39
40
41
# File 'lib/fluent/plugin/filter_ipinfo.rb', line 36

def initialize
  super
  # "maxsize": 4096         # Number of entries to keep in cache
  # "ttl": 60 * 60 * 24 * 7 # Keep the data in cache for one week
  @ipinfo_settings = {:ttl => 604800, :maxsize => 4096}
end

Instance Method Details

#configure(conf) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/fluent/plugin/filter_ipinfo.rb', line 43

def configure(conf)
  super
  unless @access_token.nil?
    @ipinfo_handler = IPinfo::create(@access_token, @ipinfo_settings)
  else
    @ipinfo_handler = IPinfo::create(nil, @ipinfo_settings)
  end
end

#extract_subhash(h, a = []) ⇒ Object



77
78
79
# File 'lib/fluent/plugin/filter_ipinfo.rb', line 77

def extract_subhash(h, a=[])
  h.select {|k, v| a.include?(k) }
end

#filter(tag, time, record) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fluent/plugin/filter_ipinfo.rb', line 52

def filter(tag, time, record)
  ip_address = record[@key_name]
  unless ip_address.nil?
    # Fetch geolocation details using IPInfo API
    ipinfo_details = @ipinfo_handler.details(ip_address)
    # IPInfo ruby wrapper returns a dict based on symbols, we need to stringify the symbols
    # to be able to use them easily
    all_details = ipinfo_details.all
    geodata = all_details.stringify_keys
    # Get the final list of fields by running a join operation on the fields provided by the user and the ones
    # returned by IPInfo API
    ipinfo_returned_fields = geodata.keys
    final_fields = ipinfo_returned_fields & @fields
    if final_fields.length() != @fields.length()
      ignored_fields = @fields - final_fields
      ignored_fields.each{|field|
        log.warn "Field \"" + field + "\" not present in IPInfo payload. Ignoring it."
      }
    end
    # Extract a subhash from the geolocation data returned by IPInfo API using the final_fields list as keys.
    record[@out_key] = extract_subhash(geodata, final_fields)
  end
  record
end