From b73abeacfa3b6f6fe4e0f98aee01e1ea9a72f4a7 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Mon, 9 Jan 2023 12:13:37 +0100 Subject: [PATCH] Add Sentry.add_global_event_processor (#1976) --- CHANGELOG.md | 11 +++++++ sentry-ruby/lib/sentry-ruby.rb | 17 +++++++++++ sentry-ruby/lib/sentry/scope.rb | 22 ++++++++++++-- sentry-ruby/spec/sentry/scope_spec.rb | 41 +++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d159b026..e58d84883 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/sentry-ruby/lib/sentry-ruby.rb b/sentry-ruby/lib/sentry-ruby.rb index fc6293c78..3760b74ed 100644 --- a/sentry-ruby/lib/sentry-ruby.rb +++ b/sentry-ruby/lib/sentry-ruby.rb @@ -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 diff --git a/sentry-ruby/lib/sentry/scope.rb b/sentry-ruby/lib/sentry/scope.rb index 09c4b7cde..888bed2ae 100644 --- a/sentry-ruby/lib/sentry/scope.rb +++ b/sentry-ruby/lib/sentry/scope.rb @@ -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 @@ -315,6 +317,22 @@ def runtime_context version: RUBY_DESCRIPTION || Sentry.sys_command("ruby -v") } end + + # Returns the global event processors array. + # @return [Array] + 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 diff --git a/sentry-ruby/spec/sentry/scope_spec.rb b/sentry-ruby/spec/sentry/scope_spec.rb index 98b1b6d3f..a075b963b 100644 --- a/sentry-ruby/spec/sentry/scope_spec.rb +++ b/sentry-ruby/spec/sentry/scope_spec.rb @@ -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"}) @@ -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)