class Fluent::LogmaticOutput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_logmatic.rb, line 26
def initialize
  super
end

Public Instance Methods

client() click to toggle source
# File lib/fluent/plugin/out_logmatic.rb, line 47
def client

 @_socket ||= if @use_ssl
    context    = OpenSSL::SSL::SSLContext.new
    socket     = TCPSocket.new @host, @ssl_port
    ssl_client = OpenSSL::SSL::SSLSocket.new socket, context
    ssl_client.connect
  else
    TCPSocket.new @host, @port
  end

  @_socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)

  begin
       @_socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPIDLE, 10)
       @_socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPINTVL, 3)
       @_socket.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPCNT, 3)
  rescue
       # JRuby defines SOL_TCP, but it doesn't work
       @_socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_KEEPINTVL, 3)
       @_socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_KEEPCNT, 3)
  end

  return @_socket

end
configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_logmatic.rb, line 35
def configure(conf)
  super
end
format(tag, time, record) click to toggle source

This method is called when an event reaches Fluentd.

# File lib/fluent/plugin/out_logmatic.rb, line 75
def format(tag, time, record)
  return [tag, record].to_msgpack
end
send_to_logmatic(data) click to toggle source
# File lib/fluent/plugin/out_logmatic.rb, line 101
def send_to_logmatic(data)


  retries = 0

  begin

    # Check the connectivity and write messages
    #connected,x = client.recv(0)
    #log.trace  "Connected=#{connected},#{x}"
    #raise Errno::ECONNREFUSED, "Client has lost server connection" if connected == 0
    log.trace "New attempt to Logmatic attempt=#{retries}" if retries > 0
    log.trace "Send nb_event=#{data.size} events to Logmatic"
    data.each do |event|
      client.write(event)
    end


  # Handle some failures
  rescue Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ETIMEDOUT, Errno::EPIPE => e

    if retries < @max_retries || max_retries == -1
      @_socket = nil
      a_couple_of_seconds = retries ** 2
      a_couple_of_seconds = 30 unless a_couple_of_seconds < 30
      retries += 1
      log.warn "Could not push logs to Logmatic, attempt=#{retries} max_attempts=#{max_retries} wait=#{a_couple_of_seconds}s error=#{e.message}"
      sleep a_couple_of_seconds
      retry
    end
    raise ConnectionFailure, "Could not push logs to Logmatic after #{retries} retries, #{e.message}"
  end
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_logmatic.rb, line 43
def shutdown
  super
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_logmatic.rb, line 39
def start
  super
end
write(chunk) click to toggle source

NOTE! This method is called by internal thread, not Fluentd's main thread. 'chunk' is a buffer chunk that includes multiple formatted events.

# File lib/fluent/plugin/out_logmatic.rb, line 81
def write(chunk)

  messages = Array.new
  chunk.msgpack_each do |tag, record|
    next unless record.is_a? Hash
    next unless record.has_key? "message"

    if @include_tag_key
      record[@tag_key] = tag
    end
    if @use_json
      messages.push "#{api_key} " + Yajl.dump(record) + "\n"
    else
      messages.push "#{api_key} " + record["message"].rstrip() + "\n"
    end
  end
  send_to_logmatic(messages)

end