Skip to content

Commit

Permalink
feat: Support prepend SQL comment for PG instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
keiko713 authored and simi committed Jan 11, 2024
1 parent 8b0c63e commit 676425c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def span_attrs(kind, *args)

def extract_operation(sql)
# From: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/9244a08a8d014afe26b82b91cf86e407c2599d73/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts#L35
sql.to_s.split[0].to_s.upcase
# Ignores prepend comment
comment_regex = %r{\A\/\*.*?\*\/}m
sql.to_s.sub(comment_regex, '').split[0].to_s.upcase
end

def span_name(operation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@
end
end

it 'ignores prepend comment to extract operation' do
client.query('/* comment */ SELECT 1')

_(span.name).must_equal 'SELECT postgres'
_(span.attributes['db.system']).must_equal 'postgresql'
_(span.attributes['db.name']).must_equal 'postgres'
_(span.attributes['db.statement']).must_equal '/* comment */ SELECT 1'
_(span.attributes['db.operation']).must_equal 'SELECT'
_(span.attributes['net.peer.name']).must_equal host.to_s
_(span.attributes['net.peer.port']).must_equal port.to_i
end

it 'only caches 50 prepared statement names' do
51.times { |i| client.prepare("foo#{i}", "SELECT $1 AS foo#{i}") }
client.exec_prepared('foo0', [1])
Expand Down

0 comments on commit 676425c

Please sign in to comment.