Class: Opentsdb::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/opentsdb/client.rb

Overview

ruby client for OpenTsdb HTTP API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
# File 'lib/opentsdb/client.rb', line 11

def initialize(options = {})
  @host = options.delete(:host) || Opentsdb.host
  @port = options.delete(:port) || Opentsdb.port
  @query_options = options.merge(Opentsdb.options)
  @faraday = Opentsdb::Faraday.new(query_url, @query_options)
  @query_commads = parse_queries
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/opentsdb/client.rb', line 8

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/opentsdb/client.rb', line 8

def port
  @port
end

#query_commadsObject

Returns the value of attribute query_commads.



9
10
11
# File 'lib/opentsdb/client.rb', line 9

def query_commads
  @query_commads
end

#query_optionsObject (readonly)

Returns the value of attribute query_options.



8
9
10
# File 'lib/opentsdb/client.rb', line 8

def query_options
  @query_options
end

Instance Method Details

#parse_queriesObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/opentsdb/client.rb', line 33

def parse_queries
  [].tap do |qs|
    query_options[:q].split(';').each do |q|
      query = QueryParser.parse(q)
      query.interval   = query_options[:interval]
      query.start_time = query_options[:begin].to_i
      query.end_time   = query_options[:end].to_i
      qs << query
    end
  end
end

#queryObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/opentsdb/client.rb', line 19

def query
  [].tap do |results|
    query_commads.each do |query_commad|
      start = Time.now
      Opentsdb.logger.debug "[OpenTSDB] query: #{query_commad.to_json}"
      res = post query_commad.to_json
      Opentsdb.logger.debug "[OpenTSDB] result: #{res.body}"
      Opentsdb.logger.info "[OpenTSDB] consume: #{Time.now - start} s"
      status = res.status.to_i == 200 ? 'ok' : 'error'
      results << { status: status, condition: query_commad, result: res.body }
    end
  end
end