diff --git a/sentry-ruby/lib/sentry/metric_event.rb b/sentry-ruby/lib/sentry/metric_event.rb index ea161a501..0116fee76 100644 --- a/sentry-ruby/lib/sentry/metric_event.rb +++ b/sentry-ruby/lib/sentry/metric_event.rb @@ -43,7 +43,7 @@ def to_h private def serialize_attributes - @attributes.transform_values! { |v| attribute_hash(v) } + @attributes.transform_values { |v| attribute_hash(v) } end end end diff --git a/sentry-ruby/spec/sentry/metric_event_spec.rb b/sentry-ruby/spec/sentry/metric_event_spec.rb index 5806aab16..cf6708c79 100644 --- a/sentry-ruby/spec/sentry/metric_event_spec.rb +++ b/sentry-ruby/spec/sentry/metric_event_spec.rb @@ -117,6 +117,18 @@ expect(attributes["unknown"][:value]).to include("Object") end + it "does not mutate the original attributes hash" do + attributes = { "foo" => "bar" } + event1 = described_class.new(name: "test.metric", type: :counter, value: 1, attributes: attributes) + event1.to_h + + event2 = described_class.new(name: "test.metric", type: :counter, value: 1, attributes: attributes) + hash = event2.to_h + + expect(attributes).to eq({ "foo" => "bar" }) + expect(hash[:attributes]["foo"]).to eq({ type: "string", value: "bar" }) + end + it "merges custom attributes with default attributes" do event = described_class.new( name: "test.metric",