promenade 0.2.1 → 0.5.0

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
  SHA256:
3
- metadata.gz: 7705979e8349af87c849139112e61e3a50cb09264e672363d6fdb5d1282da328
4
- data.tar.gz: 2d849d1877183ab91baa10195d2035abffc3619692ca62b7285af5d03cab9ca1
3
+ metadata.gz: 21a119e5d3d87a758957487577e0167dfff5f3d9bf04938b9c2b257f91d46c7c
4
+ data.tar.gz: 37698954cc28bdfca381663448821c190bfb5e5d4f67373780668c45e90669e5
5
5
  SHA512:
6
- metadata.gz: 72e7318b7db15017fd6882889abff0e194ba32a996dda10da4939f3c9de377c9f2c37903926fcb72066a1f311da662d5bd40f64aff0985415839abc114b876b0
7
- data.tar.gz: 7f71477ab1237c414dd79f02dee1c6995ffd3afbdbfb25f3d9ec0877b7eec727108aa0425e1297759809f0e3a6a9817567460cdd6fe540977e90e5e5c7803657
6
+ metadata.gz: 9fc56c5ea2851e9dfaeae6e56ef63795cab6e2acbd11eed8ecc0e70b9f8198186f92ca74d37afaa92bba579db826b06820fad7b78ee2dcd756aa019d8dd7967f
7
+ data.tar.gz: dd5269ee49251537374407c60cf9f31a00d3fcb6ffcfa7c0c0bfa9e8b76d23ca29c868d25ff5732a815e9adef193d73d4da626dd3f223f90d61ae31b640a9340
@@ -0,0 +1,20 @@
1
+ name: CI
2
+ on:
3
+ push:
4
+ branches:
5
+ - master
6
+ pull_request:
7
+ jobs:
8
+ test:
9
+ strategy:
10
+ fail-fast: false
11
+ matrix:
12
+ ruby: ['2.7', '3.0', "3.1"]
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - uses: ruby/setup-ruby@v1
17
+ with:
18
+ ruby-version: ${{ matrix.ruby }}
19
+ bundler-cache: true
20
+ - run: bundle exec rake
data/.gitignore CHANGED
@@ -1,13 +1,16 @@
1
+ .rspec_status
2
+ .byebug_history
3
+ .rubocop-https*yml
1
4
  /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
5
  /coverage/
5
6
  /doc/
7
+ /log/
6
8
  /pkg/
9
+ /.sass-cache/
7
10
  /spec/reports/
11
+ /spec/dummy/log
12
+ /spec/dummy/tmp
8
13
  /tmp/
9
- .rubocop-https*yml
10
- .sass-cache/
11
-
12
- # rspec failure tracking
13
- .rspec_status
14
+ /vendor/
15
+ /.yardoc
16
+ /_yardoc/
data/.rubocop.yml CHANGED
@@ -2,9 +2,17 @@ inherit_from:
2
2
  - https://raw.githubusercontent.com/cookpad/guides/master/.rubocop.yml
3
3
 
4
4
  AllCops:
5
- TargetRubyVersion: 2.4
5
+ TargetRubyVersion: 2.7
6
+ NewCops: enable
7
+ Exclude:
8
+ - vendor/**/*
9
+ - vendor/**/.*
6
10
 
7
11
  Style/TrailingCommaInHashLiteral:
8
12
  EnforcedStyleForMultiline: comma
9
13
  Style/TrailingCommaInArguments:
10
14
  EnforcedStyleForMultiline: comma
15
+ Rails/RakeEnvironment:
16
+ Enabled: false
17
+ Rails/EnvironmentVariableAccess:
18
+ Enabled: false
data/Gemfile CHANGED
@@ -4,3 +4,4 @@ source "https://rubygems.org"
4
4
  gemspec
5
5
 
6
6
  gem "codecov"
7
+ gem "webrick"
data/Gemfile.lock CHANGED
@@ -1,124 +1,269 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- promenade (0.2.1)
5
- activesupport
6
- prometheus-client-mmap (~> 0.9.3)
4
+ promenade (0.5.0)
5
+ actionpack
6
+ activesupport (> 6.0, < 8.0)
7
+ prometheus-client-mmap (~> 0.16.0)
7
8
  rack
8
9
 
9
10
  GEM
10
11
  remote: https://rubygems.org/
11
12
  specs:
