-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: dup string if frozen in trilogy query (#863)
* fix: propagate context from trilogy instrumentation for frozen strings
- Loading branch information
1 parent
6eaadb4
commit a2669ad
Showing
3 changed files
with
50 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -375,14 +375,52 @@ | |
describe 'when propagator is set to vitess' do | ||
let(:config) { { propagator: 'vitess' } } | ||
|
||
it 'does inject context' do | ||
it 'does inject context on frozen strings' do | ||
sql = 'SELECT * from users where users.id = 1 and users.email = "[email protected]"' | ||
assert(sql.frozen?) | ||
propagator = OpenTelemetry::Instrumentation::Trilogy::Instrumentation.instance.propagator | ||
|
||
arg_cache = {} # maintain handles to args | ||
allow(client).to receive(:query).and_wrap_original do |m, *args| | ||
arg_cache[:query_input] = args[0] | ||
assert(args[0].frozen?) | ||
m.call(args[0]) | ||
end | ||
|
||
allow(propagator).to receive(:inject).and_wrap_original do |m, *args| | ||
arg_cache[:inject_input] = args[0] | ||
refute(args[0].frozen?) | ||
assert_match(sql, args[0]) | ||
m.call(args[0], context: args[1][:context]) | ||
end | ||
|
||
expect do | ||
client.query(sql) | ||
end.must_raise Trilogy::Error | ||
|
||
# arg_cache[:inject_input] _was_ a mutable string, so it has the context injected | ||
encoded = Base64.strict_encode64("{\"uber-trace-id\":\"#{span.hex_trace_id}:#{span.hex_span_id}:0:1\"}") | ||
assert_equal(arg_cache[:inject_input], "/*VT_SPAN_CONTEXT=#{encoded}*/#{sql}") | ||
|
||
# arg_cache[:inject_input] is now frozen | ||
assert(arg_cache[:inject_input].frozen?) | ||
end | ||
|
||
it 'does inject context on unfrozen strings' do | ||
# inbound SQL is not frozen (string prefixed with +) | ||
sql = +'SELECT * from users where users.id = 1 and users.email = "[email protected]"' | ||
original_sql = sql.dup | ||
refute(sql.frozen?) | ||
|
||
# dup sql for comparison purposes, since propagator mutates it | ||
cached_sql = sql.dup | ||
|
||
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}" | ||
assert_equal(sql, "/*VT_SPAN_CONTEXT=#{encoded}*/#{cached_sql}") | ||
refute(sql.frozen?) | ||
end | ||
end | ||
|
||
|