class Fluent::TimberOutput

Constants

CONTENT_TYPE
HOST
MAX_ATTEMPTS
PATH
RETRYABLE_CODES
USER_AGENT
VERSION

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_timber.rb, line 22
def configure(conf)
  source_id = conf["source_id"]
  api_key = conf["api_key"]
  @path = "/sources/#{source_id}/frames"
  @headers = {
    "Authorization" => "Bearer #{api_key}",
    "Content-Type" => CONTENT_TYPE,
    "User-Agent" => USER_AGENT
  }
  super
end
format(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_timber.rb, line 46
def format(tag, time, record)
  dt_iso8601 = Time.at(time).utc.iso8601
  record.merge("dt" => dt_iso8601).to_msgpack
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_timber.rb, line 41
def shutdown
  @http_client.close if @http_client
  super
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_timber.rb, line 34
def start
  super
  require 'http'
  HTTP.default_options = {:keep_alive_timeout => 29}
  @http_client = HTTP.persistent(HOST)
end
write(chunk) click to toggle source
# File lib/fluent/plugin/out_timber.rb, line 51
def write(chunk)
  deliver(chunk, 1)
end

Private Instance Methods

deliver(chunk, attempt) click to toggle source
# File lib/fluent/plugin/out_timber.rb, line 56
def deliver(chunk, attempt)
  if attempt > MAX_ATTEMPTS
    log.error("msg=\"Max attempts exceeded dropping chunk\" attempt=#{attempt}")
    return false
  end

  body = chunk.read
  response = @http_client.headers(@headers).post(@path, body: body)
  response.flush
  code = response.code

  if code >= 200 && code <= 299
    true
  elsif RETRYABLE_CODES.include?(code)
    sleep_time = sleep_for_attempt(attempt)
    log.warn("msg=\"Retryable response from the Timber API\" " +
      "code=#{code} attempt=#{attempt} sleep=#{sleep_time}")
    sleep(sleep_time)
    deliver(chunk, attempt + 1)
  else
    log.error("msg=\"Fatal response from the Timber API\" code=#{code} attempt=#{attempt}")
    false
  end
end
sleep_for_attempt(attempt) click to toggle source
# File lib/fluent/plugin/out_timber.rb, line 81
def sleep_for_attempt(attempt)
  sleep_for = attempt ** 2
  sleep_for = sleep_for <= 60 ? sleep_for : 60
  (sleep_for / 2) + (rand(0..sleep_for) / 2)
end