Skip to content

Commit

Permalink
feat(trilogy): instrument connect and ping (#704)
Browse files Browse the repository at this point in the history
These can be just as slow as a query if not slower.

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
  • Loading branch information
casperisfine and byroot authored Oct 21, 2023
1 parent 27448c4 commit 6e7f8da
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ module Client # rubocop:disable Metrics/ModuleLength

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

def initialize(options = {})
@connection_options = options # This is normally done by Trilogy#initialize

tracer.in_span(
'connect',
attributes: client_attributes.merge!(OpenTelemetry::Instrumentation::Trilogy.attributes),
kind: :client
) do
super
end
end

def ping(...)
tracer.in_span(
'ping',
attributes: client_attributes.merge!(OpenTelemetry::Instrumentation::Trilogy.attributes),
kind: :client
) do
super
end
end

def query(sql)
tracer.in_span(
database_span_name(sql),
Expand All @@ -60,22 +82,24 @@ def query(sql)

private

def client_attributes(sql)
def client_attributes(sql = nil)
attributes = {
::OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM => 'mysql',
::OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => connection_options.fetch(:host, 'unknown sock')
::OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => connection_options&.fetch(:host, 'unknown sock') || 'unknown sock'
}

attributes[::OpenTelemetry::SemanticConventions::Trace::DB_NAME] = database_name if database_name
attributes[::OpenTelemetry::SemanticConventions::Trace::DB_USER] = database_user if database_user
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
attributes[::OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT] = obfuscate_sql(sql)
when :include
attributes[::OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT] = sql
if sql
case config[:db_statement]
when :obfuscate
attributes[::OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT] = obfuscate_sql(sql)
when :include
attributes[::OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT] = sql
end
end

attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
describe OpenTelemetry::Instrumentation::Trilogy do
let(:instrumentation) { OpenTelemetry::Instrumentation::Trilogy::Instrumentation.instance }
let(:exporter) { EXPORTER }
let(:span) { exporter.finished_spans.first }
let(:span) { exporter.finished_spans[1] }
let(:config) { {} }
let(:driver_options) do
{
Expand Down Expand Up @@ -166,6 +166,37 @@
end
end

describe 'when connecting' do
let(:span) { exporter.finished_spans.first }

it 'spans will include database name' do
_(client.connected_host).wont_be_nil

_(span.name).must_equal 'connect'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_NAME]).must_equal(database)
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_USER]).must_equal(username)
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM]).must_equal 'mysql'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME]).must_equal(host)
_(span.attributes['db.mysql.instance.address']).must_be_nil
end
end

describe 'when pinging' do
let(:span) { exporter.finished_spans[2] }

it 'spans will include database name' do
_(client.connected_host).wont_be_nil

client.ping

_(span.name).must_equal 'ping'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_NAME]).must_equal(database)
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_USER]).must_equal(username)
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM]).must_equal 'mysql'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME]).must_equal(host)
end
end

describe 'when quering for the connected host' do
it 'spans will include the net.peer.name attribute' do
_(client.connected_host).wont_be_nil
Expand Down

0 comments on commit 6e7f8da

Please sign in to comment.