tsd_client 0.0.1

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.
Files changed (3) hide show
  1. data/lib/tsd_client/format.rb +68 -0
  2. data/lib/tsd_client.rb +49 -0
  3. metadata +48 -0
@@ -0,0 +1,68 @@
1
+ require 'uri'
2
+
3
+ module TSD
4
+ module Format
5
+ def self.time input
6
+ case input.class.name
7
+ when 'Time'
8
+ input.strftime '%Y/%m/%d-%H:%M:%S'
9
+ when 'Fixnum'
10
+ time Time.at input
11
+ else
12
+ input
13
+ end
14
+ end
15
+
16
+ def self.tags tags
17
+ tags.collect do |tag, value|
18
+ [tag, value].join '='
19
+ end
20
+ end
21
+
22
+ def self.query_metric options
23
+ raise 'must provide key: :metric' unless options[:metric]
24
+ options = {aggregator: 'sum', rate: false}.merge options
25
+
26
+ metric_query = [options[:aggregator]]
27
+ metric_query << options[:downsample] if options[:downsample]
28
+ metric_query << 'rate' if options[:rate]
29
+
30
+ if options[:tags]
31
+ metric_query << [options[:metric], '{' + tags(options[:tags]).join(',') + '}'].join
32
+ else
33
+ metric_query << options[:metric]
34
+ end
35
+
36
+ metric_query.join ':'
37
+ end
38
+
39
+ def self.query options
40
+ raise 'must provide key: :start' unless options[:start]
41
+ options_filter = [:metric, :aggregator, :rate, :downsample, :tags]
42
+
43
+ # process query params
44
+ params = options.reduce({m: query_metric(options)}) do |params, (option, value)|
45
+ unless options_filter.include? option
46
+ params[option] = case option
47
+ when :start, :end # time formatting
48
+ time value
49
+ else
50
+ value
51
+ end
52
+ end
53
+
54
+ params
55
+ end
56
+
57
+ # assemble query
58
+ URI.escape '/q?' + params.map {|option, value| [option, value].join('=')}.unshift('ascii').join('&')
59
+ end
60
+
61
+ def self.put options
62
+ [:metric, :value].any? {|key| raise "missing key: #{key}" unless options.has_key? key}
63
+ options = {time: Time.now, tags: {}}.merge options
64
+
65
+ ['put', options[:metric], options[:time].to_i, options[:value], tags(options[:tags])].flatten.join("\s")
66
+ end
67
+ end
68
+ end
data/lib/tsd_client.rb ADDED
@@ -0,0 +1,49 @@
1
+ $:.unshift File.join File.dirname(__FILE__), 'tsd_client'
2
+
3
+ require 'socket'
4
+ require 'timeout'
5
+ require 'net/http'
6
+ require 'tsd_client/format'
7
+
8
+ module TSD
9
+ class Client
10
+ attr_reader :options
11
+
12
+ def initialize options
13
+ @options = {
14
+ host: '0.0.0.0',
15
+ port: 4242,
16
+ timeout: 120, # seconds
17
+ }.merge options
18
+ end
19
+
20
+ def query options
21
+ response = nil
22
+
23
+ Timeout::timeout @options[:timeout] do
24
+ response = Net::HTTP.start @options[:host], @options[:port] do |http|
25
+ http.request Net::HTTP::Get.new Format.query options
26
+ end
27
+ end
28
+
29
+ if response.kind_of? Net::HTTPSuccess
30
+ response.body.split("\n").collect do |record|
31
+ metric, timestamp, value, *tags = record.split "\s"
32
+
33
+ Hash[[:metric, :time, :value, :tags].zip([
34
+ metric, Time.at(timestamp.to_i), value.to_f, Hash[tags.collect {|tag| tag.split('=')}]])]
35
+ end
36
+ else
37
+ raise 'query failed: ' + response.code
38
+ end
39
+ end
40
+
41
+ def put options
42
+ Timeout::timeout @options[:timeout] do
43
+ TCPSocket.open @options[:host], @options[:port] do |socket|
44
+ socket.puts Format.put options
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tsd_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dallas Marlow
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ - dallasmarlow@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/tsd_client.rb
22
+ - lib/tsd_client/format.rb
23
+ homepage: https://github.com/dallasmarlow/tsd_client
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.23
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: OpenTSDB client
47
+ test_files: []
48
+ has_rdoc: