Skip to content

Commit 55b9d89

Browse files
committed
Trilogy: introduce record_exception setting
1 parent 4189529 commit 55b9d89

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
2828
option :span_name, default: :statement_type, validate: %I[statement_type db_name db_operation_and_name]
2929
option :obfuscation_limit, default: 2000, validate: :integer
3030
option :propagator, default: nil, validate: :string
31+
option :record_exception, default: true, validate: :boolean
3132

3233
attr_reader :propagator
3334

instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/patches/client.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module Client
1616
def initialize(options = {})
1717
@connection_options = options # This is normally done by Trilogy#initialize
1818

19-
tracer.in_span(
19+
in_span(
2020
'connect',
2121
attributes: client_attributes.merge!(OpenTelemetry::Instrumentation::Trilogy.attributes),
2222
kind: :client
@@ -26,7 +26,7 @@ def initialize(options = {})
2626
end
2727

2828
def ping(...)
29-
tracer.in_span(
29+
in_span(
3030
'ping',
3131
attributes: client_attributes.merge!(OpenTelemetry::Instrumentation::Trilogy.attributes),
3232
kind: :client
@@ -36,7 +36,7 @@ def ping(...)
3636
end
3737

3838
def query(sql)
39-
tracer.in_span(
39+
in_span(
4040
OpenTelemetry::Helpers::MySQL.database_span_name(
4141
sql,
4242
OpenTelemetry::Instrumentation::Trilogy.attributes[
@@ -64,6 +64,19 @@ def query(sql)
6464

6565
private
6666

67+
def in_span(name, attributes: nil, kind: nil)
68+
span = tracer.start_span(name, attributes: attributes, kind: kind)
69+
OpenTelemetry::Trace.with_span(span) { |s, c| yield s, c }
70+
rescue Exception => e # rubocop:disable Lint/RescueException
71+
if config[:record_exception]
72+
span&.record_exception(e)
73+
end
74+
span&.status = OpenTelemetry::Trace::Status.error("Exception of type: #{e.class}")
75+
raise e
76+
ensure
77+
span&.finish
78+
end
79+
6780
def client_attributes(sql = nil)
6881
attributes = {
6982
::OpenTelemetry::SemanticConventions::Trace::DB_SYSTEM => 'mysql',

instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/instrumentation_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,18 @@
277277
_(span.events.first.attributes['exception.message']).wont_be_nil
278278
_(span.events.first.attributes['exception.stacktrace']).wont_be_nil
279279
end
280+
281+
describe 'when record_exception is false' do
282+
let(:config) { { record_exception: false } }
283+
284+
it 'does not record exception when record_exception is false' do
285+
expect do
286+
client.query('SELECT INVALID')
287+
end.must_raise Trilogy::Error
288+
289+
_(span.events).must_be_nil
290+
end
291+
end
280292
end
281293

282294
describe 'when db_statement is set to include' do

0 commit comments

Comments
 (0)