Skip to content

set_context doesn't allow to overwrite values #1722

Closed

Description

Issue Description

When calling set_context multiple times with the same context key and the same keys within the value, the value hashes will be merged into one another. But if the same keys are used within these hashes, the new values won't overwrite the old ones.

Example:
When iterating over objects and doing possibly problematic things, I'd like to recognize which object caused the error in the resulting sentry issue. But only the first value put into the context will be submitted. In my opinion (and the usecase above), it would be more intuitive to let the new values overwrite the old ones.

Reproduction Steps

object_ids = [1, 2, 3]

object_ids.each do |id|
  Sentry.configure_scope { |scope| scope.set_context(:objects, object_id: id) }
  Sentry.capture_message("cannot handle object") if id == 2
end

Expected Behavior

The context :objects should be { object_id: 2 }.

Actual Behavior

The context :objects is { object_id: 1 }, always the first item in the list.

This is due to the merge direction:

def set_contexts(contexts_hash)
check_argument_type!(contexts_hash, Hash)
@contexts.merge!(contexts_hash) do |key, old, new|
new.merge(old)
end
end

In line 176, the merge direction should be reversed:

old.merge(new)

Ruby Version

3.0.0

SDK Version

5.1.0

Integration and Its Version

Sentry::Rack::CaptureExceptions

Sentry Config

Sentry.init do |config|
  config.dsn = "..."
  config.environment = "..."
  config.release = "..."
  config.breadcrumbs_logger = [:sentry_logger, :http_logger]
  config.traces_sample_rate = 1.0
end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions