Skip to content

Commit 3d27cae

Browse files
committed
Improve default slug generation for Crons mixin
Closes #2138
1 parent e082644 commit 3d27cae

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Unreleased
22

3+
### Features
4+
5+
- Improve default slug generation for Crons [#2168](https://github.com/getsentry/sentry-ruby/pull/2168)
6+
37
### Bug Fixes
48

59
- Fixed a deprecation in `sidekiq-ruby` error handler [#2160](https://github.com/getsentry/sentry-ruby/pull/2160)

sentry-rails/spec/sentry/rails/activejob_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def perform(event, hint)
340340
expect(first.to_hash).to include(
341341
type: 'check_in',
342342
check_in_id: check_in_id,
343-
monitor_slug: "NormalJobWithCron",
343+
monitor_slug: "normaljobwithcron",
344344
status: :in_progress
345345
)
346346

@@ -350,7 +350,7 @@ def perform(event, hint)
350350
:duration,
351351
type: 'check_in',
352352
check_in_id: check_in_id,
353-
monitor_slug: "NormalJobWithCron",
353+
monitor_slug: "normaljobwithcron",
354354
status: :ok
355355
)
356356
end

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module Sentry
22
module Cron
33
module MonitorCheckIns
4+
MAX_SLUG_LENGTH = 50
5+
46
module Patch
57
def perform(*args)
6-
slug = self.class.sentry_monitor_slug || self.class.name
8+
slug = self.class.sentry_monitor_slug
79
monitor_config = self.class.sentry_monitor_config
810

911
check_in_id = Sentry.capture_check_in(slug,
@@ -43,7 +45,10 @@ def sentry_monitor_check_ins(slug: nil, monitor_config: nil)
4345
end
4446

4547
def sentry_monitor_slug
46-
@sentry_monitor_slug
48+
@sentry_monitor_slug ||= begin
49+
slug = name.gsub('::', '-').downcase
50+
slug[-MAX_SLUG_LENGTH..-1] || slug
51+
end
4752
end
4853

4954
def sentry_monitor_config

sentry-ruby/spec/sentry/cron/monitor_check_ins_spec.rb

+27-2
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ def perform(a, b = 42, c: 99)
104104

105105
it 'calls capture_check_in twice' do
106106
expect(Sentry).to receive(:capture_check_in).with(
107-
'Job',
107+
'job',
108108
:in_progress,
109109
hash_including(monitor_config: nil)
110110
).ordered.and_call_original
111111

112112
expect(Sentry).to receive(:capture_check_in).with(
113-
'Job',
113+
'job',
114114
:ok,
115115
hash_including(:check_in_id, monitor_config: nil, duration: 0)
116116
).ordered.and_call_original
@@ -119,6 +119,31 @@ def perform(a, b = 42, c: 99)
119119
end
120120
end
121121

122+
context 'with very long class name' do
123+
before do
124+
mod = described_class
125+
126+
job_class = Class.new do
127+
include mod
128+
129+
sentry_monitor_check_ins
130+
131+
def work(a, b, c); end
132+
133+
def perform(a, b = 42, c: 99)
134+
work(a, b, c)
135+
end
136+
end
137+
138+
stub_const('VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job', job_class)
139+
end
140+
141+
it 'truncates from the beginning and parameterizes slug' do
142+
slug = VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job.sentry_monitor_slug
143+
expect(slug).to eq('ongoutermodule-veryveryveryverylonginnermodule-job')
144+
end
145+
end
146+
122147
context 'patched with custom options' do
123148
let(:config) { Sentry::Cron::MonitorConfig::from_interval(1, :minute) }
124149

sentry-sidekiq/spec/sentry/sidekiq_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def retry_last_failed_job
268268
expect(first.to_hash).to include(
269269
type: 'check_in',
270270
check_in_id: check_in_id,
271-
monitor_slug: "HappyWorkerWithCron",
271+
monitor_slug: "happyworkerwithcron",
272272
status: :in_progress
273273
)
274274

@@ -278,7 +278,7 @@ def retry_last_failed_job
278278
:duration,
279279
type: 'check_in',
280280
check_in_id: check_in_id,
281-
monitor_slug: "HappyWorkerWithCron",
281+
monitor_slug: "happyworkerwithcron",
282282
status: :ok
283283
)
284284
end

0 commit comments

Comments
 (0)