mini_graphite 0.0.5 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6a5d36496ba99732fa43057a862b1c0583fe551
4
- data.tar.gz: 9c26d7b512a3777b73dfaa5c7ef542a112650393
3
+ metadata.gz: 92947456f8f553f0a525e9d8f97acbe1f2773e6a
4
+ data.tar.gz: 05a251b574bda29749c5c0517a686b198d4681fe
5
5
  SHA512:
6
- metadata.gz: 7927a29a703f55f6f35db557cdaf49856fe9f055953ecdd03dde95272f040c9e7d4be691d17bdce56033268873c03f43211618384b1d3fa22cf88bea6b22c2bf
7
- data.tar.gz: 3e8247385c1f9d482bf7bb51a554049f2422e4f0798d92a7b4d8a179c567243aafc943faea4a917851b0491b750a733fe1b35028ec46640611a0d0f9cd30c9da
6
+ metadata.gz: 4128d24584db3382ff7bf91003f438d9a8f4598bfc06b631298ee09567068709c4ea52e0bb9434afc07d62b40a1427b9ebb07a67ff136c7154a489bd31122b68
7
+ data.tar.gz: 24f18c3064f099276adcab1e9379e90b94bd8a7cecae34d123a6482d49a45065388dc5c6aa3b53b3af568827dd4351ff713c24b813c77026e1fbe4fb89a53e83
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.0.0-p247
1
+ 2.3.1
data/README.md CHANGED
@@ -22,6 +22,8 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
+ ### Config
26
+
25
27
  Dalia::MiniGraphite.config({
26
28
  :graphite_host => "my.graphite.server.com",
27
29
  :graphite_port => 2003, # default 2003
@@ -31,25 +33,51 @@ Or install it yourself as:
31
33
  :debug_mode => true # default false
32
34
  })
33
35
 
36
+ ### Simple signals
37
+
34
38
  Dalia::MiniGraphite.datapoint("my.key", 120, Time.now) # to Graphite
35
39
  Dalia::MiniGraphite.counter("my.key", 120) # to StatSD
36
40
 
41
+ ### Block wrapper benchmark
42
+
43
+ result =
44
+ Dalia::MiniGraphite.benchmark_wrapper("key_prefix", [send_result={true,false}]) do
45
+ sleep(1)
46
+ "RESULT"
47
+ end
48
+
49
+ puts result # => RESULT
37
50
 
38
51
  This will send 4 signals:
39
52
 
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
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 `send_result` paramater is `true`.
55
+ - *key_prefix.time, [ms]* # At the end of the block, with the Benchmark.realtime result of the execution
56
+ - *key_prefix.end* # At the end of the block
57
+
58
+ ### Routes reporter for Sinatra
59
+
60
+ The rack middleware must be added to the Rack chain in config.ru passing along with it a block with config options:
61
+
62
+ - *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
+
64
+ "[graphite_key].count" # The counter of the times a particular route is requested.
65
+ "[graphite_key].duration" # The duration in milliseconds of each request/response cycle.
66
+
67
+ If the requested url doesn't match any configured regular expression the Graphite metrics will not be sent.
68
+
69
+ Example:
44
70
 
45
- result =
46
- Dalia::MiniGraphite.benchmark_wrapper("key_prefix") do
47
- sleep(1)
48
- "RESULT"
49
- end
71
+ # config.ru
50
72
 
51
- puts result # => RESULT
73
+ use Dalia::MiniGraphite::RoutesReporter do
74
+ set_graphite_key "app.my_app.production.routes.active_surveys", /active_surveys\/this_should_be_a_password/
75
+ set_graphite_key "app.my_app.production.routes.get_surveys", /\:offer_click_id\/\:panel_user_id\/\:panel_user_id_kind/
76
+ set_graphite_key "app.my_app.production.routes.error", /error/
77
+ set_graphite_key "app.my_app.production.routes.homepage", /\/$/
78
+ end
52
79
 
80
+ run MyApp::App
53
81
 
54
82
  ## Contributing
55
83
 
