metrics_machine 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 959afc0e7a0386b750f3abbb92cd4e289ddd0d1a
4
- data.tar.gz: 27dc436065b5622347edb567da5e52856ba67023
3
+ metadata.gz: d63e2c33f29fd0071d4475d746503353ad13bcb3
4
+ data.tar.gz: c9dee614e7817bef5e0b91a2bc620983b54134eb
5
5
  SHA512:
6
- metadata.gz: 0943e9aafb8ffac93ec84444a9e9eec05754d72a95210767a016b1133bcdc5121a9402bb6eeb72674e48ce0fc633b2c2bf618d472637fb1717c4b94218fea9f3
7
- data.tar.gz: 3006307a9676afad7c3b4eed8e7296737a6fe16b2cd31b01afab4250238bf9e778f90f52c20307e6a8ea34fca229801eb4b2e119d0c2f3384e8b116a050d7c17
6
+ metadata.gz: a69a717a85bff5f6965e787a83236e36f3285bc0f747949916743b5cae7bdbcb298374edebec0d78f63da59e3c3639bf9d12e2eb90840393568f8c08ec277282
7
+ data.tar.gz: 681f7fdacd6e60f680559054408dab40eae050646fb368987511c8ad3f12d06ae9c5b24053d4f1f168f9460f2bc1e0370b9e23c58d8b241a4923bb215305409b
data/config/statsd.yml ADDED
@@ -0,0 +1,7 @@
1
+ default: &DEFAULT
2
+ host: <%= ENV['STATSD_URL'] || 'localhost' %>
3
+ port: <%= ENV['STATSD_PORT'] || 8125 %>
4
+
5
+ development: *DEFAULT
6
+ test: *DEFAULT
7
+ production: *DEFAULT
@@ -2,37 +2,41 @@ require "metrics_machine/version"
2
2
  require "metrics_machine/monitor"
3
3
  require "statsd"
4
4
  require "eventmachine"
5
+ require "erb"
5
6
  require "psych"
6
7
 
7
8
  module MetricsMachine
8
9
  autoload :Monitor, "metrics_machine/monitor"
9
- autoload :Railtie, "metrics_machine/railtie"
10
+ require "metrics_machine/railtie" if defined? Rails
11
+ autoload :Mysql, "metrics_machine/mysql"
10
12
 
11
13
  def self.start options = {}, &block
14
+ monitor.configure &block if block_given?
12
15
 
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
16
+ p = lambda { EM.run { monitor.run! } }
17
+ if defined?(PhusionPassenger)
18
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
19
+ if forked && EM.reactor_running?
20
+ EM.stop
21
+ end
22
+
23
+ Thread.new &p
24
+ die_gracefully_on_signal
25
+ end
26
+ else
27
+ # faciliates debugging
28
+ Thread.abort_on_exception = true
29
+ # just spawn a thread and start it up
30
+ Thread.new &p
31
+ end
32
+ end
29
33
 
30
- Monitor.new reporter, options, &block if block_given?
31
- thread
34
+ def self.monitor
35
+ @monitor ||= Monitor.new reporter
32
36
  end
33
37
 
34
- def self.configure options = {}, &block
35
- Monitor.new reporter, options, &block if block_given?
38
+ def self.configure &block
39
+ monitor.configure &block
36
40
  end
37
41
 
38
42
  def self.die_gracefully_on_signal
@@ -47,14 +51,14 @@ module MetricsMachine
47
51
  private
48
52
  def self.reporter_config
49
53
  @reporter_config ||= begin
50
- path = if has_rails? and File.exists? "#{Rails.root}/config/statsd.yml"
54
+ path = if defined? Rails and File.exists? "#{Rails.root}/config/statsd.yml"
51
55
  "#{Rails.root}/config/statsd.yml"
52
56
  else
53
- File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "statsd.yml"))
57
+ File.expand_path(File.join(File.dirname(__FILE__), "..", "config", "statsd.yml"))
54
58
  end
55
- data = Psych.load_file(path)
59
+ data = Psych.load(ERB.new(IO.read(path)).result(binding))
56
60
 
57
- if has_rails?
61
+ if defined? Rails
58
62
  data[Rails.env]
