Skip to content

Commit

Permalink
feat: Support prepend SQL comment for PG instrumentation (#690)
Browse files Browse the repository at this point in the history
Co-authored-by: Ariel Valentin <arielvalentin@users.noreply.github.com>
Co-authored-by: Kayla Reopelle (she/her) <87386821+kaylareopelle@users.noreply.github.com>
  • Loading branch information
3 people authored May 10, 2024
1 parent 7905d11 commit 081a3b0
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 @@ -112,7 +112,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 081a3b0

Please sign in to comment.