12
- activesupport (6.0.0)
13
+ actioncable (7.0.3)
14
+ actionpack (= 7.0.3)
15
+ activesupport (= 7.0.3)
16
+ nio4r (~> 2.0)
17
+ websocket-driver (>= 0.6.1)
18
+ actionmailbox (7.0.3)
19
+ actionpack (= 7.0.3)
20
+ activejob (= 7.0.3)
21
+ activerecord (= 7.0.3)
22
+ activestorage (= 7.0.3)
23
+ activesupport (= 7.0.3)
24
+ mail (>= 2.7.1)
25
+ net-imap
26
+ net-pop
27
+ net-smtp
28
+ actionmailer (7.0.3)
29
+ actionpack (= 7.0.3)
30
+ actionview (= 7.0.3)
31
+ activejob (= 7.0.3)
32
+ activesupport (= 7.0.3)
33
+ mail (~> 2.5, >= 2.5.4)
34
+ net-imap
35
+ net-pop
36
+ net-smtp
37
+ rails-dom-testing (~> 2.0)
38
+ actionpack (7.0.3)
39
+ actionview (= 7.0.3)
40
+ activesupport (= 7.0.3)
41
+ rack (~> 2.0, >= 2.2.0)
42
+ rack-test (>= 0.6.3)
43
+ rails-dom-testing (~> 2.0)
44
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
45
+ actiontext (7.0.3)
46
+ actionpack (= 7.0.3)
47
+ activerecord (= 7.0.3)
48
+ activestorage (= 7.0.3)
49
+ activesupport (= 7.0.3)
50
+ globalid (>= 0.6.0)
51
+ nokogiri (>= 1.8.5)
52
+ actionview (7.0.3)
53
+ activesupport (= 7.0.3)
54
+ builder (~> 3.1)
55
+ erubi (~> 1.4)
56
+ rails-dom-testing (~> 2.0)
57
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
58
+ activejob (7.0.3)
59
+ activesupport (= 7.0.3)
60
+ globalid (>= 0.3.6)
61
+ activemodel (7.0.3)
62
+ activesupport (= 7.0.3)
63
+ activerecord (7.0.3)
64
+ activemodel (= 7.0.3)
65
+ activesupport (= 7.0.3)
66
+ activestorage (7.0.3)
67
+ actionpack (= 7.0.3)
68
+ activejob (= 7.0.3)
69
+ activerecord (= 7.0.3)
70
+ activesupport (= 7.0.3)
71
+ marcel (~> 1.0)
72
+ mini_mime (>= 1.1.0)
73
+ activesupport (7.0.3)
13
74
  concurrent-ruby (~> 1.0, >= 1.0.2)
14
- i18n (>= 0.7, < 2)
15
- minitest (~> 5.1)
16
- tzinfo (~> 1.1)
17
- zeitwerk (~> 2.1, >= 2.1.8)
18
- ast (2.4.0)
19
- backports (3.15.0)
20
- binding_of_caller (0.8.0)
75
+ i18n (>= 1.6, < 2)
76
+ minitest (>= 5.1)
77
+ tzinfo (~> 2.0)
78
+ ast (2.4.2)
79
+ backports (3.23.0)
80
+ binding_of_caller (1.0.0)
21
81
  debug_inspector (>= 0.0.1)
22
- climate_control (0.2.0)
23
- codecov (0.1.14)
24
- json
25
- simplecov
26
- url
27
- coderay (1.1.2)
28
- concurrent-ruby (1.1.5)
29
- debug_inspector (0.0.3)
30
- deep-cover (0.7.5)
31
- deep-cover-core (= 0.7.5)
82
+ builder (3.2.4)
83
+ byebug (11.1.3)
84
+ climate_control (1.1.1)
85
+ codecov (0.6.0)
86
+ simplecov (>= 0.15, < 0.22)
87
+ coderay (1.1.3)
88
+ concurrent-ruby (1.1.10)
89
+ crass (1.0.6)
90
+ debug_inspector (1.1.0)
91
+ deep-cover (1.1.0)
92
+ deep-cover-core (= 1.1.0)
32
93
  highline
33
94
  thor (>= 0.20.3)
34
95
  with_progress
35
- deep-cover-core (0.7.5)
96
+ deep-cover-core (1.1.0)
36
97
  backports (>= 3.11.0)
37
98
  binding_of_caller
38
- parser (>= 2.5, < 2.7)
99
+ parser (>= 2.5)
39
100
  pry
40
101
  term-ansicolor
41
102
  terminal-table
42
- diff-lcs (1.3)
43
- docile (1.3.2)
44
- highline (2.0.2)
45
- i18n (1.6.0)
103
+ diff-lcs (1.5.0)
104
+ digest (3.1.0)
105
+ docile (1.4.0)
106
+ erubi (1.10.0)
107
+ globalid (1.0.0)
108
+ activesupport (>= 5.0)
109
+ highline (2.0.3)
110
+ i18n (1.10.0)
46
111
  concurrent-ruby (~> 1.0)
