mini_graphite 0.0.3 → 0.0.5

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: 96afb08d730f3a00ba47725ce3b39eae492368e4
4
- data.tar.gz: 1b2a2350de858f406a671e2e4e2390e6f66b001e
3
+ metadata.gz: d6a5d36496ba99732fa43057a862b1c0583fe551
4
+ data.tar.gz: 9c26d7b512a3777b73dfaa5c7ef542a112650393
5
5
  SHA512:
6
- metadata.gz: 09f08e5c5fd678fb01bd59c7faf730022fc9aaed46d9f2cb70eb890b72dd993de8978e80591b566b8e37e9a2af8f8f96ed3af6184621672f62c3ede42078f62f
7
- data.tar.gz: bfcc21cfeca12874cf435d9491bf6434e1613324392e69abe2692a84e625288f1fa97a173c9639c09e843cc928eef1889ee0693a11e0cb4c42962d8abbd4cf30
6
+ metadata.gz: 7927a29a703f55f6f35db557cdaf49856fe9f055953ecdd03dde95272f040c9e7d4be691d17bdce56033268873c03f43211618384b1d3fa22cf88bea6b22c2bf
7
+ data.tar.gz: 3e8247385c1f9d482bf7bb51a554049f2422e4f0798d92a7b4d8a179c567243aafc943faea4a917851b0491b750a733fe1b35028ec46640611a0d0f9cd30c9da
data/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  Simple wrapper for Graphite and Statsd
4
4
 
5
+ ## Instructions
6
+
7
+ Check the `test` folder for examples, if you need more explanations please contact us.
8
+
5
9
  ## Installation
6
10
 
7
11
  Add this line to your application's Gemfile:
@@ -18,7 +22,34 @@ Or install it yourself as:
18
22
 
19
23
  ## Usage
20
24
 
21
- TODO: Write usage instructions here
25
+ Dalia::MiniGraphite.config({
26
+ :graphite_host => "my.graphite.server.com",
27
+ :graphite_port => 2003, # default 2003
28
+ :statsd_host => "my.graphite.server.com",
29
+ :statsd_port => 8125, # default 8125
30
+ :mock_mode => false, # default false
31
+ :debug_mode => true # default false
32
+ })
33
+
34
+ Dalia::MiniGraphite.datapoint("my.key", 120, Time.now) # to Graphite
35
+ Dalia::MiniGraphite.counter("my.key", 120) # to StatSD
36
+
37
+
38
+ This will send 4 signals:
39
+
40
+ - key_prefix.ini # At the begining of the block
41
+ - key_prefix.count # At the begining of the block, keep it for compatibility
42
+ - key_prefix.time, ~1000 # At the end of the block, with the Benchmark.realtime result of the execution
43
+ - key_prefix.end # At the end of the block
44
+
45
+ result =
46
+ Dalia::MiniGraphite.benchmark_wrapper("key_prefix") do
47
+ sleep(1)
48
+ "RESULT"
49
+ end
50
+
51
+ puts result # => RESULT
52
+
22
53
 
23
54
  ## Contributing
24
55
 
data/lib/mini_graphite.rb CHANGED
@@ -1,61 +1,80 @@
1
1
  require_relative "mini_graphite/version"
2
2
  require_relative "mini_graphite/logger"
3
+ require "benchmark"
3
4
 
4
5
  require "socket"
5
6
 
6
7
  module Dalia
