Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions sentry-ruby/lib/sentry/cron/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions sentry-ruby/lib/sentry/cron/monitor_check_ins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 51 additions & 0 deletions sentry-ruby/lib/sentry/cron/slug.rb
Original file line number Diff line number Diff line change
@@ -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-+/, "")
Expand All @@ -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
30 changes: 29 additions & 1 deletion sentry-ruby/spec/sentry/cron/monitor_check_ins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions sentry-ruby/spec/sentry/cron/slug_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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