Sha256: f3b5bdf685349ada9ef756634ca430dbec1fcb3540b9d401ceada1cad621195f

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require 'eventmachine'

module Ganymed
  module Client

    ##
    # An EventMachine Protocol that can send samples to a Ganymed sampler.
    #
    module Sampler
      include EM::Deferrable

      # Emit a new sample.
      #
      # @param [String, Symbol] ds  Sample data source.
      # @param [String] ns  Event namespace.
      # @param [Fixnum, Float] value  Sample value.
      def emit(ds, ns, value)
        data = [ds.to_s, ns, @origin, value.to_f]
        send_datagram(data.pack("Z*Z*Z*G"), @host, @port)
      end

      # Connect to a Ganymed sampler.
      #
      # @param [String] host  Host to connect to.
      # @param [Fixnum] port  Port to connect to.
      # @param [String] origin Origin of events. Defaults to the
      #                        fully-qualified hostname.
      def self.connect(host, port, origin=nil)
        EM.open_datagram_socket("0.0.0.0", 0, self, host, port, origin)
      end

      # @private
      def initialize(host, port, origin=nil)
        @host, @port = host, port
        @origin = origin || ::Socket.gethostbyname(::Socket.gethostname).first
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ganymed-client-0.2.0 lib/ganymed/client/sampler.rb