Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Separate logical MySQL host from connected host #487

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ module Client # rubocop:disable Metrics/ModuleLength

FULL_SQL_REGEXP = Regexp.union(MYSQL_COMPONENTS.map { |component| COMPONENTS_REGEX_MAP[component] })

def initialize(args)
@_otel_net_peer_name = args[:host]
super
end

def query(sql)
tracer.in_span(
database_span_name(sql),
Expand All @@ -68,11 +63,12 @@ def query(sql)
def client_attributes(sql)
attributes = {
::OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM => 'mysql',
::OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => net_peer_name
::OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => connection_options.fetch(:host, 'unknown sock')
}

attributes[::OpenTelemetry::SemanticConventions::Trace::DB_NAME] = database_name if database_name
attributes[::OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service] unless config[:peer_service].nil?
attributes['db.mysql.instance.address'] = @connected_host if defined?(@connected_host)

case config[:db_statement]
when :obfuscate
Expand Down Expand Up @@ -135,16 +131,6 @@ def database_name
connection_options[:database]
end

def net_peer_name
if defined?(@connected_host)
@connected_host
elsif @_otel_net_peer_name
@_otel_net_peer_name
else
'unknown sock'
end
end

def tracer
Trilogy::Instrumentation.instance.tracer
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,24 @@
describe '#compatible?' do
describe 'when an unsupported version is installed' do
it 'is incompatible' do
stub_const('Trilogy::VERSION', '2.0.0.beta') do
_(instrumentation.compatible?).must_equal false
end
stub_const('Trilogy::VERSION', '2.2.0')
_(instrumentation.compatible?).must_equal false

stub_const('Trilogy::VERSION', '3.0.0') do
_(instrumentation.compatible?).must_equal false
end
stub_const('Trilogy::VERSION', '2.3.0.beta')
_(instrumentation.compatible?).must_equal false

stub_const('Trilogy::VERSION', '3.0.0')
_(instrumentation.compatible?).must_equal false
end
end

describe 'when supported version is installed' do
it 'is compatible' do
stub_const('Trilogy::VERSION', '2.0.0') do
_(instrumentation.compatible?).must_equal true
end
stub_const('Trilogy::VERSION', '2.3.0')
_(instrumentation.compatible?).must_equal true

stub_const('Trilogy::VERSION', '3.0.0.rc1') do
_(instrumentation.compatible?).must_equal true
end
stub_const('Trilogy::VERSION', '3.0.0.rc1')
_(instrumentation.compatible?).must_equal true
end
end
end
Expand Down Expand Up @@ -128,20 +127,18 @@
client.query('SELECT 1')

_(span.name).must_equal 'select'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_NAME]).must_equal(database)
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM]).must_equal 'mysql'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_equal 'SELECT ?'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME]).must_equal(host)
end

it 'includes database name' do
it 'includes database connection information' do
client.query('SELECT 1')

_(span.name).must_equal 'select'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_NAME]).must_equal(database)
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM]).must_equal 'mysql'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_equal 'SELECT ?'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME]).must_equal(host)
_(span.attributes['db.mysql.instance.address']).must_be_nil
end

it 'extracts statement type' do
Expand Down Expand Up @@ -175,6 +172,7 @@
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM]).must_equal 'mysql'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_equal 'select @@hostname'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME]).must_equal(host)
_(span.attributes['db.mysql.instance.address']).must_be_nil

client.query('SELECT 1')

Expand All @@ -184,8 +182,8 @@
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_NAME]).must_equal(database)
_(last_span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM]).must_equal 'mysql'
_(last_span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_equal 'SELECT ?'
_(last_span.attributes[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME]).wont_equal(host)
_(last_span.attributes[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME]).must_equal client.connected_host
_(last_span.attributes[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME]).must_equal(host)
_(last_span.attributes['db.mysql.instance.address']).must_equal client.connected_host
end
end

Expand Down