Skip to content

Commit f6e46ea

Browse files
amkiskoAuto
andcommitted
feat(crons): add monitor slug format selection with underscore option
Rebase onto current master and align with #3215. Default class-name slugs stay legacy; `:underscore` adds word breaks and hash truncation via shared `Sentry::Cron::Slug` helpers. No CHANGELOG edit. Co-Authored-By: Auto <noreply@example.com>
1 parent 84fee8b commit f6e46ea

5 files changed

Lines changed: 122 additions & 3 deletions

File tree

‎sentry-ruby/lib/sentry/cron/configuration.rb‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ class Configuration
1818
# tz database style timezone string
1919
# @return [String, nil]
2020
attr_accessor :default_timezone
21+
22+
# How default (class-name-derived) cron monitor slugs are formatted.
23+
# Explicit `sentry_monitor_check_ins(slug: ...)` always wins over this.
24+
#
25+
# - +:legacy+ - CamelCase mashed to lowercase (`HappyWorker` -> `happyworker`)
26+
# - +:underscore+ - Rails-style word breaks (`HappyWorker` -> `happy_worker`)
27+
#
28+
# Only one format is active per process; check-ins are not dual-emitted.
29+
# @return [Symbol]
30+
attr_accessor :monitor_slug_format
31+
32+
def initialize
33+
@monitor_slug_format = :legacy
34+
end
2135
end
2236
end
2337
end

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def sentry_monitor_check_ins(slug: nil, monitor_config: nil)
6565

6666
def sentry_monitor_slug(name: self.name)
6767
@sentry_monitor_slug ||= begin
68-
slug = name.gsub("::", "-").downcase
69-
slug[-MAX_SLUG_LENGTH..-1] || slug
68+
format = Sentry.configuration&.cron&.monitor_slug_format || :legacy
69+
Slug.from_class_name(name, format: format)
7070
end
7171
end
7272

‎sentry-ruby/lib/sentry/cron/slug.rb‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# frozen_string_literal: true
22

3+
require "digest"
4+
35
module Sentry
46
module Cron
57
module Slug
8+
SLUG_HASH_LENGTH = 10
9+
610
module_function
711

12+
# Sanitize an already-formed key/name into a monitor slug.
813
# Returns nil when nothing usable is left.
914
def from(name)
1015
slug = name.to_s.downcase.gsub(/[^a-z0-9_-]+/, "-").sub(/\A-+/, "")
@@ -13,6 +18,52 @@ def from(name)
1318
last = slug.rindex(/[^-]/)
1419
last && slug[0..last]
1520
end
21+
22+
# Derive a monitor slug from a Ruby class name.
23+
#
24+
# @param name [String] fully-qualified class name
25+
# @param format [Symbol] +:legacy+ (mashed downcase) or +:underscore+ (word breaks)
26+
# @return [String]
27+
def from_class_name(name, format: :legacy)
28+
case format
29+
when :underscore
30+
underscore_class_name(name)
31+
else
32+
legacy_class_name(name)
33+
end
34+
end
35+
36+
# Current behavior: CamelCase -> lowercased token, +::+ -> +-+, right-truncated.
37+
def legacy_class_name(name)
38+
slug = name.to_s.gsub("::", "-").downcase
39+
slug[-MonitorCheckIns::MAX_SLUG_LENGTH..-1] || slug
40+
end
41+
42+
# Rails-style underscore word breaks, +::+ -> +-+.
43+
# Long slugs keep a hash of the dropped prefix plus a readable tail.
44+
def underscore_class_name(name)
45+
slug = name.to_s
46+
.gsub("::", "-")
47+
.gsub(/([A-Z]{1,1000})([A-Z][a-z])/, '\1_\2')
48+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
49+
.downcase
50+
51+
return slug if slug.length <= MonitorCheckIns::MAX_SLUG_LENGTH
52+
53+
# "#{hash}_#{tail}" must fit MAX_SLUG_LENGTH.
54+
drop_length = slug.length + 1 + SLUG_HASH_LENGTH - MonitorCheckIns::MAX_SLUG_LENGTH
55+
trim_part = +""
56+
57+
slug.scan(/([^_-]+)([_-])/) do |segment, separator|
58+
trim_part << segment << separator
59+
break if trim_part.length >= drop_length
60+
end
61+
62+
trim_part = slug[0, drop_length] if trim_part.length < drop_length
63+
64+
hash = Digest::SHA256.hexdigest(trim_part)[0, SLUG_HASH_LENGTH]
65+
"#{hash}_#{slug.delete_prefix(trim_part)}"
66+
end
1667
end
1768
end
1869
end

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,38 @@ def perform
181181
stub_const('VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job', job_class)
182182
end
183183

184-
it 'truncates from the beginning and parameterizes slug' do
184+
it 'truncates from the beginning with legacy format by default' do
185185
slug = VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job.sentry_monitor_slug
186186
expect(slug).to eq('ongoutermodule-veryveryveryverylonginnermodule-job')
187187
end
188+
189+
it 'uses underscore format with hash truncation when configured' do
190+
Sentry.configuration.cron.monitor_slug_format = :underscore
191+
192+
job_class = Class.new do
193+
include Sentry::Cron::MonitorCheckIns
194+
sentry_monitor_check_ins
195+
def perform; end
196+
end
197+
stub_const('VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::UnderscoreJob', job_class)
198+
199+
slug = VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::UnderscoreJob.sentry_monitor_slug
200+
expect(slug.length).to be <= described_class::MAX_SLUG_LENGTH
201+
expect(slug).to eq('994055c06d_very_long_inner_module-underscore_job')
202+
end
203+
204+
it 'ignores monitor_slug_format when an explicit slug is set' do
205+
Sentry.configuration.cron.monitor_slug_format = :underscore
206+
207+
job_class = Class.new do
208+
include Sentry::Cron::MonitorCheckIns
209+
sentry_monitor_check_ins slug: 'custom_slug'
210+
def perform; end
211+
end
212+
stub_const('ExplicitSlugJob', job_class)
213+
214+
expect(ExplicitSlugJob.sentry_monitor_slug).to eq('custom_slug')
215+
end
188216
end
189217

190218
context 'patched with monitor config' do

‎sentry-ruby/spec/sentry/cron/slug_spec.rb‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,30 @@
1818
expect(described_class.from("!!!")).to be_nil
1919
end
2020
end
21+
22+
describe ".from_class_name" do
23+
it "defaults to legacy mashed formatting" do
24+
expect(described_class.from_class_name("HappyWorkerWithCron"))
25+
.to eq("happyworkerwithcron")
26+
end
27+
28+
it "underscores CamelCase and keeps :: as -" do
29+
expect(described_class.from_class_name("HappyWorkerWithCron", format: :underscore))
30+
.to eq("happy_worker_with_cron")
31+
end
32+
33+
it "legacy-truncates from the start without hashing" do
34+
name = "VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job"
35+
expect(described_class.from_class_name(name, format: :legacy))
36+
.to eq("ongoutermodule-veryveryveryverylonginnermodule-job")
37+
end
38+
39+
it "hash-truncates long underscore slugs and stays within max length" do
40+
name = "VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job"
41+
slug = described_class.from_class_name(name, format: :underscore)
42+
43+
expect(slug.length).to be <= Sentry::Cron::MonitorCheckIns::MAX_SLUG_LENGTH
44+
expect(slug).to eq("675905e0c9_very_very_very_long_inner_module-job")
45+
end
46+
end
2147
end

0 commit comments

Comments
 (0)