metrix 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,45 +1,21 @@
1
1
  require "socket"
2
2
  require "metrix"
3
+ require "metrix/tcp_reporter"
3
4
 
4
5
  module Metrix
5
- class Graphite
6
+ class Graphite < TcpReporter
6
7
  attr_reader :host, :port
7
8
 
8
9
  def initialize(host, port = 2003)
9
- @host = host
10
- @port = port
10
+ super(host, port, 100)
11
11
  end
12
12
 
13
- def <<(metric)
14
- metric.metrics.each do |m|
15
- logger.debug "adding #{m.to_graphite}"
16
- buffers << m.to_graphite
17
- flush if buffers.count > 90
18
- end
13
+ def window_size
14
+ 90
19
15
  end
20
16
 
21
- def buffers
22
- @buffers ||= []
23
- end
24
-
25
- def flush
26
- Timeout.timeout(1) do
27
- if buffers.empty?
28
- logger.info "nothing to send"
29
- return
30
- end
31
- started = Time.now
32
- Socket.tcp(@host, @port) do |socket|
33
- socket.puts(buffers.join("\n"))
34
- end
35
- logger.info "sent #{buffers.count} in %.06fs" % [Time.now - started]
36
- end
37
- ensure
38
- buffers.clear
39
- end
40
-
41
- def logger
42
- Metrix.logger
17
+ def serialize_metric(m)
18
+ m.to_graphite
43
19
  end
44
20
  end
45
21
  end
@@ -1,39 +1,22 @@
1
1
  require "socket"
2
2
  require "timeout"
3
+ require "benchmark"
4
+ require "metrix/tcp_reporter"
3
5
 
4
6
  module Metrix
5
- class OpenTSDB
6
- attr_reader :host, :port
7
-
8
- def initialize(host, port = 4242)
9
- @host = host
10
- @port = port
11
- end
12
-
7
+ class OpenTSDB < TcpReporter
13
8
  def <<(metric)
14
9
  metric.metrics.each do |m|
15
10
  Metrix.logger.debug "buffering #{m.to_opentsdb}"
16
11
  buffers << m.to_opentsdb
17
- flush if buffers.count >= 90
12
+ flush if buffers.count >= 1000
18
13
  end
19
14
  rescue => err
20
15
  Metrix.logger.error "#{err.message} #{err.inspect}"
21
16
  end
22
17
 
23
- def flush
24
- Timeout.timeout(1) do
25
- return if buffers.empty?
26
- Metrix.logger.info "sending #{buffers.count} to #{@host}:#{@port}"
27
- Socket.tcp(@host, @port) do |socket|
28
- socket.puts buffers.join("\n")
29
- end
30
- end
31
- ensure
32
- buffers.clear
33
- end
34
-
35
- def buffers
36
- @buffers ||= []
18
+ def serialize_metric(m)
19
+ m.to_opentsdb
37
20
  end
38
21
  end
39
22
  end
@@ -19,7 +19,11 @@ module Metrix
19
19
  end
20
20
 
21
21
  def name
22
- comm.gsub(/^\(/, "").gsub(/\)$/, "")
22
+ normalize_name(comm)
23
+ end
24
+
25
+ def normalize_name(the_name)
26
+ the_name.gsub(/^\(/, "").gsub(/\)$/, "").gsub(/[^\w]+/i, "_")
23
27
  end
24
28
 
25
29
  def chunks
@@ -0,0 +1,53 @@
1
+ module Metrix
2
+ class TcpReporter
3
+ attr_reader :host, :port
4
+
5
+ def initialize(host, port = 4242, window_size = nil)
6
+ @host = host
7
+ @port = port
8
+ @window_size = window_size
9
+ end
10
+
11
+ def window_size
12
+ @window_size || 1000
13
+ end
14
+
15
+ def buffers
16
+ @buffers ||= []
17
+ end
18
+
19
+ def <<(metric)
20
+ metric.metrics.each do |m|
21
+ line = serialize_metric(m)
22
+ Metrix.logger.debug "buffering #{line}"
23
+ buffers << line
24
+ flush if buffers.count >= window_size
25
+ end
26
+ rescue => err
27
+ logger.error "#{err.message} #{err.inspect}"
28
+ end
29
+
30
+ def flush
31
+ Timeout.timeout(1) do
32
+ return if buffers.empty?
33
+ cnt = buffers.count
34
+ Metrix.logger.info "sending #{cnt} to #{@host}:#{@port}"
35
+ ms = Benchmark.measure do
36
+ Socket.tcp(@host, @port) do |socket|
37
+ buffers.each do |line|
38
+ socket.puts line
39
+ end
40
+ socket.flush
41
+ end
42
+ end
43
+ logger.info "sent %d metrics in %.06f" % [cnt, ms.real]
44
+ end
45
+ ensure
46
+ buffers.clear
47
+ end
48
+
49
+ def logger
50
+ Metrix.logger
51
+ end
52
+ end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module Metrix
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.14"
3
3
  end
@@ -0,0 +1 @@
1
+ 5 (kworker/0:0H) S 2 0 0 0 -1 69238880 0 0 0 0 0 0 0 0 0 -20 1 0 12 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 18446744071579336871 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0
@@ -13,6 +13,24 @@ describe "Metrix::ProcessMetric" do
13
13
  it { subject.utime.should eq(32337) }
14
14
  it { subject.stime.should eq(34740) }
15
15
 
16
+ describe "#normalize_name", :wip do
17
+ {
18
+ "kworker/0:0H" => "kworker_0_0H",
19
+ "(java)" => "java"
20
+ }.each do |from, to|
21
+ it "normalized #{from.inspect} to #{to.inspect}" do
22
+ subject.normalize_name(from).should eq(to)
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "with crazy characters", :wip do
28
+ let(:data) { FIXTURES_PATH.join("proc.5.txt").read }
29
+ subject(:process) { Metrix::ProcessMetric.new(data) }
30
+
31
+ it { subject.name.should eq("kworker_0_0H") }
32
+ end
33
+
16
34
  describe "#metrics" do
17
35
  subject(:metrics) do
18
36
  hash_metrics(process.metrics)
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.13
4
+ version: 0.0.14
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-05 00:00:00.000000000 Z
12
+ date: 2013-06-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: SyslogLogger
@@ -107,6 +107,7 @@ files:
107
107
  - lib/metrix/process_metric.rb
108
108
  - lib/metrix/reporter/stdout.rb
109
109
  - lib/metrix/system.rb
110
+ - lib/metrix/tcp_reporter.rb
110
111
  - lib/metrix/version.rb
111
112
  - metrix.gemspec
112
113
  - spec/fixtures/df.txt
@@ -120,6 +121,7 @@ files:
120
121
  - spec/fixtures/nginx.status.txt
121
122
  - spec/fixtures/php.fpm.status.txt
122
123
  - spec/fixtures/proc.26928.txt
124
+ - spec/fixtures/proc.5.txt
123
125
  - spec/fixtures/proc.stat.txt
124
126
  - spec/lib/metrix/base_spec.rb
125
127
  - spec/lib/metrix/cli_spec.rb
@@ -174,6 +176,7 @@ test_files:
174
176
  - spec/fixtures/nginx.status.txt
175
177
  - spec/fixtures/php.fpm.status.txt
176
178
  - spec/fixtures/proc.26928.txt
179
+ - spec/fixtures/proc.5.txt
177
180
  - spec/fixtures/proc.stat.txt
178
181
  - spec/lib/metrix/base_spec.rb
179
182
  - spec/lib/metrix/cli_spec.rb