Skip to content

Commit

Permalink
- Move files to Utils namespace (#898)
Browse files Browse the repository at this point in the history
- Add spec for context filter
- Use context filter for both Sidekiq and DelayedJOb
- fixing specs
  • Loading branch information
alissonbrunosa committed Sep 21, 2020
1 parent e05c8c5 commit f022ca4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/raven/integrations/delayed_job.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'delayed_job'
require 'raven/utils/context_filter'

module Delayed
module Plugins
Expand Down Expand Up @@ -29,7 +30,7 @@ class Raven < ::Delayed::Plugin
extra[:handler] = job.handler[0...1000] if job.handler

if job.respond_to?('payload_object') && job.payload_object.respond_to?('job_data')
extra[:active_job] = job.payload_object.job_data
extra[:active_job] = Utils::ContextFilter.filter_context(job.payload_object.job_data)
end
::Raven.capture_exception(e,
:logger => 'delayed_job',
Expand Down
4 changes: 2 additions & 2 deletions lib/raven/integrations/sidekiq/error_handler.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'raven/integrations/sidekiq/context_filter'
require 'raven/utils/context_filter'

module Raven
module Sidekiq
class ErrorHandler
SIDEKIQ_NAME = "Sidekiq".freeze

def call(ex, context)
context = ContextFilter.filter_context(context)
context = Utils::ContextFilter.filter_context(context)
Raven.context.transaction.push transaction_from_context(context)
Raven.capture_exception(
ex,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Raven
module Sidekiq
module Utils
module ContextFilter
class << self
ACTIVEJOB_RESERVED_PREFIX = "_aj_".freeze
ACTIVEJOB_RESERVED_PREFIX_REGEX = /^_aj_/.freeze
HAS_GLOBALID = const_defined?('GlobalID')

# Once an ActiveJob is queued, ActiveRecord references get serialized into
Expand All @@ -25,7 +25,7 @@ def filter_context(context)
private

def filter_context_hash(key, value)
(key = key[3..-1]) if key [0..3] == ACTIVEJOB_RESERVED_PREFIX
key = key.to_s.sub(ACTIVEJOB_RESERVED_PREFIX_REGEX, "") if key.match(ACTIVEJOB_RESERVED_PREFIX_REGEX)
[key, filter_context(value)]
end

Expand Down
2 changes: 1 addition & 1 deletion spec/raven/integrations/sidekiq/error_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
aj_context["_aj_globalid"] = GlobalID.new('gid://app/model/id')
expected_context = aj_context.dup
expected_context.delete("_aj_globalid")
expected_context["_globalid"] = "gid://app/model/id"
expected_context["globalid"] = "gid://app/model/id"
expected_options = {
:message => exception.message,
:extra => { :sidekiq => expected_context }
Expand Down
41 changes: 41 additions & 0 deletions spec/raven/utils/context_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "spec_helper"

require "raven/utils/context_filter"

RSpec.describe Raven::Utils::ContextFilter do
context "filters out ActiveJob keys from context" do
let(:context) do
{ :_aj_globalid => "gid://app/model/id", :key => "value" }
end
let(:expected_context) do
{ "globalid" => "gid://app/model/id", :key => "value" }
end

it "removes reserved keys" do
new_context = described_class.filter_context(context)

expect(new_context).to eq(expected_context)
end
end

context "filters out ActiveJob keys from nested context" do
let(:context) do
{
:_aj_globalid => "gid://app/model/id",
:arguments => { :key => "value", :_aj_symbol_keys => ["key"] }
}
end
let(:expected_context) do
{
"globalid" => "gid://app/model/id",
:arguments => { :key => "value", "symbol_keys" => ["key"] }
}
end

it "removes reserved keys" do
new_context = described_class.filter_context(context)

expect(new_context).to eq(expected_context)
end
end
end

0 comments on commit f022ca4

Please sign in to comment.