7
- module MiniGraphite
8
- DEFAULTS = {
9
- :graphite_host => "graphite.host.com",
10
- :graphite_port => 2003,
11
- :statsd_host => "statsd.host.com",
12
- :statsd_port => 8125,
13
- :mock_mode => false,
14
- :debug_mode => false
15
- }
16
-
17
- def self.config(opts = {})
18
- @opts = DEFAULTS.merge(opts)
19
- @logger = Dalia::MiniGraphite::Logger.new(opts[:debug_mode])
20
- logger.debug("Initalized with opts")
21
- logger.debug(opts.inspect)
22
- end
23
-
24
- def self.datapoint(key, value = 1, timestamp = Time.now)
25
- signal = "#{key} #{value} #{timestamp.to_i}"
26
- logger.debug("Sending datapoint: '#{signal}'")
27
-
28
- send_tcp(signal) if !opts[:mock_mode]
29
- end
30
-
31
- def self.counter(key, value = 1)
32
- signal = "#{key}:#{value}|c"
33
- logger.debug("Sending counter: '#{signal}'")
34
-
35
- send_udp(signal) if !opts[:mock_mode]
36
- end
37
-
38
- private
39
-
40
- def self.opts
41
- @opts
42
- end
43
-
44
- def self.logger
45
- @logger
46
- end
47
-
48
- def self.send_tcp(message)
49
- socket = TCPSocket.new(opts[:graphite_host], opts[:graphite_port])
50
- socket.print("#{message}\n")
51
- socket.close
52
- end
53
-
54
- def self.send_udp(message)
55
- socket = UDPSocket.new
56
- socket.send(message, 0, opts[:statsd_host], opts[:statsd_port])
57
- socket.close
58
- end
59
-
60
- end
8
+ module MiniGraphite
9
+ DEFAULTS = {
10
+ :graphite_host => "graphite.host.com",
11
+ :graphite_port => 2003,
12
+ :statsd_host => "statsd.host.com",
13
+ :statsd_port => 8125,
14
+ :mock_mode => false,
15
+ :debug_mode => false
16
+ }
17
+
18
+ def self.config(opts = {})
19
+ @opts = DEFAULTS.merge(opts)
20
+ @logger = Dalia::MiniGraphite::Logger.new(opts[:debug_mode])
21
+ logger.debug("Initalized with opts")
22
+ logger.debug(opts.inspect)
23
+ end
24
+
25
+ def self.datapoint(key, value = 1, timestamp = Time.now)
26
+ signal = "#{key} #{value} #{timestamp.to_i}"
27
+ logger.debug("Sending datapoint: '#{signal}'")
28
+
29
+ send_tcp(signal) if !opts[:mock_mode]
30
+ end
31
+
32
+ def self.counter(key, value = 1)
33
+ signal = "#{key}:#{value}|c"
34
+ logger.debug("Sending counter: '#{signal}'")
35
+
36
+ send_udp(signal) if !opts[:mock_mode]
37
+ end
38
+
39
+ def self.benchmark_wrapper(key)
40
+ counter("#{key}.ini")
41
+
42
+ result = nil
43
+
44
+ time =
45
+ Benchmark.realtime do
46
+ result = yield
47
+ end
48
+
49
+ counter("#{key}.count")
50
+ counter("#{key}.time", time * 1000)
51
+
52
+ counter("#{key}.end")
53
+
54
+ result
55
+ end
56
+
57
+ private
58
+
59
+ def self.opts
60
+ @opts
61
+ end
62
+
63
+ def self.logger
64
+ @logger
65
+ end
66
+
67
+ def self.send_tcp(message)
68
+ socket = TCPSocket.new(opts[:graphite_host], opts[:graphite_port])
69
+ socket.print("#{message}\n")
70
+ socket.close
71
+ end
72
+
73
+ def self.send_udp(message)
74
+ socket = UDPSocket.new
75
+ socket.send(message, 0, opts[:statsd_host], opts[:statsd_port])
76
+ socket.close
77
+ end
78
+
79
+ end
61
80
  end
@@ -1,28 +1,28 @@
1
1
  module Dalia
2
2
 
3
- module MiniGraphite
3
+ module MiniGraphite
4
4
 
5
- class Logger
6
- attr_reader :debug_mode
5
+ class Logger
6
+ attr_reader :debug_mode
7
7
 
8
- def initialize(debug_mode = true)
9
- @debug_mode = debug_mode
10
- end
8
+ def initialize(debug_mode = true)
9
+ @debug_mode = debug_mode
10
+ end
11
11
 
12
- def debug(message)
13
- return unless debug_mode
12
+ def debug(message)
13
+ return unless debug_mode
14
14
 
15
- result = "Dalia::MiniGraphite [#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}]: #{message}"
15
+ result = "Dalia::MiniGraphite [#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}]: #{message}"
16
16
 
17
- if defined? ::Rails
18
- ::Rails.logger.info result
19
- else
20
- Kernel.puts result
21
- end
22
- end
17
+ if defined? ::Rails
18
+ ::Rails.logger.info result
19
+ else
20
+ Kernel.puts result
21
+ end
22
+ end
23
23
 
