Skip to content

Commit 852436b

Browse files
nateberkopecclaude
andcommitted
Refactor tests and improve smoketest coverage
- Remove railtie_test.rb (now covered by smoketest) - Add rake task test to smoketest to verify metrics disabled during db: tasks - Update verify_metrics.rb to check for forbidden metrics - Refactor high-complexity test methods: - PumaTest#test_collects_metrics_with_clustered_mode_stats (57.6 → 4.5) - SidekiqTest#test_monitors_all_queues_by_default (45.9 → 5.6) - SidekiqTest#test_filters_queues_when_configured (45.9 → 5.6) - Configure flog/flay to only scan lib and test directories - Exclude smoketest directory from standard linting 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7684147 commit 852436b

File tree

12 files changed

+320
-226
lines changed

12 files changed

+320
-226
lines changed

.standard.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# https://github.com/standardrb/standard
33
ruby_version: 2.7
44

5+
ignore:
6+
- 'smoketest/**/*'
7+
58
require:
69
- ./.rubocop/forbidden_methods.rb
710

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ gem "rack"
1313
gem "rake", "~> 13.0"
1414
gem "sidekiq"
1515
gem "standard", ">= 1.35.1"
16+
gem "rails"

Gemfile.lock

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,85 @@ PATH
77
GEM
88
remote: https://rubygems.org/
99
specs:
10+
actioncable (7.1.6)
11+
actionpack (= 7.1.6)
12+
activesupport (= 7.1.6)
13+
nio4r (~> 2.0)
14+
websocket-driver (>= 0.6.1)
15+
zeitwerk (~> 2.6)
16+
actionmailbox (7.1.6)
17+
actionpack (= 7.1.6)
18+
activejob (= 7.1.6)
19+
activerecord (= 7.1.6)
20+
activestorage (= 7.1.6)
21+
activesupport (= 7.1.6)
22+
mail (>= 2.7.1)
23+
net-imap
24+
net-pop
25+
net-smtp
26+
actionmailer (7.1.6)
27+
actionpack (= 7.1.6)
28+
actionview (= 7.1.6)
29+
activejob (= 7.1.6)
30+
activesupport (= 7.1.6)
31+
mail (~> 2.5, >= 2.5.4)
32+
net-imap
33+
net-pop
34+
net-smtp
35+
rails-dom-testing (~> 2.2)
36+
actionpack (7.1.6)
37+
actionview (= 7.1.6)
38+
activesupport (= 7.1.6)
39+
cgi
40+
nokogiri (>= 1.8.5)
41+
racc
42+
rack (>= 2.2.4)
43+
rack-session (>= 1.0.1)
44+
rack-test (>= 0.6.3)
45+
rails-dom-testing (~> 2.2)
46+
rails-html-sanitizer (~> 1.6)
47+
actiontext (7.1.6)
48+
actionpack (= 7.1.6)
49+
activerecord (= 7.1.6)
50+
activestorage (= 7.1.6)
51+
activesupport (= 7.1.6)
52+
globalid (>= 0.6.0)
53+
nokogiri (>= 1.8.5)
54+
actionview (7.1.6)
55+
activesupport (= 7.1.6)
56+
builder (~> 3.1)
57+
cgi
58+
erubi (~> 1.11)
59+
rails-dom-testing (~> 2.2)
60+
rails-html-sanitizer (~> 1.6)
61+
activejob (7.1.6)
62+
activesupport (= 7.1.6)
63+
globalid (>= 0.3.6)
64+
activemodel (7.1.6)
65+
activesupport (= 7.1.6)
66+
activerecord (7.1.6)
67+
activemodel (= 7.1.6)
68+
activesupport (= 7.1.6)
69+
timeout (>= 0.4.0)
70+
activestorage (7.1.6)
71+
actionpack (= 7.1.6)
72+
activejob (= 7.1.6)
73+
activerecord (= 7.1.6)
74+
activesupport (= 7.1.6)
75+
marcel (~> 1.0)
76+
activesupport (7.1.6)
77+
base64
78+
benchmark (>= 0.3)
79+
bigdecimal
80+
concurrent-ruby (~> 1.0, >= 1.0.2)
81+
connection_pool (>= 2.2.5)
82+
drb
83+
i18n (>= 1.6, < 2)
84+
logger (>= 1.4.2)
85+
minitest (>= 5.1)
86+
mutex_m
87+
securerandom (>= 0.3)
88+
tzinfo (~> 2.0)
1089
ast (2.4.3)
1190
aws-eventstream (1.4.0)
1291
aws-partitions (1.1179.0)
@@ -24,10 +103,15 @@ GEM
24103
aws-sigv4 (1.12.1)
25104
aws-eventstream (~> 1, >= 1.0.2)
26105
base64 (0.3.0)
106+
benchmark (0.5.0)
27107
bigdecimal (3.3.1)
108+
builder (3.3.0)
28109
cgi (0.5.0)
110+
concurrent-ruby (1.3.5)
29111
connection_pool (2.5.4)
112+
crass (1.0.6)
30113
date (3.5.0)
114+
drb (2.2.3)
31115
erb (4.0.4)
32116
cgi (>= 0.3.3)
33117
erubi (1.13.1)
@@ -40,6 +124,10 @@ GEM
40124
path_expander (~> 1.0)
41125
ruby_parser (~> 3.1, > 3.1.0)
42126
sexp_processor (~> 4.8)
127+
globalid (1.3.0)
128+
activesupport (>= 6.1)
129+
i18n (1.14.7)
130+
concurrent-ruby (~> 1.0)
43131
io-console (0.8.1)
44132
irb (1.15.2)
45133
pp (>= 0.6.0)
@@ -50,8 +138,35 @@ GEM
50138
language_server-protocol (3.17.0.5)
51139
lint_roller (1.1.0)
52140
logger (1.7.0)
141+
loofah (2.24.1)
142+
crass (~> 1.0.2)
143+
nokogiri (>= 1.12.0)
144+
mail (2.9.0)
145+
logger
146+
mini_mime (>= 0.1.1)
147+
net-imap
148+
net-pop
149+
net-smtp
150+
marcel (1.1.0)
151+
mini_mime (1.1.5)
152+
mini_portile2 (2.8.9)
53153
minitest (5.26.0)
154+
mutex_m (0.3.0)
155+
net-imap (0.4.22)
156+
date
157+
net-protocol
158+
net-pop (0.1.2)
159+
net-protocol
160+
net-protocol (0.2.2)
161+
timeout
162+
net-smtp (0.5.1)
163+
net-protocol
54164
nio4r (2.7.4)
165+
nokogiri (1.15.7)
166+
mini_portile2 (~> 2.8.2)
167+
racc (~> 1.4)
168+
nokogiri (1.15.7-arm64-darwin)
169+
racc (~> 1.4)
55170
parallel (1.27.0)
56171
parser (3.3.10.0)
57172
ast (~> 2.4.1)
@@ -68,6 +183,44 @@ GEM
68183
nio4r (~> 2.0)
69184
racc (1.8.1)
70185
rack (3.2.3)
186+
rack-session (2.1.1)
187+
base64 (>= 0.1.0)
188+
rack (>= 3.0.0)
189+
rack-test (2.2.0)
190+
rack (>= 1.3)
191+
rackup (2.2.1)
192+
rack (>= 3)
193+
rails (7.1.6)
194+
actioncable (= 7.1.6)
195+
actionmailbox (= 7.1.6)
196+
actionmailer (= 7.1.6)
197+
actionpack (= 7.1.6)
198+
actiontext (= 7.1.6)
199+
actionview (= 7.1.6)
200+
activejob (= 7.1.6)
201+
activemodel (= 7.1.6)
202+
activerecord (= 7.1.6)
203+
activestorage (= 7.1.6)
204+
activesupport (= 7.1.6)
205+
bundler (>= 1.15.0)
206+
railties (= 7.1.6)
207+
rails-dom-testing (2.3.0)
208+
activesupport (>= 5.0.0)
209+
minitest
210+
nokogiri (>= 1.6)
211+
rails-html-sanitizer (1.6.2)
212+
loofah (~> 2.21)
213+
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
214+
railties (7.1.6)
215+
actionpack (= 7.1.6)
216+
activesupport (= 7.1.6)
217+
cgi
218+
irb
219+
rackup (>= 1.0.0)
220+
rake (>= 12.2)
221+
thor (~> 1.0, >= 1.2.2)
222+
tsort (>= 0.2)
223+
zeitwerk (~> 2.6)
71224
rainbow (3.1.1)
72225
rake (13.3.1)
73226
rdoc (6.15.0)
@@ -101,6 +254,7 @@ GEM
101254
ruby_parser (3.21.1)
102255
racc (~> 1.5)
103256
sexp_processor (~> 4.16)
257+
securerandom (0.3.2)
104258
sexp_processor (4.17.4)
105259
sidekiq (7.3.9)
106260
base64
@@ -121,8 +275,17 @@ GEM
121275
lint_roller (~> 1.1)
122276
rubocop-performance (~> 1.21.0)
123277
stringio (3.1.7)
278+
thor (1.4.0)
279+
timeout (0.4.4)
124280
tsort (0.2.0)
281+
tzinfo (2.0.6)
282+
concurrent-ruby (~> 1.0)
125283
unicode-display_width (2.6.0)
284+
websocket-driver (0.8.0)
285+
base64
286+
websocket-extensions (>= 0.1.0)
287+
websocket-extensions (0.1.5)
288+
zeitwerk (2.6.18)
126289

