Skip to content

Commit b420bf8

Browse files
authored
Allow setting defaults for MonitorConfig objects with new Cron::Confi… (#2211)
* Allow setting defaults for MonitorConfig objects with new Cron::Configuration * Make danger happy? * dumdum
1 parent 2f9ffaf commit b420bf8

File tree

7 files changed

+92
-14
lines changed

7 files changed

+92
-14
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
- Implement proper flushing logic on ``close`` for Client Reports and Sessions [#2206](https://github.com/getsentry/sentry-ruby/pull/2206)
2525
- Support cron with timezone for `sidekiq-scheduler` patch [#2209](https://github.com/getsentry/sentry-ruby/pull/2209)
2626
- Fixes [#2187](https://github.com/getsentry/sentry-ruby/issues/2187)
27+
- Add `Cron::Configuration` object that holds defaults for all ``MonitorConfig`` objects [#2211](https://github.com/getsentry/sentry-ruby/pull/2211)
28+
29+
```ruby
30+
Sentry.init do |config|
31+
# ...
32+
config.cron.default_checkin_margin = 1
33+
config.cron.default_max_runtime = 30
34+
config.cron.default_timezone = 'America/New_York'
35+
end
36+
```
2737

2838
## 5.15.2
2939

sentry-ruby/lib/sentry/configuration.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require "sentry/dsn"
88
require "sentry/release_detector"
99
require "sentry/transport/configuration"
10+
require "sentry/cron/configuration"
1011
require "sentry/linecache"
1112
require "sentry/interfaces/stacktrace_builder"
1213

@@ -226,10 +227,14 @@ def capture_exception_frame_locals=(value)
226227
# @return [String]
227228
attr_accessor :server_name
228229

229-
# Return a Transport::Configuration object for transport-related configurations.
230-
# @return [Transport]
230+
# Transport related configuration.
231+
# @return [Transport::Configuration]
231232
attr_reader :transport
232233

234+
# Cron related configuration.
235+
# @return [Cron::Configuration]
236+
attr_reader :cron
237+
233238
# Take a float between 0.0 and 1.0 as the sample rate for tracing events (transactions).
234239
# @return [Float, nil]
235240
attr_reader :traces_sample_rate
@@ -380,6 +385,7 @@ def initialize
380385
self.enable_tracing = nil
381386

382387
@transport = Transport::Configuration.new
388+
@cron = Cron::Configuration.new
383389
@gem_specs = Hash[Gem::Specification.map { |spec| [spec.name, spec.version.to_s] }] if Gem::Specification.respond_to?(:map)
384390

385391
run_post_initialization_callbacks
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
module Sentry
4+
module Cron
5+
class Configuration
6+
# Defaults set here will apply to all {Cron::MonitorConfig} objects unless overwritten.
7+
8+
# How long (in minutes) after the expected checkin time will we wait
9+
# until we consider the checkin to have been missed.
10+
# @return [Integer, nil]
11+
attr_accessor :default_checkin_margin
12+
13+
# How long (in minutes) is the checkin allowed to run for in in_progress
14+
# before it is considered failed.
15+
# @return [Integer, nil]
16+
attr_accessor :default_max_runtime
17+
18+
# tz database style timezone string
19+
# @return [String, nil]
20+
attr_accessor :default_timezone
21+
end
22+
end
23+
end

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class MonitorConfig
2525

2626
def initialize(schedule, checkin_margin: nil, max_runtime: nil, timezone: nil)
2727
@schedule = schedule
28-
@checkin_margin = checkin_margin
29-
@max_runtime = max_runtime
30-
@timezone = timezone
28+
@checkin_margin = checkin_margin || Sentry.configuration&.cron&.default_checkin_margin
29+
@max_runtime = max_runtime || Sentry.configuration&.cron&.default_max_runtime
30+
@timezone = timezone || Sentry.configuration&.cron&.default_timezone
3131
end
3232

3333
def self.from_crontab(crontab, **options)

sentry-ruby/spec/sentry/configuration_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,15 @@
256256
end
257257
end
258258

259+
describe "#cron" do
260+
it "returns an initialized Cron::Configuration object" do
261+
expect(subject.cron).to be_a(Sentry::Cron::Configuration)
262+
expect(subject.cron.default_checkin_margin).to eq(nil)
263+
expect(subject.cron.default_max_runtime).to eq(nil)
264+
expect(subject.cron.default_timezone).to eq(nil)
265+
end
266+
end
267+
259268
describe "#spotlight" do
260269
it "false by default" do
261270
expect(subject.spotlight).to eq(false)

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

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
require 'spec_helper'
22

33
RSpec.describe Sentry::Cron::MonitorConfig do
4+
before do
5+
perform_basic_setup do |config|
6+
config.cron.default_checkin_margin = 1
7+
config.cron.default_max_runtime = 30
8+
config.cron.default_timezone = 'America/New_York'
9+
end
10+
end
11+
412
describe '.from_crontab' do
513
it 'has correct attributes' do
614
subject = described_class.from_crontab(
715
'5 * * * *',
816
checkin_margin: 10,
9-
max_runtime: 30,
17+
max_runtime: 20,
1018
timezone: 'Europe/Vienna'
1119
)
1220

1321
expect(subject.schedule).to be_a(Sentry::Cron::MonitorSchedule::Crontab)
1422
expect(subject.schedule.value).to eq('5 * * * *')
1523
expect(subject.checkin_margin).to eq(10)
16-
expect(subject.max_runtime).to eq(30)
24+
expect(subject.max_runtime).to eq(20)
1725
expect(subject.timezone).to eq('Europe/Vienna')
1826
end
27+
28+
it 'fills in correct defaults from cron configuration' do
29+
subject = described_class.from_crontab('5 * * * *')
30+
31+
expect(subject.schedule).to be_a(Sentry::Cron::MonitorSchedule::Crontab)
32+
expect(subject.schedule.value).to eq('5 * * * *')
33+
expect(subject.checkin_margin).to eq(1)
34+
expect(subject.max_runtime).to eq(30)
35+
expect(subject.timezone).to eq('America/New_York')
36+
end
1937
end
2038

2139
describe '.from_interval' do
@@ -28,33 +46,44 @@
2846
5,
2947
:hour,
3048
checkin_margin: 10,
31-
max_runtime: 30,
49+
max_runtime: 20,
3250
timezone: 'Europe/Vienna'
3351
)
3452

3553
expect(subject.schedule).to be_a(Sentry::Cron::MonitorSchedule::Interval)
3654
expect(subject.schedule.value).to eq(5)
3755
expect(subject.schedule.unit).to eq(:hour)
3856
expect(subject.checkin_margin).to eq(10)
39-
expect(subject.max_runtime).to eq(30)
57+
expect(subject.max_runtime).to eq(20)
4058
expect(subject.timezone).to eq('Europe/Vienna')
4159
end
60+
61+
it 'fills in correct defaults from cron configuration' do
62+
subject = described_class.from_interval(5, :minute)
63+
64+
expect(subject.schedule).to be_a(Sentry::Cron::MonitorSchedule::Interval)
65+
expect(subject.schedule.value).to eq(5)
66+
expect(subject.schedule.unit).to eq(:minute)
67+
expect(subject.checkin_margin).to eq(1)
68+
expect(subject.max_runtime).to eq(30)
69+
expect(subject.timezone).to eq('America/New_York')
70+
end
4271
end
4372

4473
describe '#to_hash' do
4574
it 'returns hash with correct attributes for crontab' do
4675
subject = described_class.from_crontab(
4776
'5 * * * *',
4877
checkin_margin: 10,
49-
max_runtime: 30,
78+
max_runtime: 20,
5079
timezone: 'Europe/Vienna'
5180
)
5281

5382
hash = subject.to_hash
5483
expect(hash).to eq({
5584
schedule: { type: :crontab, value: '5 * * * *' },
5685
checkin_margin: 10,
57-
max_runtime: 30,
86+
max_runtime: 20,
5887
timezone: 'Europe/Vienna'
5988
})
6089
end
@@ -64,15 +93,15 @@
6493
5,
6594
:hour,
6695
checkin_margin: 10,
67-
max_runtime: 30,
96+
max_runtime: 20,
6897
timezone: 'Europe/Vienna'
6998
)
7099

71100
hash = subject.to_hash
72101
expect(hash).to eq({
73102
schedule: { type: :interval, value: 5, unit: :hour },
74103
checkin_margin: 10,
75-
max_runtime: 30,
104+
max_runtime: 20,
76105
timezone: 'Europe/Vienna'
77106
})
78107
end

sentry-ruby/spec/sentry/hub_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@
217217
status: :ok,
218218
check_in_id: "xxx-yyy",
219219
duration: 30,
220-
monitor_config: { schedule: { type: :crontab, value: "* * * * *" } }
221220
)
221+
222+
expect(event[:monitor_config]).to include({ schedule: { type: :crontab, value: "* * * * *" } })
222223
end
223224

224225
it_behaves_like "capture_helper" do

0 commit comments

Comments
 (0)