Sha256: c896c6886979d4a2a45dc9d0af3d11b7ddd5bd12eee1f333c9e130b794a662c4

Contents?: true

Size: 1.12 KB

Versions: 6

Compression:

Stored size: 1.12 KB

Contents

module Graphiterb
  class Sender
    def initialize
      open!
    end

    def open!
      begin
        Graphiterb.log.warn "Connecting to server #{Settings.carbon_server} port #{Settings.carbon_port}"
        @socket = TCPSocket.new(Settings.carbon_server, Settings.carbon_port)
      rescue StandardError => e
        Graphiterb.log.warn "Couldn't connect to server #{Settings.carbon_server} port #{Settings.carbon_port}: #{e.class} #{e}"
        $stderr
      end
    end

    def socket
      @socket ||= open!
    end

    def safely &block
      begin
        block.call
      rescue StandardError => e
        Graphiterb.log.warn "Sleeping #{Settings.on_error_delay}: #{e.class} #{e}"
        sleep Settings.on_error_delay
        @socket = nil
        return nil
      end
    end

    def timestamp
      Time.now.to_i
    end

    def send *metrics
      return if metrics.blank?
      now = timestamp
      safely do
        message = metrics.map{|metric, val, ts| [metric, val, (ts||now)].join(" ") }.join("\n")+"\n"
        socket.puts(message)
        Graphiterb.log.info message.gsub(/\n+/, "\t")
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graphiterb-0.2.13 lib/graphiterb/sender.rb
graphiterb-0.2.12 lib/graphiterb/sender.rb
graphiterb-0.2.11 lib/graphiterb/sender.rb
graphiterb-0.2.10 lib/graphiterb/sender.rb
graphiterb-0.2.9 lib/graphiterb/sender.rb
graphiterb-0.2.7 lib/graphiterb/sender.rb