diff --git a/instrumentation/trilogy/README.md b/instrumentation/trilogy/README.md index 2d2183f5a1..cf2b1058b6 100644 --- a/instrumentation/trilogy/README.md +++ b/instrumentation/trilogy/README.md @@ -63,7 +63,7 @@ This gem requires Trilogy 2.11 or higher and will not work with a future Trilogy | `obfuscation_limit` | `2000` | Maximum length of the obfuscated SQL statement. Statements exceeding this limit are truncated. | | `peer_service` | `nil` | Deprecated with no replacement. Sets the `peer.service` attribute on spans (old semantic conventions only). | | `propagator` | `'none'` | Propagator for injecting trace context into SQL comments. `'none'` disables propagation, `'tracecontext'` uses W3C Trace Context, `'vitess'` uses Vitess-style propagation (requires `opentelemetry-propagator-vitess` gem). | -| `record_exception` | `true` | Records exceptions as span events when an error occurs. | +| `record_exception` | `true` when using old semconv, otherwise `false` | Records exceptions as span events when an error occurs. | | `span_name` | `:statement_type` | Controls span naming (old semantic conventions only). `:statement_type` uses the SQL operation (e.g., `SELECT`), `:db_name` uses the database name, `:db_operation_and_name` combines both. | ## Semantic Conventions diff --git a/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb b/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb index b78f97bff5..434a617216 100644 --- a/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb +++ b/instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy/instrumentation.rb @@ -46,7 +46,7 @@ module Trilogy # # ### `:record_exception` # - # Records exceptions as span events when an error occurs. Default is `true`. + # Records exceptions as span events when an error occurs. Default is `true` for old semconv, otherwise `false`. # # ### `:span_name` # @@ -91,7 +91,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base option :span_name, default: :statement_type, validate: %I[statement_type db_name db_operation_and_name] option :obfuscation_limit, default: 2000, validate: :integer option :propagator, default: 'none', validate: %w[none tracecontext vitess] - option :record_exception, default: true, validate: :boolean + option :record_exception, default: nil, validate: :boolean attr_reader :propagator, :semconv @@ -100,6 +100,8 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base def require_dependencies @semconv = determine_semconv + @config[:record_exception] = (@semconv == :old) if @config[:record_exception].nil? + case @semconv when :old require_relative 'patches/old/client' diff --git a/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/patches/dup/instrumentation_test.rb b/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/patches/dup/instrumentation_test.rb index a19551967b..5c29116235 100644 --- a/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/patches/dup/instrumentation_test.rb +++ b/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/patches/dup/instrumentation_test.rb @@ -333,6 +333,7 @@ end describe 'when queries fail' do + let(:config) { { record_exception: true } } it 'sets span status to error' do expect do client.query('SELECT INVALID') @@ -392,6 +393,18 @@ _(exporter.finished_spans.last.attributes['error.type']).must_equal 'Trilogy::ConnectionClosed' end + describe 'when record_exception is default' do + let(:config) { {} } + + it 'does not record exception when record_exception is default' do + expect do + client.query('SELECT INVALID') + end.must_raise Trilogy::Error + + _(span.events).must_be_nil + end + end + describe 'when record_exception is false' do let(:config) { { record_exception: false } } diff --git a/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/patches/stable/instrumentation_test.rb b/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/patches/stable/instrumentation_test.rb index c50d7996cc..dbe5143438 100644 --- a/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/patches/stable/instrumentation_test.rb +++ b/instrumentation/trilogy/test/opentelemetry/instrumentation/trilogy/patches/stable/instrumentation_test.rb @@ -240,6 +240,8 @@ end describe 'when queries fail' do + let(:config) { { record_exception: true } } + it 'sets span status to error' do expect do client.query('SELECT INVALID') @@ -291,6 +293,17 @@ _(exporter.finished_spans.last.attributes['error.type']).must_equal 'Trilogy::ConnectionClosed' end + describe 'when record_exception is default' do + let(:config) { {} } + it 'does not record exception when record_exception is default' do + expect do + client.query('SELECT INVALID') + end.must_raise Trilogy::Error + + _(span.events).must_be_nil + end + end + describe 'when record_exception is false' do let(:config) { { record_exception: false } }