Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting defaults for MonitorConfig objects with new Cron::Confi… #2211

Merged
merged 3 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
- Implement proper flushing logic on ``close`` for Client Reports and Sessions [#2206](https://github.com/getsentry/sentry-ruby/pull/2206)
- Support cron with timezone for `sidekiq-scheduler` patch [#2209](https://github.com/getsentry/sentry-ruby/pull/2209)
- Fixes [#2187](https://github.com/getsentry/sentry-ruby/issues/2187)
- Add `Cron::Configuration` object that holds defaults for all ``MonitorConfig`` objects [#2211](https://github.com/getsentry/sentry-ruby/pull/2211)

```ruby
Sentry.init do |config|
# ...
config.cron.default_checkin_margin = 1
config.cron.default_max_runtime = 30
config.cron.default_timezone = 'America/New_York'
end
```

## 5.15.2

Expand Down
10 changes: 8 additions & 2 deletions sentry-ruby/lib/sentry/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "sentry/dsn"
require "sentry/release_detector"
require "sentry/transport/configuration"
require "sentry/cron/configuration"
require "sentry/linecache"
require "sentry/interfaces/stacktrace_builder"

Expand Down Expand Up @@ -226,10 +227,14 @@ def capture_exception_frame_locals=(value)
# @return [String]
attr_accessor :server_name

# Return a Transport::Configuration object for transport-related configurations.
# @return [Transport]
# Transport related configuration.
# @return [Transport::Configuration]
attr_reader :transport

# Cron related configuration.
# @return [Cron::Configuration]
attr_reader :cron

# Take a float between 0.0 and 1.0 as the sample rate for tracing events (transactions).
# @return [Float, nil]
attr_reader :traces_sample_rate
Expand Down Expand Up @@ -380,6 +385,7 @@ def initialize
self.enable_tracing = nil

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

run_post_initialization_callbacks
Expand Down
23 changes: 23 additions & 0 deletions sentry-ruby/lib/sentry/cron/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Sentry
module Cron
class Configuration
# Defaults set here will apply to all {Cron::MonitorConfig} objects unless overwritten.

# How long (in minutes) after the expected checkin time will we wait
# until we consider the checkin to have been missed.
# @return [Integer, nil]
attr_accessor :default_checkin_margin

# How long (in minutes) is the checkin allowed to run for in in_progress
# before it is considered failed.
# @return [Integer, nil]
attr_accessor :default_max_runtime

# tz database style timezone string
# @return [String, nil]
attr_accessor :default_timezone
end
end
end
6 changes: 3 additions & 3 deletions sentry-ruby/lib/sentry/cron/monitor_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class MonitorConfig

def initialize(schedule, checkin_margin: nil, max_runtime: nil, timezone: nil)
@schedule = schedule
@checkin_margin = checkin_margin
@max_runtime = max_runtime
@timezone = timezone
@checkin_margin = checkin_margin || Sentry.configuration&.cron&.default_checkin_margin
@max_runtime = max_runtime || Sentry.configuration&.cron&.default_max_runtime
@timezone = timezone || Sentry.configuration&.cron&.default_timezone
end

def self.from_crontab(crontab, **options)
Expand Down
9 changes: 9 additions & 0 deletions sentry-ruby/spec/sentry/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@
end
end

describe "#cron" do
it "returns an initialized Cron::Configuration object" do
expect(subject.cron).to be_a(Sentry::Cron::Configuration)
expect(subject.cron.default_checkin_margin).to eq(nil)
expect(subject.cron.default_max_runtime).to eq(nil)
expect(subject.cron.default_timezone).to eq(nil)
end
end

describe "#spotlight" do
it "false by default" do
expect(subject.spotlight).to eq(false)
Expand Down
45 changes: 37 additions & 8 deletions sentry-ruby/spec/sentry/cron/monitor_config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
require 'spec_helper'

RSpec.describe Sentry::Cron::MonitorConfig do
before do
perform_basic_setup do |config|
config.cron.default_checkin_margin = 1
config.cron.default_max_runtime = 30
config.cron.default_timezone = 'America/New_York'
end
end

describe '.from_crontab' do
it 'has correct attributes' do
subject = described_class.from_crontab(
'5 * * * *',
checkin_margin: 10,
max_runtime: 30,
max_runtime: 20,
timezone: 'Europe/Vienna'
)

expect(subject.schedule).to be_a(Sentry::Cron::MonitorSchedule::Crontab)
expect(subject.schedule.value).to eq('5 * * * *')
expect(subject.checkin_margin).to eq(10)
expect(subject.max_runtime).to eq(30)
expect(subject.max_runtime).to eq(20)
expect(subject.timezone).to eq('Europe/Vienna')
end

it 'fills in correct defaults from cron configuration' do
subject = described_class.from_crontab('5 * * * *')

expect(subject.schedule).to be_a(Sentry::Cron::MonitorSchedule::Crontab)
expect(subject.schedule.value).to eq('5 * * * *')
expect(subject.checkin_margin).to eq(1)
expect(subject.max_runtime).to eq(30)
expect(subject.timezone).to eq('America/New_York')
end
end

describe '.from_interval' do
Expand All @@ -28,33 +46,44 @@
5,
:hour,
checkin_margin: 10,
max_runtime: 30,
max_runtime: 20,
timezone: 'Europe/Vienna'
)

expect(subject.schedule).to be_a(Sentry::Cron::MonitorSchedule::Interval)
expect(subject.schedule.value).to eq(5)
expect(subject.schedule.unit).to eq(:hour)
expect(subject.checkin_margin).to eq(10)
expect(subject.max_runtime).to eq(30)
expect(subject.max_runtime).to eq(20)
expect(subject.timezone).to eq('Europe/Vienna')
end

it 'fills in correct defaults from cron configuration' do
subject = described_class.from_interval(5, :minute)

expect(subject.schedule).to be_a(Sentry::Cron::MonitorSchedule::Interval)
expect(subject.schedule.value).to eq(5)
expect(subject.schedule.unit).to eq(:minute)
expect(subject.checkin_margin).to eq(1)
expect(subject.max_runtime).to eq(30)
expect(subject.timezone).to eq('America/New_York')
end
end

describe '#to_hash' do
it 'returns hash with correct attributes for crontab' do
subject = described_class.from_crontab(
'5 * * * *',
checkin_margin: 10,
max_runtime: 30,
max_runtime: 20,
timezone: 'Europe/Vienna'
)

hash = subject.to_hash
expect(hash).to eq({
schedule: { type: :crontab, value: '5 * * * *' },
checkin_margin: 10,
max_runtime: 30,
max_runtime: 20,
timezone: 'Europe/Vienna'
})
end
Expand All @@ -64,15 +93,15 @@
5,
:hour,
checkin_margin: 10,
max_runtime: 30,
max_runtime: 20,
timezone: 'Europe/Vienna'
)

hash = subject.to_hash
expect(hash).to eq({
schedule: { type: :interval, value: 5, unit: :hour },
checkin_margin: 10,
max_runtime: 30,
max_runtime: 20,
timezone: 'Europe/Vienna'
})
end
Expand Down
3 changes: 2 additions & 1 deletion sentry-ruby/spec/sentry/hub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@
status: :ok,
check_in_id: "xxx-yyy",
duration: 30,
monitor_config: { schedule: { type: :crontab, value: "* * * * *" } }
)

expect(event[:monitor_config]).to include({ schedule: { type: :crontab, value: "* * * * *" } })
end

it_behaves_like "capture_helper" do
Expand Down