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

Add sentry-resque #1476

Merged
merged 3 commits into from
Jun 22, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/sentry_resque_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: sentry-resque Test

on:
workflow_dispatch:
push:
branches:
- master
- \d+-\d+
pull_request:
jobs:
test:
defaults:
run:
working-directory: sentry-resque
name: Test on ruby ${{ matrix.ruby_version }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
ruby_version: [2.4, 2.5, 2.6, 2.7, '3.0', jruby]
os: [ubuntu-latest]
include:
- ruby_version: '3.0'
env: RUBYOPT="--enable-frozen-string-literal --debug=frozen-string-literal"
steps:
- uses: actions/checkout@v1
- name: Set up Ruby ${{ matrix.ruby_version }}
uses: ruby/setup-ruby@v1
with:
bundler: 1
ruby-version: ${{ matrix.ruby_version }}

- name: Start Redis
uses: supercharge/redis-github-action@1.1.0
with:
redis-version: 5

- name: Run specs
run: |
bundle install --jobs 4 --retry 3
bundle exec rake
11 changes: 11 additions & 0 deletions sentry-resque/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status
2 changes: 2 additions & 0 deletions sentry-resque/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
74 changes: 74 additions & 0 deletions sentry-resque/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Contributor Covenant Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience,
nationality, personal appearance, race, religion, or sexual identity and
orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at stan001212@gmail.com. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [https://contributor-covenant.org/version/1/4][version]

[homepage]: https://contributor-covenant.org
[version]: https://contributor-covenant.org/version/1/4/
16 changes: 16 additions & 0 deletions sentry-resque/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
source "https://rubygems.org"

# Specify your gem's dependencies in sentry-ruby.gemspec
gemspec

gem "rake", "~> 12.0"
gem "rspec", "~> 3.0"
gem "codecov", "0.2.12"

gem "resque"
gem "rails"

gem "sentry-ruby", path: "../sentry-ruby"
gem "sentry-rails", path: "../sentry-rails"

gem "pry"
21 changes: 21 additions & 0 deletions sentry-resque/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 st0012

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions sentry-resque/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build:
bundle install
gem build sentry-resque.gemspec
34 changes: 34 additions & 0 deletions sentry-resque/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<p align="center">
<a href="https://sentry.io" target="_blank" align="center">
<img src="https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" width="280">
</a>
<br>
</p>

# sentry-resque, the Resque integration for Sentry's Ruby client

---


[![Gem Version](https://img.shields.io/gem/v/sentry-resque.svg)](https://rubygems.org/gems/sentry-resque)
![Build Status](https://github.com/getsentry/sentry-ruby/workflows/sentry-resque%20Test/badge.svg)
[![Coverage Status](https://img.shields.io/codecov/c/github/getsentry/sentry-ruby/master?logo=codecov)](https://codecov.io/gh/getsentry/sentry-ruby/branch/master)
[![Gem](https://img.shields.io/gem/dt/sentry-resque.svg)](https://rubygems.org/gems/sentry-resque/)
[![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=sentry-resque&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=sentry-resque&package-manager=bundler&version-scheme=semver)


[Documentation](https://docs.sentry.io/platforms/ruby/guides/resque/) | [Bug Tracker](https://github.com/getsentry/sentry-ruby/issues) | [Forum](https://forum.sentry.io/) | IRC: irc.freenode.net, #sentry

The official Ruby-language client and integration layer for the [Sentry](https://github.com/getsentry/sentry) error reporting API.


## Getting Started

### Install

```ruby
gem "sentry-ruby"
gem "sentry-resque"
```

Then you're all set! `sentry-resque` will automatically insert a custom middleware and error handler to capture exceptions from your workers!
8 changes: 8 additions & 0 deletions sentry-resque/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec).tap do |task|
task.rspec_opts = "--order rand"
end

task :default => :spec
14 changes: 14 additions & 0 deletions sentry-resque/bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "sentry/ruby"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions sentry-resque/bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
9 changes: 9 additions & 0 deletions sentry-resque/example/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source "https://rubygems.org"

gem "rails"
gem "sqlite3"
gem "resque"
gem "sentry-resque", path: "../"
gem "sentry-ruby", path: "../../sentry-ruby"

gem "pry"
44 changes: 44 additions & 0 deletions sentry-resque/example/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require "pry"
require "active_job"
require "resque"
require "sentry-resque"

Sentry.init do |config|
config.breadcrumbs_logger = [:sentry_logger]
# replace it with your sentry dsn
config.dsn = 'https://2fb45f003d054a7ea47feb45898f7649@o447951.ingest.sentry.io/5434472'
end

class MyJob < ActiveJob::Base
self.queue_adapter = :resque

def perform
raise "foo"
end
end

worker = Resque::Worker.new(:default)

MyJob.perform_later

begin
worker.work(0)
rescue => e
puts("active job failed because of \"#{e.message}\"")
end

class Foo
def self.perform
1 / 0
end
end

Resque::Job.create(:default, Foo)

begin
worker.work(0)
rescue => e
puts("inline job failed because of \"#{e.message}\"")
end


24 changes: 24 additions & 0 deletions sentry-resque/lib/sentry-resque.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "resque"
require "sentry-ruby"
require "sentry/integrable"
require "sentry/resque/configuration"
require "sentry/resque/version"
require "sentry/resque"

module Sentry
module Resque
extend Sentry::Integrable

register_integration name: "resque", version: Sentry::Resque::VERSION

if defined?(::Rails::Railtie)
class Railtie < ::Rails::Railtie
config.after_initialize do
next unless Sentry.initialized?

Sentry.configuration.rails.skippable_job_adapters << "ActiveJob::QueueAdapters::ResqueAdapter"
end
end
end
end
end
52 changes: 52 additions & 0 deletions sentry-resque/lib/sentry/resque.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require "resque"

module Sentry
module Resque
def perform
return super unless Sentry.initialized?

Sentry.with_scope do |scope|
begin
scope.set_contexts(**generate_contexts)
scope.set_tags("resque.queue" => queue)

super
rescue Exception => exception
::Sentry::Resque.capture_exception(exception, hint: { background: false })
raise
end
end
end

def generate_contexts
context = {}

if payload["class"] == "ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper"
active_job_payload = payload["args"].first

context[:"Active-Job"] = {
job_class: active_job_payload["job_class"],
job_id: active_job_payload["job_id"],
arguments: active_job_payload["arguments"],
executions: active_job_payload["executions"],
exception_executions: active_job_payload["exception_executions"],
locale: active_job_payload["locale"],
enqueued_at: active_job_payload["enqueued_at"],
queue: queue,
worker: worker.to_s
}
else
context[:"Resque"] = {
job_class: payload["class"],
arguments: payload["args"],
queue: queue,
worker: worker.to_s
}
end

context
end
end
end

Resque::Job.send(:prepend, Sentry::Resque)
21 changes: 21 additions & 0 deletions sentry-resque/lib/sentry/resque/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Sentry
class Configuration
attr_reader :resque

add_post_initialization_callback do
@resque = Sentry::Resque::Configuration.new
end
end

module Resque
class Configuration
# Set this option to true if you want Sentry to only capture the last job
# retry if it fails.
attr_accessor :report_after_job_retries

def initialize
@report_after_job_retries = false
end
end
end
end
Loading