Skip to content

Commit

Permalink
Add Sentry::Scope.add_global_event_processor
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Jan 5, 2023
1 parent 31e5423 commit 1b83757
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

- Allow [tags](https://docs.sentry.io/platforms/ruby/enriching-events/tags/) to be passed via the context hash when reporting errors using ActiveSupport::ErrorReporter and Sentry::Rails::ErrorSubscriber in `sentry-rails` [#1932](https://github.com/getsentry/sentry-ruby/pull/1932)
- Pass a `cached: true` tag for SQL query spans that utilized the ActiveRecord QueryCache when using ActiveRecordSubscriber in `sentry-rails` [#1968](https://github.com/getsentry/sentry-ruby/pull/1968)
- Add `Sentry.add_global_event_processor` API [#1976](https://github.com/getsentry/sentry-ruby/pull/1976)

Users can now configure global event processors without configuring scope as well.

```rb
Sentry.add_global_event_processor do |event, hint|
event.tags = { foo: 42 }
event
end
```


### Bug Fixes

Expand Down
17 changes: 17 additions & 0 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,23 @@ def exception_captured?(exc)
!!exc.instance_variable_get(CAPTURED_SIGNATURE)
end

# Add a global event processor [Proc].
# These run before scope event processors.
#
# @yieldparam event [Event]
# @yieldparam hint [Hash, nil]
# @return [void]
#
# @example
# Sentry.add_global_event_processor do |event, hint|
# event.tags = { foo: 42 }
# event
# end
#
def add_global_event_processor(&block)
Scope.add_global_event_processor(&block)
end

##### Helpers #####

# @!visibility private
Expand Down
29 changes: 27 additions & 2 deletions sentry-ruby/lib/sentry/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class Scope

attr_reader(*ATTRIBUTES)

# Sometimes we need a global event processor without needing to configure scope.
# These run before scope event processors.
@@global_event_processors = []

# @param max_breadcrumbs [Integer] the maximum number of breadcrumbs to be stored in the scope.
def initialize(max_breadcrumbs: nil)
@max_breadcrumbs = max_breadcrumbs
Expand Down Expand Up @@ -58,8 +62,10 @@ def apply_to_event(event, hint = nil)
event.breadcrumbs = breadcrumbs
event.rack_env = rack_env if rack_env

unless @event_processors.empty?
@event_processors.each do |processor_block|
all_event_processors = self.class.global_event_processors + @event_processors

unless all_event_processors.empty?
all_event_processors.each do |processor_block|
event = processor_block.call(event, hint)
end
end
Expand Down Expand Up @@ -266,6 +272,25 @@ def add_event_processor(&block)
@event_processors << block
end

# Adds a new global event processor [Proc].
# @param block [Proc]
# @return [void]
def self.add_global_event_processor(&block)
@@global_event_processors << block
end

# Returns the global event processors array.
# @return [Array<Proc>]
def self.global_event_processors
@@global_event_processors
end

# Clear the global event processors array.
# @return [void]
def self.clear_global_event_processors
@@global_event_processors = []
end

protected

# for duplicating scopes internally
Expand Down
41 changes: 41 additions & 0 deletions sentry-ruby/spec/sentry/scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@
end
end

describe ".add_global_event_processor" do
after { described_class.clear_global_event_processors }

it "adds the global processor to the scope" do
expect(described_class.global_event_processors.count).to eq(0)

expect do
described_class.add_global_event_processor { |e| e }
end.to change { described_class.global_event_processors.count }.by(1)
end
end

describe "#clear" do
it "resets the scope's data" do
subject.set_tags({foo: "bar"})
Expand Down Expand Up @@ -235,6 +247,35 @@
expect(event.extra).to eq({ foo: "bar" })
end

context "with global event processor" do
before do
described_class.add_global_event_processor do |event, hint|
event.tags = { bar: 99 }
event.extra = hint
event
end
end

after { described_class.clear_global_event_processors }

it "applies global event processors to the event" do
subject.apply_to_event(event, { foo: 42 })

expect(event.tags).to eq({ bar: 99 })
expect(event.extra).to eq({ foo: 42 })
end

it "scope event processors take precedence over global event processors" do
subject.add_event_processor do |event, hint|
event.tags = { foo: 42 }
event
end

subject.apply_to_event(event)
expect(event.tags).to eq({ foo: 42 })
end
end

it "sets trace context if there's a span" do
transaction = Sentry::Transaction.new(op: "foo", hub: hub)
subject.set_span(transaction)
Expand Down

0 comments on commit 1b83757

Please sign in to comment.