Plugin Helper: Http Server

http_server helper creates http server. This helper was introduced since v1.6.0.

Here is the code example with http_server helper:

require 'fluent/plugin/input'
module Fluent::Plugin
class ExampleInput < Input
Fluent::Plugin.register_output('example', self)
# 1. load http_server helper
helpers :http_server
config_param :bind, :string
config_param :port, :integer
def start
# 2. create and start http server
create_http_server(:example_http_server, addr: @bind, port: @port, logger: log) do |serv|
# define endpoint `/hello` with GET method
serv.get('/hello') { [200, { 'Content-Type' => 'text/plain' }, 'hello!'] }

Launched http server is managed by the helper. No need to stop http server in plugin's stop method. The plugin stops launched http server automatically.


create_http_server(title, addr:, port:, logger:, default_app: nil, &block)

This method creats and runs http server with given routes which are defined in &block.

  • title: the name of listen thread. this name must be unique

  • addr: Adderess to listen to

  • port: Port to listen to

  • logger: Logger which is used in server helper

  • default_app: Use this object when server received a request whose path is not registered. This object must have #call or be a Proc object.

Define other HTTP methods

create_http_server(:example_http_server, addr: @bind, port: @port, logger: log) do |serv|
# define POST method `/hello`'/hello') { [200, { 'Content-Type' => 'text/plain' }, 'hello!'] }
# define HEAD method `/hello`
serv.head('/hello') { [200, { 'Content-Type' => 'text/plain' }, nil] }

Detail of request and send response


request has following methods.

  • query_string: query string like hoge=v1&fuga=v2

  • query: query which is query_string parsed with CGI.parse

  • body: request body

  • path: request path


http server helper expects an array as return value like below.

[${response_status}, ${headers}, ${body}]

  • ${response_status} should be an Integer

  • ${headers} should be a Hash

  • ${body} should be a String or nil

Example of recieving json request and return json response

create_http_server(:example_json_http_server, addr: @bind, port: @port, logger: log) do |serv|'/hello.json') do |req|
body = JSON.parse(req.body)
[code, { 'Content-Type' => 'application/json' }, { 'status' => 'success' }.to_json]

http_server used plugins

If this article is incorrect or outdated, or omits critical information, please let us know. Fluentd is a open source project under Cloud Native Computing Foundation (CNCF). All components are available under the Apache 2 License.