Class: Fluent::Plugin::UaParserFilter

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

Instance Method Summary collapse

Constructor Details

#initializeUaParserFilter

Returns a new instance of UaParserFilter.



9
10
11
12
# File 'lib/fluent/plugin/filter_ua_parser.rb', line 9

def initialize
  @ua_cache = LruRedux::Cache.new(4096)
  super
end

Instance Method Details

#configure(conf) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/fluent/plugin/filter_ua_parser.rb', line 20

def configure(conf)
  super
  begin
    @parser = UserAgentParser::Parser.new(patterns_path: @patterns_path)
  rescue => e
    @parser = UserAgentParser::Parser.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) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fluent/plugin/filter_ua_parser.rb', line 31

def filter(tag, time, record)
  ua_string = record[@key_name]
  record.delete(@key_name) if @delete_key
  unless ua_string.nil?
    user_agent_detail = @ua_cache.getset(ua_string) { get_ua_detail(ua_string) }
    if flatten
      record.merge! hash_flatten(user_agent_detail, [@out_key])
    else
      record[@out_key] = user_agent_detail
    end
  end
  record
end