metriks-instrumental 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +10 -0
- data/lib/metriks-instrumental.rb +3 -0
- data/lib/metriks-instrumental/version.rb +5 -0
- data/lib/metriks/reporter/instrumental.rb +134 -0
- data/metriks-instrumental.gemspec +26 -0
- data/test/instrumental_reporter_test.rb +35 -0
- data/test/test_helper.rb +7 -0
- metadata +128 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Chris Zelenak
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# metriks-instrumental
|
2
|
+
|
3
|
+
A gem to report your [Metriks][metriks] data to [Instrumental][instrumental].
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Send metrics to [Instrumental][instrumental] every 60 seconds.
|
8
|
+
|
9
|
+
``` ruby
|
10
|
+
# Using an API key
|
11
|
+
reporter = Metriks::Reporter::Instrumental.new(:api_key => "Your Instrumental API key")
|
12
|
+
|
13
|
+
# Using an already instantiated agent
|
14
|
+
reporter = Metriks::Reporter::Instrumental.new(:agent => existing_instrumental_agent)
|
15
|
+
|
16
|
+
reporter.start
|
17
|
+
```
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
gem 'metriks-instrumental'
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install metriks-instrumental
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
40
|
+
|
41
|
+
[metriks]: http://github.com/eric/metriks
|
42
|
+
[instrumental]: https://instrumentalapp.com/
|
data/Rakefile
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'metriks/time_tracker'
|
2
|
+
require 'instrumental_agent'
|
3
|
+
|
4
|
+
module Metriks::Reporter
|
5
|
+
|
6
|
+
# Reports metrics to Instrumental (http://instrumentalapp.com/)
|
7
|
+
class Instrumental
|
8
|
+
attr_accessor :prefix, :source, :agent
|
9
|
+
|
10
|
+
# You MUST provide either :api_token or :agent as an argument to this method.
|
11
|
+
#
|
12
|
+
# options:
|
13
|
+
# :api_token:: Your Instrumental API token
|
14
|
+
# :agent:: A specific instance of the Instrumental Agent to use with this reporter
|
15
|
+
# :prefix:: A string prefix to prepend to all your metrics
|
16
|
+
# :registry:: The Metriks registry that will be providing your metrics
|
17
|
+
# :interval:: How often to report metrics to Instrumental (default value is every 60 seconds, cannot be lower)
|
18
|
+
# :on_error:: A callable object to be executed when an error occurs. This WILL be called
|
19
|
+
# from a separate thread, you must ensure that your provided code will be
|
20
|
+
# thread safe.
|
21
|
+
|
22
|
+
def initialize(options = {})
|
23
|
+
raise "You must provide either :agent or :api_token as an option" unless options[:agent] || options[:api_token]
|
24
|
+
@agent = options[:agent] || ::Instrumental::Agent.new(options[:api_token])
|
25
|
+
@prefix = options[:prefix]
|
26
|
+
@registry = options[:registry] || Metriks::Registry.default
|
27
|
+
interval = options[:interval] || 60
|
28
|
+
interval = [interval, 60].max
|
29
|
+
@time_tracker = Metriks::TimeTracker.new(interval)
|
30
|
+
@on_error = options[:on_error] || proc { |ex| }
|
31
|
+
end
|
32
|
+
|
33
|
+
def start
|
34
|
+
@thread ||= Thread.new do
|
35
|
+
loop do
|
36
|
+
@time_tracker.sleep
|
37
|
+
|
38
|
+
Thread.new do
|
39
|
+
begin
|
40
|
+
write
|
41
|
+
rescue Exception => ex
|
42
|
+
@on_error[ex] rescue nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def stop
|
50
|
+
@thread.kill if @thread
|
51
|
+
@thread = nil
|
52
|
+
end
|
53
|
+
|
54
|
+
def restart
|
55
|
+
stop
|
56
|
+
start
|
57
|
+
end
|
58
|
+
|
59
|
+
def write
|
60
|
+
@registry.each do |name, metric|
|
61
|
+
case metric
|
62
|
+
when Metriks::Meter
|
63
|
+
send_metric name, metric, [
|
64
|
+
:count, :one_minute_rate, :five_minute_rate,
|
65
|
+
:fifteen_minute_rate, :mean_rate
|
66
|
+
]
|
67
|
+
when Metriks::Counter
|
68
|
+
send_metric name, metric, [
|
69
|
+
:count
|
70
|
+
]
|
71
|
+
when Metriks::UtilizationTimer
|
72
|
+
send_metric name, metric, [
|
73
|
+
:count, :one_minute_rate, :five_minute_rate,
|
74
|
+
:fifteen_minute_rate, :mean_rate,
|
75
|
+
:min, :max, :mean, :stddev,
|
76
|
+
:one_minute_utilization, :five_minute_utilization,
|
77
|
+
:fifteen_minute_utilization, :mean_utilization,
|
78
|
+
], [
|
79
|
+
:median, :get_95th_percentile
|
80
|
+
]
|
81
|
+
when Metriks::Timer
|
82
|
+
send_metric name, metric, [
|
83
|
+
:count, :one_minute_rate, :five_minute_rate,
|
84
|
+
:fifteen_minute_rate, :mean_rate,
|
85
|
+
:min, :max, :mean, :stddev
|
86
|
+
], [
|
87
|
+
:median, :get_95th_percentile
|
88
|
+
]
|
89
|
+
when Metriks::Histogram
|
90
|
+
send_metric name, metric, [
|
91
|
+
:count, :min, :max, :mean, :stddev
|
92
|
+
], [
|
93
|
+
:median, :get_95th_percentile
|
94
|
+
]
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def send_metric(base_name, metric, keys, snapshot_keys = [])
|
100
|
+
time = @time_tracker.now_floored
|
101
|
+
|
102
|
+
base_name = base_name.to_s.gsub(/ +/, '_')
|
103
|
+
if @prefix
|
104
|
+
base_name = "#{@prefix}.#{base_name}"
|
105
|
+
end
|
106
|
+
|
107
|
+
keys.flatten.each do |key|
|
108
|
+
name = key.to_s.gsub(/^get_/, '').to_s
|
109
|
+
full_name = "#{base_name}.#{name}"
|
110
|
+
value = metric.send(key)
|
111
|
+
if name == "count"
|
112
|
+
@agent.increment(full_name, value, time)
|
113
|
+
else
|
114
|
+
@agent.gauge(full_name, value, time)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
unless snapshot_keys.empty?
|
119
|
+
snapshot = metric.snapshot
|
120
|
+
snapshot_keys.flatten.each do |key|
|
121
|
+
name = key.to_s.gsub(/^get_/, '').to_s
|
122
|
+
full_name = "#{base_name}.#{name}"
|
123
|
+
value = snapshot.send(key)
|
124
|
+
if name == "count"
|
125
|
+
@agent.increment(full_name, value, time)
|
126
|
+
else
|
127
|
+
@agent.gauge(full_name, value, time)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'metriks-instrumental/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "metriks-instrumental"
|
8
|
+
gem.version = Metriks::Instrumental::VERSION
|
9
|
+
gem.authors = ["Chris Zelenak"]
|
10
|
+
gem.email = ["chris@fastestforward.com"]
|
11
|
+
gem.description = %q{A Metriks reporter that submits to Instrumental}
|
12
|
+
gem.summary = %q{A Metriks reporter that submits to Instrumental}
|
13
|
+
gem.homepage = "http://github.com/fastestforward/metriks-instrumental"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency("metriks", ["~> 0.9.9"])
|
21
|
+
gem.add_dependency("instrumental_agent", ["~> 0.12"])
|
22
|
+
gem.add_development_dependency("rake", ["~> 10"])
|
23
|
+
gem.add_development_dependency("mocha", ["~> 0.10"])
|
24
|
+
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'metriks/reporter/instrumental'
|
3
|
+
|
4
|
+
class InstrumentalReporterTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@registry = Metriks::Registry.new
|
8
|
+
@agent = Instrumental::Agent.new('SOME_TOKEN', :enabled => false)
|
9
|
+
@reporter = Metriks::Reporter::Instrumental.new(:registry => @registry, :agent => @agent)
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
@reporter.stop
|
14
|
+
@registry.stop
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_write
|
18
|
+
@registry.meter('meter.testing').mark
|
19
|
+
@registry.counter('counter.testing').increment
|
20
|
+
@registry.timer('timer.testing').update(1.5)
|
21
|
+
@registry.histogram('histogram.testing').update(1.5)
|
22
|
+
@registry.utilization_timer('utilization_timer.testing').update(1.5)
|
23
|
+
|
24
|
+
@agent.expects(:gauge).at_least(4)
|
25
|
+
@agent.expects(:increment).at_least(1)
|
26
|
+
@reporter.write
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_invalid_arguments()
|
30
|
+
assert_raise(::RuntimeError) { Metriks::Reporter::Instrumental.new }
|
31
|
+
assert_nothing_thrown { Metriks::Reporter::Instrumental.new(:api_token => "TEST") }
|
32
|
+
assert_nothing_thrown { Metriks::Reporter::Instrumental.new(:agent => Instrumental::Agent.new("TEST", :enabled => false)) }
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: metriks-instrumental
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Zelenak
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-10 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
|
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
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: instrumental_agent
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0.12'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0.12'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: mocha
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.10'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0.10'
|
78
|
+
description: A Metriks reporter that submits to Instrumental
|
79
|
+
email:
|
80
|
+
- chris@fastestforward.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/metriks-instrumental.rb
|
91
|
+
- lib/metriks-instrumental/version.rb
|
92
|
+
- lib/metriks/reporter/instrumental.rb
|
93
|
+
- metriks-instrumental.gemspec
|
94
|
+
- test/instrumental_reporter_test.rb
|
95
|
+
- test/test_helper.rb
|
96
|
+
homepage: http://github.com/fastestforward/metriks-instrumental
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
hash: -651605823
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
segments:
|
118
|
+
- 0
|
119
|
+
hash: -651605823
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.8.24
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: A Metriks reporter that submits to Instrumental
|
126
|
+
test_files:
|
127
|
+
- test/instrumental_reporter_test.rb
|
128
|
+
- test/test_helper.rb
|