Skip to content

Commit

Permalink
fix: update pg instrumentation to handle non primitive argument (#1146)
Browse files Browse the repository at this point in the history
* fix: update pg instrumentation to handle non primitive argument

Co-authored-by: Ariel Valentin <arielvalentin@users.noreply.github.com>
  • Loading branch information
ericmustin and arielvalentin committed Mar 29, 2022
1 parent 9beb5e8 commit 8eac4a1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ def lru_cache
def span_attrs(kind, *args) # rubocop:disable Metrics/AbcSize
if kind == :query
operation = extract_operation(args[0])
sql = obfuscate_sql(args[0])
sql = obfuscate_sql(args[0]).to_s
else
statement_name = args[0]

if kind == :prepare
sql = obfuscate_sql(args[1])
sql = obfuscate_sql(args[1]).to_s
lru_cache[statement_name] = sql
operation = 'PREPARE'
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'opentelemetry-api', '~> 1.0'
spec.add_dependency 'opentelemetry-instrumentation-base', '~> 0.19.0'

spec.add_development_dependency 'activerecord'
spec.add_development_dependency 'appraisal', '~> 2.2.0'
spec.add_development_dependency 'bundler', '>= 1.17'
spec.add_development_dependency 'minitest', '~> 5.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# SPDX-License-Identifier: Apache-2.0

require 'test_helper'
require 'active_record'
require 'pg'

require_relative '../../../../lib/opentelemetry/instrumentation/pg'
Expand Down Expand Up @@ -126,6 +127,20 @@
end
end

%i[exec query sync_exec async_exec].each do |method|
it "after request using Arel (with method: #{method})" do
client.send(method, Arel.sql('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 '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_s
end
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
1 change: 1 addition & 0 deletions instrumentation/pg/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# SPDX-License-Identifier: Apache-2.0

require 'active_record'
require 'pg'

require 'opentelemetry/sdk'
Expand Down

0 comments on commit 8eac4a1

Please sign in to comment.