Skip to content

Commit

Permalink
revision
Browse files Browse the repository at this point in the history
  • Loading branch information
xuan-cao-swi committed Feb 9, 2024
1 parent 84231ec commit f7c6d1e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,19 @@ module Patches
# Module to prepend to Mysql2::Client for instrumentation
module Client
def query(sql, options = {})
attributes = client_attributes
attributes.merge!(span_attributes(sql))

tracer.in_span(
span_name(sql),
attributes: attributes.merge!(OpenTelemetry::Instrumentation::Mysql2.attributes),
_otel_span_name(sql),
attributes: _otel_span_attributes(sql),
kind: :client
) do
super(sql, options)
end
end

def prepare(sql)
attributes = client_attributes
attributes.merge!(span_attributes(sql))

tracer.in_span(
span_name(sql),
attributes: attributes.merge!(OpenTelemetry::Instrumentation::Mysql2.attributes),
_otel_span_name(sql),
attributes: _otel_span_attributes(sql),
kind: :client
) do
super(sql)
Expand All @@ -41,19 +35,19 @@ def prepare(sql)

private

def span_name(sql)
def _otel_span_name(sql)
OpenTelemetry::Helpers::MySQL.database_span_name(
sql,
OpenTelemetry::Instrumentation::Mysql2.attributes[
SemanticConventions::Trace::DB_OPERATION
],
database_name,
_otel_database_name,
config
)
end

def span_attributes(sql)
attributes = {}
def _otel_span_attributes(sql)
attributes = _otel_client_attributes
case config[:db_statement]
when :include
attributes[SemanticConventions::Trace::DB_STATEMENT] = sql
Expand All @@ -63,15 +57,18 @@ def span_attributes(sql)
sql, obfuscation_limit: config[:obfuscation_limit], adapter: :mysql
)
end

attributes.merge!(OpenTelemetry::Instrumentation::Mysql2.attributes)
attributes.compact!
attributes
end

def database_name
def _otel_database_name
# https://github.com/brianmario/mysql2/blob/ca08712c6c8ea672df658bb25b931fea22555f27/lib/mysql2/client.rb#L78
(query_options[:database] || query_options[:dbname] || query_options[:db])&.to_s
end

def client_attributes
def _otel_client_attributes
# The client specific attributes can be found via the query_options instance variable
# exposed on the mysql2 Client
# https://github.com/brianmario/mysql2/blob/ca08712c6c8ea672df658bb25b931fea22555f27/lib/mysql2/client.rb#L25-L26
Expand All @@ -83,8 +80,9 @@ def client_attributes
SemanticConventions::Trace::NET_PEER_NAME => host,
SemanticConventions::Trace::NET_PEER_PORT => port
}
attributes[SemanticConventions::Trace::DB_NAME] = database_name if database_name
attributes[SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service] if config[:peer_service]

attributes[SemanticConventions::Trace::DB_NAME] = _otel_database_name
attributes[SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service]
attributes
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
end

it 'query ? sequences for db.statement with prepare' do
sql = "SELECT * from users where users.id = ? and users.email = ?"
sql = 'SELECT * from users where users.id = ? and users.email = ?'
expect do
client.prepare(sql)
end.must_raise Mysql2::Error
Expand All @@ -126,12 +126,12 @@
end

it 'query invalid byte sequences for db.statement without prepare' do
sql = "SELECT * from users where users.id = ? and users.email = ?"
sql = 'SELECT * from users where users.id = ? and users.email = ?'
expect do
client.query(sql)
end.must_raise Mysql2::Error

_(span.events[0].attributes['exception.message'].slice(0,37)).must_equal "You have an error in your SQL syntax;"
_(span.events[0].attributes['exception.message'].slice(0, 37)).must_equal 'You have an error in your SQL syntax;'
end
end

Expand Down

0 comments on commit f7c6d1e

Please sign in to comment.