metrics_machine 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4a0172ebed9fc844a07a32b5da53ffada18d5b0a
4
+ data.tar.gz: 91e401e181020b207c78ef5aade81eb597d87db2
5
+ SHA512:
6
+ metadata.gz: 39100605467a6ba214b733235371cdc75ec86d6a2bda6710d549fd93098d012d575fd0bdb81229ad8ef4ce5845d8ca3e63ac30cfbed91188156d14aa523a9f6a
7
+ data.tar.gz: 9e90d083a6716a04c7d00108e58ffe83190ff112ececb7a6ea1bc69e7e18ce92af92e9c38cf073a7200a8f3d56ec247a0b7fbadb9cd3b351791dc60076f47167
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dlss-metrics.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ #############################################################################################################
2
+ # Copyright (c) 2012 by The Board of Trustees of the Leland Stanford Junior University. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chris Beer
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,29 @@
1
+ # MetricsMachine
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'metrics_machine'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install metrics_machine
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,51 @@
1
+ require 'active_support/inflector/methods'
2
+ require 'active_support/inflector/transliterate'
3
+
4
+ module MetricsMachine
5
+ class Monitor
6
+
7
+ attr_reader :prefix, :monitors, :reporter
8
+
9
+ def initialize reporter, options = {}, &block
10
+ @reporter = reporter
11
+ @monitors = {}
12
+ @prefix = options.fetch(:prefix, "metrics_machine")
13
+ instance_eval &block
14
+ run!
15
+ end
16
+
17
+ def run!
18
+ monitors.each do |name,monitor|
19
+ EM.add_periodic_timer(monitor.interval) do
20
+ report name, monitor
21
+ end
22
+ end
23
+ end
24
+
25
+ def report name, monitor
26
+ monitor.statistics.each do |k,v|
27
+ reporter.gauge "#{prefix}.#{name}.#{ActiveSupport::Inflector.underscore(k)}", v
28
+ end
29
+ end
30
+
31
+ def monitor symbol_or_class, *args
32
+ key = nil
33
+ klass = case symbol_or_class
34
+ when Symbol
35
+ MetricsMachine.const_get(symbol_or_class.to_s.capitalize)
36
+ when Class
37
+ symbol_or_class
38
+ when Hash
39
+ key = symbol_or_class.keys.first
40
+ symbol_or_class[key]
41
+ end
42
+
43
+ c = klass.new *args
44
+ key ||= c.key if c.respond_to? :key
45
+ key ||= klass.key if klass.respond_to? :key
46
+ key ||= ActiveSupport::Inflector.underscore(klass.to_s.gsub('MetricsMachine::', ''))
47
+ monitors[key] = c
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,39 @@
1
+ module MetricsMachine
2
+ class Mysql
3
+ INTERVAL = 15
4
+
5
+ attr_reader :connection, :options
6
+
7
+ def initialize connection, *args
8
+ @options = args.extract_options!
9
+ @connection = connection
10
+ end
11
+
12
+ def interval
13
+ self.class.INTERVAL
14
+ end
15
+
16
+ def statistics
17
+ fetch_status.each do |k,v|
18
+ fetch_status[k] = case v
19
+ when "OFF", "NULL", "NONE"
20
+ 0
21
+ when "ON", "TRUE"
22
+ 1
23
+ else
24
+ v.to_i
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def fetch_status
32
+ Hash[*connection.execute("SHOW GLOBAL STATUS").map.to_a.flatten]
33
+ end
34
+
35
+ def fetch_variables
36
+ Hash[*connection.execute("SHOW GLOBAL VARIABLES").map.to_a.flatten]
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,17 @@
1
+ module MetricsMachine
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer "metricsmachine.start_em" do
5
+ unless EM.reactor_running?
6
+ MetricsMachine.start
7
+ end
8
+ end
9
+
10
+ initializer "metricsmachine.configure_rails_initialization" do
11
+ MetricsMachine.configure do
12
+ monitor :mysql, ActiveRecord::Base.connection if ActiveRecord::Base.connection.is_a? ActiveRecord::ConnectionAdapters::Mysql2Adapter
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module MetricsMachine
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,68 @@
1
+ require "metrics_machine/version"
2
+ require "metrics_machine/monitor"
3
+ require "statsd"
4
+ require "eventmachine"
5
+ require "psych"
6
+
7
+ module MetricsMachine
8
+ autoload :Monitor, "metrics_machine/monitor"
9
+ autoload :Railtie, "metrics_machine/railtie"
10
+
11
+ def self.start &block
12
+
13
+ thread = if defined?(PhusionPassenger)
14
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
15
+ if forked && EM.reactor_running?
16
+ EM.stop
17
+ end
18
+ Thread.new { EM.run }
19
+ die_gracefully_on_signal
20
+ end
21
+ else
22
+ # faciliates debugging
23
+ Thread.abort_on_exception = true
24
+ # just spawn a thread and start it up
25
+ Thread.new {
26
+ EM.run
27
+ }
28
+ end
29
+
30
+ Monitor.new reporter, &block if block_given?
31
+ thread
32
+ end
33
+
34
+ def self.configure &block
35
+ Monitor.new reporter, &block if block_given?
36
+ end
37
+
38
+ def self.die_gracefully_on_signal
39
+ Signal.trap("INT") { EM.stop }
40
+ Signal.trap("TERM") { EM.stop }
41
+ end
42
+
43
+ def self.reporter
44
+ @statsd ||= Statsd.new(reporter_config['host'],reporter_config['port'])
45
+ end
46
+
47
+ private
48
+ def self.reporter_config
49
+ @reporter_config ||= begin
50
+ path = if has_rails? and File.exists? "#{Rails.root}/config/statsd.yml"
51
+ "#{Rails.root}/config/statsd.yml"
52
+ else
53
+ File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "statsd.yml"))
54
+ end
55
+ data = Psych.load_file(path)
56
+
57
+ if has_rails?
58
+ data[Rails.env]
59
+ else
60
+ data["default"]
61
+ end
62
+ end
63
+ end
64
+
65
+ def self.has_rails?
66
+ const_defined? "Rails"
67
+ end
68
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'metrics_machine/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "metrics_machine"
8
+ spec.version = MetricsMachine::VERSION
9
+ spec.authors = ["Chris Beer"]
10
+ spec.email = ["cabeer@stanford.edu"]
11
+ spec.description = %q{Utilities for writing application metrics into statsd/graphite}
12
+ spec.summary = %q{Utilities for writing application metrics into statsd/graphite}
13
+ spec.homepage = ""
14
+ spec.license = "Apache License, Version 2.0"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "statsd-ruby"
22
+ spec.add_dependency "eventmachine"
23
+ spec.add_dependency "activesupport"
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe MetricsMachine do
4
+
5
+ DummyMonitor = Class.new do
6
+ def interval
7
+ 1
8
+ end
9
+
10
+ def statistics
11
+ { "some-value" => 50 }
12
+ end
13
+ end
14
+
15
+ it 'should do something' do
16
+
17
+ reporter = double()
18
+
19
+ reporter.should_receive(:gauge).at_least(3).at_most(6).times.with("metrics_machine.dummy_monitor.some_value", 50)
20
+ MetricsMachine.stub(:reporter => reporter)
21
+
22
+
23
+ thread = MetricsMachine.start do
24
+ monitor DummyMonitor
25
+ end
26
+
27
+ sleep 5
28
+
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ require 'metrics_machine'
2
+ require 'rspec'
3
+
4
+ RSpec.configure do |c|
5
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metrics_machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Beer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: statsd-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: eventmachine
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Utilities for writing application metrics into statsd/graphite
98
+ email:
99
+ - cabeer@stanford.edu
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .travis.yml
106
+ - Gemfile
107
+ - LICENSE
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/metrics_machine.rb
112
+ - lib/metrics_machine/monitor.rb
113
+ - lib/metrics_machine/mysql.rb
114
+ - lib/metrics_machine/railtie.rb
115
+ - lib/metrics_machine/version.rb
116
+ - metrics_machine.gemspec
117
+ - spec/lib/metrics_machine/metrics_spec.rb
118
+ - spec/spec_helper.rb
119
+ homepage: ''
120
+ licenses:
121
+ - Apache License, Version 2.0
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.0.3
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Utilities for writing application metrics into statsd/graphite
143
+ test_files:
144
+ - spec/lib/metrics_machine/metrics_spec.rb
145
+ - spec/spec_helper.rb