Skip to content

Commit

Permalink
Add Sentry::Cron::MonitorCheckIns module for automatic monitoring o…
Browse files Browse the repository at this point in the history
…f jobs

Standard job frameworks such as `ActiveJob` and `Sidekiq` can now use this module to automatically capture check ins.

```rb
class ExampleJob < ApplicationJob
  include Sentry::Cron::MonitorCheckIns

  sentry_monitor_check_ins

  def perform(*args)
    # do stuff
  end
end
```

```rb
class SidekiqJob
  include Sidekiq::Job
  include Sentry::Cron::MonitorCheckIns

  sentry_monitor_check_ins

  def perform(*args)
    # do stuff
  end
end
```

You can pass in optional attributes to `sentry_monitor_check_ins` as follows.
```rb
sentry_monitor_check_ins slug: 'custom_slug'

sentry_monitor_check_ins monitor_config: Sentry::Cron::MonitorConfig.from_interval(1, :minute)

sentry_monitor_check_ins monitor_config: Sentry::Cron::MonitorConfig.from_crontab('5 * * * *')
```
  • Loading branch information
sl0thentr0py committed Oct 9, 2023
1 parent 3d0ed07 commit 3dab5f8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 7 deletions.
55 changes: 48 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,56 @@

- Record client reports for profiles [#2107](https://github.com/getsentry/sentry-ruby/pull/2107)
- Adopt Rails 7.1's new BroadcastLogger [#2120](https://github.com/getsentry/sentry-ruby/pull/2120)
- Add `Sentry.capture_check_in` API for Cron Monitoring [#2117](https://github.com/getsentry/sentry-ruby/pull/2117)
- Add [Cron Monitoring](https://docs.sentry.io/product/crons/) support
- Add `Sentry.capture_check_in` API for Cron Monitoring [#2117](https://github.com/getsentry/sentry-ruby/pull/2117)

You can now track progress of long running scheduled jobs.
You can now track progress of long running scheduled jobs.

```rb
check_in_id = Sentry.capture_check_in('job_name', :in_progress)
# do job stuff
Sentry.capture_check_in('job_name', :ok, check_in_id: check_in_id)
```
```rb
check_in_id = Sentry.capture_check_in('job_name', :in_progress)
# do job stuff
Sentry.capture_check_in('job_name', :ok, check_in_id: check_in_id)
```
- Add `Sentry::Cron::MonitorCheckIns` module for automatic monitoring of jobs [#2130](https://github.com/getsentry/sentry-ruby/pull/2130)

Standard job frameworks such as `ActiveJob` and `Sidekiq` can now use this module to automatically capture check ins.

```rb
class ExampleJob < ApplicationJob
include Sentry::Cron::MonitorCheckIns
sentry_monitor_check_ins
def perform(*args)
# do stuff
end
end
```

```rb
class SidekiqJob
include Sidekiq::Job
include Sentry::Cron::MonitorCheckIns
sentry_monitor_check_ins
def perform(*args)
# do stuff
end
end
```

You can pass in optional attributes to `sentry_monitor_check_ins` as follows.
```rb
# slug defaults to the job class name
sentry_monitor_check_ins slug: 'custom_slug'
# define the monitor config with an interval
sentry_monitor_check_ins monitor_config: Sentry::Cron::MonitorConfig.from_interval(1, :minute)
# define the monitor config with a crontab
sentry_monitor_check_ins monitor_config: Sentry::Cron::MonitorConfig.from_crontab('5 * * * *')
```

### Bug Fixes

Expand Down
1 change: 1 addition & 0 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
require "sentry/hub"
require "sentry/background_worker"
require "sentry/session_flusher"
require "sentry/cron/monitor_check_ins"

[
"sentry/rake",
Expand Down
60 changes: 60 additions & 0 deletions sentry-ruby/lib/sentry/cron/monitor_check_ins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module Sentry
module Cron
module MonitorCheckIns
module Patch
def perform(*args)
# TODO-neel-crons check args and tests
slug = self.class.sentry_monitor_slug || self.class.name
monitor_config = self.class.sentry_monitor_config

Check warning on line 8 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L7-L8

Added lines #L7 - L8 were not covered by tests

check_in_id = Sentry.capture_check_in(slug,

Check warning on line 10 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L10

Added line #L10 was not covered by tests
:in_progress,
monitor_config: monitor_config)

start = Sentry.utc_now.to_i
super
duration = Sentry.utc_now.to_i - start

Check warning on line 16 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L14-L16

Added lines #L14 - L16 were not covered by tests

Sentry.capture_check_in(slug,

Check warning on line 18 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L18

Added line #L18 was not covered by tests
:ok,
check_in_id: check_in_id,
duration: duration,
monitor_config: monitor_config)
rescue Exception
duration = Sentry.utc_now.to_i - start

Check warning on line 24 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L24

Added line #L24 was not covered by tests

Sentry.capture_check_in(slug,

Check warning on line 26 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L26

Added line #L26 was not covered by tests
:error,
check_in_id: check_in_id,
duration: duration,
monitor_config: monitor_config)

raise

Check warning on line 32 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L32

Added line #L32 was not covered by tests
end
end

module ClassMethods
def sentry_monitor_check_ins(slug: nil, monitor_config: nil)
@sentry_monitor_slug = slug
@sentry_monitor_config = monitor_config

Check warning on line 39 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L38-L39

Added lines #L38 - L39 were not covered by tests

prepend Patch

Check warning on line 41 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L41

Added line #L41 was not covered by tests
end

def sentry_monitor_slug
@sentry_monitor_slug

Check warning on line 45 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L45

Added line #L45 was not covered by tests
end

def sentry_monitor_config
@sentry_monitor_config

Check warning on line 49 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L49

Added line #L49 was not covered by tests
end
end

extend ClassMethods

def self.included(base)
base.extend(ClassMethods)

Check warning on line 56 in sentry-ruby/lib/sentry/cron/monitor_check_ins.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/cron/monitor_check_ins.rb#L56

Added line #L56 was not covered by tests
end
end
end
end

0 comments on commit 3dab5f8

Please sign in to comment.