Skip to content

Commit

Permalink
UTF-8 encode extract_statement sql
Browse files Browse the repository at this point in the history
  • Loading branch information
kaylareopelle committed Oct 10, 2023
1 parent 5fa5039 commit b8b2605
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion helpers/mysql/lib/opentelemetry/helpers/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0module OpenTelemetry
require 'opentelemetry-common'

module OpenTelemetry
module Helpers
Expand Down Expand Up @@ -36,7 +37,7 @@ class MySQL
'create table'
].freeze

QUERY_NAME_REGEX = Regexp.new("(#{QUERY_NAMES.join('|')})", Regexp::IGNORECASE)
QUERY_NAME_REGEX = Regexp.new("\\b(#{QUERY_NAMES.join('|')})\\b", Regexp::IGNORECASE)

# This is a span naming utility intended for use in MySQL database
# adapter instrumentation.
Expand All @@ -62,6 +63,8 @@ def self.database_span_name(sql, operation, database_name, config)

# @api private
def self.extract_statement_type(sql)
sql = OpenTelemetry::Common::Utilities.utf8_encode(sql, binary: true)

QUERY_NAME_REGEX.match(sql) { |match| match[1].downcase } unless sql.nil?
rescue StandardError => e
OpenTelemetry.handle_error(message: 'Error extracting SQL statement type', exception: e)
Expand Down
1 change: 1 addition & 0 deletions helpers/mysql/opentelemetry-helpers-mysql.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 3.0'

spec.add_dependency 'opentelemetry-api', '~> 1.0'
spec.add_dependency 'opentelemetry-common', '~> 0.20'

spec.add_development_dependency 'bundler', '~> 2.4'
spec.add_development_dependency 'minitest', '~> 5.0'
Expand Down
17 changes: 17 additions & 0 deletions helpers/mysql/test/helpers/mysql_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@
end
end

describe 'when sql contains invalid byte sequences' do
let(:sql) { "SELECT * from users where users.id = 1 and users.email = 'test@test.com\255'" }

it 'extracts the statement' do
assert_equal('select', extract_statement_type)
end
end

describe 'when sql contains unknown query statement' do
let(:sql) { 'DESELECT 1' }

# nil sets the span name to 'mysql'
it 'returns nil' do
assert_nil(extract_statement_type)
end
end

describe 'when sql has marginalia-style prepended comments' do
let(:sql) do
'/*application:web,controller:blob,action:show,correlation_id:01EZVMR923313VV44ZJDJ7PMEZ,' \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
client.query(sql)
end.must_raise Mysql2::Error

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
client.query(sql)
end.must_raise Trilogy::Error

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

Expand Down

0 comments on commit b8b2605

Please sign in to comment.