Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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,7 @@
_(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'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
option :peer_service, default: nil, validate: :string
option :db_statement, default: :obfuscate, validate: %I[omit include obfuscate]
option :span_name, default: :statement_type, validate: %I[statement_type db_name db_operation_and_name]
option :obfuscation_limit, default: 2000, validate: :integer

private

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ def client_attributes(sql)
end

def obfuscate_sql(sql)
if sql.size > 2000
'SQL query too large to remove sensitive data ...'
if sql.size > config[:obfuscation_limit]
truncated_sql = sql[..sql.index(FULL_SQL_REGEXP) - 1]
truncated_sql + "...\nSQL truncated (> #{config[:obfuscation_limit]} characters)"
else
obfuscated = OpenTelemetry::Common::Utilities.utf8_encode(sql, binary: true)
obfuscated = obfuscated.gsub(FULL_SQL_REGEXP, '?')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,20 @@
_(span.name).must_equal 'mysql'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_equal obfuscated_sql
end

describe 'with obfuscation_limit' do
let(:config) { { db_statement: :obfuscate, obfuscation_limit: 10 } }

it 'truncates SQL using config limit' do
sql = "SELECT * from users where users.id = 1 and users.email = '[email protected]'"
obfuscated_sql = "SELECT * from users where users.id = ...\nSQL truncated (> 10 characters)"
expect do
client.query(sql)
end.must_raise Trilogy::Error

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

describe 'when db_statement is set to omit' do
Expand Down