24
- end
24
+ end
25
25
 
26
- end
26
+ end
27
27
 
28
28
  end
@@ -1,5 +1,5 @@
1
1
  module Dalia
2
2
  module MiniGraphite
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
data/test/logger_test.rb CHANGED
@@ -4,23 +4,23 @@ require_relative "../lib/mini_graphite"
4
4
 
5
5
  class MiniGraphiteTest < MiniTest::Unit::TestCase
6
6
 
7
- def test_debug
8
- Kernel.expects(:puts).with(regexp_matches(/MESSAGE/))
9
- logger = Dalia::MiniGraphite::Logger.new
10
- logger.debug("MESSAGE")
11
- end
7
+ def test_debug
8
+ Kernel.expects(:puts).with(regexp_matches(/MESSAGE/))
9
+ logger = Dalia::MiniGraphite::Logger.new
10
+ logger.debug("MESSAGE")
11
+ end
12
12
 
13
- def test_debug_when_not_debug_mode
14
- Kernel.expects(:puts).never
15
- logger = Dalia::MiniGraphite::Logger.new(false)
16
- logger.debug("MESSAGE")
17
- end
13
+ def test_debug_when_not_debug_mode
14
+ Kernel.expects(:puts).never
15
+ logger = Dalia::MiniGraphite::Logger.new(false)
16
+ logger.debug("MESSAGE")
17
+ end
18
18
 
19
- def test_debug_when_rails_actived
20
- Kernel.const_set("Rails", mock(:logger => mock(:info)))
21
- logger = Dalia::MiniGraphite::Logger.new
22
- logger.debug("MESSAGE")
23
- Kernel.send(:remove_const, :Rails)
24
- end
19
+ def test_debug_when_rails_actived
20
+ Kernel.const_set("Rails", mock(:logger => mock(:info)))
21
+ logger = Dalia::MiniGraphite::Logger.new
22
+ logger.debug("MESSAGE")
23
+ Kernel.send(:remove_const, :Rails)
24
+ end
25
25
 
26
26
  end
@@ -4,59 +4,74 @@ require_relative "../lib/mini_graphite"
4
4
 
5
5
  class MiniGraphiteTest < MiniTest::Unit::TestCase
6
6
 
