Skip to content

Commit

Permalink
fix: capture host during init
Browse files Browse the repository at this point in the history
  • Loading branch information
arielvalentin committed Dec 19, 2021
1 parent ef1f302 commit a195f6c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ module Client

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

def initialize(**kwargs)
@_otel_init_host = kwargs[:host]
super
end

def query(sql)
attributes = client_attributes
attributes = {
::OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM => 'mysql',
::OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => net_peer_name
}

case config[:db_statement]
when :obfuscate
Expand Down Expand Up @@ -104,10 +112,12 @@ def database_span_name(sql)
'mysql'
end

def client_attributes
attributes = { ::OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM => 'mysql' }
attributes[::OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME] = @connected_host if defined?(@connected_host)
attributes
def net_peer_name
if defined?(@connected_host)
@connected_host
else
@_otel_init_host
end
end

def tracer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM]).must_equal 'mysql'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_NAME]).must_be_nil
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::DB_STATEMENT]).must_equal 'SELECT ?'
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME]).must_be_nil
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME]).must_equal(host)
end

it 'extracts statement type' do
Expand Down Expand Up @@ -125,7 +125,7 @@
_(span.name).must_equal 'select'
_(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_be_nil
_(span.attributes[OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME]).must_equal(host)

client.query('SELECT 1')

Expand All @@ -134,6 +134,7 @@
_(last_span.name).must_equal 'select'
_(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
end
end
Expand Down

0 comments on commit a195f6c

Please sign in to comment.