Skip to content

Commit

Permalink
Improve default slug generation for Crons mixin
Browse files Browse the repository at this point in the history
Closes #2138
  • Loading branch information
sl0thentr0py committed Nov 13, 2023
1 parent e082644 commit ab0364d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

### Features

- Improve default slug generation for Crons [#2168](https://github.com/getsentry/sentry-ruby/pull/2168)

### Bug Fixes

- Fixed a deprecation in `sidekiq-ruby` error handler [#2160](https://github.com/getsentry/sentry-ruby/pull/2160)
Expand Down
9 changes: 7 additions & 2 deletions sentry-ruby/lib/sentry/cron/monitor_check_ins.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module Sentry
module Cron
module MonitorCheckIns
MAX_SLUG_LENGTH = 50

module Patch
def perform(*args)
slug = self.class.sentry_monitor_slug || self.class.name
slug = self.class.sentry_monitor_slug

Check warning on line 8 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

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

Added line #L8 was not covered by tests
monitor_config = self.class.sentry_monitor_config

check_in_id = Sentry.capture_check_in(slug,
Expand Down Expand Up @@ -43,7 +45,10 @@ def sentry_monitor_check_ins(slug: nil, monitor_config: nil)
end

def sentry_monitor_slug
@sentry_monitor_slug
@sentry_monitor_slug ||= begin
slug = name.gsub('::', '-').downcase
slug[-MAX_SLUG_LENGTH..] || slug

Check warning on line 50 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L48-L50

Added lines #L48 - L50 were not covered by tests
end
end

def sentry_monitor_config
Expand Down
29 changes: 27 additions & 2 deletions sentry-ruby/spec/sentry/cron/monitor_check_ins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ def perform(a, b = 42, c: 99)

it 'calls capture_check_in twice' do
expect(Sentry).to receive(:capture_check_in).with(
'Job',
'job',
:in_progress,
hash_including(monitor_config: nil)
).ordered.and_call_original

expect(Sentry).to receive(:capture_check_in).with(
'Job',
'job',
:ok,
hash_including(:check_in_id, monitor_config: nil, duration: 0)
).ordered.and_call_original
Expand All @@ -119,6 +119,31 @@ def perform(a, b = 42, c: 99)
end
end

context 'with very long class name' do
before do
mod = described_class

job_class = Class.new do
include mod

sentry_monitor_check_ins

def work(a, b, c); end

def perform(a, b = 42, c: 99)
work(a, b, c)
end
end

stub_const('VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job', job_class)
end

it 'truncates from the beginning and parameterizes slug' do
slug = VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job.sentry_monitor_slug
expect(slug).to eq('ongoutermodule-veryveryveryverylonginnermodule-job')
end
end

context 'patched with custom options' do
let(:config) { Sentry::Cron::MonitorConfig::from_interval(1, :minute) }

Expand Down

0 comments on commit ab0364d

Please sign in to comment.