127290
PLATFORMS
128291
arm64-darwin-24
@@ -135,6 +298,7 @@ DEPENDENCIES
135298
minitest
136299
puma
137300
rack
301+
rails
138302
rake (~> 13.0)
139303
sidekiq
140304
speedshop-cloudwatch!

README.md

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,24 @@ This gem is for **infrastructure and queue metrics**, not application performanc
3838

3939
## Installation
4040

41-
```
41+
```ruby
4242
gem `speedshop_cloudwatch`
4343
```
4444

45+
```ruby
46+
# config/initializers/speedshop-cloudwatch.rb
47+
Speedshop::Cloudwatch::Puma.register
48+
Speedshop::Cloudwatch::Sidekiq.register
49+
```
50+
51+
```ruby
52+
# app/jobs/application_job.rb
53+
54+
class ApplicationJob
55+
include Speedshop::Cloudwatch::ActiveJob
56+
end
57+
```
58+
4559
## Configuration
4660

4761
You can configure which integrations are enabled, which metrics are reported, and the CloudWatch namespace for each integration:
@@ -54,7 +68,7 @@ Speedshop::Cloudwatch.configure do |config|
5468
# Optional: Custom logger (defaults to Rails.logger if available, otherwise STDOUT)
5569
config.logger = Logger.new(Rails.root.join("log", "cloudwatch.log"))
5670

57-
# Disable an entire integration
71+
# Disable an entire integration.
5872
config.enabled[:rack] = false
5973

6074
# Customize which metrics to report (whitelist)
@@ -72,33 +86,6 @@ Speedshop::Cloudwatch.configure do |config|
7286
end
7387
```
7488

