metrix 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,20 +6,26 @@ TODO: Write a gem description
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'metrix'
9
+ gem install 'metrix'
10
10
 
11
11
  ## Configuration
12
12
 
13
- #/etc/metrix.tml
13
+ # in /etc/metrix.tml
14
+
15
+ # local metrics
16
+ load: true
17
+ system: true
18
+ processes: true
19
+
20
+ # http metrics
14
21
  elasticsearch: http://127.0.0.1:9200/_status
15
22
  mongodb: http://127.0.0.1:28017/serverStatus
16
23
  fpm: http://127.0.0.1:9001/fpm-status
17
24
  nginx: http://127.0.0.1:8000/
25
+
26
+ # reporters
18
27
  opentsdb: tcp://127.0.0.1:4242/
19
28
  graphite: tcp://127.0.0.1:2003/
20
- load: true
21
- system: true
22
- processes: true
23
29
 
24
30
  ## Start
25
31
 
@@ -13,5 +13,18 @@ module Metrix
13
13
  def hostname
14
14
  @hostname ||= `hostname`.strip
15
15
  end
16
+
17
+ def known_metrics
18
+ Dir.glob(File.expand_path("../metrix/*.rb", __FILE__)).each do |path|
19
+ require path
20
+ end
21
+ Base.subclasses.map do |clazz|
22
+ raise "known_metrics not set for #{clazz}" if clazz.known_metrics.nil?
23
+ raise "prefix not set for #{clazz}" if clazz.prefix.nil?
24
+ clazz.known_metrics.map do |m|
25
+ "#{clazz.prefix}.#{m}"
26
+ end
27
+ end.flatten.compact.sort
28
+ end
16
29
  end
17
30
  end
@@ -11,9 +11,33 @@ module Metrix
11
11
  @ignore = [metrics].flatten
12
12
  end
13
13
 
14
+ def inherited(clazz)
15
+ subclasses << clazz
16
+ end
17
+
18
+ def set_prefix(prefix)
19
+ @prefix = prefix
20
+ end
21
+
22
+ def prefix
23
+ @prefix
24
+ end
25
+
26
+ def subclasses
27
+ @subclasses ||= []
28
+ end
29
+
14
30
  def ignore
15
31
  @ignore ||= []
16
32
  end
33
+
34
+ def set_known_metrics(*metrics)
35
+ @known_metrics = metrics.flatten
36
+ end
37
+
38
+ def known_metrics
39
+ @known_metrics
40
+ end
17
41
  end
18
42
 
19
43
  def initialize(raw, time = Time.now)
@@ -24,7 +48,15 @@ module Metrix
24
48
  def metrics
25
49
  unfiltered_metrics.reject { |k, v| ignore_metric?(k) }.map do |k, v|
26
50
  Metric.new("#{prefix}.#{k}", v, @time, tags)
27
- end
51
+ end + tagged_metrics
52
+ end
53
+
54
+ def tagged_metrics
55
+ []
56
+ end
57
+
58
+ def prefix
59
+ self.class.prefix
28
60
  end
29
61
 
30
62
  def tags
@@ -5,7 +5,7 @@ require "metrix/nginx"
5
5
  require "metrix/system"
6
6
  require "metrix/load"
7
7
  require "metrix/fpm"
8
- require "metrix/process"
8
+ require "metrix/process_metric"
9
9
  require "metrix/load"
10
10
  require "logger"
11
11
  require "fileutils"
@@ -32,9 +32,9 @@ module Metrix
32
32
 
33
33
  def run
34
34
  parse!
35
- load_configs_from_file!
36
35
  case @action
37
36
  when "start"
37
+ load_configs_from_file!
38
38
  if running?
39
39
  logger.warn "refuse to run. seems that #{pid_path} exists!"
40
40
  abort "not allowed to run" if running?
@@ -65,7 +65,10 @@ module Metrix
65
65
  system "kill #{pid}"
66
66
  puts "killed #{pid}"
67
67
  when "configtest"
68
+ load_configs_from_file!
68
69
  puts "running configtest #{attributes.inspect}"
70
+ when "list_metrics"
71
+ puts Metrix.known_metrics.join("\n")
69
72
  else
