Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@
_(span.attributes['net.peer.name']).must_equal host.to_s
_(span.attributes['net.peer.port']).must_equal port.to_s
end

it 'encodes invalid byte sequences for db.statement' do
# \255 is off-limits https://en.wikipedia.org/wiki/UTF-8#Codepage_layout
sql = "SELECT * from users where users.id = 1 and users.email = '[email protected]\255'"
obfuscated_sql = 'SELECT * from users where users.id = ? and users.email = ?'

expect do
client.query(sql)
end.must_raise Mysql2::Error

_(span.name).must_equal 'mysql'
_(span.attributes['db.statement']).must_equal obfuscated_sql
end
end

describe 'when db_statement set as omit' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,13 @@ def obfuscate_sql(sql)
if sql.size > 2000
'SQL query too large to remove sensitive data ...'
else
obfuscated = sql.gsub(FULL_SQL_REGEXP, '?')
obfuscated = OpenTelemetry::Common::Utilities.utf8_encode(sql, binary: true)
obfuscated = obfuscated.gsub(FULL_SQL_REGEXP, '?')
obfuscated = 'Failed to obfuscate SQL query - quote characters remained after obfuscation' if detect_unmatched_pairs(obfuscated)
obfuscated
end
rescue StandardError
'OpenTelemetry error: failed to obfuscate sql'
end

def detect_unmatched_pairs(obfuscated)
Expand Down Expand Up @@ -140,6 +143,7 @@ def extract_statement_type(sql)
QUERY_NAME_RE.match(sql) { |match| match[1].downcase } unless sql.nil?
rescue StandardError => e
OpenTelemetry.logger.error("Error extracting sql statement type: #{e.message}")
nil
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
port: port,
username: username,
password: password,
database: database,
ssl: false
}
end
Expand Down Expand Up @@ -230,6 +231,19 @@
_(span.name).must_equal 'select'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_equal obfuscated_sql
end

it 'encodes invalid byte sequences for db.statement' do
# \255 is off-limits https://en.wikipedia.org/wiki/UTF-8#Codepage_layout
sql = "SELECT * from users where users.id = 1 and users.email = '[email protected]\255'"
obfuscated_sql = 'SELECT * from users where users.id = ? and users.email = ?'

expect do
client.query(sql)
end.must_raise Trilogy::Error

_(span.name).must_equal 'mysql'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_equal obfuscated_sql
end
end

describe 'when db_statement is set to omit' do
Expand All @@ -245,5 +259,64 @@
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_be_nil
end
end

describe 'when db_statement is configured via environment variable' do
describe 'when db_statement set as omit' do
it 'omits db.statement attribute' do
OpenTelemetry::TestHelpers.with_env('OTEL_RUBY_INSTRUMENTATION_TRILOGY_CONFIG_OPTS' => 'db_statement=omit;') do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install
sql = "SELECT * from users where users.id = 1 and users.email = '[email protected]'"
expect do
client.query(sql)
end.must_raise Trilogy::Error

_(span.attributes['db.system']).must_equal 'mysql'
_(span.name).must_equal 'select'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_be_nil
end
end
end

describe 'when db_statement set as obfuscate' do
it 'obfuscates SQL parameters in db.statement' do
OpenTelemetry::TestHelpers.with_env('OTEL_RUBY_INSTRUMENTATION_TRILOGY_CONFIG_OPTS' => 'db_statement=obfuscate;') do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install

sql = "SELECT * from users where users.id = 1 and users.email = '[email protected]'"
obfuscated_sql = 'SELECT * from users where users.id = ? and users.email = ?'
expect do
client.query(sql)
end.must_raise Trilogy::Error

_(span.attributes['db.system']).must_equal 'mysql'
_(span.name).must_equal 'select'
_(span.attributes['db.statement']).must_equal obfuscated_sql
end
end
end

describe 'when db_statement is set differently than local config' do
let(:config) { { db_statement: :omit } }

it 'overrides local config and obfuscates SQL parameters in db.statement' do
OpenTelemetry::TestHelpers.with_env('OTEL_RUBY_INSTRUMENTATION_TRILOGY_CONFIG_OPTS' => 'db_statement=obfuscate') do
instrumentation.instance_variable_set(:@installed, false)
instrumentation.install

sql = "SELECT * from users where users.id = 1 and users.email = '[email protected]'"
obfuscated_sql = 'SELECT * from users where users.id = ? and users.email = ?'
expect do
client.query(sql)
end.must_raise Trilogy::Error

_(span.attributes['db.system']).must_equal 'mysql'
_(span.name).must_equal 'select'
_(span.attributes['db.statement']).must_equal obfuscated_sql
end
end
end
end
end
end