47
- jaro_winkler (1.5.3)
48
- json (2.2.0)
49
- method_source (0.9.2)
50
- minitest (5.12.0)
51
- parallel (1.17.0)
52
- parser (2.6.4.1)
53
- ast (~> 2.4.0)
54
- prometheus-client-mmap (0.9.10)
55
- pry (0.12.2)
56
- coderay (~> 1.1.0)
57
- method_source (~> 0.9.0)
58
- rack (2.0.7)
59
- rainbow (3.0.0)
60
- rake (10.5.0)
61
- rspec (3.8.0)
62
- rspec-core (~> 3.8.0)
63
- rspec-expectations (~> 3.8.0)
64
- rspec-mocks (~> 3.8.0)
65
- rspec-core (3.8.2)
66
- rspec-support (~> 3.8.0)
67
- rspec-expectations (3.8.4)
112
+ loofah (2.18.0)
113
+ crass (~> 1.0.2)
114
+ nokogiri (>= 1.5.9)
115
+ mail (2.7.1)
116
+ mini_mime (>= 0.1.1)
117
+ marcel (1.0.2)
118
+ method_source (1.0.0)
119
+ mini_mime (1.1.2)
120
+ mini_portile2 (2.8.0)
121
+ minitest (5.16.0)
122
+ net-imap (0.2.3)
123
+ digest
124
+ net-protocol
125
+ strscan
126
+ net-pop (0.1.1)
127
+ digest
128
+ net-protocol
129
+ timeout
130
+ net-protocol (0.1.3)
131
+ timeout
132
+ net-smtp (0.3.1)
133
+ digest
134
+ net-protocol
135
+ timeout
136
+ nio4r (2.5.8)
137
+ nokogiri (1.13.6)
138
+ mini_portile2 (~> 2.8.0)
139
+ racc (~> 1.4)
140
+ parallel (1.22.1)
141
+ parser (3.1.2.0)
142
+ ast (~> 2.4.1)
143
+ prometheus-client-mmap (0.16.0)
144
+ pry (0.14.1)
145
+ coderay (~> 1.1)
146
+ method_source (~> 1.0)
147
+ racc (1.6.0)
148
+ rack (2.2.3.1)
149
+ rack-test (1.1.0)
150
+ rack (>= 1.0, < 3)
151
+ rails (7.0.3)
152
+ actioncable (= 7.0.3)
153
+ actionmailbox (= 7.0.3)
154
+ actionmailer (= 7.0.3)
155
+ actionpack (= 7.0.3)
156
+ actiontext (= 7.0.3)
157
+ actionview (= 7.0.3)
158
+ activejob (= 7.0.3)
159
+ activemodel (= 7.0.3)
160
+ activerecord (= 7.0.3)
161
+ activestorage (= 7.0.3)
162
+ activesupport (= 7.0.3)
163
+ bundler (>= 1.15.0)
164
+ railties (= 7.0.3)
165
+ rails-dom-testing (2.0.3)
166
+ activesupport (>= 4.2.0)
167
+ nokogiri (>= 1.6)
168
+ rails-html-sanitizer (1.4.3)
169
+ loofah (~> 2.3)
170
+ railties (7.0.3)
171
+ actionpack (= 7.0.3)
172
+ activesupport (= 7.0.3)
173
+ method_source
174
+ rake (>= 12.2)
175
+ thor (~> 1.0)
176
+ zeitwerk (~> 2.5)
177
+ rainbow (3.1.1)
178
+ rake (13.0.6)
179
+ regexp_parser (2.5.0)
180
+ rexml (3.2.5)
181
+ rspec (3.11.0)
182
+ rspec-core (~> 3.11.0)
183
+ rspec-expectations (~> 3.11.0)
184
+ rspec-mocks (~> 3.11.0)
185
+ rspec-core (3.11.0)
186
+ rspec-support (~> 3.11.0)
187
+ rspec-expectations (3.11.0)
68
188
  diff-lcs (>= 1.2.0, < 2.0)
69
- rspec-support (~> 3.8.0)
70
- rspec-mocks (3.8.1)
189
+ rspec-support (~> 3.11.0)
190
+ rspec-mocks (3.11.1)
71
191
  diff-lcs (>= 1.2.0, < 2.0)
72
- rspec-support (~> 3.8.0)
73
- rspec-support (3.8.2)
74
- rubocop (0.74.0)
75
- jaro_winkler (~> 1.5.1)
192
+ rspec-support (~> 3.11.0)
193
+ rspec-rails (5.1.2)
194
+ actionpack (>= 5.2)
195
+ activesupport (>= 5.2)
196
+ railties (>= 5.2)
197
+ rspec-core (~> 3.10)
198
+ rspec-expectations (~> 3.10)
199
+ rspec-mocks (~> 3.10)
200
+ rspec-support (~> 3.10)
201
+ rspec-support (3.11.0)
202
+ rubocop (1.30.1)
76
203
  parallel (~> 1.10)
77
- parser (>= 2.6)
204
+ parser (>= 3.1.0.0)
78
205
  rainbow (>= 2.2.2, < 4.0)
206
+ regexp_parser (>= 1.8, < 3.0)
207
+ rexml (>= 3.2.5, < 4.0)
208
+ rubocop-ast (>= 1.18.0, < 2.0)
79
209
  ruby-progressbar (~> 1.7)
80
- unicode-display_width (>= 1.4.0, < 1.7)
81
- rubocop-performance (1.4.1)
82
- rubocop (>= 0.71.0)
83
- rubocop-rails (2.3.2)
210
+ unicode-display_width (>= 1.4.0, < 3.0)
211
+ rubocop-ast (1.18.0)
212
+ parser (>= 3.1.1.0)
213
+ rubocop-performance (1.14.2)
214
+ rubocop (>= 1.7.0, < 2.0)
215
+ rubocop-ast (>= 0.4.0)
216
+ rubocop-rails (2.15.0)
217
+ activesupport (>= 4.2.0)
84
218
  rack (>= 1.1)
85
- rubocop (>= 0.72.0)
86
- ruby-progressbar (1.10.1)
87
- simplecov (0.17.1)
219
+ rubocop (>= 1.7.0, < 2.0)
220
+ ruby-progressbar (1.11.0)
221
+ simplecov (0.21.2)
88
222
  docile (~> 1.1)
89
- json (>= 1.8, < 3)
90
- simplecov-html (~> 0.10.0)
91
- simplecov-html (0.10.2)
223
+ simplecov-html (~> 0.11)
224
+ simplecov_json_formatter (~> 0.1)
225
+ simplecov-html (0.12.3)
226
+ simplecov_json_formatter (0.1.4)
227
+ strscan (3.0.3)
228
+ sync (0.5.0)
92
229
  term-ansicolor (1.7.1)
93
230
  tins (~> 1.0)
94
- terminal-table (1.8.0)
95
- unicode-display_width (~> 1.1, >= 1.1.1)
96
- thor (0.20.3)
97
- thread_safe (0.3.6)
98
- tins (1.21.1)
99
- tzinfo (1.2.5)
100
- thread_safe (~> 0.1)
101
- unicode-display_width (1.6.0)
102
- url (0.3.2)
231
+ terminal-table (3.0.2)
232
+ unicode-display_width (>= 1.1.1, < 3)
233
+ thor (1.2.1)
234
+ timeout (0.3.0)
235
+ tins (1.31.1)
236
+ sync
237
+ tzinfo (2.0.4)
238
+ concurrent-ruby (~> 1.0)
239
+ unicode-display_width (2.1.0)
240
+ webrick (1.7.0)
241
+ websocket-driver (0.7.5)
242
+ websocket-extensions (>= 0.1.0)
243
+ websocket-extensions (0.1.5)
103
244
  with_progress (1.0.1)
