Skip to content

Commit

Permalink
feat: add option for untraced commands
Browse files Browse the repository at this point in the history
  • Loading branch information
zacheryph committed May 17, 2024
1 parent c4e9fc8 commit 83190ef
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
7 changes: 6 additions & 1 deletion instrumentation/pg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ OpenTelemetry::SDK.configure do |c|

# By default, this instrumentation obfuscate/sanitize the executed SQL as the `db.statement`
# semantic attribute. Optionally, you may disable the inclusion of this attribute entirely by
# setting this option to :omit or disbale sanitization the attribute by setting to :include
# setting this option to :omit or disable sanitization the attribute by setting to :include
db_statement: :include,

# Add certain commands that you do not want to produce spans within traces. This can
# be useful if you find something like PREPARE statements too noisy in your trace data.
# note: these must be uppercase to match the command.
untraced_commands: %w[PREPARE DEALLOCATE],
}
end
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
option :peer_service, default: nil, validate: :string
option :db_statement, default: :obfuscate, validate: %I[omit include obfuscate]
option :obfuscation_limit, default: 2000, validate: :integer
option :untraced_commands, default: [], validate: :array

private

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ module Patches
module Connection # rubocop:disable Metrics/ModuleLength
PG::Constants::EXEC_ISH_METHODS.each do |method|
define_method method do |*args, &block|
span_name, attrs = span_attrs(:query, *args)
tracer.in_span(span_name, attributes: attrs, kind: :client) do
within_span(:query, args) do
if block
block.call(super(*args))
else
Expand All @@ -29,17 +28,15 @@ module Connection # rubocop:disable Metrics/ModuleLength

PG::Constants::PREPARE_ISH_METHODS.each do |method|
define_method method do |*args|
span_name, attrs = span_attrs(:prepare, *args)
tracer.in_span(span_name, attributes: attrs, kind: :client) do
within_span(:prepare, args) do
super(*args)
end
end
end

PG::Constants::EXEC_PREPARED_ISH_METHODS.each do |method|
define_method method do |*args, &block|
span_name, attrs = span_attrs(:execute, *args)
tracer.in_span(span_name, attributes: attrs, kind: :client) do
within_span(:execute, args) do
if block
block.call(super(*args))
else
Expand All @@ -51,6 +48,16 @@ module Connection # rubocop:disable Metrics/ModuleLength

private

def within_span(kind, args, &block)
span_name, attrs = span_attrs(kind, *args)

if config[:untraced_commands].include?(attrs['db.operation'])
yield
else
tracer.in_span(span_name, attributes: attrs, kind: :client, &block)
end
end

def obfuscate_sql(sql)
return sql unless config[:db_statement] == :obfuscate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@
_(span.attributes['net.peer.name']).must_equal host.to_s
_(span.attributes['net.peer.port']).must_equal port.to_i
end

describe 'untraced commands' do
let(:config) { { untraced_commands: ['SELECT'] } }

it 'does not produce a span' do
_(span).must_be_nil
end
end
end

%i[exec_params async_exec_params sync_exec_params].each do |method|
Expand All @@ -130,6 +138,14 @@
_(span.attributes['net.peer.name']).must_equal host.to_s
_(span.attributes['net.peer.port']).must_equal port.to_i
end

describe 'untraced commands' do
let(:config) { { untraced_commands: ['SELECT'] } }

it 'does not produce a span' do
_(span).must_be_nil
end
end
end

%i[prepare async_prepare sync_prepare].each do |method|
Expand All @@ -145,6 +161,14 @@
_(span.attributes['net.peer.name']).must_equal host.to_s
_(span.attributes['net.peer.port']).must_equal port.to_i
end

describe 'untraced commands' do
let(:config) { { untraced_commands: ['PREPARE'] } }

it 'does not produce a span' do
_(span).must_be_nil
end
end
end

%i[exec_prepared async_exec_prepared sync_exec_prepared].each do |method|
Expand All @@ -161,6 +185,14 @@
_(last_span.attributes['net.peer.name']).must_equal host.to_s
_(last_span.attributes['net.peer.port']).must_equal port.to_i
end

describe 'untraced commands' do
let(:config) { { untraced_commands: ['EXECUTE'] } }

it 'does not produce a span' do
_(span).must_be_nil
end
end
end

%i[exec query sync_exec async_exec].each do |method|
Expand All @@ -175,6 +207,14 @@
_(span.attributes['net.peer.name']).must_equal host.to_s
_(span.attributes['net.peer.port']).must_equal port.to_i
end

describe 'untraced commands' do
let(:config) { { untraced_commands: ['SELECT'] } }

it 'does not produce a span' do
_(span).must_be_nil
end
end
end

it 'ignores prepend comment to extract operation' do
Expand Down

0 comments on commit 83190ef

Please sign in to comment.