diff --git a/sentry-ruby/lib/sentry/cron/configuration.rb b/sentry-ruby/lib/sentry/cron/configuration.rb index 897f5a511..58995875e 100644 --- a/sentry-ruby/lib/sentry/cron/configuration.rb +++ b/sentry-ruby/lib/sentry/cron/configuration.rb @@ -18,6 +18,20 @@ class Configuration # tz database style timezone string # @return [String, nil] attr_accessor :default_timezone + + # How default (class-name-derived) cron monitor slugs are formatted. + # Explicit `sentry_monitor_check_ins(slug: ...)` always wins over this. + # + # - +:mashed+ - CamelCase mashed to lowercase (`HappyWorker` -> `happyworker`) + # - +:underscore+ - Rails-style word breaks (`HappyWorker` -> `happy_worker`) + # + # Only one format is active per process; check-ins are not dual-emitted. + # @return [Symbol] + attr_accessor :monitor_slug_format + + def initialize + @monitor_slug_format = :mashed + end end end end diff --git a/sentry-ruby/lib/sentry/cron/monitor_check_ins.rb b/sentry-ruby/lib/sentry/cron/monitor_check_ins.rb index cdc4e6692..6b0214541 100644 --- a/sentry-ruby/lib/sentry/cron/monitor_check_ins.rb +++ b/sentry-ruby/lib/sentry/cron/monitor_check_ins.rb @@ -65,8 +65,8 @@ def sentry_monitor_check_ins(slug: nil, monitor_config: nil) def sentry_monitor_slug(name: self.name) @sentry_monitor_slug ||= begin - slug = name.gsub("::", "-").downcase - slug[-MAX_SLUG_LENGTH..-1] || slug + format = Sentry.configuration&.cron&.monitor_slug_format || :mashed + Slug.from_class_name(name, format: format) end end diff --git a/sentry-ruby/lib/sentry/cron/slug.rb b/sentry-ruby/lib/sentry/cron/slug.rb index 47b5a28b9..66fc5d623 100644 --- a/sentry-ruby/lib/sentry/cron/slug.rb +++ b/sentry-ruby/lib/sentry/cron/slug.rb @@ -1,10 +1,15 @@ # frozen_string_literal: true +require "digest" + module Sentry module Cron module Slug + SLUG_HASH_LENGTH = 10 + module_function + # Sanitize an already-formed key/name into a monitor slug. # Returns nil when nothing usable is left. def from(name) slug = name.to_s.downcase.gsub(/[^a-z0-9_-]+/, "-").sub(/\A-+/, "") @@ -13,6 +18,52 @@ def from(name) last = slug.rindex(/[^-]/) last && slug[0..last] end + + # Derive a monitor slug from a Ruby class name. + # + # @param name [String] fully-qualified class name + # @param format [Symbol] +:mashed+ (downcase, no word breaks) or +:underscore+ (word breaks) + # @return [String] + def from_class_name(name, format: :mashed) + case format + when :underscore + underscore_class_name(name) + else + mashed_class_name(name) + end + end + + # Current behavior: CamelCase -> lowercased token, +::+ -> +-+, right-truncated. + def mashed_class_name(name) + slug = name.to_s.gsub("::", "-").downcase + slug[-MonitorCheckIns::MAX_SLUG_LENGTH..-1] || slug + end + + # Rails-style underscore word breaks, +::+ -> +-+. + # Long slugs keep a hash of the dropped prefix plus a readable tail. + def underscore_class_name(name) + slug = name.to_s + .gsub("::", "-") + .gsub(/([A-Z]{1,1000})([A-Z][a-z])/, '\1_\2') + .gsub(/([a-z\d])([A-Z])/, '\1_\2') + .downcase + + return slug if slug.length <= MonitorCheckIns::MAX_SLUG_LENGTH + + # "#{hash}_#{tail}" must fit MAX_SLUG_LENGTH. + drop_length = slug.length + 1 + SLUG_HASH_LENGTH - MonitorCheckIns::MAX_SLUG_LENGTH + trim_part = +"" + + slug.scan(/([^_-]+)([_-])/) do |segment, separator| + trim_part << segment << separator + break if trim_part.length >= drop_length + end + + trim_part = slug[0, drop_length] if trim_part.length < drop_length + + hash = Digest::SHA256.hexdigest(trim_part)[0, SLUG_HASH_LENGTH] + "#{hash}_#{slug.delete_prefix(trim_part)}" + end end end end diff --git a/sentry-ruby/spec/sentry/cron/monitor_check_ins_spec.rb b/sentry-ruby/spec/sentry/cron/monitor_check_ins_spec.rb index b1aafc4b2..81b6141a6 100644 --- a/sentry-ruby/spec/sentry/cron/monitor_check_ins_spec.rb +++ b/sentry-ruby/spec/sentry/cron/monitor_check_ins_spec.rb @@ -181,10 +181,38 @@ def perform stub_const('VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job', job_class) end - it 'truncates from the beginning and parameterizes slug' do + it 'truncates from the beginning with mashed format by default' do slug = VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job.sentry_monitor_slug expect(slug).to eq('ongoutermodule-veryveryveryverylonginnermodule-job') end + + it 'uses underscore format with hash truncation when configured' do + Sentry.configuration.cron.monitor_slug_format = :underscore + + job_class = Class.new do + include Sentry::Cron::MonitorCheckIns + sentry_monitor_check_ins + def perform; end + end + stub_const('VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::UnderscoreJob', job_class) + + slug = VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::UnderscoreJob.sentry_monitor_slug + expect(slug.length).to be <= described_class::MAX_SLUG_LENGTH + expect(slug).to eq('994055c06d_very_long_inner_module-underscore_job') + end + + it 'ignores monitor_slug_format when an explicit slug is set' do + Sentry.configuration.cron.monitor_slug_format = :underscore + + job_class = Class.new do + include Sentry::Cron::MonitorCheckIns + sentry_monitor_check_ins slug: 'custom_slug' + def perform; end + end + stub_const('ExplicitSlugJob', job_class) + + expect(ExplicitSlugJob.sentry_monitor_slug).to eq('custom_slug') + end end context 'patched with monitor config' do diff --git a/sentry-ruby/spec/sentry/cron/slug_spec.rb b/sentry-ruby/spec/sentry/cron/slug_spec.rb index 5901e3f73..20723ee78 100644 --- a/sentry-ruby/spec/sentry/cron/slug_spec.rb +++ b/sentry-ruby/spec/sentry/cron/slug_spec.rb @@ -18,4 +18,30 @@ expect(described_class.from("!!!")).to be_nil end end + + describe ".from_class_name" do + it "defaults to mashed formatting" do + expect(described_class.from_class_name("HappyWorkerWithCron")) + .to eq("happyworkerwithcron") + end + + it "underscores CamelCase and keeps :: as -" do + expect(described_class.from_class_name("HappyWorkerWithCron", format: :underscore)) + .to eq("happy_worker_with_cron") + end + + it "mashed-truncates from the start without hashing" do + name = "VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job" + expect(described_class.from_class_name(name, format: :mashed)) + .to eq("ongoutermodule-veryveryveryverylonginnermodule-job") + end + + it "hash-truncates long underscore slugs and stays within max length" do + name = "VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job" + slug = described_class.from_class_name(name, format: :underscore) + + expect(slug.length).to be <= Sentry::Cron::MonitorCheckIns::MAX_SLUG_LENGTH + expect(slug).to eq("675905e0c9_very_very_very_long_inner_module-job") + end + end end