From 246db3720b965448de3855adc8e8f040fd24b9a0 Mon Sep 17 00:00:00 2001 From: Sam Handler Date: Fri, 9 Feb 2024 11:11:50 -0700 Subject: [PATCH] dup string if frozen in trilogy query --- .../instrumentation/trilogy/patches/client.rb | 1 + .../instrumentation/trilogy/instrumentation_test.rb | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/patches/client.rb b/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/patches/client.rb index 1f7ae1fae8..237e4582e2 100644 --- a/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/patches/client.rb +++ b/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/patches/client.rb @@ -50,6 +50,7 @@ def query(sql) ), kind: :client ) do |_span, context| + sql = sql.dup unless !sql.frozen? || propagator.is_a?(OpenTelemetry::Instrumentation::Trilogy::NoopPropagator) propagator.inject(sql, context: context) super(sql) end diff --git a/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb b/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb index 0bc3a63241..f10dc214d4 100644 --- a/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb +++ b/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb @@ -376,13 +376,14 @@ let(:config) { { propagator: 'vitess' } } it 'does inject context' do - sql = +'SELECT * from users where users.id = 1 and users.email = "test@test.com"' - original_sql = sql.dup + sql = 'SELECT * from users where users.id = 1 and users.email = "test@test.com"'.freeze + propagator = double("propagator") + allow(propagator).to receive(:inject) + allow(client).to receive(:propagator).and_return(propagator) expect do client.query(sql) end.must_raise Trilogy::Error - encoded = Base64.strict_encode64("{\"uber-trace-id\":\"#{span.hex_trace_id}:#{span.hex_span_id}:0:1\"}") - _(sql).must_equal "/*VT_SPAN_CONTEXT=#{encoded}*/#{original_sql}" + expect(propagator).to have_received(:inject).with(sql, context: instance_of(OpenTelemetry::Context)).once end end