tsd_client 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ require 'socket'
1
2
  require 'uri'
2
3
 
3
4
  module TSD
@@ -6,6 +7,8 @@ module TSD
6
7
  case input.class.name
7
8
  when 'Time'
8
9
  input.strftime '%Y/%m/%d-%H:%M:%S'
10
+ when 'Date'
11
+ time input.to_time
9
12
  when 'Fixnum'
10
13
  time Time.at input
11
14
  else
@@ -36,7 +39,7 @@ module TSD
36
39
  metric_query.join ':'
37
40
  end
38
41
 
39
- def self.query options
42
+ def self.query options, format = 'ascii'
40
43
  raise 'must provide key: :start' unless options[:start]
41
44
  options_filter = [:metric, :aggregator, :rate, :downsample, :tags]
42
45
 
@@ -55,14 +58,16 @@ module TSD
55
58
  end
56
59
 
57
60
  # assemble query
58
- URI.escape '/q?' + params.map {|option, value| [option, value].join('=')}.unshift('ascii').join('&')
61
+ URI.escape '/q?' + params.map {|option, value| [option, value].join('=')}.unshift(format).join('&')
59
62
  end
60
63
 
61
64
  def self.put options
62
65
  [:metric, :value].any? {|key| raise "missing key: #{key}" unless options.has_key? key}
63
- options = {time: Time.now, tags: {}}.merge options
66
+
67
+ options[:time] ||= Time.now
68
+ options[:tags] ||= {host: Socket.gethostname}
64
69
 
65
70
  ['put', options[:metric], options[:time].to_i, options[:value], tags(options[:tags])].flatten.join("\s")
66
71
  end
67
72
  end
68
- end
73
+ end
data/lib/tsd_client.rb CHANGED
@@ -7,9 +7,7 @@ require 'tsd_client/format'
7
7
 
8
8
  module TSD
9
9
  class Client
10
- attr_reader :options
11
-
12
- def initialize options
10
+ def initialize options = {}
13
11
  @options = {
14
12
  host: '0.0.0.0',
15
13
  port: 4242,
@@ -18,20 +16,18 @@ module TSD
18
16
  end
19
17
 
20
18
  def query options
21
- response = nil
22
-
23
- Timeout::timeout @options[:timeout] do
24
- response = Net::HTTP.start @options[:host], @options[:port] do |http|
19
+ response = Timeout::timeout @options[:timeout] do
20
+ Net::HTTP.start @options[:host], @options[:port] do |http|
25
21
  http.request Net::HTTP::Get.new Format.query options
26
22
  end
27
23
  end
28
24
 
29
25
  if response.kind_of? Net::HTTPSuccess
30
- response.body.split("\n").collect do |record|
26
+ response.body.split("\n").map do |record|
31
27
  metric, timestamp, value, *tags = record.split "\s"
32
28
 
33
29
  Hash[[:metric, :time, :value, :tags].zip([
34
- metric, Time.at(timestamp.to_i), value.to_f, Hash[tags.collect {|tag| tag.split('=')}]])]
30
+ metric, Time.at(timestamp.to_i), value.to_f, Hash[tags.map {|tag| tag.split('=')}]])]
35
31
  end
36
32
  else
37
33
  raise 'query failed: ' + response.code
@@ -42,8 +38,11 @@ module TSD
42
38
  Timeout::timeout @options[:timeout] do
43
39
  TCPSocket.open @options[:host], @options[:port] do |socket|
44
40
  socket.puts Format.put options
41
+
42
+ response, _, _ = socket.recvmsg_nonblock rescue nil
43
+ response.chomp if response
45
44
  end
46
45
  end
47
46
  end
48
47
  end
49
- end
48
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tsd_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-24 00:00:00.000000000 Z
12
+ date: 2014-04-26 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email:
@@ -45,4 +45,3 @@ signing_key:
45
45
  specification_version: 3
46
46
  summary: OpenTSDB client
47
47
  test_files: []
48
- has_rdoc: