kairosdb 0.2.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9f9ee9138b5221d86f49c49be02be4658bfb472d
4
+ data.tar.gz: 32d7f983cda6e4c571773af6a4063850635d2035
5
+ SHA512:
6
+ metadata.gz: 16723f83fd45856f24b8f69440cd73aacb4cdb9a288eef125ca99984f1308feccb885c2562163bab9255a1438437b3da55aa60f2d1deaed33410d4f0d7934c3a
7
+ data.tar.gz: 48ae45911a0319a8eafc385959c8b4fb9ddd8a0ac74337e10194293b6b991ad88d4f7dbc689c89f1708172465f0dbd55ab29711d6087b0599a8e005d293fd836
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at andre.leblanc@accelerated.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kairosdb-ruby.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Andre LeBlanc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # KairosDB::Ruby
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kairosdb`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'kairosdb-ruby'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install kairosdb-ruby
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kairosdb-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kairosdb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kairosdb"
8
+ spec.version = KairosDB::VERSION
9
+ spec.authors = ["Andre LeBlanc"]
10
+ spec.email = ["andre.leblanc88@gmail.com"]
11
+
12
+ spec.summary = %q{An unofficial Ruby library for KairosDB.}
13
+ spec.description = %q{This is an unofficial Ruby library for KairosDB.}
14
+ spec.homepage = "https://github.com/andremleblanc/kairosdb-ruby"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.11"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "webmock", "~> 1.24.2"
34
+ end
@@ -0,0 +1,9 @@
1
+ require 'kairosdb/config'
2
+ require 'kairosdb/errors'
3
+ require 'kairosdb/logging'
4
+ require 'kairosdb/version'
5
+ require 'kairosdb/query/core'
6
+ require 'kairosdb/query/metrics'
7
+
8
+ require 'kairosdb/client/http'
9
+ require 'kairosdb/client'
@@ -0,0 +1,35 @@
1
+ module KairosDB
2
+ class Client
3
+ attr_reader :config, :writer
4
+
5
+ include KairosDB::HTTP
6
+ include KairosDB::Logging
7
+ include KairosDB::Query::Core
8
+ include KairosDB::Query::Metrics
9
+
10
+ # (see KairosDB::Config#initialize)
11
+ # @example
12
+ # params = {
13
+ # host: '172.0.0.1',
14
+ # port: 8086
15
+ # }
16
+ # KairosDB::Client.new(params)
17
+ def initialize(opts = {})
18
+ @config = KairosDB::Config.new(opts)
19
+ @writer = self
20
+ @stopped = false
21
+
22
+ at_exit { stop! } if config.retry > 0
23
+ end
24
+
25
+ protected
26
+
27
+ def stop!
28
+ @stopped = true
29
+ end
30
+
31
+ def stopped?
32
+ @stopped
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,93 @@
1
+ module KairosDB
2
+ module HTTP
3
+ def post(url, data)
4
+ headers = { 'Content-Type' => 'application/json' }
5
+ connect_with_retry do |http|
6
+ response = do_request http, Net::HTTP::Post.new(url, headers), JSON.generate(data)
7
+ if response.is_a? Net::HTTPSuccess
8
+ handle_successful_response(response)
9
+ else
10
+ resolve_error(response)
11
+ end
12
+ end
13
+ end
14
+
15
+ def delete(url)
16
+ connect_with_retry do |http|
17
+ response = do_request http, Net::HTTP::Delete.new(url)
18
+ if response.is_a? Net::HTTPSuccess
19
+ handle_successful_response(response)
20
+ elsif response.is_a? Net::HTTPNotFound
21
+ false
22
+ else
23
+ resolve_error(response)
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def connect_with_retry(&block)
31
+ host = config.host
32
+ delay = config.initial_delay
33
+ retry_count = 0
34
+
35
+ begin
36
+ http = Net::HTTP.new(host, config.port)
37
+ http.open_timeout = config.open_timeout
38
+ http.read_timeout = config.read_timeout
39
+
40
+ block.call(http)
41
+
42
+ rescue Timeout::Error, *KairosDB::NET_HTTP_EXCEPTIONS => e
43
+ retry_count += 1
44
+ if (config.retry == -1 || retry_count <= config.retry) && !stopped?
45
+ log :error, "Failed to contact host #{host}: #{e.inspect} - retrying in #{delay}s."
46
+ sleep delay
47
+ delay = [config.max_delay, delay * 2].min
48
+ retry
49
+ else
50
+ raise KairosDB::ConnectionError, "Tried #{retry_count - 1} times to reconnect but failed."
51
+ end
52
+ ensure
53
+ http.finish if http.started?
54
+ end
55
+ end
56
+
57
+ def do_request(http, req, data = nil)
58
+ req.body = data if data
59
+ http.request(req)
60
+ end
61
+
62
+ def errors_from_response(parsed_response)
63
+ parsed_response.is_a?(Hash) && parsed_response.fetch('errors', nil)
64
+ end
65
+
66
+ def handle_successful_response(response)
67
+ parsed_response(response)
68
+ end
69
+
70
+ def parsed_response(response)
71
+ response.body ? JSON.parse(response.body) : response
72
+ end
73
+
74
+ def resolve_error(response)
75
+ parsed_response = parsed_response(response)
76
+ errors = errors_from_response(parsed_response) if parsed_response
77
+ raise KairosDB::QueryError, errors if errors
78
+ raise KairosDB::Error, response
79
+ end
80
+
81
+ def config
82
+ raise NotImplementedError, "This #{self.class} cannot respond to ##{__method__}"
83
+ end
84
+
85
+ def log(level, message)
86
+ raise NotImplementedError, "This #{self.class} cannot respond to ##{__method__}"
87
+ end
88
+
89
+ def stopped?
90
+ raise NotImplementedError, "This #{self.class} cannot respond to ##{__method__}"
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,24 @@
1
+ module KairosDB
2
+ class Config
3
+ attr_accessor :host, :port, :initial_delay, :max_delay, :open_timeout,
4
+ :read_timeout, :retry
5
+
6
+ # @param [Hash]
7
+ # @option opts [String] :host the host url of KairosDB.
8
+ # @option opts [Integer] :port the port KairosDB is accessible on.
9
+ # @option opts [Float] :initial_delay minimum delay before retrying an HTTP connection.
10
+ # @option opts [Float] :max_delay maximum delay before retrying an HTTP connection.
11
+ # @option opts [Float] :write_timeout
12
+ # @option opts [Float] :read_timeout
13
+ # @option opts [Boolean] :retry number of times to retry an HTTP connection, setting this to -1 will cause infinite retries
14
+ def initialize(opts = {})
15
+ @host = opts.fetch(:host, 'localhost')
16
+ @port = opts.fetch(:port, 8080)
17
+ @initial_delay = opts.fetch(:initial_delay, 0.01)
18
+ @max_delay = opts.fetch(:max_delay, 30)
19
+ @open_timeout = opts.fetch(:write_timeout, 5)
20
+ @read_timeout = opts.fetch(:read_timeout, 300)
21
+ @retry = opts.fetch(:retry, 3)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ require "net/http"
2
+
3
+ module KairosDB
4
+ class Error < StandardError ; end
5
+ class ConnectionError < Error ; end
6
+ class QueryError < Error ; end
7
+
8
+ # Adopted from: https://github.com/lostisland/faraday/blob/master/lib/faraday/adapter/net_http.rb
9
+ NET_HTTP_EXCEPTIONS = [
10
+ EOFError,
11
+ Errno::ECONNABORTED,
12
+ Errno::ECONNREFUSED,
13
+ Errno::ECONNRESET,
14
+ Errno::EHOSTUNREACH,
15
+ Errno::EINVAL,
16
+ Errno::ENETUNREACH,
17
+ Net::HTTPBadResponse,
18
+ Net::HTTPHeaderSyntaxError,
19
+ Net::ProtocolError,
20
+ SocketError
21
+ ]
22
+ end
@@ -0,0 +1,20 @@
1
+ require 'logger'
2
+
3
+ module KairosDB
4
+ module Logging
5
+ PREFIX = "[KairosDB] "
6
+
7
+ class << self
8
+ attr_writer :logger
9
+ end
10
+
11
+ def self.logger
12
+ return false if @logger == false
13
+ @logger ||= ::Logger.new(STDERR).tap { |logger| logger.level = Logger::INFO }
14
+ end
15
+
16
+ def log(level, message)
17
+ KairosDB::Logging.logger.send(level.to_sym, PREFIX + message) if KairosDB::Logging.logger
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,59 @@
1
+ require 'json'
2
+
3
+ module KairosDB
4
+ module Query
5
+ module Core
6
+ KAIROSDB_DELETE_PATH = '/api/v1/metric'
7
+ KAIROSDB_QUERY_PATH = '/api/v1/datapoints/query'
8
+ KAIROSDB_WRITE_PATH = '/api/v1/datapoints'
9
+
10
+ # @param [Hash] opts
11
+ def query(opts = {})
12
+ url = full_path(KAIROSDB_QUERY_PATH)
13
+ metrics = fetch_metrics(post(url, opts))
14
+
15
+ if block_given?
16
+ metrics.each do |metric|
17
+ yield metric['name'], metric['tags'], metric['values']
18
+ end
19
+ else
20
+ metrics
21
+ end
22
+ end
23
+
24
+ # @param [Hash] opts
25
+ # @option opts [String] :name name of the measurement being recorded
26
+ # @option opts [Array<Integer, Numeric>] :datapoints one or many points to be recorded
27
+ # @option opts [Hash] :tags a hash of tags for the data points
28
+ def write_data(data)
29
+ writer.write(data)
30
+ end
31
+
32
+ protected
33
+
34
+ def write(data)
35
+ url = full_path(KAIROSDB_WRITE_PATH)
36
+ post(url, data)
37
+ end
38
+
39
+ private
40
+
41
+ def fetch_metrics(response)
42
+ response.fetch('queries', [])
43
+ end
44
+
45
+ def full_path(path, params = {})
46
+ query = params.map { |k, v| [CGI.escape(k.to_s), "=", CGI.escape(v.to_s)].join }.join("&")
47
+ URI::Generic.build(path: path, query: query).to_s
48
+ end
49
+
50
+ def config
51
+ raise NotImplementedError, "This #{self.class} cannot respond to ##{__method__}"
52
+ end
53
+
54
+ def writer
55
+ raise NotImplementedError, "This #{self.class} cannot respond to ##{__method__}"
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,10 @@
1
+ module KairosDB
2
+ module Query
3
+ module Metrics
4
+ def delete_metric(name)
5
+ url = KairosDB::Query::Core::KAIROSDB_DELETE_PATH + '/' + name
6
+ delete(url)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module KairosDB
2
+ VERSION = "0.2.0"
3
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kairosdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Andre LeBlanc
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.24.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.24.2
69
+ description: This is an unofficial Ruby library for KairosDB.
70
+ email:
71
+ - andre.leblanc88@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - kairosdb-ruby.gemspec
85
+ - lib/kairosdb.rb
86
+ - lib/kairosdb/client.rb
87
+ - lib/kairosdb/client/http.rb
88
+ - lib/kairosdb/config.rb
89
+ - lib/kairosdb/errors.rb
90
+ - lib/kairosdb/logging.rb
91
+ - lib/kairosdb/query/core.rb
92
+ - lib/kairosdb/query/metrics.rb
93
+ - lib/kairosdb/version.rb
94
+ homepage: https://github.com/andremleblanc/kairosdb-ruby
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ allowed_push_host: https://rubygems.org
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.4.5.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: An unofficial Ruby library for KairosDB.
119
+ test_files: []
120
+ has_rdoc: