diff --git a/sentry-rails/lib/sentry/rails/tracing/active_record_subscriber.rb b/sentry-rails/lib/sentry/rails/tracing/active_record_subscriber.rb index bc41b7111..761e24b7f 100644 --- a/sentry-rails/lib/sentry/rails/tracing/active_record_subscriber.rb +++ b/sentry-rails/lib/sentry/rails/tracing/active_record_subscriber.rb @@ -13,19 +13,31 @@ def self.subscribe! next if EXCLUDED_EVENTS.include? payload[:name] record_on_current_span(op: SPAN_PREFIX + event_name, start_timestamp: payload[START_TIMESTAMP_NAME], description: payload[:sql], duration: duration) do |span| - span.set_data(:connection_id, payload[:connection_id]) if payload[:connection_id] span.set_tag(:cached, true) if payload.fetch(:cached, false) # cached key is only set for hits in the QueryCache, from Rails 5.1 - if payload[:connection] - db_config = payload[:connection].pool.db_config.configuration_hash + connection = payload[:connection] - span.set_data(Span::DataConventions::DB_NAME, db_config[:database]) - span.set_data(Span::DataConventions::DB_SYSTEM, db_config[:adapter]) + if payload[:connection_id] + span.set_data(:connection_id, payload[:connection_id]) - span.set_data(Span::DataConventions::SERVER_ADDRESS, db_config[:host]) if db_config[:host] - span.set_data(Span::DataConventions::SERVER_PORT, db_config[:port]) if db_config[:port] - span.set_data(Span::DataConventions::SERVER_SOCKET_ADDRESS, db_config[:socket]) if db_config[:socket] + # we fallback to the base connection on rails < 6.0.0 since the payload doesn't have it + base_connection = ActiveRecord::Base.connection + connection ||= base_connection if payload[:connection_id] == base_connection.object_id end + + next unless connection + + db_config = if connection.pool.respond_to?(:db_config) + connection.pool.db_config.configuration_hash + elsif connection.pool.respond_to?(:spec) + connection.pool.spec.config + end + + span.set_data(Span::DataConventions::DB_NAME, db_config[:database]) if db_config[:database] + span.set_data(Span::DataConventions::DB_SYSTEM, db_config[:adapter]) if db_config[:adapter] + span.set_data(Span::DataConventions::SERVER_ADDRESS, db_config[:host]) if db_config[:host] + span.set_data(Span::DataConventions::SERVER_PORT, db_config[:port]) if db_config[:port] + span.set_data(Span::DataConventions::SERVER_SOCKET_ADDRESS, db_config[:socket]) if db_config[:socket] end end end diff --git a/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb index e081717e7..21dbb86e4 100644 --- a/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb @@ -34,7 +34,7 @@ expect(span[:trace_id]).to eq(transaction.dig(:contexts, :trace, :trace_id)) data = span[:data] - expect(data["db.name"]).to eq("db") + expect(data["db.name"]).to include("db") expect(data["db.system"]).to eq("sqlite3") end @@ -61,7 +61,7 @@ expect(cached_query_span[:tags]).to include({cached: true}) data = cached_query_span[:data] - expect(data["db.name"]).to eq("db") + expect(data["db.name"]).to include("db") expect(data["db.system"]).to eq("sqlite3") end end