Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add ActiveRecord SQL caller stack #1483

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/ddtrace/contrib/active_record/events/sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def span_name
end

def process(span, event, _id, payload)
caller_path = Utils.extract_caller_path(caller)
config = Utils.connection_config(payload[:connection], payload[:connection_id])
settings = Datadog.configuration[:active_record, config]
adapter_name = Datadog::Utils::Database.normalize_vendor(config[:adapter])
Expand Down Expand Up @@ -56,6 +57,7 @@ def process(span, event, _id, payload)
span.set_tag(Ext::TAG_DB_VENDOR, adapter_name)
span.set_tag(Ext::TAG_DB_NAME, config[:database])
span.set_tag(Ext::TAG_DB_CACHED, cached) if cached
span.set_tag(Ext::SPAN_CALLER_STACK, caller_path) if caller_path.present? # Only supported under Rails
span.set_tag(Datadog::Ext::NET::TARGET_HOST, config[:host]) if config[:host]
span.set_tag(Datadog::Ext::NET::TARGET_PORT, config[:port]) if config[:port]
rescue StandardError => e
Expand Down
1 change: 1 addition & 0 deletions lib/ddtrace/contrib/active_record/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Ext
ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_ACTIVE_RECORD_ANALYTICS_SAMPLE_RATE'.freeze
ENV_ANALYTICS_SAMPLE_RATE_OLD = 'DD_ACTIVE_RECORD_ANALYTICS_SAMPLE_RATE'.freeze
SERVICE_NAME = 'active_record'.freeze
SPAN_CALLER_STACK = 'active_record.query_caller_stack'.freeze
SPAN_INSTANTIATION = 'active_record.instantiation'.freeze
SPAN_SQL = 'active_record.sql'.freeze
SPAN_TYPE_INSTANTIATION = 'custom'.freeze
Expand Down
11 changes: 11 additions & 0 deletions lib/ddtrace/contrib/active_record/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ def self.db_config(connection_pool)
connection_pool.spec.config
end
end

def self.extract_caller_path(callers)
return nil unless defined?(::Rails)

caller_path = callers
.select { |c| c =~ /^#{::Rails.root}/ }
.map { |c| c.gsub ::Rails.root.to_s, '' }[0...5]
# Handle cases when the db query is triggered from inside of a Rubygem
caller_path = callers[0...5] if caller_path.nil?
caller_path.join(",\n")
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/ddtrace/contrib/sinatra/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def migrate_db
expect(sqlite_span.get_tag('active_record.db.name')).to eq(':memory:')
expect(sqlite_span.get_tag('out.host')).to eq(adapter_host.to_s) unless adapter_host.nil?
expect(sqlite_span.get_tag('out.port')).to eq(adapter_port.to_s) unless adapter_port.nil?
expect(
sqlite_span.get_tag('active_record.query_caller_stack').split("\n").size
).to eq(5)
expect(sqlite_span.span_type).to eq(Datadog::Ext::SQL::TYPE)
expect(sqlite_span).to_not have_error
expect(sqlite_span.parent).to eq(route_span)
Expand Down