mini_graphite 0.2.1 → 0.3.7
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 +4 -4
- data/.travis.yml +5 -0
- data/README.md +26 -11
- data/lib/mini_graphite.rb +37 -12
- data/lib/mini_graphite/logger.rb +1 -1
- data/lib/mini_graphite/routes_reporter.rb +2 -1
- data/lib/mini_graphite/version.rb +2 -2
- data/minigraphite.gemspec +2 -2
- data/test/logger_test.rb +2 -3
- data/test/minigraphite_test.rb +68 -14
- data/test/routes_reporter_test.rb +5 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0978431fccda978ce88aa42cc663872044f184e9
|
4
|
+
data.tar.gz: d75a452d76d670b6595415b3488df2b6f6b41d16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6665b43f77ec8da1ef2e8ef350e081aecc958c96e642196d424f56f63ac3b9bdbcae2895f0a8b9b727bc1f29d355df2c43f9996d2d30b890d16a1dd3fd001900
|
7
|
+
data.tar.gz: 5d05a3e739bc09300b7e36f1640812fb1190f4455bb26a5ee38fa83841879e1f52429ad8011373a08e274158cae871ee79ec09949b05a76ad0abda36a7663238
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Simple wrapper for Graphite and Statsd
|
4
4
|
|
5
|
-
##
|
5
|
+
## Instructions
|
6
6
|
|
7
7
|
Check the `test` folder for examples, if you need more explanations please contact us.
|
8
8
|
|
@@ -27,7 +27,18 @@ Or install it yourself as:
|
|
27
27
|
Dalia::MiniGraphite.config({
|
28
28
|
:graphite_host => "my.graphite.server.com",
|
29
29
|
:graphite_port => 2003, # default 2003
|
30
|
-
:statsd_host => "my.
|
30
|
+
:statsd_host => "my.statsd.server.com",
|
31
|
+
:statsd_port => 8125, # default 8125
|
32
|
+
:mock_mode => false, # default false
|
33
|
+
:debug_mode => true # default false
|
34
|
+
})
|
35
|
+
|
36
|
+
Also support multiple servers:
|
37
|
+
|
38
|
+
Dalia::MiniGraphite.config({
|
39
|
+
:graphite_host => ["my.graphite1.server.com", "my.graphite2.server.com"],
|
40
|
+
:graphite_port => 2003, # default 2003
|
41
|
+
:statsd_host => ["my.statsd1.server.com", "my.statsd2.server.com", "my.statsd3.server.com"],
|
31
42
|
:statsd_port => 8125, # default 8125
|
32
43
|
:mock_mode => false, # default false
|
33
44
|
:debug_mode => true # default false
|
@@ -36,24 +47,27 @@ Or install it yourself as:
|
|
36
47
|
### Simple signals
|
37
48
|
|
38
49
|
Dalia::MiniGraphite.datapoint("my.key", 120, Time.now) # to Graphite
|
39
|
-
Dalia::MiniGraphite.counter("my.key", 120) # to StatSD
|
50
|
+
Dalia::MiniGraphite.counter("my.key", 120) # to StatSD with `c` modifyer
|
51
|
+
Dalia::MiniGraphite.time("my.key", 120) # to StatSD with `ms` modifyer
|
40
52
|
|
41
53
|
### Block wrapper benchmark
|
42
54
|
|
55
|
+
# Dalia::MiniGraphite.benchmark_wrapper(key_prefix, result_send_method)
|
43
56
|
result =
|
44
|
-
Dalia::MiniGraphite.benchmark_wrapper("key_prefix",
|
57
|
+
Dalia::MiniGraphite.benchmark_wrapper("key_prefix", :length) do
|
45
58
|
sleep(1)
|
46
59
|
"RESULT"
|
47
60
|
end
|
48
61
|
|
49
62
|
puts result # => RESULT
|
50
63
|
|
51
|
-
This will send
|
64
|
+
This will send several signals:
|
52
65
|
|
53
|
-
- *key_prefix.ini* # At the begining of the block
|
54
|
-
- *key_prefix.result* # At the end of the block, send the block result. Only if `
|
55
|
-
- *key_prefix.time
|
56
|
-
- *key_prefix.end* # At the end of the block
|
66
|
+
- *key_prefix.ini:1|c* # At the begining of the block
|
67
|
+
- *key_prefix.result:6|c* # At the end of the block, send the block `result.send(result_send_method)`. Only if `result_send_method` paramater is present.
|
68
|
+
- *key_prefix.time:1000|c* # At the end of the block, with the Benchmark.realtime result of the execution
|
69
|
+
- *key_prefix.end:1|c* # At the end of the block
|
70
|
+
- *key_prefix.time_stats:1000|ms* # At the end of the block, with the Benchmark.realtime result of the execution
|
57
71
|
|
58
72
|
### Routes reporter for Sinatra
|
59
73
|
|
@@ -61,8 +75,9 @@ The rack middleware must be added to the Rack chain in config.ru passing along w
|
|
61
75
|
|
62
76
|
- *set_graphite_key "graphite_key", /route_regexp/* # The regular expression will be used to match the Sinatra route and the corresponding graphite_key will be used to build the Graphite metrics:
|
63
77
|
|
64
|
-
"[
|
65
|
-
"[
|
78
|
+
- "[graphite\_key].count" # The counter of the times a particular route is requested.
|
79
|
+
- "[graphite\_key].duration" # The duration in milliseconds of each request/response cycle.
|
80
|
+
- "[graphite\_key].duration\_stats" # The duration in milliseconds of each request/response cycle in a "ms" metric
|
66
81
|
|
67
82
|
If the requested url doesn't match any configured regular expression the Graphite metrics will not be sent.
|
68
83
|
|
data/lib/mini_graphite.rb
CHANGED
@@ -30,14 +30,22 @@ module Dalia
|
|
30
30
|
send_tcp(signal) if !opts[:mock_mode]
|
31
31
|
end
|
32
32
|
|
33
|
-
def self.counter(key, value =
|
33
|
+
def self.counter(key, value = nil)
|
34
|
+
value ||= 1
|
34
35
|
signal = "#{key}:#{value}|c"
|
35
36
|
logger.debug("Sending counter: '#{signal}'")
|
36
37
|
|
37
38
|
send_udp(signal) if !opts[:mock_mode]
|
38
39
|
end
|
39
40
|
|
40
|
-
def self.
|
41
|
+
def self.time(key, value = 0)
|
42
|
+
signal = "#{key}:#{value}|ms"
|
43
|
+
logger.debug("Sending time: '#{signal}'")
|
44
|
+
|
45
|
+
send_udp(signal) if !opts[:mock_mode]
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.benchmark_wrapper(key, result_send_method = nil)
|
41
49
|
counter("#{key}.ini")
|
42
50
|
|
43
51
|
result = nil
|
@@ -48,33 +56,50 @@ module Dalia
|
|
48
56
|
end
|
49
57
|
|
50
58
|
counter("#{key}.time", time * 1000)
|
51
|
-
counter("#{key}.result", result) if
|
59
|
+
counter("#{key}.result", result.send(result_send_method)) if result_send_method
|
52
60
|
counter("#{key}.end")
|
53
61
|
|
62
|
+
time("#{key}.time_stats", time * 1000)
|
63
|
+
|
54
64
|
result
|
55
65
|
end
|
56
66
|
|
57
|
-
|
67
|
+
def self.send_tcp(message)
|
68
|
+
hosts = [opts[:graphite_host]].flatten
|
58
69
|
|
59
|
-
|
60
|
-
|
70
|
+
hosts.each do |host|
|
71
|
+
send_tcp_on_host(host, opts[:graphite_port], message)
|
72
|
+
end
|
61
73
|
end
|
62
74
|
|
63
|
-
def self.
|
64
|
-
|
75
|
+
def self.send_udp(message)
|
76
|
+
hosts = [opts[:statsd_host]].flatten
|
77
|
+
|
78
|
+
hosts.each do |host|
|
79
|
+
send_udp_on_host(host, opts[:statsd_port], message)
|
80
|
+
end
|
65
81
|
end
|
66
82
|
|
67
|
-
def self.
|
68
|
-
socket = TCPSocket.new(
|
83
|
+
def self.send_tcp_on_host(host, port, message)
|
84
|
+
socket = TCPSocket.new(host, port)
|
69
85
|
socket.print("#{message}\n")
|
70
86
|
socket.close
|
71
87
|
end
|
72
88
|
|
73
|
-
def self.
|
89
|
+
def self.send_udp_on_host(host, port, message)
|
74
90
|
socket = UDPSocket.new
|
75
|
-
socket.send(message, 0,
|
91
|
+
socket.send(message, 0, host, port)
|
76
92
|
socket.close
|
77
93
|
end
|
78
94
|
|
95
|
+
private
|
96
|
+
|
97
|
+
def self.opts
|
98
|
+
@opts
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.logger
|
102
|
+
@logger
|
103
|
+
end
|
79
104
|
end
|
80
105
|
end
|
data/lib/mini_graphite/logger.rb
CHANGED
@@ -20,6 +20,7 @@ module Dalia
|
|
20
20
|
|
21
21
|
Dalia::MiniGraphite.counter("#{graphite_key}.count") if graphite_key
|
22
22
|
Dalia::MiniGraphite.counter("#{graphite_key}.duration", time_taken) if graphite_key
|
23
|
+
Dalia::MiniGraphite.time("#{graphite_key}.duration_stats", time_taken) if graphite_key
|
23
24
|
end
|
24
25
|
|
25
26
|
[status, headers, body]
|
@@ -31,4 +32,4 @@ module Dalia
|
|
31
32
|
end
|
32
33
|
end
|
33
34
|
end
|
34
|
-
end
|
35
|
+
end
|
data/minigraphite.gemspec
CHANGED
@@ -6,8 +6,8 @@ require 'mini_graphite/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "mini_graphite"
|
8
8
|
spec.version = Dalia::MiniGraphite::VERSION
|
9
|
-
spec.authors = ["Sevastianos Komianos"]
|
10
|
-
spec.email = ["
|
9
|
+
spec.authors = ["Sevastianos Komianos", "Fernando Guillen"]
|
10
|
+
spec.email = ["it@daliaresearch.com"]
|
11
11
|
spec.summary = "Simple wrapper for Graphite and Statsd"
|
12
12
|
spec.description = "Simple wrapper for Graphite and Statsd"
|
13
13
|
spec.homepage = "https://github.com/DaliaResearch/MiniGraphite"
|
data/test/logger_test.rb
CHANGED
@@ -2,7 +2,7 @@ require "minitest/autorun"
|
|
2
2
|
require "mocha/setup"
|
3
3
|
require_relative "../lib/mini_graphite"
|
4
4
|
|
5
|
-
class MiniGraphiteTest < MiniTest::
|
5
|
+
class MiniGraphiteTest < MiniTest::Test
|
6
6
|
|
7
7
|
def test_debug
|
8
8
|
Kernel.expects(:puts).with(regexp_matches(/MESSAGE/))
|
@@ -17,10 +17,9 @@ class MiniGraphiteTest < MiniTest::Unit::TestCase
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_debug_when_rails_actived
|
20
|
-
|
20
|
+
Rails.expects(:logger => mock(:info))
|
21
21
|
logger = Dalia::MiniGraphite::Logger.new
|
22
22
|
logger.debug("MESSAGE")
|
23
|
-
Kernel.send(:remove_const, :Rails)
|
24
23
|
end
|
25
24
|
|
26
25
|
end
|
data/test/minigraphite_test.rb
CHANGED
@@ -2,28 +2,78 @@ require "minitest/autorun"
|
|
2
2
|
require "mocha/setup"
|
3
3
|
require_relative "../lib/mini_graphite"
|
4
4
|
|
5
|
-
class MiniGraphiteTest < MiniTest::
|
5
|
+
class MiniGraphiteTest < MiniTest::Test
|
6
6
|
|
7
|
-
def
|
8
|
-
|
7
|
+
def test_send_tcp_on_host
|
8
|
+
socket_mock = mock()
|
9
|
+
TCPSocket.expects(:new).with("HOST", "PORT").returns(socket_mock)
|
10
|
+
socket_mock.expects(:print).with("MESSAGE\n")
|
11
|
+
socket_mock.expects(:close)
|
9
12
|
|
13
|
+
Dalia::MiniGraphite.send_tcp_on_host("HOST", "PORT", "MESSAGE")
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_send_udp_on_host
|
10
17
|
socket_mock = mock()
|
11
|
-
|
12
|
-
socket_mock.expects(:
|
18
|
+
UDPSocket.expects(:new).returns(socket_mock)
|
19
|
+
socket_mock.expects(:send).with("MESSAGE", 0, "HOST", "PORT" )
|
13
20
|
socket_mock.expects(:close)
|
14
21
|
|
22
|
+
Dalia::MiniGraphite.send_udp_on_host("HOST", "PORT", "MESSAGE")
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_send_tcp
|
26
|
+
Dalia::MiniGraphite.config({ :graphite_host => "HOST", :graphite_port => "PORT" })
|
27
|
+
Dalia::MiniGraphite.expects(:send_tcp_on_host).with("HOST", "PORT", "MESSAGE")
|
28
|
+
Dalia::MiniGraphite.send_tcp("MESSAGE")
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_send_tcp_when_multiple_hosts
|
32
|
+
Dalia::MiniGraphite.config({ :graphite_host => ["HOST1", "HOST2"], :graphite_port => "PORT" })
|
33
|
+
|
34
|
+
Dalia::MiniGraphite.expects(:send_tcp_on_host).with("HOST1", "PORT", "MESSAGE")
|
35
|
+
Dalia::MiniGraphite.expects(:send_tcp_on_host).with("HOST2", "PORT", "MESSAGE")
|
36
|
+
|
37
|
+
Dalia::MiniGraphite.send_tcp("MESSAGE")
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_send_udp
|
41
|
+
Dalia::MiniGraphite.config({ :statsd_host => "HOST", :statsd_port => "PORT" })
|
42
|
+
Dalia::MiniGraphite.expects(:send_udp_on_host).with("HOST", "PORT", "MESSAGE")
|
43
|
+
Dalia::MiniGraphite.send_udp("MESSAGE")
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_send_udp_when_multiple_hosts
|
47
|
+
Dalia::MiniGraphite.config({ :statsd_host => ["HOST1", "HOST2"], :statsd_port => "PORT" })
|
48
|
+
|
49
|
+
Dalia::MiniGraphite.expects(:send_udp_on_host).with("HOST1", "PORT", "MESSAGE")
|
50
|
+
Dalia::MiniGraphite.expects(:send_udp_on_host).with("HOST2", "PORT", "MESSAGE")
|
51
|
+
|
52
|
+
Dalia::MiniGraphite.send_udp("MESSAGE")
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_datapoint
|
56
|
+
Dalia::MiniGraphite.config({ :graphite_host => "graphite.host.com", :graphite_port => 2003 })
|
57
|
+
Dalia::MiniGraphite.expects(:send_tcp).with("test.age 31 1357117860")
|
15
58
|
Dalia::MiniGraphite.datapoint("test.age", 31, Time.new(2013,1,2,10,11))
|
16
59
|
end
|
17
60
|
|
18
61
|
def test_counter
|
19
62
|
Dalia::MiniGraphite.config({ :statsd_host => "statsd.host.com", :statsd_port => 8125 })
|
63
|
+
Dalia::MiniGraphite.expects(:send_udp).with("height:231|c")
|
64
|
+
Dalia::MiniGraphite.counter("height", 231)
|
65
|
+
end
|
20
66
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
67
|
+
def test_time
|
68
|
+
Dalia::MiniGraphite.config({ :statsd_host => "statsd.host.com", :statsd_port => 8125 })
|
69
|
+
Dalia::MiniGraphite.expects(:send_udp).with("my_time:231|ms")
|
70
|
+
Dalia::MiniGraphite.time("my_time", 231)
|
71
|
+
end
|
25
72
|
|
26
|
-
|
73
|
+
def test_counter_when_nil_value
|
74
|
+
Dalia::MiniGraphite.config({ :statsd_host => "statsd.host.com", :statsd_port => 8125 })
|
75
|
+
Dalia::MiniGraphite.expects(:send_udp).with("height:1|c")
|
76
|
+
Dalia::MiniGraphite.counter("height", nil)
|
27
77
|
end
|
28
78
|
|
29
79
|
def test_on_config_should_debug
|
@@ -49,13 +99,13 @@ class MiniGraphiteTest < MiniTest::Unit::TestCase
|
|
49
99
|
|
50
100
|
def test_on_datapoint_not_send_tcp_if_mock_mode
|
51
101
|
Dalia::MiniGraphite.config(:mock_mode => true)
|
52
|
-
|
102
|
+
Dalia::MiniGraphite.expects(:send_tcp).never
|
53
103
|
Dalia::MiniGraphite.datapoint("test.age")
|
54
104
|
end
|
55
105
|
|
56
106
|
def test_on_counter_not_send_udp_if_mock_mode
|
57
107
|
Dalia::MiniGraphite.config(:mock_mode => true)
|
58
|
-
|
108
|
+
Dalia::MiniGraphite.expects(:send_udp).never
|
59
109
|
Dalia::MiniGraphite.counter("test.age")
|
60
110
|
end
|
61
111
|
|
@@ -65,6 +115,8 @@ class MiniGraphiteTest < MiniTest::Unit::TestCase
|
|
65
115
|
Dalia::MiniGraphite.expects(:counter).with("key_prefix.result").never
|
66
116
|
Dalia::MiniGraphite.expects(:counter).with("key_prefix.end")
|
67
117
|
|
118
|
+
Dalia::MiniGraphite.expects(:time).with("key_prefix.time_stats", is_a(Float))
|
119
|
+
|
68
120
|
result =
|
69
121
|
Dalia::MiniGraphite.benchmark_wrapper("key_prefix") do
|
70
122
|
sleep(1)
|
@@ -77,11 +129,13 @@ class MiniGraphiteTest < MiniTest::Unit::TestCase
|
|
77
129
|
def test_benchmark_wrapper_sending_result
|
78
130
|
Dalia::MiniGraphite.expects(:counter).with("key_prefix.ini")
|
79
131
|
Dalia::MiniGraphite.expects(:counter).with("key_prefix.time", is_a(Float))
|
80
|
-
Dalia::MiniGraphite.expects(:counter).with("key_prefix.result",
|
132
|
+
Dalia::MiniGraphite.expects(:counter).with("key_prefix.result", 6)
|
81
133
|
Dalia::MiniGraphite.expects(:counter).with("key_prefix.end")
|
82
134
|
|
135
|
+
Dalia::MiniGraphite.expects(:time).with("key_prefix.time_stats", is_a(Float))
|
136
|
+
|
83
137
|
result =
|
84
|
-
Dalia::MiniGraphite.benchmark_wrapper("key_prefix",
|
138
|
+
Dalia::MiniGraphite.benchmark_wrapper("key_prefix", :length) do
|
85
139
|
sleep(1)
|
86
140
|
"RESULT"
|
87
141
|
end
|
@@ -24,21 +24,25 @@ class RoutesReporter < Minitest::Test
|
|
24
24
|
def test_send_routes_metrics_to_graphite
|
25
25
|
Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.homepage.duration", instance_of(Float))
|
26
26
|
Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.homepage.count")
|
27
|
+
Dalia::MiniGraphite.expects(:time).with("app.test_app.test.routes.homepage.duration_stats", instance_of(Float))
|
27
28
|
@request.get("/", {"sinatra.route" => "/"})
|
28
29
|
|
29
30
|
Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.get_surveys.duration", instance_of(Float))
|
30
31
|
Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.get_surveys.count")
|
32
|
+
Dalia::MiniGraphite.expects(:time).with("app.test_app.test.routes.get_surveys.duration_stats", instance_of(Float))
|
31
33
|
@request.get("/OFFER_CLICK_ID/PANEL_USER_ID/PANEL_USER_ID_KIND", {"sinatra.route" => "/:offer_click_id/:panel_user_id/:panel_user_id_kind/"})
|
32
34
|
|
33
35
|
Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.error.duration", instance_of(Float))
|
34
36
|
Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.error.count")
|
37
|
+
Dalia::MiniGraphite.expects(:time).with("app.test_app.test.routes.error.duration_stats", instance_of(Float))
|
35
38
|
@request.get("/error", {"sinatra.route" => "/error"})
|
36
39
|
|
37
40
|
Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.active_surveys.duration", instance_of(Float))
|
38
41
|
Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.active_surveys.count")
|
42
|
+
Dalia::MiniGraphite.expects(:time).with("app.test_app.test.routes.active_surveys.duration_stats", instance_of(Float))
|
39
43
|
@request.get("/active_surveys/this_should_be_a_password", {"sinatra.route" => "/active_surveys/this_should_be_a_password" })
|
40
44
|
|
41
45
|
Dalia::MiniGraphite.expects(:counter).never
|
42
46
|
@request.get("/ANOTHER_URL")
|
43
47
|
end
|
44
|
-
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_graphite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sevastianos Komianos
|
8
|
+
- Fernando Guillen
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2017-
|
12
|
+
date: 2017-10-16 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: bundler
|
@@ -82,13 +83,14 @@ dependencies:
|
|
82
83
|
version: '0'
|
83
84
|
description: Simple wrapper for Graphite and Statsd
|
84
85
|
email:
|
85
|
-
-
|
86
|
+
- it@daliaresearch.com
|
86
87
|
executables: []
|
87
88
|
extensions: []
|
88
89
|
extra_rdoc_files: []
|
89
90
|
files:
|
90
91
|
- ".gitignore"
|
91
92
|
- ".ruby-version"
|
93
|
+
- ".travis.yml"
|
92
94
|
- Gemfile
|
93
95
|
- LICENSE.txt
|
94
96
|
- README.md
|