Class: Fluent::HttpRecordModifier
- Inherits:
-
Filter
- Object
- Filter
- Fluent::HttpRecordModifier
- Defined in:
- lib/fluent/plugin/filter_http_record_modifier.rb
Defined Under Namespace
Classes: PlaceholderExpander
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #create_request(tag, time, record) ⇒ Object
- #deserialize_body(res) ⇒ Object
- #filter_stream(tag, es) ⇒ Object
- #format_params(tag, time, record) ⇒ Object
-
#initialize ⇒ HttpRecordModifier
constructor
A new instance of HttpRecordModifier.
- #send_request(req, uri) ⇒ Object
- #set_body(req, tag, time, record) ⇒ Object
Constructor Details
#initialize ⇒ HttpRecordModifier
Returns a new instance of HttpRecordModifier.
5 6 7 8 9 10 11 |
# File 'lib/fluent/plugin/filter_http_record_modifier.rb', line 5 def initialize require 'socket' require 'yajl' require 'net/http' require 'uri' super end |
Instance Method Details
#configure(conf) ⇒ Object
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 61 62 |
# File 'lib/fluent/plugin/filter_http_record_modifier.rb', line 26 def configure(conf) super @method ||= conf['method'] @map = {} # <record></record> directive conf.elements.select { |element| element.name == 'record' }.each do |element| element.each_pair do |k, v| element.has_key?(k) # to suppress unread configuration warning @map[k] = parse_value(v) end end @maped_params = {} # <params></params> directive conf.elements.select { |element| element.name == 'params' }.each do |element| element.each_pair do |k, v| element.has_key?(k) # to suppress unread configuration warning @maped_params[k] = parse_value(v) end end if @remove_keys @remove_keys = @remove_keys.split(',') end if @keep_keys raise Fluent::ConfigError, "`renew_record` must be true to use `keep_keys`" unless @renew_record @keep_keys = @keep_keys.split(',') end = PlaceholderExpander.new({ :log => log, :auto_typecast => @auto_typecast, }) @hostname = Socket.gethostname end |
#create_request(tag, time, record) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/fluent/plugin/filter_http_record_modifier.rb', line 134 def create_request(tag, time, record) url = URI.encode(@endpoint_url.to_s) uri = URI.parse(url) params = format_params(tag, time, record) uri.query = URI.encode_www_form(params) req = Net::HTTP.const_get(@method.to_s.capitalize).new(uri) unless @method.to_s.capitalize == 'Get' set_body(req, tag, time, record) end return req, uri end |
#deserialize_body(res) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/fluent/plugin/filter_http_record_modifier.rb', line 95 def deserialize_body(res) clean = {} body = res.body if res.content_type == 'application/json' body = Yajl.load(body) if body.is_a? Hash clean = Yajl.load(res.body) end end clean['body'] = body clean end |
#filter_stream(tag, es) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/fluent/plugin/filter_http_record_modifier.rb', line 64 def filter_stream(tag, es) new_es = MultiEventStream.new tag_parts = tag.split('.') tag_prefix = tag_prefix(tag_parts) tag_suffix = tag_suffix(tag_parts) placeholders = { 'tag' => tag, 'tag_parts' => tag_parts, 'tag_prefix' => tag_prefix, 'tag_suffix' => tag_suffix, 'hostname' => @hostname, } last_record = nil es.each do |time, record| last_record = record # for debug log req, uri = create_request(tag, time, record) res = send_request(req, uri) body = deserialize_body(res) new_record = reform(time, record, placeholders, body) if @renew_time_key && new_record.has_key?(@renew_time_key) time = new_record[@renew_time_key].to_i end new_es.add(time, new_record) end new_es rescue => e log.warn "failed to reform records", :error_class => e.class, :error => e. log.warn_backtrace log.debug "map:#{@map} record:#{last_record} placeholders:#{placeholders}" end |
#format_params(tag, time, record) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/fluent/plugin/filter_http_record_modifier.rb', line 108 def format_params(tag, time, record) tag_parts = tag.split('.') tag_prefix = tag_prefix(tag_parts) tag_suffix = tag_suffix(tag_parts) placeholders = { 'tag' => tag, 'tag_parts' => tag_parts, 'tag_prefix' => tag_prefix, 'tag_suffix' => tag_suffix, 'hostname' => @hostname, } .prepare_placeholders(time, record, placeholders) (@maped_params) end |
#send_request(req, uri) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/fluent/plugin/filter_http_record_modifier.rb', line 146 def send_request(req, uri) res = nil begin if @auth and @auth == :basic req.basic_auth(@username, @password) end res = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) } rescue => e # rescue all StandardErrors # server didn't respond $log.warn "Net::HTTP.#{req.method.capitalize} raises exception: #{e.class}, '#{e.message}'" raise e if @raise_on_error end # end begin end |
#set_body(req, tag, time, record) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/fluent/plugin/filter_http_record_modifier.rb', line 124 def set_body(req, tag, time, record) if @serializer == :json req['Content-Type'] = 'application/json' else req.set_form_data(record) end req end |