class Fluent::Plugin::GeoipFilter

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/filter_geoip.rb, line 9
def initialize
  @geoip_cache = LruRedux::Cache.new(8192)
  super
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/filter_geoip.rb, line 19
def configure(conf)
  super
  begin
    @geoip = GeoIP.new(@database_path)
  rescue => e
    @geoip = GeoIP.new
    log.warn "Failed to configure parser. Use default pattern.", :error_class => e.class, :error => e.message
    log.warn_backtrace
  end
end
filter(tag, time, record) click to toggle source
# File lib/fluent/plugin/filter_geoip.rb, line 30
def filter(tag, time, record)
  ip_addr = record[@key_name]
  unless ip_addr.nil?
    geo_ip = @geoip_cache.getset(ip_addr) { get_geoip(ip_addr) }
    if flatten
      record.merge! hash_flatten(geo_ip, [@out_key])
    else
      record[@out_key] = geo_ip
    end
  end
  record
end

Private Instance Methods

get_geoip(ip_addr) click to toggle source
# File lib/fluent/plugin/filter_geoip.rb, line 45
def get_geoip(ip_addr)
  geo_ip = @geoip.city(ip_addr)
  data = {}
  return data if geo_ip.nil?
  data["coordinates"] = [geo_ip["longitude"], geo_ip["latitude"]]
  data["country_code"] = geo_ip["country_code2"]
  data["city"] = geo_ip["city_name"]
  data["region_code"] = geo_ip["region_name"]
  data
end
hash_flatten(a, keys=[]) click to toggle source
# File lib/fluent/plugin/filter_geoip.rb, line 56
def hash_flatten(a, keys=[])
  ret = {}
  a.each{|k,v|
    ks = keys + [k]
    if v.class == Hash
      ret.merge!(hash_flatten(v, ks))
    else
      ret.merge!({ks.join('_')=> v})
    end
  }
  ret
end