Skip to content

Commit

Permalink
Notify users when their custom options are discarded
Browse files Browse the repository at this point in the history
It should save users from some debugging if we can tell them what options
they pass are discarded and how they can fix it.

The warning is only enabled at debug level so it should not be too noisy.
  • Loading branch information
st0012 committed Apr 29, 2024
1 parent 07aaf5e commit af47fa6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
7 changes: 6 additions & 1 deletion sentry-ruby/lib/sentry/hub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ def capture_event(event, **options, &block)
elsif custom_scope = options[:scope]
scope.update_from_scope(custom_scope)
elsif !options.empty?
scope.update_from_options(**options)
unsupported_option_keys = scope.update_from_options(**options)

Check warning on line 196 in sentry-ruby/lib/sentry/hub.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/hub.rb#L196

Added line #L196 was not covered by tests

configuration.log_debug <<~MSG

Check warning on line 198 in sentry-ruby/lib/sentry/hub.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/hub.rb#L198

Added line #L198 was not covered by tests
Options #{unsupported_option_keys} are not supported and will not be applied to the event.
You may want to set them under the `extra` option.
MSG
end

event = current_client.capture_event(event, scope, hint)
Expand Down
5 changes: 4 additions & 1 deletion sentry-ruby/lib/sentry/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def update_from_scope(scope)
# @param user [Hash]
# @param level [String, Symbol]
# @param fingerprint [Array]
# @return [void]
# @return [Array]
def update_from_options(
contexts: nil,
extra: nil,
Expand All @@ -144,6 +144,9 @@ def update_from_options(
self.user = user if user
self.level = level if level
self.fingerprint = fingerprint if fingerprint

# Returns unsupported option keys so we can notify users.
options.keys

Check warning on line 149 in sentry-ruby/lib/sentry/scope.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/scope.rb#L149

Added line #L149 was not covered by tests
end

# Sets the scope's rack_env attribute.
Expand Down
8 changes: 8 additions & 0 deletions sentry-ruby/spec/sentry/hub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@
end
end

it "reminds users about unsupported options" do
expect do
subject.capture_event(event, unsupported: true)
end.not_to raise_error

expect(string_io.string).to include("Options [:unsupported] are not supported and will not be applied to the event.")
end

context "when event is a transaction" do
it "transaction.set_context merges and takes precedence over scope.set_context" do
scope.set_context(:foo, { val: 42 })
Expand Down
10 changes: 5 additions & 5 deletions sentry-ruby/spec/sentry/scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@

describe '#update_from_options' do
it 'updates data from arguments' do
subject.update_from_options(
result = subject.update_from_options(
contexts: { context: 1 },
extra: { foo: 42 },
tags: { tag: 2 },
Expand All @@ -354,12 +354,12 @@
expect(subject.user).to eq({ name: 'jane' })
expect(subject.level).to eq(:info)
expect(subject.fingerprint).to eq('ABCD')
expect(result).to eq([])
end

it 'does not throw when arbitrary options passed' do
expect do
subject.update_from_options(foo: 42)
end.not_to raise_error
it 'returns unsupported option keys' do
result = subject.update_from_options(foo: 42, bar: 43)
expect(result).to eq([:foo, :bar])
end
end
end

0 comments on commit af47fa6

Please sign in to comment.