class OpenTSDB::Client

Attributes

connection[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/opentsdb/client.rb, line 10
def initialize(options = {})
  hostname    = options[:hostname] || 'localhost'
  port        = options[:port] || 4242
  @connection = TCPSocket.new(hostname, port)
rescue
  raise Errors::UnableToConnectError
end

Public Instance Methods

build_command(input) click to toggle source
# File lib/opentsdb/client.rb, line 27
def build_command(input)
  if input.is_a?(Array)
    input.collect { |unit| to_command(unit) }.join("\n")
  else
    to_command(input)
  end.chomp
end
put(options = {}) click to toggle source
# File lib/opentsdb/client.rb, line 35
def put(options = {})
  @connection.puts(build_command(options))
end
to_command(options) click to toggle source
# File lib/opentsdb/client.rb, line 18
def to_command(options)
  timestamp   = options[:timestamp].to_i
  metric_name = options[:metric]
  value       = options[:value].to_f
  tags        = options[:tags].map { |k, v| "#{k}=#{v}" }.join(' ')

  "put #{metric_name} #{timestamp} #{value} #{tags}"
end