70
73
  logger.warn "action #{action} unknown!"
71
74
  abort "action #{action} unknown!"
@@ -1,11 +1,28 @@
1
- require "metrix/json"
1
+ require "metrix/json_metric"
2
2
 
3
3
  module Metrix
4
- class ElasticSearch < Json
5
- ignore_metrics []
4
+ class ElasticSearch < Base
5
+ include JsonMetric
6
+ set_prefix "elasticsearch"
7
+ set_known_metrics %w(
8
+ _shards.total _shards.successful _shards.failed index.primary_size_in_bytes
9
+ index.size_in_bytes translog.operations docs.num_docs docs.max_doc
10
+ docs.deleted_docs merges.current merges.current_docs merges.current_size_in_bytes
11
+ merges.total merges.total_time_in_millis merges.total_docs
12
+ merges.total_size_in_bytes refresh.total refresh.total_time_in_millis flush.total
13
+ flush.total_time_in_millis
14
+ )
6
15
 
7
- def prefix
8
- "elasticsearch"
16
+ DATABASE_INDEX = /^indices\./
17
+ def metrics
18
+ unfiltered_metrics.map do |k, v|
19
+ if k.match(DATABASE_INDEX)
20
+ _, index_name, key = k.split(".", 3)
21
+ Metric.new("#{prefix}.#{key}", v, time, index: index_name)
22
+ else
23
+ Metric.new("#{prefix}.#{k}", v, time)
24
+ end
25
+ end.compact
9
26
  end
10
27
  end
11
28
  end
@@ -2,6 +2,13 @@ require "metrix/base"
2
2
 
3
3
  module Metrix
4
4
  class FPM < Base
5
+ set_prefix "fpm"
6
+ set_known_metrics %w(
7
+ accepted_conn start_since accepted_conn listen_queue max_listen_queue
8
+ listen_queue_len idle_processes active_processes total_processes max_active_processes
9
+ max_children_reached slow_requests
10
+ )
11
+
5
12
  def initialize(data)
6
13
  @data = data
7
14
  @time = Time.now
@@ -1,7 +1,7 @@
1
1
  require "metrix/base"
2
2
 
3
3
  module Metrix
4
- class Json < Base
4
+ module JsonMetric
5
5
  def attributes
6
6
  @attributes ||= JSON.load(@raw)
7
7
  end
@@ -2,6 +2,9 @@ require "metrix/base"
2
2
 
3
3
  module Metrix
4
4
  class Load < Base
5
+ set_prefix "system.load"
6
+ set_known_metrics %w(load1 load5 load15)
7
+
5
8
  def initialize(data)
6
9
  @data = data
7
10
  @time = Time.now
@@ -1,28 +1,45 @@
1
- require "metrix/json"
1
+ require "metrix/json_metric"
2
2
  require "metrix/metric"
3
3
 
4
4
  module Metrix