7
- def test_datapoint
8
- Dalia::MiniGraphite.config({ :graphite_host => "graphite.host.com", :graphite_port => 2003 })
9
-
10
- socket_mock = mock()
11
- TCPSocket.expects(:new).with("graphite.host.com", 2003).returns(socket_mock)
12
- socket_mock.expects(:print).with("test.age 31 1357117860\n")
13
- socket_mock.expects(:close)
14
-
15
- Dalia::MiniGraphite.datapoint("test.age", 31, Time.new(2013,1,2,10,11))
16
- end
17
-
18
- def test_counter
19
- Dalia::MiniGraphite.config({ :statsd_host => "statsd.host.com", :statsd_port => 8125 })
20
-
21
- socket_mock = mock()
22
- UDPSocket.expects(:new).returns(socket_mock)
23
- socket_mock.expects(:send).with("height:231|c", 0, "statsd.host.com", 8125 )
24
- socket_mock.expects(:close)
25
-
26
- Dalia::MiniGraphite.counter("height", 231)
27
- end
28
-
29
- def test_on_config_should_debug
30
- Dalia::MiniGraphite::Logger.any_instance.expects(:debug).at_least_once
31
- Dalia::MiniGraphite.config()
32
- end
33
-
34
- def test_on_counter_should_debug
35
- Dalia::MiniGraphite.expects(:send_udp)
36
- Dalia::MiniGraphite.config()
37
-
38
- Dalia::MiniGraphite::Logger.any_instance.expects(:debug).with("Sending counter: 'test.age:31|c'")
39
- Dalia::MiniGraphite.counter("test.age", 31)
40
- end
41
-
42
- def test_on_datapoint_should_debug
43
- Dalia::MiniGraphite.expects(:send_tcp)
44
- Dalia::MiniGraphite.config()
45
-
46
- Dalia::MiniGraphite::Logger.any_instance.expects(:debug).with("Sending datapoint: 'test.age 31 1357117860'")
47
- Dalia::MiniGraphite.datapoint("test.age", 31, Time.new(2013,1,2,10,11))
48
- end
49
-
50
- def test_on_datapoint_not_send_tcp_if_mock_mode
51
- Dalia::MiniGraphite.config(:mock_mode => true)
52
- TCPSocket.expects(:new).never
53
- Dalia::MiniGraphite.datapoint("test.age")
54
- end
55
-
56
- def test_on_counter_not_send_udp_if_mock_mode
57
- Dalia::MiniGraphite.config(:mock_mode => true)
58
- UDPSocket.expects(:new).never
59
- Dalia::MiniGraphite.counter("test.age")
60
- end
7
+ def test_datapoint
8
+ Dalia::MiniGraphite.config({ :graphite_host => "graphite.host.com", :graphite_port => 2003 })
9
+
10
+ socket_mock = mock()
11
+ TCPSocket.expects(:new).with("graphite.host.com", 2003).returns(socket_mock)
12
+ socket_mock.expects(:print).with("test.age 31 1357117860\n")
13
+ socket_mock.expects(:close)
14
+
15
+ Dalia::MiniGraphite.datapoint("test.age", 31, Time.new(2013,1,2,10,11))
16
+ end
17
+
18
+ def test_counter
19
+ Dalia::MiniGraphite.config({ :statsd_host => "statsd.host.com", :statsd_port => 8125 })
20
+
21
+ socket_mock = mock()
22
+ UDPSocket.expects(:new).returns(socket_mock)
23
+ socket_mock.expects(:send).with("height:231|c", 0, "statsd.host.com", 8125 )
24
+ socket_mock.expects(:close)
25
+
26
+ Dalia::MiniGraphite.counter("height", 231)
27
+ end
28
+
29
+ def test_on_config_should_debug
30
+ Dalia::MiniGraphite::Logger.any_instance.expects(:debug).at_least_once
31
+ Dalia::MiniGraphite.config()
32
+ end
33
+
34
+ def test_on_counter_should_debug
35
+ Dalia::MiniGraphite.expects(:send_udp)
36
+ Dalia::MiniGraphite.config()
37
+
38
+ Dalia::MiniGraphite::Logger.any_instance.expects(:debug).with("Sending counter: 'test.age:31|c'")
39
+ Dalia::MiniGraphite.counter("test.age", 31)
40
+ end
41
+
42
+ def test_on_datapoint_should_debug
43
+ Dalia::MiniGraphite.expects(:send_tcp)
44
+ Dalia::MiniGraphite.config()
45
+
46
+ Dalia::MiniGraphite::Logger.any_instance.expects(:debug).with("Sending datapoint: 'test.age 31 1357117860'")
47
+ Dalia::MiniGraphite.datapoint("test.age", 31, Time.new(2013,1,2,10,11))
48
+ end
49
+
50
+ def test_on_datapoint_not_send_tcp_if_mock_mode
51
+ Dalia::MiniGraphite.config(:mock_mode => true)
52
+ TCPSocket.expects(:new).never
53
+ Dalia::MiniGraphite.datapoint("test.age")
54
+ end
55
+
56
+ def test_on_counter_not_send_udp_if_mock_mode
57
+ Dalia::MiniGraphite.config(:mock_mode => true)
58
+ UDPSocket.expects(:new).never
59
+ Dalia::MiniGraphite.counter("test.age")
60
+ end
61
+
62
+ def test_benchmark_wrapper
63
+ Dalia::MiniGraphite.expects(:counter).with("key_prefix.ini")
64
+ Dalia::MiniGraphite.expects(:counter).with("key_prefix.count")
65
+ Dalia::MiniGraphite.expects(:counter).with("key_prefix.time", is_a(Float))
66
+ Dalia::MiniGraphite.expects(:counter).with("key_prefix.end")
67
+
68
+ result =
69
+ Dalia::MiniGraphite.benchmark_wrapper("key_prefix") do
70
+ sleep(1)
71
+ "RESULT"
72
+ end
73
+
74
+ assert_equal("RESULT", result)
75
+ end
61
76
 
62
77
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_graphite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sevastianos Komianos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-14 00:00:00.000000000 Z
11
+ date: 2015-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler