Sha256: ff86f21ff6ad197afeccc52cbbaf29ba9fc51cdfb9149344703f906ba1fb1657

Contents?: true

Size: 1020 Bytes

Versions: 1

Compression:

Stored size: 1020 Bytes

Contents

require 'socket'

module Ganymed
  module Client

    ##
    # An EventMachine Protocol that can send samples to a Ganymed sampler.
    #
    class Sampler < UDPSocket
      attr_accessor :origin

      # 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(data.pack("Z*Z*Z*G"), 0)
      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)
        new.tap do |socket|
          socket.connect(host, port)
          socket.origin = origin || ::Socket.gethostbyname(::Socket.gethostname).first
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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