75-
### Rails
76-
77-
By default, if you're using Rails, the gem automatically inserts the Rack middleware at the top of the middleware stack.
78-
79-
The reporter starts automatically when the first metric is reported - no manual setup required.
80-
81-
If you want full control over initialization, add `require: false` to your Gemfile:
82-
83-
```ruby
84-
gem 'speedshop_cloudwatch', require: false
85-
```
86-
87-
Then manually require the core module without the railtie:
88-
89-
```ruby
90-
require 'speedshop/cloudwatch'
91-
92-
# Insert middleware manually (if using Rack integration)
93-
Rails.application.config.middleware.insert_before 0, Speedshop::Cloudwatch::RackMiddleware
94-
95-
# Register integrations
96-
Speedshop::Cloudwatch::Puma.register # if using Puma
97-
Speedshop::Cloudwatch::Sidekiq.register # if using Sidekiq
98-
99-
# Reporter starts automatically when first metric is reported
100-
```
101-
10289
### Puma Integration
10390

10491
Add to your `config/puma.rb`:
@@ -185,3 +172,28 @@ QueueLatency - Time job spent waiting in queue before execution (Seconds)
185172
```
186173

187174
This metric includes JobClass and QueueName dimensions.
175+
176+
### Rails
177+
178+
We do two things when we're running in a Rails app:
179+
180+
1. Automatically insert a Rack middleware at index 0.
181+
2. Disable metric collection if running in Rake or a console.
182+
183+
If you want full control over these behaviors, add `require: false` to your Gemfile:
184+
185+
```ruby
186+
gem 'speedshop_cloudwatch', require: false
187+
```
188+
189+
Then manually require the core module without the railtie:
190+
191+
```ruby
192+
# config/initializers/speedshop-cloudwatch.rb
193+
require 'speedshop/cloudwatch'
194+
195+
# Insert middleware manually (if using Rack integration)
196+
Rails.application.config.middleware.insert_before 0, Speedshop::Cloudwatch::RackMiddleware
197+
198+
# At this point, the auto-disable behavior in Rake is disabled. You'll have to re-implement yourself.
199+
```

Rakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ end
1515

1616
desc "Run flog"
1717
task :flog do
18-
flog_output = `bundle exec flog -a lib`
18+
flog_output = `bundle exec flog -a lib test`
1919
puts flog_output
2020
method_scores = flog_output.lines.grep(/^\s+[0-9]+\.[0-9]+:.*#/).map { |line| line.split.first.to_f }
2121
max_score = method_scores.max
@@ -27,7 +27,7 @@ end
2727

2828
desc "Run flay"
2929
task :flay do
30-
flay_output = `bundle exec flay lib`
30+
flay_output = `bundle exec flay lib test`
3131
puts flay_output
3232
flay_score = flay_output[/Total score.*?=\s*(\d+)/, 1]&.to_i
3333
if flay_score && flay_score > FLAY_THRESHOLD

smoketest/config/initializers/cloudwatch_stub.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
headers: request.headers
1717
}
1818

19-
File.open(Rails.root.join("tmp", "captured_metrics.json"), "w") do |f|
20-
f.write(JSON.pretty_generate($captured_metrics))
21-
end
19+
File.write(Rails.root.join("tmp", "captured_metrics.json"), JSON.pretty_generate($captured_metrics))
2220

2321
{status: 200, body: '<?xml version="1.0"?><PutMetricDataResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/"><ResponseMetadata><RequestId>test-request-id</RequestId></ResponseMetadata></PutMetricDataResponse>'}
2422
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
namespace :db do
4+
desc "Test that metrics are disabled in db rake tasks"
5+
task test_metric_report: :environment do
6+
puts "Attempting to report metric from db rake task..."
7+
Speedshop::Cloudwatch.reporter.report("RakeTaskMetric", 1, namespace: "Test")
8+
puts "Metric report attempted (should be disabled)"
9+
end
10+
end

0 commit comments

Comments
 (0)