104
245
  ruby-progressbar (~> 1.4)
105
- zeitwerk (2.1.10)
246
+ zeitwerk (2.6.0)
106
247
 
107
248
  PLATFORMS
108
249
  ruby
109
250
 
110
251
  DEPENDENCIES
111
252
  bundler (~> 2.0)
253
+ byebug
112
254
  climate_control
113
255
  codecov
114
256
  deep-cover
115
257
  promenade!
116
- rake (~> 10.0)
117
- rspec (~> 3.0)
258
+ rails (> 3.0, < 8.0)
259
+ rake
260
+ rspec (~> 3.11)
261
+ rspec-rails (~> 5.1)
118
262
  rubocop
119
263
  rubocop-performance
120
264
  rubocop-rails
121
265
  simplecov
266
+ webrick
122
267
 
123
268
  BUNDLED WITH
124
- 2.0.2
269
+ 2.3.16
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # Promenade
2
2
 
3
- [![Build Status](https://travis-ci.org/errm/promenade.svg?branch=master)](https://travis-ci.org/errm/promenade)
3
+ [![CI](https://github.com/errm/promenade/actions/workflows/ci.yaml/badge.svg)](https://github.com/errm/promenade/actions/workflows/ci.yaml)
4
4
  [![Gem Version](https://badge.fury.io/rb/promenade.svg)](https://badge.fury.io/rb/promenade)
5
- [![codecov](https://codecov.io/gh/errm/promenade/branch/master/graph/badge.svg)](https://codecov.io/gh/errm/promenade)
6
5
 
7
6
  Promenade is a library to simplify instrumenting Ruby applications with Prometheus.
8
7
 
@@ -130,6 +129,58 @@ This is ideal if you are worried about accidentally exposing your metrics, are c
130
129
 
131
130
  The exporter runs by default on port `9394` and the metrics are available at the standard path of `/metrics`, the stand-alone exporter is configured to use gzip.
132
131
 
132
+
133
+ ### Rails Middleware
134
+
135
+ Promenade provides custom Rack middleware to track HTTP response times for requests in your Rails application.
136
+
137
+ This was originally inspired by [prometheus-client-mmap](https://gitlab.com/gitlab-org/prometheus-client-mmap/-/blob/master/lib/prometheus/client/rack/collector.rb).
138
+
139
+ **This middleware is automatically added to your Rack stack if your application is a Ruby on Rails app.**
140
+
141
+ We recommend you add the middleware after `ActionDispatch::ShowExceptions` in your stack, so you can accurately record the controller and action where an exception was raised.
142
+
143
+ If you want to change the position, or customise the labels and exception handling behaviour, simply remove the middleware from the stack and re-insert it with your own preferences.
144
+
145
+ ``` ruby
146
+ Rails.application.middleware.delete(Promenade::Client::Rack::Collector)
147
+ Rails.application.middleware.insert_after(Rails::Rack::Logger, Promenade::Client::Rack::Collector)
148
+ ```
149
+
150
+ #### Customising the labels recorded for each request
151
+
152
+ If you would like to collect different labels with each request, you may do so by customising the middleware installation:
153
+
154
+ ``` ruby
155
+ label_builder = Proc.new do |env|
156
+ {
157
+ method: env["REQUEST_METHOD"].to_s.downcase,
158
+ host: env["HTTP_HOST"].to_s,
159
+ controller: env.dig("action_dispatch.request.parameters", "controller") || "unknown",
160
+ action: env.dig("action_dispatch.request.parameters", "action") || "unknown"
161
+ }
162
+ end
163
+ Rails.application.config.middleware.insert_after ActionDispatch::ShowExceptions,
164
+ Promenade::Client::Rack::Collector
165
+ label_builder: label_builder
166
+ ```
167
+
168
+ #### Customising how the middleware handles exceptions
169
+
170
+ The default implementation will capture exceptions, count the execption class name (e.g. `"StandardError"`), and then re-raise the exception.
171
+
172
+ If you would like to customise this behaviour, you may do so by customising the middleware installation:
173
+
174
+ ``` ruby
175
+ exception_handler = Proc.new do |exception, exception_counter, env_hash, request_duration_seconds|
176
+ # This simple example just re-raises the execption
177
+ raise exception
178
+ end
179
+ Rails.application.config.middleware.insert_after ActionDispatch::ShowExceptions,
180
+ Promenade::Client::Rack::Collector
181
+ exception_handler: exception_handler
182
+ ```
183
+
133
184
  ### Configuration
134
185
 
135
186
  If you are using rails it should load a railtie and configure promenade.
data/Rakefile CHANGED
@@ -5,12 +5,16 @@ require "rubocop/rake_task"
5
5
  RSpec::Core::RakeTask.new(:spec)
6
6
  RuboCop::RakeTask.new
7
7
 
8
- task default: %i(spec rubocop)
8
+ task default: %i(spec rubocop integration)
9
9
 
10
10
  task :clean do
11
11
  sh "rm -rf tmp/promenade"
12
12
  end
13
13
 
14
+ task integration: :clean do
15
+ sh "bin/integration_test"
16
+ end
17
+
14
18
  task spec: :clean
15
19
 
16
- task release: :spec
20
+ task release: :default
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "promenade"
5
+ require "fileutils"
6
+ require "net/http"
7
+ require "./spec/support/integration_tests/metrics_line"
8
+ require "./spec/support/integration_tests/label_value"
9
+
10
+ def test_http_body(expected)
11
+ expectation = MetricsLine.new(expected)
12
+ uri = URI("http://localhost:9394/metrics")
13
+
14
+ Net::HTTP.start(uri.host, uri.port) do |http|
15
+ request = Net::HTTP::Get.new uri
16
+ response = http.request request
17
+ unless response.body.each_line.detect do |string|
18
+ next if string.start_with?("#")
19
+
20
+ MetricsLine.new(string) == expectation
21
+ end
22
+ fail "#{response.body} didn't include #{expected}"
23
+ end
24
+ end
25
+ end
26
+
27
+ Promenade.setup
28
+
29
+ pid = Process.spawn "exe/promenade"
30
+ sleep 1
31
+
32
+ at_exit do
33
+ Process.kill "SIGTERM", pid
34
+ Process.waitpid(pid)
35
+ end
36
+
37
+ Promenade.counter :widgets_created do
38
+ doc "Records how many widgets are created"
39
+ end
40
+
41
+ Promenade.metric(:widgets_created).increment({ type: "guinness" })
42
+ test_http_body('widgets_created{type="guinness"} 1')
43
+
44
+ Promenade.metric(:widgets_created).increment({ type: "john-smiths" })
45
+ test_http_body('widgets_created{type="guinness"} 1')
46
+ test_http_body('widgets_created{type="john-smiths"} 1')
47
+
48
+ Promenade.histogram :calculator_time_taken do
49
+ doc "Records how long it takes to do the adding"
50
+ buckets [0.25, 0.5, 1, 2, 4]
51
+ end
52
+
53
+ Promenade.metric(:calculator_time_taken).observe({ operation: "addition" }, 0.5)
54
+ Promenade.metric(:calculator_time_taken).observe({ operation: "addition" }, 0.5)
55
+ Promenade.metric(:calculator_time_taken).observe({ operation: "addition" }, 1)
56
+
57
+ test_http_body('calculator_time_taken_bucket{operation="addition",le="0.25"} 0')
58
+ test_http_body('calculator_time_taken_bucket{operation="addition",le="0.5"} 2')
59
+ test_http_body('calculator_time_taken_bucket{operation="addition",le="1"} 3')
60
+ test_http_body('calculator_time_taken_bucket{operation="addition",le="1"} 3')
61
+
62
+ exit
data/bin/rails ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails gems
3
+ # installed from the root of your application.
4
+
5
+ ENGINE_ROOT = File.expand_path("..", __dir__)
6
+ ENGINE_PATH = File.expand_path("../lib/promenade/engine", __dir__)
7
+ APP_PATH = File.expand_path("../spec/dummy/config/application", __dir__)
8
+
9
+ # Set up gems listed in the Gemfile.
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
11
+ require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
12
+
13
+ require "rails/all"
14
+ require "rails/engine/commands"
@@ -0,0 +1,113 @@
1
+ require "prometheus/client"
2
+ require_relative "request_labeler"
3
+ require_relative "exception_handler"
4
+
5
+ module Promenade
6
+ module Client
7
+ module Rack
8
+ # Original code taken from Prometheus Client MMap
9
+ # https://gitlab.com/gitlab-org/prometheus-client-mmap/-/blob/master/lib/prometheus/client/rack/collector.rb
10
+ #
11
+ # Collector is a Rack middleware that provides a sample implementation of
12
+ # a HTTP tracer. The default label builder can be modified to export a
13
+ # different set of labels per recorded metric.
14
+ class Collector
15
+ REQUEST_METHOD = "REQUEST_METHOD".freeze
16
+
17
+ HTTP_HOST = "HTTP_HOST".freeze
18
+
19
+ PATH_INFO = "PATH_INFO".freeze
20
+
21
+ HISTOGRAM_NAME = :http_req_duration_seconds
22
+
23
+ REQUESTS_COUNTER_NAME = :http_requests_total
24
+
25
+ EXCEPTIONS_COUNTER_NAME = :http_exceptions_total
26
+
27
+ private_constant *%i(
28
+ REQUEST_METHOD
29
+ HTTP_HOST
30
+ PATH_INFO
31
+ HISTOGRAM_NAME
32
+ REQUESTS_COUNTER_NAME
33
+ EXCEPTIONS_COUNTER_NAME
34
+ )
35
+
36
+ def initialize(app,
37
+ registry: ::Prometheus::Client.registry,
38
+ label_builder: RequestLabeler,
39
+ exception_handler: nil)
40
+ @app = app
41
+ @registry = registry
42
+ @label_builder = label_builder
43
+ @latency_buckets = Promenade.configuration.rack_latency_buckets
44
+ @exception_handler = exception_handler || default_exception_handler
45
+ register_metrics!
46
+ end
47
+
48
+ def call(env)
49
+ trace(env) { app.call(env) }
50
+ end
51
+
52
+ private
53
+
54
+ attr_reader :app,
55
+ :registry,
56
+ :label_builder,
57
+ :latency_buckets,
58
+ :exception_handler
59
+
60
+ def trace(env)
61
+ start = current_time
62
+ begin
63
+ response = yield
64
+ record(labels(env, response), duration_since(start))
65
+ response
66
+ rescue StandardError => e
67
+ exception_handler.call(e, env, duration_since(start))
68
+ end
69
+ end
70
+
71
+ def labels(env, response)
72
+ label_builder.call(env).merge!(code: response.first.to_s)
73
+ end
74
+
75
+ def record(labels, duration)
76
+ requests_counter.increment(labels)
77
+ durations_histogram.observe(labels, duration)
78
+ end
79
+
80
+ def duration_since(start_time)
81
+ current_time - start_time
82
+ end
83
+
84
+ def current_time
85
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
86
+ end
87
+
88
+ def durations_histogram
89
+ registry.get(HISTOGRAM_NAME)
90
+ end
91
+
92
+ def requests_counter
93
+ registry.get(REQUESTS_COUNTER_NAME)
94
+ end
95
+
96
+ def register_metrics!
97
+ registry.counter(REQUESTS_COUNTER_NAME, "A counter of the total number of HTTP requests made.")
98
+ registry.histogram(HISTOGRAM_NAME, "A histogram of the response latency.", {}, latency_buckets)
99
+ registry.counter(EXCEPTIONS_COUNTER_NAME, "A counter of the total number of exceptions raised.")
100
+ end
101
+
102
+ def default_exception_handler
103
+ ExceptionHandler.initialize_singleton(
104
+ histogram_name: HISTOGRAM_NAME,
105
+ requests_counter_name: REQUESTS_COUNTER_NAME,
106
+ exceptions_counter_name: EXCEPTIONS_COUNTER_NAME,
107
+ registry: registry,
108
+ )
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,51 @@
1
+ require "action_dispatch/middleware/exception_wrapper"
2
+ require_relative "singleton_caller"
3
+ require_relative "request_labeler"
4
+
5
+ module Promenade
6
+ module Client
7
+ module Rack
8
+ class ExceptionHandler
9
+ extend SingletonCaller
10
+
11
+ attr_reader :histogram_name, :requests_counter_name, :exceptions_counter_name, :registry
12
+
13
+ def initialize(histogram_name:, requests_counter_name:, exceptions_counter_name:, registry:)
14
+ @histogram_name = histogram_name
15
+ @requests_counter_name = requests_counter_name
16
+ @exceptions_counter_name = exceptions_counter_name
17
+ @registry = registry
18
+ end
19
+
20
+ def call(exception, env_hash, duration)
21
+ labels = RequestLabeler.call(env_hash)
22
+ labels.merge!(code: status_code_for_exception(exception))
23
+
24
+ histogram.observe(labels, duration.to_f)
25
+ requests_counter.increment(labels)
26
+ exceptions_counter.increment(exception: exception.class.name)
27
+
28
+ raise exception
29
+ end
30
+
31
+ private
32
+
33
+ def histogram
34
+ registry.get(histogram_name)
35
+ end
36
+
37
+ def requests_counter
38
+ registry.get(requests_counter_name)
39
+ end
40
+
41
+ def exceptions_counter
42
+ registry.get(exceptions_counter_name)
43
+ end
44
+
45
+ def status_code_for_exception(exception)
46
+ ActionDispatch::ExceptionWrapper.new(nil, exception).status_code.to_s
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,50 @@
1
+ module Promenade
2
+ module Client
3
+ module Rack
4
+ class RequestLabeler
5
+ require_relative "singleton_caller"
6
+ extend SingletonCaller
7
+
8
+ REQUEST_METHOD = "REQUEST_METHOD".freeze
9
+
10
+ HTTP_HOST = "HTTP_HOST".freeze
11
+
12
+ PARAMS_KEY = "action_dispatch.request.parameters".freeze
13
+
14
+ PATH_PARAMS_KEY = "action_dispatch.request.path_parameters".freeze
15
+
16
+ CONTROLLER = "controller".freeze
17
+
18
+ ACTION = "action".freeze
19
+
20
+ UNKNOWN = "unknown".freeze
21
+
22
+ SEPARATOR = "#".freeze
23
+
24
+ private_constant :REQUEST_METHOD, :HTTP_HOST, :PARAMS_KEY, :CONTROLLER, :ACTION, :UNKNOWN, :SEPARATOR
25
+
26
+ def call(env)
27
+ {
28
+ method: env[REQUEST_METHOD].to_s.downcase,
29
+ host: env[HTTP_HOST].to_s,
30
+ controller_action: controller_action_from_env(env),
31
+ }
32
+ end
33
+
34
+ private
35
+
36
+ def controller_action_from_env(env)
37
+ controller = env.dig(PARAMS_KEY, CONTROLLER) ||
38
+ env.dig(PATH_PARAMS_KEY, CONTROLLER.to_sym) ||
39
+ UNKNOWN
40
+
41
+ action = env.dig(PARAMS_KEY, ACTION) ||
42
+ env.dig(PATH_PARAMS_KEY, ACTION.to_sym) ||
43
+ UNKNOWN
44
+
45
+ [controller, action].join(SEPARATOR)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,19 @@
1
+ module Promenade
2
+ module Client
3
+ module Rack
4
+ module SingletonCaller
5
+ def initialize_singleton(...)
6
+ @singleton = new(...)
7
+ end
8
+
9
+ def call(...)
10
+ singleton.call(...)
11
+ end
12
+
13
+ def singleton
14
+ @singleton || initialize_singleton
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ module Promenade
2
+ class Configuration
3
+ attr_accessor :rack_latency_buckets
4
+
5
+ DEFAULT_RACK_LATENCY_BUCKETS = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10].freeze
6
+
7
+ def initialize
8
+ @rack_latency_buckets = DEFAULT_RACK_LATENCY_BUCKETS
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Promenade
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Promenade
4
+ end
5
+ end
@@ -90,7 +90,7 @@ module Promenade
90
90
  Promenade.metric(:kafka_consumer_time_lag).set(labels, time_lag) if time_lag
91
91
  end
92
92
 
93
- def process_batch(event) # rubocop:disable Metrics/AbcSize
93
+ def process_batch(event)
94
94
  labels = get_labels(event)
95
95
  offset_lag = event.payload.fetch(:offset_lag)
96
96
  messages = event.payload.fetch(:message_count)
@@ -35,7 +35,7 @@ module Promenade
35
35
  class Options
36
36
  BUCKET_PRESETS = {
37
37
  network: [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10].freeze,
38
- memory: (0..10).map { |i| 128 * 2**i },
38
+ memory: (0..10).map { |i| 128 * (2**i) },
39
39
  }.freeze
40
40
 
41
41
  def initialize
@@ -1,9 +1,13 @@
1
1
  require "promenade/setup"
2
+ require "promenade/engine"
3
+ require "promenade/client/rack/collector"
2
4
 
3
5
  module Promenade
4
6
  class Railtie < ::Rails::Railtie
5
7
  initializer "promenade.configure_rails_initialization" do
6
8
  Promenade.setup
9
+ Rails.application.config.middleware.insert_after ActionDispatch::ShowExceptions,
10
+ Promenade::Client::Rack::Collector
7
11
  end
8
12
  end
9
13
  end
@@ -1,16 +1,25 @@
1
1
  require "pathname"
2
2
 
3
3
  module Promenade
4
- def self.root_dir
5
- rails_root = defined?(Rails) && Rails.root
6
- rails_root || Pathname.new(ENV.fetch("RAILS_ROOT", Dir.pwd))
4
+ module_function
5
+
6
+ def root_dir
7
+ if rails_defined?
8
+ Rails.root
9
+ else
10
+ Pathname.new(ENV.fetch("RAILS_ROOT", Dir.pwd))
11
+ end
12
+ end
13
+
14
+ def rails_defined?
15
+ defined?(Rails)
7
16
  end
8
17
 
9
- def self.multiprocess_files_dir
18
+ def multiprocess_files_dir
10
19
  ENV.fetch("PROMETHEUS_MULTIPROC_DIR", root_dir.join("tmp", "promenade"))
11
20
  end
12
21
 
13
- def self.setup
22
+ def setup
14
23
  unless File.directory? multiprocess_files_dir
15
24
  FileUtils.mkdir_p multiprocess_files_dir
16
25
  end
@@ -1,3 +1,3 @@
1
1
  module Promenade
2
- VERSION = "0.2.1".freeze
2
+ VERSION = "0.5.0".freeze
3
3
  end
data/lib/promenade.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "promenade/version"
2
2
  require "promenade/setup"
3
+ require "promenade/configuration"
3
4
  require "promenade/railtie" if defined? ::Rails::Railtie
4
5
  require "promenade/prometheus"
5
6
 
@@ -14,5 +15,13 @@ module Promenade
14
15
  def metric(name)
15
16
  Promenade::Prometheus.metric(name)
16
17
  end
18
+
19
+ def configuration
20
+ @_configuration ||= Configuration.new
21
+ end
22
+
23
+ def configure
24
+ yield(configuration)
25
+ end
17
26
  end
18
27
  end
data/promenade.gemspec CHANGED
@@ -2,7 +2,7 @@ lib = File.expand_path("lib", __dir__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
  require "promenade/version"
4
4
 
5
- Gem::Specification.new do |spec|
5
+ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
6
6
  spec.name = "promenade"
7
7
  spec.version = Promenade::VERSION
8
8
  spec.authors = ["Ed Robinson"]
@@ -22,17 +22,23 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ["lib"]
24
24
 
25
- spec.add_dependency "activesupport"
26
- spec.add_dependency "prometheus-client-mmap", "~> 0.9.3"
27
- spec.add_dependency "rack"
25
+ spec.required_ruby_version = ">= 2.7", "< 3.2"
28
26
 
27
+ spec.add_dependency "actionpack"
28
+ spec.add_dependency "activesupport", "> 6.0", "< 8.0"
29
+ spec.add_dependency "prometheus-client-mmap", "~> 0.16.0"
30
+ spec.add_dependency "rack"
29
31
  spec.add_development_dependency "bundler", "~> 2.0"
32
+ spec.add_development_dependency "byebug"
30
33
  spec.add_development_dependency "climate_control"
31
34
  spec.add_development_dependency "deep-cover"
32
- spec.add_development_dependency "rake", "~> 10.0"
33
- spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "rails", "> 3.0", "< 8.0"
36
+ spec.add_development_dependency "rake"
37
+ spec.add_development_dependency "rspec", "~> 3.11"
38
+ spec.add_development_dependency "rspec-rails", "~> 5.1"
34
39
  spec.add_development_dependency "rubocop"
35
40
  spec.add_development_dependency "rubocop-performance"
36
41
  spec.add_development_dependency "rubocop-rails"
37
42
  spec.add_development_dependency "simplecov"
43
+ spec.metadata["rubygems_mfa_required"] = "true"
38
44
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: promenade
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ed Robinson
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-24 00:00:00.000000000 Z
11
+ date: 2022-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activesupport
14
+ name: actionpack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -24,20 +24,40 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '8.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">"
42
+ - !ruby/object:Gem::Version
43
+ version: '6.0'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '8.0'
27
47
  - !ruby/object:Gem::Dependency
28
48
  name: prometheus-client-mmap
29
49
  requirement: !ruby/object:Gem::Requirement
30
50
  requirements:
31
51
  - - "~>"
32
52
  - !ruby/object:Gem::Version
33
- version: 0.9.3
53
+ version: 0.16.0
34
54
  type: :runtime
35
55
  prerelease: false
36
56
  version_requirements: !ruby/object:Gem::Requirement
37
57
  requirements:
38
58
  - - "~>"
39
59
  - !ruby/object:Gem::Version
40
- version: 0.9.3
60
+ version: 0.16.0
41
61
  - !ruby/object:Gem::Dependency
42
62
  name: rack
43
63
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +86,20 @@ dependencies:
66
86
  - - "~>"
67
87
  - !ruby/object:Gem::Version
68
88
  version: '2.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: byebug
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
69
103
  - !ruby/object:Gem::Dependency
70
104
  name: climate_control
71
105
  requirement: !ruby/object:Gem::Requirement
@@ -94,34 +128,68 @@ dependencies:
94
128
  - - ">="
95
129
  - !ruby/object:Gem::Version
96
130
  version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: rails
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">"
136
+ - !ruby/object:Gem::Version
137
+ version: '3.0'
138
+ - - "<"
139
+ - !ruby/object:Gem::Version
140
+ version: '8.0'
141
+ type: :development
142
+ prerelease: false
143
+ version_requirements: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">"
146
+ - !ruby/object:Gem::Version
147
+ version: '3.0'
148
+ - - "<"
149
+ - !ruby/object:Gem::Version
150
+ version: '8.0'
97
151
  - !ruby/object:Gem::Dependency
98
152
  name: rake
153
+ requirement: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ type: :development
159
+ prerelease: false
160
+ version_requirements: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ - !ruby/object:Gem::Dependency
166
+ name: rspec
99
167
  requirement: !ruby/object:Gem::Requirement
100
168
  requirements:
101
169
  - - "~>"
102
170
  - !ruby/object:Gem::Version
103
- version: '10.0'
171
+ version: '3.11'
104
172
  type: :development
105
173
  prerelease: false
106
174
  version_requirements: !ruby/object:Gem::Requirement
107
175
  requirements:
108
176
  - - "~>"
109
177
  - !ruby/object:Gem::Version
110
- version: '10.0'
178
+ version: '3.11'
111
179
  - !ruby/object:Gem::Dependency
112
- name: rspec
180
+ name: rspec-rails
113
181
  requirement: !ruby/object:Gem::Requirement
114
182
  requirements:
115
183
  - - "~>"
116
184
  - !ruby/object:Gem::Version
117
- version: '3.0'
185
+ version: '5.1'
118
186
  type: :development
119
187
  prerelease: false
120
188
  version_requirements: !ruby/object:Gem::Requirement
121
189
  requirements:
122
190
  - - "~>"
123
191
  - !ruby/object:Gem::Version
124
- version: '3.0'
192
+ version: '5.1'
125
193
  - !ruby/object:Gem::Dependency
126
194
  name: rubocop
127
195
  requirement: !ruby/object:Gem::Requirement
@@ -178,7 +246,7 @@ dependencies:
178
246
  - - ">="
179
247
  - !ruby/object:Gem::Version
180
248
  version: '0'
181
- description:
249
+ description:
182
250
  email:
183
251
  - edward-robinson@cookpad.com
184
252
  executables:
@@ -186,10 +254,10 @@ executables:
186
254
  extensions: []
187
255
  extra_rdoc_files: []
188
256
  files:
257
+ - ".github/workflows/ci.yaml"
189
258
  - ".gitignore"
190
259
  - ".rspec"
191
260
  - ".rubocop.yml"
192
- - ".travis.yml"
193
261
  - CODE_OF_CONDUCT.md
194
262
  - Gemfile
195
263
  - Gemfile.lock
@@ -198,9 +266,17 @@ files:
198
266
  - README.md
199
267
  - Rakefile
200
268
  - bin/console
269
+ - bin/integration_test
270
+ - bin/rails
201
271
  - bin/setup
202
272
  - exe/promenade
203
273
  - lib/promenade.rb
274
+ - lib/promenade/client/rack/collector.rb
275
+ - lib/promenade/client/rack/exception_handler.rb
276
+ - lib/promenade/client/rack/request_labeler.rb
277
+ - lib/promenade/client/rack/singleton_caller.rb
278
+ - lib/promenade/configuration.rb
279
+ - lib/promenade/engine.rb
204
280
  - lib/promenade/kafka.rb
205
281
  - lib/promenade/kafka/async_producer_subscriber.rb
206
282
  - lib/promenade/kafka/connection_subscriber.rb
@@ -216,8 +292,9 @@ files:
216
292
  homepage: https://github.com/errm/promenade
217
293
  licenses:
218
294
  - MIT
219
- metadata: {}
220
- post_install_message:
295
+ metadata:
296
+ rubygems_mfa_required: 'true'
297
+ post_install_message:
221
298
  rdoc_options: []
222
299
  require_paths:
223
300
  - lib
@@ -225,15 +302,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
225
302
  requirements:
226
303
  - - ">="
227
304
  - !ruby/object:Gem::Version
228
- version: '0'
305
+ version: '2.7'
306
+ - - "<"
307
+ - !ruby/object:Gem::Version
308
+ version: '3.2'
229
309
  required_rubygems_version: !ruby/object:Gem::Requirement
230
310
  requirements:
231
311
  - - ">="
232
312
  - !ruby/object:Gem::Version
233
313
  version: '0'
234
314
  requirements: []
235
- rubygems_version: 3.0.1
236
- signing_key:
315
+ rubygems_version: 3.2.32
316
+ signing_key:
237
317
  specification_version: 4
238
318
  summary: Promenade makes it simple to instrument Ruby apps for prometheus scraping
239
319
  test_files: []
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.5.5
5
- - 2.6.3
6
- before_install: gem install bundler -v 2.0.2