5
- class Mongodb < Json
6
- ignore_metrics %w(ok mem.bits pid uptime uptimeMillis uptimeEstimate)
7
-
8
- def prefix
9
- "mongodb"
10
- end
5
+ class Mongodb < Base
6
+ include JsonMetric
7
+ set_prefix "mongodb"
8
+ set_known_metrics %w(
9
+ asserts.msg asserts.regular asserts.rollovers asserts.user asserts.warning backgroundFlushing.average_ms
10
+ backgroundFlushing.flushes backgroundFlushing.last_ms backgroundFlushing.total_ms connections.available connections.current
11
+ connections.totalCreated cursors.clientCursors_size cursors.timedOut cursors.totalOpen dur.commits dur.commitsInWriteLock
12
+ dur.compression dur.earlyCommits dur.journaledMB dur.timeMs.dt dur.timeMs.prepLogBuffer dur.timeMs.remapPrivateView
13
+ dur.timeMs.writeToDataFiles dur.timeMs.writeToJournal dur.writeToDataFilesMB extra_info.page_faults globalLock.activeClients.readers
14
+ globalLock.activeClients.total globalLock.activeClients.writers globalLock.currentQueue.readers globalLock.currentQueue.total
15
+ globalLock.currentQueue.writers globalLock.lockTime globalLock.totalTime indexCounters.accesses indexCounters.hits
16
+ indexCounters.missRatio indexCounters.misses indexCounters.resets mem.mapped mem.mappedWithJournal mem.resident mem.virtual
17
+ metrics.document.deleted metrics.document.inserted metrics.document.returned metrics.document.updated metrics.getLastError.wtime.num
18
+ metrics.getLastError.wtime.totalMillis metrics.getLastError.wtimeouts metrics.operation.fastmod metrics.operation.idhack
19
+ metrics.operation.scanAndOrder metrics.queryExecutor.scanned metrics.record.moves metrics.repl.apply.batches.num
20
+ metrics.repl.apply.batches.totalMillis metrics.repl.apply.ops metrics.repl.buffer.count metrics.repl.buffer.maxSizeBytes
21
+ metrics.repl.buffer.sizeBytes metrics.repl.network.bytes metrics.repl.network.getmores.num metrics.repl.network.getmores.totalMillis
22
+ metrics.repl.network.ops metrics.repl.network.readersCreated metrics.repl.oplog.insert.num metrics.repl.oplog.insert.totalMillis
23
+ metrics.repl.oplog.insertBytes metrics.repl.preload.docs.num metrics.repl.preload.docs.totalMillis metrics.repl.preload.indexes.num
24
+ metrics.repl.preload.indexes.totalMillis metrics.ttl.deletedDocuments metrics.ttl.passes network.bytesIn network.bytesOut
25
+ network.numRequests opcounters.command opcounters.delete opcounters.getmore opcounters.insert opcounters.query
26
+ opcounters.update opcountersRepl.command opcountersRepl.delete opcountersRepl.getmore opcountersRepl.insert opcountersRepl.query
27
+ opcountersRepl.update recordStats.accessesNotInMemory recordStats.pageFaultExceptionsThrown recordStats.timeAcquiringMicros.R
28
+ recordStats.timeAcquiringMicros.W recordStats.timeAcquiringMicros.r recordStats.timeAcquiringMicros.r recordStats.timeAcquiringMicros.w
29
+ recordStats.timeAcquiringMicros.w recordStats.timeLockedMicros.R recordStats.timeLockedMicros.W recordStats.timeLockedMicros.r
30
+ recordStats.timeLockedMicros.r recordStats.timeLockedMicros.w recordStats.timeLockedMicros.w uptime
31
+ )
11
32
 
12
33
  DATABASE_RECORD_SET = /^recordStats\.(.*?)\.(.*)/
13
34
  DATABASE_LOCK = /^locks/
14
35
 
15
- def ignore_metric?(metric)
16
- metric[DATABASE_RECORD_SET] ||
17
- metric[DATABASE_LOCK] ||
18
- super
19
- end
20
-
21
- def tagged_metrics
36
+ def metrics
22
37
  unfiltered_metrics.map do |k, v|
38
+ next unless self.class.known_metrics.include?(k)
23
39
  if k.match(DATABASE_RECORD_SET)
40
+ next if %w(accessesNotInMemory pageFaultExceptionsThrown).include?($2)
24
41
  database = $1
25
- Metric.new($2, "#{prefix}.locks.#{v}", time, database: database)
42
+ Metric.new("#{prefix}.#{$2}", v, time, database: database)
26
43
  elsif k.match(DATABASE_LOCK)
27
44
  chunks = k.split(".")
28
45
  offset = 0
@@ -31,6 +48,8 @@ module Metrix
31
48
  metric = chunks[(2 + offset)..-1].join(".")
32
49
  database = "." if database == ""
33
50
  Metric.new("#{prefix}.recordStats.#{metric}", v, time, database: database)
51
+ else
52
+ Metric.new("#{prefix}.#{k}", v, time)
34
53
  end
35
54
  end.compact
36
55
  end
@@ -2,6 +2,9 @@ require "metrix/base"
2
2
 
3
3
  module Metrix
4
4
  class Nginx < Base
5
+ set_prefix "nginx"
6
+ set_known_metrics %w(accepts handled requests active_connections reading writing waiting)
7
+
5
8
  def initialize(data)
