Class: Fluent::GeoipOutput

Inherits:
BufferedOutput
  • Object
show all
Includes:
HandleTagNameMixin, Mixin::RewriteTagName, SetTagKeyMixin
Defined in:
lib/fluent/plugin/out_geoip.rb

Constant Summary collapse

REGEXP_JSON =
/(^[\[\{].+[\]\}]$|^[\d\.\-]+$)/
REGEXP_PLACEHOLDER_SINGLE =
/^\$\{(?<geoip_key>-?[^\[]+)\['(?<record_key>-?[^']+)'\]\}$/
REGEXP_PLACEHOLDER_SCAN =
/(\$\{[^\}]+?\})/
GEOIP_KEYS =
%w(city latitude longitude country_code3 country_code2 country_code country_name dma_code area_code region)

Instance Method Summary collapse

Constructor Details

#initializeGeoipOutput

Returns a new instance of GeoipOutput.



30
31
32
33
34
35
# File 'lib/fluent/plugin/out_geoip.rb', line 30

def initialize
  require 'geoip'
  require 'yajl'

  super
end

Instance Method Details

#configure(conf) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fluent/plugin/out_geoip.rb', line 37

def configure(conf)
  super

  @map = {}
  @geoip_lookup_key = @geoip_lookup_key.split(/\s*,\s*/)

  # enable_key_* format (legacy format)
  conf.keys.select{|k| k =~ /^enable_key_/}.each do |key|
    geoip_key = key.sub('enable_key_','')
    raise Fluent::ConfigError, "geoip: unsupported key #{geoip_key}" unless GEOIP_KEYS.include?(geoip_key)
    @geoip_lookup_key.zip(conf[key].split(/\s*,\s*/)).each do |lookup_field,record_key|
      if record_key.nil?
        raise Fluent::ConfigError, "geoip: missing value found at '#{key} #{lookup_field}'"
      end
      @map.store(record_key, "${#{geoip_key}['#{lookup_field}']}")
    end
  end
  if conf.keys.select{|k| k =~ /^enable_key_/}.size > 0
    log.warn "geoip: 'enable_key_*' config format is obsoleted. use <record></record> directive for now."
    log.warn "geoip: for further details referable to https://github.com/y-ken/fluent-plugin-geoip"
  end

  # <record></record> directive
  conf.elements.select { |element| element.name == 'record' }.each { |element|
    element.each_pair { |k, v|
      element.has_key?(k) # to suppress unread configuration warning
      @map[k] = v
      validate_json = Proc.new { 
        begin
          dummy_text = Yajl::Encoder.encode('dummy_text')
          Yajl::Parser.parse(v.gsub(REGEXP_PLACEHOLDER_SCAN, dummy_text))
        rescue Yajl::ParseError => e
          raise Fluent::ConfigError, "geoip: failed to parse '#{v}' as json."
        end
      }
      validate_json.call if v.match(REGEXP_JSON)
    }
  }
  @placeholder_keys = @map.values.join.scan(REGEXP_PLACEHOLDER_SCAN).map{ |placeholder| placeholder[0] }.uniq
  @placeholder_keys.each do |key|
    geoip_key = key.match(REGEXP_PLACEHOLDER_SINGLE)[:geoip_key]
    raise Fluent::ConfigError, "geoip: unsupported key #{geoip_key}" unless GEOIP_KEYS.include?(geoip_key)
  end
  @placeholder_expander = PlaceholderExpander.new

  if ( !@tag && !@remove_tag_prefix && !@remove_tag_suffix && !@add_tag_prefix && !@add_tag_suffix )
    raise Fluent::ConfigError, "geoip: required at least one option of 'tag', 'remove_tag_prefix', 'remove_tag_suffix', 'add_tag_prefix', 'add_tag_suffix'."
  end

  @geoip = GeoIP::City.new(@geoip_database, :memory, false)
end

#format(tag, time, record) ⇒ Object



93
94
95
# File 'lib/fluent/plugin/out_geoip.rb', line 93

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#shutdownObject



97
98
99
# File 'lib/fluent/plugin/out_geoip.rb', line 97

def shutdown
  super
end

#startObject



89
90
91
# File 'lib/fluent/plugin/out_geoip.rb', line 89

def start
  super
end

#write(chunk) ⇒ Object



101
102
103
104
105
# File 'lib/fluent/plugin/out_geoip.rb', line 101

def write(chunk)
  chunk.msgpack_each do |tag, time, record|
    Fluent::Engine.emit(tag, time, add_geoip_field(record))
  end
end