Class: Fluent::Plugin::HttpInput

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

Defined Under Namespace

Classes: Handler, KeepaliveManager

Constant Summary collapse

EMPTY_GIF_IMAGE =
"GIF89a\u0001\u0000\u0001\u0000\x80\xFF\u0000\xFF\xFF\xFF\u0000\u0000\u0000,\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u0000\u0000\u0002\u0002D\u0001\u0000;".force_encoding("UTF-8")
EVENT_RECORD_PARAMETER =
'_event_record'
RES_TEXT_HEADER =
{'Content-Type' => 'text/plain'}.freeze
RESPONSE_200 =
["200 OK".freeze, RES_TEXT_HEADER, "".freeze].freeze
RESPONSE_204 =
["204 No Content".freeze, {}.freeze].freeze
RESPONSE_IMG =
["200 OK".freeze, {'Content-Type'=>'image/gif; charset=utf-8'}.freeze, EMPTY_GIF_IMAGE].freeze
RES_400_STATUS =
"400 Bad Request".freeze
RES_500_STATUS =
"500 Internal Server Error".freeze

Constants inherited from Base

Base::LOCK_FILE_BUCKETS

Constants included from Configurable

Configurable::CONFIG_TYPE_REGISTRY

Instance Attribute Summary

Attributes included from Fluent::PluginLoggerMixin

#log

Attributes inherited from Base

#under_plugin_development

Instance Method Summary collapse

Methods inherited from Input

#metric_callback, #statistics, #zero_downtime_restart_ready?

Methods included from Fluent::PluginHelper::Mixin

included

Methods included from Fluent::PluginLoggerMixin

included, #terminate

Methods included from Fluent::PluginId

#plugin_id, #plugin_id_configured?, #plugin_id_for_test?, #plugin_root_dir, #stop

Methods inherited from Base

#acquire_worker_lock, #after_shutdown, #after_shutdown?, #after_start, #after_started?, #before_shutdown, #before_shutdown?, #called_in_test?, #closed?, #configured?, #context_router, #context_router=, #fluentd_worker_id, #get_lock_path, #has_router?, #inspect, #plugin_root_dir, #reloadable_plugin?, #shutdown, #shutdown?, #started?, #stop, #stopped?, #string_safe_encoding, #terminate, #terminated?

Methods included from SystemConfig::Mixin

#system_config, #system_config_override

Methods included from Configurable

#config, #configure_proxy_generate, #configured_section_create, included, lookup_type, register_type

Constructor Details

#initialize ⇒ HttpInput

Returns a new instance of HttpInput.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/fluent/plugin/in_http.rb', line 117

def initialize
  super

  @km = nil
  @nodes = []
  @format_name = nil
  @parser_time_key = nil

  # default parsers
  @parser_msgpack = nil
  @parser_json = nil
  @default_time_parser = nil
  @default_keep_time_key = nil
  @float_time_parser = nil

  # <parse> configured parser
  @custom_parser = nil
end

Instance Method Details

#close ⇒ Object



218
219
220
221
# File 'lib/fluent/plugin/in_http.rb', line 218

def close
  server_wait_until_stop
  super
end

#configure(conf) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/fluent/plugin/in_http.rb', line 136

def configure(conf)
  compat_parameters_convert(conf, :parser)

  super

  if @cors_allow_credentials
    if @cors_allow_origins.nil? || @cors_allow_origins.include?('*')
      raise Fluent::ConfigError, "Cannot enable cors_allow_credentials without specific origins"
    end
  end

  raise Fluent::ConfigError, "'add_tag_prefix' parameter must not be empty" if @add_tag_prefix && @add_tag_prefix.empty?

  configure_security if @security

  m = if @parser_configs.first['@type'] == 'in_http'
        @parser_msgpack = parser_create(usage: 'parser_in_http_msgpack', type: 'msgpack')
        @parser_msgpack.time_key = nil
        @parser_msgpack.estimate_current_event = false
        @parser_json = parser_create(usage: 'parser_in_http_json', type: 'json')
        @parser_json.time_key = nil
        @parser_json.estimate_current_event = false

        default_parser = parser_create(usage: '')
        @format_name = 'default'
        @parser_time_key = default_parser.time_key
        @default_time_parser = default_parser.get_time_parser
        @default_keep_time_key = default_parser.keep_time_key
        method(:parse_params_default)
      else
        @custom_parser = parser_create
        @format_name = @parser_configs.first['@type']
        @parser_time_key = @custom_parser.time_key
        method(:parse_params_with_parser)
      end
  self.singleton_class.module_eval do
    define_method(:parse_params, m)
  end
end

#multi_workers_ready? ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/fluent/plugin/in_http.rb', line 200

def multi_workers_ready?
  true
end

#on_request(path_info, params) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/fluent/plugin/in_http.rb', line 230

def on_request(path_info, params)
  begin
    path = path_info[1..-1]  # remove /
    tag = path.split('/').join('.')
    tag = "#{@add_tag_prefix}.#{tag}" if @add_tag_prefix

    mes = Fluent::MultiEventStream.new
    parse_params(params) do |record_time, record|
      if record.nil?
        log.debug { "incoming event is invalid: path=#{path_info} params=#{params.to_json}" }
        next
      end

      add_params_to_record(record, params)

      time = if param_time = params['time']
               param_time = param_time.to_f
               param_time.zero? ? Fluent::EventTime.now : @float_time_parser.parse(param_time)
             else
               record_time.nil? ? convert_time_field(record) : record_time
             end

      mes.add(time, record)
    end
  rescue => e
    if @dump_error_log
      log.error "failed to process request", error: e
    end
    return [RES_400_STATUS, RES_TEXT_HEADER, "400 Bad Request\n#{e}\n"]
  end

  # TODO server error
  begin
    router.emit_stream(tag, mes) unless mes.empty?
  rescue => e
    if @dump_error_log
      log.error "failed to emit data", error: e
    end
    return [RES_500_STATUS, RES_TEXT_HEADER, "500 Internal Server Error\n#{e}\n"]
  end

  if @respond_with_empty_img
    return RESPONSE_IMG
  else
    if @use_204_response
      return RESPONSE_204
    else
      return RESPONSE_200
    end
  end
end

#start ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/fluent/plugin/in_http.rb', line 204

def start
  @_event_loop_run_timeout = @blocking_timeout

  super

  log.debug "listening http", bind: @bind, port: @port

  @km = KeepaliveManager.new(@keepalive_timeout)
  event_loop_attach(@km)

  server_create_connection(:in_http, @port, bind: @bind, backlog: @backlog, &method(:on_server_connect))
  @float_time_parser = Fluent::NumericTimeParser.new(:float)
end