6
9
  @data = data
7
10
  @time = Time.now
@@ -17,10 +20,6 @@ module Metrix
17
20
  end
18
21
  end
19
22
 
20
- def prefix
21
- "nginx"
22
- end
23
-
24
23
  def extract(data = nil)
25
24
  {
26
25
  accepts: accepts,
@@ -3,6 +3,10 @@ require "metrix/base"
3
3
  module Metrix
4
4
  class ProcessMetric < Base
5
5
  attr_reader :time
6
+ set_known_metrics %w(minflt cminflt majflt cmajflt utime stime cutime sctime num_threads
7
+ vsize rss
8
+ )
9
+ set_prefix "system.process"
6
10
 
7
11
  class << self
8
12
  def all
@@ -46,10 +50,6 @@ module Metrix
46
50
  }
47
51
  end
48
52
 
49
- def prefix
50
- "system.process"
51
- end
52
-
53
53
  def cast_int(str)
54
54
  Integer(str) rescue str
55
55
  end
@@ -2,6 +2,12 @@ require "metrix/base"
2
2
 
3
3
  module Metrix
4
4
  class System < Base
5
+ set_prefix "system"
6
+ set_known_metrics %w(
7
+ processes procs_running procs_blocked ctxt cpu.user cpu.nice cpu.system cpu.idle
8
+ cpu.iowait cpu.irq cpu.softirq
9
+ )
10
+
5
11
  class Cpu
6
12
  def initialize(values)
7
13
  @values = values
@@ -49,10 +55,6 @@ module Metrix
49
55
  }
50
56
  end
51
57
 
52
- def prefix
53
- "system"
54
- end
55
-
56
58
  def cpu
57
59
  Cpu.new(@raw[/^cpu (.*)/, 1].split(" ").map(&:to_i))
58
60
  end
@@ -1,3 +1,3 @@
1
1
  module Metrix
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
File without changes
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
  require "metrix/elastic_search"
3
3
 
4
- describe "Metrox::ElasticSearch" do
4
+ describe "Metrix::ElasticSearch" do
5
5
  let(:payload) { FIXTURES_PATH.join("es_status.json").read }
6
6
  subject(:es_status) { Metrix::ElasticSearch.new(payload) }
7
7
  it { should_not be_nil }
@@ -9,7 +9,8 @@ describe "Metrox::ElasticSearch" do
9
9
  it { subject.time.should_not be_nil }
10
10
 
11
11
  describe "#metrics" do
12
- subject(:metrics) { hash_metrics(es_status.metrics) }
12
+ subject(:extracted) { hash_metrics(es_status.metrics) }
13
+
13
14
  it { should be_kind_of(Hash) }
14
15
  it { should_not be_empty }
15
16
 
@@ -17,27 +18,38 @@ describe "Metrox::ElasticSearch" do
17
18
  "elasticsearch._shards.total"=>10,
18
19
  "elasticsearch._shards.successful"=>5,
19
20
  "elasticsearch._shards.failed"=>0,
20
- "elasticsearch.indices.search.index.primary_size_in_bytes"=>2008,
21
- "elasticsearch.indices.search.index.size_in_bytes"=>2008,
22
- "elasticsearch.indices.search.translog.operations"=>1,
23
- "elasticsearch.indices.search.docs.num_docs"=>1,
24
- "elasticsearch.indices.search.docs.max_doc"=>1,
25
- "elasticsearch.indices.search.docs.deleted_docs"=>0,
26
- "elasticsearch.indices.search.merges.current"=>0,
27
- "elasticsearch.indices.search.merges.current_docs"=>0,
28
- "elasticsearch.indices.search.merges.current_size_in_bytes"=>0,
29
- "elasticsearch.indices.search.merges.total"=>0,
30
- "elasticsearch.indices.search.merges.total_time_in_millis"=>0,
31
- "elasticsearch.indices.search.merges.total_docs"=>0,
32
- "elasticsearch.indices.search.merges.total_size_in_bytes"=>0,
33
- "elasticsearch.indices.search.refresh.total"=>6,
34
- "elasticsearch.indices.search.refresh.total_time_in_millis"=>124,
35
- "elasticsearch.indices.search.flush.total"=>0,
36
21
  }.each do |k, v|
