Sha256: 913ad9d2635814b2f93f1cf5d11bd84abbcf333ee58b4bf831b768be887da8c5

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module Termite
  class HasturLogger
    def initialize(socket, addr, port, labels)
      @socket, @addr, @port = socket, addr, port
      @labels = labels || {}
    end

    def to_usec(time)
      (time.to_f * 1_000_000).round
    end

    def send_message(severity, raw_message, app_data, time=Time.now, data='{}')
      severity = Logger::LOGGER_LEVEL_MAP.invert[severity].to_s
      tid = Ecology.thread_id(::Thread.current)
      hostname = Socket.gethostname
      pid = Process.pid
      application, component = app_data[:app], app_data[:component]

      message = {
        :_route => :log,
        :timestamp => to_usec(time),
        :message => raw_message
      }

      labels = {
        :severity => severity,
        :pid => pid,
        :tid => tid,
        :app => application,
        :component => component,
        :hostname => hostname
      }

      labels.merge!(MultiJson.decode(data)) if data && data != '{}'
      labels.merge!(@labels) if @labels && !@labels.empty?
      message[:labels] = labels

      @socket.send MultiJson.encode(message), 0, @addr, @port
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
termite-0.0.20 lib/termite/hastur_logger.rb