59
63
  else
60
64
  data["default"]
@@ -62,7 +66,4 @@ module MetricsMachine
62
66
  end
63
67
  end
64
68
 
65
- def self.has_rails?
66
- const_defined? "Rails"
67
- end
68
69
  end
@@ -4,14 +4,25 @@ require 'active_support/inflector/transliterate'
4
4
  module MetricsMachine
5
5
  class Monitor
6
6
 
7
- attr_reader :prefix, :monitors, :reporter
7
+ attr_reader :monitors, :reporter
8
8
 
9
9
  def initialize reporter, options = {}, &block
10
10
  @reporter = reporter
11
11
  @monitors = {}
12
12
  @prefix = options.fetch(:prefix, self.class.default_prefix)
13
+ configure &block if block_given?
14
+ end
15
+
16
+ def prefix *args
17
+ if args.empty?
18
+ @prefix
19
+ else
20
+ @prefix = args.first
21
+ end
22
+ end
23
+
24
+ def configure &block
13
25
  instance_eval &block
14
- run!
15
26
  end
16
27
 
17
28
  def run!
@@ -1,21 +1,22 @@
1
1
  module MetricsMachine
2
2
  class Mysql
3
- INTERVAL = 15
4
3
 
5
- attr_reader :connection, :options
4
+ attr_reader :base, :options
6
5
 
7
- def initialize connection, *args
6
+ def initialize base, *args
8
7
  @options = args.extract_options!
9
- @connection = connection
8
+ @base = base
10
9
  end
11
10
 
12
11
  def interval
13
- self.class.INTERVAL
12
+ 15
14
13
  end
15
14
 
16
15
  def statistics
17
- fetch_status.each do |k,v|
18
- fetch_status[k] = case v
16
+ status = fetch_status
17
+
18
+ status.each do |k,v|
19
+ status[k] = case v
19
20
  when "OFF", "NULL", "NONE"
20
21
  0
21
22
  when "ON", "TRUE"
@@ -29,11 +30,11 @@ module MetricsMachine
29
30
  private
30
31
 
31
32
  def fetch_status
32
- Hash[*connection.execute("SHOW GLOBAL STATUS").map.to_a.flatten]
33
+ Hash[*base.connection.execute("SHOW GLOBAL STATUS").map.to_a.flatten]
33
34
  end
34
35
 
35
36
  def fetch_variables
36
- Hash[*connection.execute("SHOW GLOBAL VARIABLES").map.to_a.flatten]
37
+ Hash[*base.connection.execute("SHOW GLOBAL VARIABLES").map.to_a.flatten]
37
38
  end
38
39
  end
39
40
  end
@@ -1,16 +1,10 @@
1
1
  module MetricsMachine
2
2
  class Railtie < Rails::Railtie
3
3
 
4
- if Rails.env.production?
5
- initializer "metricsmachine.start_em" do
6
- unless EM.reactor_running?
7
- MetricsMachine.start
8
- end
9
- end
10
-
11
- initializer "metricsmachine.configure_rails_initialization" do
12
- MetricsMachine.configure do
13
- monitor :mysql, ActiveRecord::Base.connection if ActiveRecord::Base.connection.is_a? ActiveRecord::ConnectionAdapters::Mysql2Adapter
4
+ if Rails.env.production? || ENV['METRICS'] == "true"
5
+ initializer "metricsmachine.initialization" do
6
+ MetricsMachine.start do
7
+ monitor :mysql, ActiveRecord::Base if ActiveRecord::Base.connection.is_a? ActiveRecord::ConnectionAdapters::Mysql2Adapter
14
8
  end
15
9
  end
16
10
  end
@@ -1,3 +1,3 @@
1
1
  module MetricsMachine
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metrics_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Beer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-13 00:00:00.000000000 Z
11
+ date: 2013-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: statsd-ruby
@@ -108,6 +108,7 @@ files:
108
108
  - LICENSE.txt
109
109
  - README.md
110
110
  - Rakefile
111
+ - config/statsd.yml
111
112
  - lib/metrics_machine.rb
112
113
  - lib/metrics_machine/monitor.rb
113
114
  - lib/metrics_machine/mysql.rb