37
22
  it "should set attributes #{k.inspect} to #{v.inspect}" do
38
23
  subject[k].value.should eq(v)
39
24
  end
40
25
  end
26
+
27
+ it { should_not include("elasticsearch.indices.search.flush.total") }
41
28
  it { subject.keys.count.should eq(20) }
42
29
  end
30
+
31
+ it { subject.unfiltered_metrics.should include("indices.search.flush.total") }
32
+
33
+ describe "#metrics" do
34
+ subject(:tagged_metrics) { es_status.metrics }
35
+ it { should be_kind_of(Array) }
36
+ it { should_not be_empty }
37
+
38
+ it { subject.count.should eq(20) }
39
+
40
+ describe "first metric" do
41
+
42
+ before :each do
43
+ es_status.stub(:time) { Time.at(10) }
44
+ end
45
+
46
+ subject(:first) { tagged_metrics.select { |m| m.key == "elasticsearch.index.primary_size_in_bytes" }.first }
47
+ it { should_not be_nil }
48
+ it { subject.key.should start_with("elasticsearch.index.primary_size_in_bytes") }
49
+ it { subject.value.should eq(2008) }
50
+ it { subject.time.should_not be_nil }
51
+ it { subject.tags.should eq(index: "search") }
52
+ it { subject.to_opentsdb.should eq("put elasticsearch.index.primary_size_in_bytes 10 2008 index=search hostname=test.host") }
53
+ end
54
+ end
43
55
  end
@@ -7,19 +7,15 @@ describe "Metrix::Mongodb" do
7
7
  subject(:status) { Metrix::Mongodb.new(payload) }
8
8
  it { should_not be_nil }
9
9
 
10
- describe "tagged_metrics" do
11
- subject(:tagged_metrics) { status.tagged_metrics }
12
- it { should be_kind_of(Array) }
13
- it { should_not be_empty }
14
- end
10
+ describe "metric keys" do
11
+ subject(:keys) { status.metrics.map(&:key) }
15
12
 
16
- describe "ignore_metric?" do
17
13
  %w(
18
- locks...timeAcquiringMicros.W
19
- recordStats.opentsdb.pageFaultExceptionsThrown
20
- ).each do |m|
21
- it "should ignore #{m}" do
22
- status.should be_ignore_metric(m)
14
+ mongodb.ok mongodb.mem.bits mongodb.pid
15
+ mongodb.uptimeMillis mongodb.uptimeEstimate
16
+ ).each do |name|
17
+ it "should not have key #{name.inspect}" do
18
+ subject.should_not include(name)
23
19
  end
24
20
  end
25
21
  end
@@ -28,17 +24,6 @@ describe "Metrix::Mongodb" do
28
24
  subject(:metrics) { hash_metrics(status.metrics) }
29
25
  it { should be_kind_of(Hash) }
30
26
 
