Class: Fluent::Plugin::ParseRequestBodyExtractor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin, conf) ⇒ ParseRequestBodyExtractor

Returns a new instance of ParseRequestBodyExtractor.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fluent/plugin/parse_request_body_extractor.rb', line 10

def initialize(plugin, conf)
  @log = plugin.log

  if plugin.is_a?(Fluent::Plugin::Output)
    unless have_tag_option?(plugin)
      raise Fluent::ConfigError, "out_parse_request_body: At least one of remove_tag_prefix/remove_tag_suffix/add_tag_prefix/add_tag_suffix is required to be set."
    end
  end

  @key = plugin.key
  @only = plugin.only
  @except = plugin.except
  @discard_key = plugin.discard_key
  @add_field_prefix = plugin.add_field_prefix
  @permit_blank_key = plugin.permit_blank_key

  if @only
    @include_keys = @only.split(/\s*,\s*/).inject({}) do |hash, i|
      hash[i] = true
      hash
    end
  end

  if @except
    @exclude_keys = @except.split(/\s*,\s*/).inject({}) do |hash, i|
      hash[i] = true
      hash
    end
  end

  @map = {}
# <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
      v = v[1..v.size-2] if quoted_value?(v)
      @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
          message = "parse_body: failed to parse '#{v}' as json."
          log.error message, error: e
          raise Fluent::ConfigError, message
        end
      }
      validate_json.call if json?(v.tr('\'"\\', ''))
    }
  }
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



8
9
10
# File 'lib/fluent/plugin/parse_request_body_extractor.rb', line 8

def log
  @log
end

Instance Method Details

#add_query_params_field(record) ⇒ Object



75
76
77
78
79
80
# File 'lib/fluent/plugin/parse_request_body_extractor.rb', line 75

def add_query_params_field(record)
  return record unless record[@key]
  add_query_params(record[@key], record)
  record.delete(@key) if @discard_key
  record
end

#add_record_field(record) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/fluent/plugin/parse_request_body_extractor.rb', line 67

def add_record_field(record)
  return record if @map.values.first.nil?
  @map.each do |record_key, value|
    record[record_key] = value
  end
  record
end

#quoted_value?(text) ⇒ Boolean

Returns:



62
63
64
65
# File 'lib/fluent/plugin/parse_request_body_extractor.rb', line 62

def quoted_value?(text)
  # to improbe compatibility with fluentd v1-config
  text.match(/(^'.+'$|^".+"$)/)
end