From 256b48785d98cde8dd64af59d8ccab4ba131ed6a Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Wed, 4 Mar 2026 13:46:34 +0100 Subject: [PATCH] fix: Don't transform attributes in place in metrics --- sentry-ruby/lib/sentry/metric_event.rb | 2 +- sentry-ruby/spec/sentry/metric_event_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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",