Skip to content

Commit

Permalink
Make stuff work with older rails
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Sep 5, 2023
1 parent 2354803 commit 3be7f22
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
28 changes: 20 additions & 8 deletions sentry-rails/lib/sentry/rails/tracing/active_record_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Check warning on line 21 in sentry-rails/lib/sentry/rails/tracing/active_record_subscriber.rb

View check run for this annotation

Codecov / codecov/patch

sentry-rails/lib/sentry/rails/tracing/active_record_subscriber.rb#L21

Added line #L21 was not covered by tests

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

Check warning on line 25 in sentry-rails/lib/sentry/rails/tracing/active_record_subscriber.rb

View check run for this annotation

Codecov / codecov/patch

sentry-rails/lib/sentry/rails/tracing/active_record_subscriber.rb#L24-L25

Added lines #L24 - L25 were not covered by tests
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

Check warning on line 33 in sentry-rails/lib/sentry/rails/tracing/active_record_subscriber.rb

View check run for this annotation

Codecov / codecov/patch

sentry-rails/lib/sentry/rails/tracing/active_record_subscriber.rb#L32-L33

Added lines #L32 - L33 were not covered by tests
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 3be7f22

Please sign in to comment.