31
- %w(
32
- mongodb.ok mongodb.mem.bits mongodb.pid mongodb.uptime
33
- mongodb.uptimeMillis mongodb.uptimeEstimate
34
- mongodb.recordStats.opentsdb.pageFaultExceptionsThrown
35
- mongodb.locks.local.timeLockedMicros.r
36
- ).each do |name|
37
- it "should not have key #{name.inspect}" do
38
- subject.should_not have_key(name)
39
- end
40
- end
41
-
42
27
  {
43
28
  "mongodb.globalLock.totalTime"=>474819000,
44
29
  "mongodb.globalLock.lockTime"=>1060706,
@@ -49,6 +34,7 @@ describe "Metrix::Mongodb" do
49
34
  "mongodb.globalLock.activeClients.readers"=>0,
50
35
  "mongodb.globalLock.activeClients.writers"=>0,
51
36
  "mongodb.mem.resident"=>9,
37
+ "mongodb.uptime"=> 475,
52
38
  "mongodb.mem.virtual"=>2814,
53
39
  "mongodb.recordStats.accessesNotInMemory" => 0,
54
40
  "mongodb.recordStats.pageFaultExceptionsThrown" => 0,
@@ -1,5 +1,5 @@
1
1
  require "spec_helper"
2
- require "metrix/process"
2
+ require "metrix/process_metric"
3
3
 
4
4
  describe "Metrix::ProcessMetric" do
5
5
  let(:data) { FIXTURES_PATH.join("proc.26928.txt").read }
@@ -0,0 +1,16 @@
1
+ require "spec_helper"
2
+
3
+ describe "Metrix" do
4
+ subject(:metrix) { Metrix }
5
+ it { should_not be_nil }
6
+
7
+ describe "#known_metrics" do
8
+ subject(:known_metrics) { Metrix.known_metrics }
9
+ it { should be_kind_of(Array) }
10
+ it { should_not be_empty }
11
+ it { subject.count.should eq(170) }
12
+
13
+ it { should include("mongodb.uptime") }
14
+ end
15
+ end
16
+
@@ -20,5 +20,7 @@ def hash_metrics(metrics)
20
20
  end
21
21
  end
22
22
 
23
+ require "metrix/process_metric"
24
+
23
25
  require "pathname"
24
26
  FIXTURES_PATH = Pathname.new(File.expand_path("../fixtures", __FILE__))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -95,13 +95,13 @@ files:
95
95
  - lib/metrix/elastic_search.rb
96
96
  - lib/metrix/fpm.rb
97
97
  - lib/metrix/graphite.rb
98
- - lib/metrix/json.rb
98
+ - lib/metrix/json_metric.rb
99
99
  - lib/metrix/load.rb
100
100
  - lib/metrix/metric.rb
101
101
  - lib/metrix/mongodb.rb
102
102
  - lib/metrix/nginx.rb
103
103
  - lib/metrix/opentsdb.rb
104
- - lib/metrix/process.rb
104
+ - lib/metrix/process_metric.rb
105
105
  - lib/metrix/reporter/stdout.rb
106
106
  - lib/metrix/system.rb
107
107
  - lib/metrix/version.rb
@@ -115,6 +115,7 @@ files:
115
115
  - spec/fixtures/php.fpm.status.txt
116
116
  - spec/fixtures/proc.26928.txt
117
117
  - spec/fixtures/proc.stat.txt
118
+ - spec/lib/metrix/base_spec.rb
118
119
  - spec/lib/metrix/cli_spec.rb
119
120
  - spec/lib/metrix/elastic_search_spec.rb
120
121
  - spec/lib/metrix/fpm_spec.rb
@@ -124,8 +125,9 @@ files:
124
125
  - spec/lib/metrix/mongodb_spec.rb
125
126
  - spec/lib/metrix/nginx_spec.rb
126
127
  - spec/lib/metrix/opentsdb_spec.rb
127
- - spec/lib/metrix/process_spec.rb
128
+ - spec/lib/metrix/process_metric_spec.rb
128
129
  - spec/lib/metrix/system_spec.rb
130
+ - spec/lib/metrix_spec.rb
129
131
  - spec/spec_helper.rb
130
132
  homepage: ''
131
133
  licenses: []
@@ -161,6 +163,7 @@ test_files:
161
163
  - spec/fixtures/php.fpm.status.txt
162
164
  - spec/fixtures/proc.26928.txt
163
165
  - spec/fixtures/proc.stat.txt
166
+ - spec/lib/metrix/base_spec.rb
164
167
  - spec/lib/metrix/cli_spec.rb
165
168
  - spec/lib/metrix/elastic_search_spec.rb
166
169
  - spec/lib/metrix/fpm_spec.rb
@@ -170,6 +173,7 @@ test_files:
170
173
  - spec/lib/metrix/mongodb_spec.rb
171
174
  - spec/lib/metrix/nginx_spec.rb
172
175
  - spec/lib/metrix/opentsdb_spec.rb
173
- - spec/lib/metrix/process_spec.rb
176
+ - spec/lib/metrix/process_metric_spec.rb
174
177
  - spec/lib/metrix/system_spec.rb
178
+ - spec/lib/metrix_spec.rb
175
179
  - spec/spec_helper.rb