@@ -0,0 +1,34 @@
1
+ module Dalia
2
+ module MiniGraphite
3
+ class RoutesReporter
4
+ def initialize(app, &routes_block)
5
+ @app = app
6
+ @routes = {}
7
+ instance_eval(&routes_block) if block_given?
8
+ end
9
+
10
+ def call(env)
11
+ start_time = Time.now
12
+ status, headers, body = @app.call(env)
13
+ time_taken = (1000 * (Time.now - start_time))
14
+
15
+ current_route = env["sinatra.route"]
16
+ route = @routes.select { |graphite_key, route_regexp| current_route =~ route_regexp}
17
+
18
+ if route
19
+ graphite_key = route.keys.first
20
+
21
+ Dalia::MiniGraphite.counter("#{graphite_key}.count") if graphite_key
22
+ Dalia::MiniGraphite.counter("#{graphite_key}.duration", time_taken) if graphite_key
23
+ end
24
+
25
+ [status, headers, body]
26
+ end
27
+
28
+ private
29
+ def set_graphite_key(graphite_key, route_regexp)
30
+ @routes[graphite_key] = route_regexp
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,5 +1,5 @@
1
1
  module Dalia
2
2
  module MiniGraphite
3
- VERSION = "0.0.5"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  end
data/lib/mini_graphite.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative "mini_graphite/version"
2
2
  require_relative "mini_graphite/logger"
3
+ require_relative "mini_graphite/routes_reporter"
3
4
  require "benchmark"
4
5
 
5
6
  require "socket"
@@ -36,7 +37,7 @@ module Dalia
36
37
  send_udp(signal) if !opts[:mock_mode]
37
38
  end
38
39
 
39
- def self.benchmark_wrapper(key)
40
+ def self.benchmark_wrapper(key, send_result = false)
40
41
  counter("#{key}.ini")
41
42
 
42
43
  result = nil
@@ -46,9 +47,8 @@ module Dalia
46
47
  result = yield
47
48
  end
48
49
 
49
- counter("#{key}.count")
50
50
  counter("#{key}.time", time * 1000)
51
-
51
+ counter("#{key}.result", result) if send_result
52
52
  counter("#{key}.end")
53
53
 
54
54
  result
data/minigraphite.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "minitest"
24
24
  spec.add_development_dependency "mocha"
25
+ spec.add_development_dependency "rack"
25
26
  end
@@ -61,8 +61,8 @@ class MiniGraphiteTest < MiniTest::Unit::TestCase
61
61
 
62
62
  def test_benchmark_wrapper
63
63
  Dalia::MiniGraphite.expects(:counter).with("key_prefix.ini")
64
- Dalia::MiniGraphite.expects(:counter).with("key_prefix.count")
65
64
  Dalia::MiniGraphite.expects(:counter).with("key_prefix.time", is_a(Float))
65
+ Dalia::MiniGraphite.expects(:counter).with("key_prefix.result").never
66
66
  Dalia::MiniGraphite.expects(:counter).with("key_prefix.end")
67
67
 
68
68
  result =
@@ -74,4 +74,19 @@ class MiniGraphiteTest < MiniTest::Unit::TestCase
74
74
  assert_equal("RESULT", result)
75
75
  end
76
76
 
77
+ def test_benchmark_wrapper_sending_result
78
+ Dalia::MiniGraphite.expects(:counter).with("key_prefix.ini")
79
+ Dalia::MiniGraphite.expects(:counter).with("key_prefix.time", is_a(Float))
80
+ Dalia::MiniGraphite.expects(:counter).with("key_prefix.result", "RESULT")
81
+ Dalia::MiniGraphite.expects(:counter).with("key_prefix.end")
82
+
83
+ result =
84
+ Dalia::MiniGraphite.benchmark_wrapper("key_prefix", true) do
85
+ sleep(1)
86
+ "RESULT"
87
+ end
88
+
89
+ assert_equal("RESULT", result)
90
+ end
91
+
77
92
  end
