metriks-librato_metrics 1.0.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.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2014 Eric Lindvall
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.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # Metriks reporter for Librato Metrics
2
+
3
+ This is the [metriks](https://github.com/eric/metriks) reporter for Librato Metrics.
4
+
5
+ ## How to use it
6
+
7
+ Sends metrics to Librato Metrics every 60 seconds.
8
+
9
+ ``` ruby
10
+ reporter = Metriks::LibratoMetricsReporter.new('email', 'token')
11
+ reporter.start
12
+ ```
13
+
14
+ # License
15
+
16
+ Copyright (c) 2012 Eric Lindvall
17
+
18
+ Published under the MIT License, see LICENSE
data/Rakefile ADDED
@@ -0,0 +1,150 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/*_test.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ desc "Generate RCov test coverage and open in your browser"
56
+ task :coverage do
57
+ require 'rcov'
58
+ sh "rm -fr coverage"
59
+ sh "rcov test/*_test.rb"
60
+ sh "open coverage/index.html"
61
+ end
62
+
63
+ require 'rdoc/task'
64
+ Rake::RDocTask.new do |rdoc|
65
+ rdoc.rdoc_dir = 'rdoc'
66
+ rdoc.title = "#{name} #{version}"
67
+ rdoc.rdoc_files.include('README*')
68
+ rdoc.rdoc_files.include('lib/**/*.rb')
69
+ end
70
+
71
+ desc "Open an irb session preloaded with this library"
72
+ task :console do
73
+ sh "irb -rubygems -r ./lib/#{name}.rb"
74
+ end
75
+
76
+ #############################################################################
77
+ #
78
+ # Custom tasks (add your own tasks here)
79
+ #
80
+ #############################################################################
81
+
82
+
83
+
84
+ #############################################################################
85
+ #
86
+ # Packaging tasks
87
+ #
88
+ #############################################################################
89
+
90
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
91
+ task :release => :build do
92
+ unless `git branch` =~ /^\* master$/
93
+ puts "You must be on the master branch to release!"
94
+ exit!
95
+ end
96
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
97
+ sh "git tag v#{version}"
98
+ sh "git push origin master"
99
+ sh "git push origin v#{version}"
100
+ sh "gem push pkg/#{name}-#{version}.gem"
101
+ end
102
+
103
+ desc "Build #{gem_file} into the pkg directory"
104
+ task :build => :gemspec do
105
+ sh "mkdir -p pkg"
106
+ sh "gem build #{gemspec_file}"
107
+ sh "mv #{gem_file} pkg"
108
+ end
109
+
110
+ desc "Generate #{gemspec_file}"
111
+ task :gemspec => :validate do
112
+ # read spec file and split out manifest section
113
+ spec = File.read(gemspec_file)
114
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
115
+
116
+ # replace name version and date
117
+ replace_header(head, :name)
118
+ replace_header(head, :version)
119
+ replace_header(head, :date)
120
+ #comment this out if your rubyforge_project has a different name
121
+ replace_header(head, :rubyforge_project)
122
+
123
+ # determine file list from git ls-files
124
+ files = `git ls-files`.
125
+ split("\n").
126
+ sort.
127
+ reject { |file| file =~ /^\./ }.
128
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
129
+ map { |file| " #{file}" }.
130
+ join("\n")
131
+
132
+ # piece file back together and write
133
+ manifest = " s.files = %w[\n#{files}\n ]\n"
134
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
135
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
136
+ puts "Updated #{gemspec_file}"
137
+ end
138
+
139
+ desc "Validate #{gemspec_file}"
140
+ task :validate do
141
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}", "lib/#{name[/^(.*?)-/, 1]}"]
142
+ unless libfiles.empty?
143
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
144
+ exit!
145
+ end
146
+ unless Dir['VERSION*'].empty?
147
+ puts "A `VERSION` file at root level violates Gem best practices."
148
+ exit!
149
+ end
150
+ end
@@ -0,0 +1,161 @@
1
+ require 'metriks/time_tracker'
2
+ require 'net/https'
3
+
4
+ module Metriks
5
+ class LibratoMetricsReporter
6
+ attr_accessor :prefix, :source, :data
7
+
8
+ def initialize(email, token, options = {})
9
+ @email = email
10
+ @token = token
11
+
12
+ @prefix = options[:prefix]
13
+ @source = options[:source]
14
+
15
+ @registry = options[:registry] || Metriks::Registry.default
16
+ @interval = options[:interval] || 60
17
+ @time_tracker = Metriks::TimeTracker.new(@interval)
18
+ @on_error = options[:on_error] || proc { |ex| }
19
+
20
+ @data = {}
21
+ @sent = {}
22
+
23
+ @last = Hash.new { |h,k| h[k] = 0 }
24
+
25
+ if options[:percentiles]
26
+ @percentiles = options[:percentiles]
27
+ else
28
+ @percentiles = [ 0.95, 0.999 ]
29
+ end
30
+ end
31
+
32
+ def start
33
+ @thread ||= Thread.new do
34
+ while true
35
+ @time_tracker.sleep
36
+
37
+ Thread.new do
38
+ begin
39
+ write
40
+ rescue Exception => ex
41
+ @on_error[ex] rescue nil
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ def stop
49
+ @thread.kill if @thread
50
+ @thread = nil
51
+ end
52
+
53
+ def restart
54
+ stop
55
+ start
56
+ end
57
+
58
+ def submit
59
+ return if @data.empty?
60
+
61
+ url = URI.parse('https://metrics-api.librato.com/v1/metrics')
62
+ req = Net::HTTP::Post.new(url.path)
63
+ req.basic_auth(@email, @token)
64
+ req.set_form_data(@data)
65
+
66
+ http = Net::HTTP.new(url.host, url.port)
67
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
68
+ http.use_ssl = true
69
+ store = OpenSSL::X509::Store.new
70
+ store.set_default_paths
71
+ http.cert_store = store
72
+
73
+ case res = http.start { |http| http.request(req) }
74
+ when Net::HTTPSuccess, Net::HTTPRedirection
75
+ # OK
76
+ else
77
+ res.error!
78
+ end
79
+
80
+ @data.clear
81
+ end
82
+
83
+ def write
84
+ time = @time_tracker.now_floored
85
+
86
+ @registry.each do |name, metric|
87
+ next if name.nil? || name.empty?
88
+ name = name.to_s.gsub(/ +/, '_')
89
+
90
+ if prefix
91
+ name = "#{prefix}.#{name}"
92
+ end
93
+
94
+ case metric
95
+ when Metriks::Meter
96
+ count = metric.count
97
+ datapoint(name, count - @last[name], time, :display_min => 0,
98
+ :summarize_function => 'sum')
99
+ @last[name] = count
100
+ when Metriks::Counter
101
+ datapoint(name, metric.count, time, :summarize_function => 'average')
102
+ when Metriks::Gauge
103
+ datapoint(name, metric.value, time, :summarize_function => 'average')
104
+ when Metriks::Histogram, Metriks::Timer, Metriks::UtilizationTimer
105
+ if Metriks::UtilizationTimer === metric || Metriks::Timer === metric
106
+ count = metric.count
107
+ datapoint(name, count - @last[name], time, :display_min => 0,
108
+ :summarize_function => 'sum')
109
+ @last[name] = count
110
+ end
111
+
112
+ if Metriks::UtilizationTimer === metric
113
+ datapoint("#{name}.one_minute_utilization",
114
+ metric.one_minute_utilization, time,
115
+ :display_min => 0, :summarize_function => 'average')
116
+ end
117
+
118
+ snapshot = metric.snapshot
119
+
120
+ datapoint("#{name}.median", snapshot.median, time, :display_min => 0,
121
+ :summarize_function => 'average')
122
+
123
+ @percentiles.each do |percentile|
124
+ percentile_name = (percentile * 100).to_f.to_s.gsub(/0+$/, '').gsub('.', '')
125
+ datapoint("#{name}.#{percentile_name}th_percentile",
126
+ snapshot.value(percentile), time, :display_min => 0,
127
+ :summarize_function => 'max')
128
+ end
129
+ end
130
+ end
131
+
132
+ if @data.length > 0
133
+ submit
134
+ end
135
+ end
136
+
137
+ def datapoint(name, value, time, attributes = {})
138
+ idx = @data.length
139
+
140
+ if prefix
141
+ name = "#{prefix}.#{name}"
142
+ end
143
+
144
+ @data["gauges[#{idx}][name]"] = name
145
+ @data["gauges[#{idx}][source]"] = @source
146
+ @data["gauges[#{idx}][measure_time]"] = time.to_i
147
+ @data["gauges[#{idx}][value]"] = value
148
+
149
+ unless @sent[name]
150
+ @sent[name] = true
151
+
152
+ @data["gauges[#{idx}][period]"] = @interval
153
+ @data["gauges[#{idx}][attributes][aggregate]"] = true
154
+
155
+ attributes.each do |k, v|
156
+ @data["gauges[#{idx}][attributes][#{k}]"] = v
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,5 @@
1
+ require 'metriks/reporter/librato_metrics'
2
+
3
+ module MetriksLibratoMetrics
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,75 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'metriks-librato_metrics'
16
+ s.version = '1.0.0'
17
+ s.date = '2014-04-24'
18
+
19
+ ## Make sure your summary is short. The description may be as long
20
+ ## as you like.
21
+ s.summary = "Librato Metrics reporter for Metriks."
22
+ s.description = "Librato Metrics reporter for Metriks."
23
+
24
+ ## List the primary authors. If there are a bunch of authors, it's probably
25
+ ## better to set the email to an email list or something. If you don't have
26
+ ## a custom homepage, consider using your GitHub URL or the like.
27
+ s.authors = ["Eric Lindvall"]
28
+ s.email = 'eric@papertrailapp.com'
29
+ s.homepage = 'https://github.com/eric/metriks-librato_metrics'
30
+
31
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
32
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
33
+ s.require_paths = %w[lib]
34
+
35
+ ## This sections is only necessary if you have C extensions.
36
+ # s.require_paths << 'ext'
37
+ # s.extensions = %w[ext/extconf.rb]
38
+
39
+ ## If your gem includes any executables, list them here.
40
+ # s.executables = ["name"]
41
+
42
+ ## Specify any RDoc options here. You'll want to add your README and
43
+ ## LICENSE files to the extra_rdoc_files list.
44
+ s.rdoc_options = ["--charset=UTF-8"]
45
+ s.extra_rdoc_files = %w[README.md LICENSE]
46
+
47
+ ## List your runtime dependencies here. Runtime dependencies are those
48
+ ## that are needed for an end user to actually USE your code.
49
+ s.add_dependency('metriks', '>= 0.9.9.6')
50
+
51
+ ## List your development dependencies here. Development dependencies are
52
+ ## those that are only needed during development
53
+ s.add_development_dependency('mocha', ['~> 0.10'])
54
+
55
+ ## Leave this section as-is. It will be automatically generated from the
56
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
57
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
58
+ # = MANIFEST =
59
+ s.files = %w[
60
+ Gemfile
61
+ LICENSE
62
+ README.md
63
+ Rakefile
64
+ lib/metriks-librato_metrics.rb
65
+ lib/metriks/librato_metrics_reporter.rb
66
+ metriks-librato_metrics.gemspec
67
+ test/librato_metrics_reporter_test.rb
68
+ test/test_helper.rb
69
+ ]
70
+ # = MANIFEST =
71
+
72
+ ## Test files will be grabbed from the file list. Make sure the path glob
73
+ ## matches what you actually use.
74
+ s.test_files = s.files.select { |path| path =~ /^test\/.*_test\.rb/ }
75
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ require 'metriks/librato_metrics_reporter'
4
+
5
+ class LibratoMetricsReporterTest < Test::Unit::TestCase
6
+ def build_reporter(options={})
7
+ Metriks::LibratoMetricsReporter.new('user', 'password', { :registry => @registry }.merge(options))
8
+ end
9
+
10
+ def setup
11
+ @registry = Metriks::Registry.new
12
+ @reporter = build_reporter
13
+ end
14
+
15
+ def teardown
16
+ @reporter.stop
17
+ @registry.stop
18
+ end
19
+
20
+ def test_write
21
+ @registry.meter('meter.testing').mark
22
+ @registry.counter('counter.testing').increment
23
+ @registry.timer('timer.testing').update(1.5)
24
+ @registry.histogram('histogram.testing').update(1.5)
25
+ @registry.utilization_timer('utilization_timer.testing').update(1.5)
26
+ @registry.gauge('gauge.testing') { 123 }
27
+
28
+ @reporter.expects(:submit)
29
+
30
+ @reporter.write
31
+
32
+ @reporter.data.detect { |(k,v)| k =~ /gauges\[\d+\]\[name\]/ && v == 'gauge.testing' } &&
33
+ @reporter.data.detect { |(k,v)| k =~ /gauges\[\d+\]\[value\]/ && v.to_s == '123' }
34
+ end
35
+
36
+ def test_empty_write
37
+ @reporter.expects(:submit).never
38
+ @reporter.write
39
+ end
40
+ end
@@ -0,0 +1,33 @@
1
+ require 'test/unit'
2
+ require 'pp'
3
+
4
+ require 'mocha/setup'
5
+
6
+ require 'metriks'
7
+
8
+ Thread.abort_on_exception = true
9
+
10
+ module ThreadHelper
11
+ require 'thread'
12
+
13
+ # Run the given block on n threads in parallel. Returns an array of the
14
+ # return values of each thread's last invocation of block. Options:
15
+
16
+ # :n: call block n times per thread. Default 1.
17
+ def thread(threads = 2, opts = {})
18
+ n = opts[:n] || 1
19
+ results = []
20
+
21
+ threads.times.map do |i|
22
+ Thread.new do
23
+ n.times do
24
+ results[i] = yield i
25
+ end
26
+ end
27
+ end.each do |thread|
28
+ thread.join
29
+ end
30
+
31
+ results
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metriks-librato_metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eric Lindvall
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-04-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: metriks
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.9.6
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.9.6
30
+ - !ruby/object:Gem::Dependency
31
+ name: mocha
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.10'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.10'
46
+ description: Librato Metrics reporter for Metriks.
47
+ email: eric@papertrailapp.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files:
51
+ - README.md
52
+ - LICENSE
53
+ files:
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/metriks-librato_metrics.rb
59
+ - lib/metriks/librato_metrics_reporter.rb
60
+ - metriks-librato_metrics.gemspec
61
+ - test/librato_metrics_reporter_test.rb
62
+ - test/test_helper.rb
63
+ homepage: https://github.com/eric/metriks-librato_metrics
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --charset=UTF-8
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.23.2
85
+ signing_key:
86
+ specification_version: 2
87
+ summary: Librato Metrics reporter for Metriks.
88
+ test_files:
89
+ - test/librato_metrics_reporter_test.rb