Skip to content

Commit

Permalink
Add Sentry.add_global_event_processor (#1976)
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py authored Jan 9, 2023
1 parent fb394a5 commit b73abea
Show file tree
Hide file tree
Showing 4 changed files with 89 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
22 changes: 20 additions & 2 deletions sentry-ruby/lib/sentry/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,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 @@ -315,6 +317,22 @@ def runtime_context
version: RUBY_DESCRIPTION || Sentry.sys_command("ruby -v")
}
end

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

# Adds a new global event processor [Proc].
# Sometimes we need a global event processor without needing to configure scope.
# These run before scope event processors.
#
# @param block [Proc]
# @return [void]
def add_global_event_processor(&block)
global_event_processors << block
end
end

end
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.global_event_processors.clear }

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.global_event_processors.clear }

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 b73abea

Please sign in to comment.