@@ -0,0 +1,44 @@
1
+ require "minitest/autorun"
2
+ require "mocha/setup"
3
+ require "rack"
4
+
5
+ require_relative "../lib/mini_graphite"
6
+
7
+ class RoutesReporter < Minitest::Test
8
+ def setup
9
+ @app = Proc.new do |env|
10
+ ['200', {'Content-Type' => 'text/html'}, ['Test App']]
11
+ end
12
+
13
+ routes_block = Proc.new do
14
+ set_graphite_key "app.test_app.test.routes.active_surveys", /active_surveys\/this_should_be_a_password/
15
+ set_graphite_key "app.test_app.test.routes.get_surveys", /\:offer_click_id\/\:panel_user_id\/\:panel_user_id_kind/
16
+ set_graphite_key "app.test_app.test.routes.error", /error/
17
+ set_graphite_key "app.test_app.test.routes.homepage", /\/$/
18
+ end
19
+
20
+ @graphite_routes_reporter = Dalia::MiniGraphite::RoutesReporter.new(@app, &routes_block)
21
+ @request = Rack::MockRequest.new(@graphite_routes_reporter)
22
+ end
23
+
24
+ def test_send_routes_metrics_to_graphite
25
+ Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.homepage.duration", instance_of(Float))
26
+ Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.homepage.count")
27
+ @request.get("/", {"sinatra.route" => "/"})
28
+
29
+ Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.get_surveys.duration", instance_of(Float))
30
+ Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.get_surveys.count")
31
+ @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
+
33
+ Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.error.duration", instance_of(Float))
34
+ Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.error.count")
35
+ @request.get("/error", {"sinatra.route" => "/error"})
36
+
37
+ Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.active_surveys.duration", instance_of(Float))
38
+ Dalia::MiniGraphite.expects(:counter).with("app.test_app.test.routes.active_surveys.count")
39
+ @request.get("/active_surveys/this_should_be_a_password", {"sinatra.route" => "/active_surveys/this_should_be_a_password" })
40
+
41
+ Dalia::MiniGraphite.expects(:counter).never
42
+ @request.get("/ANOTHER_URL")
43
+ end
44
+ end
metadata CHANGED
@@ -1,69 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_graphite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sevastianos Komianos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-09 00:00:00.000000000 Z
11
+ date: 2017-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: 1.3.5
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.3.5
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: mocha
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  description: Simple wrapper for Graphite and Statsd
@@ -73,8 +87,8 @@ executables: []
73
87
  extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
76
- - .gitignore
77
- - .ruby-version
90
+ - ".gitignore"
91
+ - ".ruby-version"
78
92
  - Gemfile
79
93
  - LICENSE.txt
80
94
  - README.md
@@ -82,10 +96,12 @@ files:
82
96
  - ci_script.sh
83
97
  - lib/mini_graphite.rb
84
98
  - lib/mini_graphite/logger.rb
99
+ - lib/mini_graphite/routes_reporter.rb
85
100
  - lib/mini_graphite/version.rb
86
101
  - minigraphite.gemspec
87
102
  - test/logger_test.rb
88
103
  - test/minigraphite_test.rb
104
+ - test/routes_reporter_test.rb
89
105
  homepage: https://github.com/DaliaResearch/MiniGraphite
90
106
  licenses:
91
107
  - MIT
@@ -96,20 +112,21 @@ require_paths:
96
112
  - lib
97
113
  required_ruby_version: !ruby/object:Gem::Requirement
98
114
  requirements:
99
- - - '>='
115
+ - - ">="
100
116
  - !ruby/object:Gem::Version
101
117
  version: '0'
102
118
  required_rubygems_version: !ruby/object:Gem::Requirement
103
119
  requirements:
104
- - - '>='
120
+ - - ">="
105
121
  - !ruby/object:Gem::Version
106
122
  version: '0'
107
123
  requirements: []
108
124
  rubyforge_project:
109
- rubygems_version: 2.0.3
125
+ rubygems_version: 2.5.1
110
126
  signing_key:
111
127
  specification_version: 4
112
128
  summary: Simple wrapper for Graphite and Statsd
113
129
  test_files:
114
130
  - test/logger_test.rb
115
131
  - test/minigraphite_test.rb
132
+ - test/routes_reporter_test.rb