Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set_context doesn't allow to overwrite values #1722

Closed
thomasjachmann opened this issue Feb 11, 2022 · 0 comments · Fixed by #1724
Closed

set_context doesn't allow to overwrite values #1722

thomasjachmann opened this issue Feb 11, 2022 · 0 comments · Fixed by #1724
Assignees
Projects
Milestone

Comments

@thomasjachmann
Copy link
Contributor

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
@st0012 st0012 added this to To do in 5.x via automation Feb 11, 2022
@st0012 st0012 added this to the 5.2.0 milestone Feb 11, 2022
5.x automation moved this from To do to Done Feb 11, 2022
st0012 pushed a commit that referenced this issue Feb 11, 2022
* Fix context merging

When using the same key in a context hash, the new value should
overwrite the old value. Before, the value having been set first would
always win.

fixes #1722

* Add changelog
@st0012 st0012 modified the milestones: 5.2.0, 5.1.1 Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
5.x
Done
Development

Successfully merging a pull request may close this issue.

2 participants