Skip to content

Commit

Permalink
Added: cached tag for ActiveRecord integrations outside of Rails
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Jan 5, 2018
1 parent 7472ab5 commit 5935934
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/ddtrace/contrib/active_record/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ def self.sql(_name, start, finish, _id, payload)
span_type: span_type
)

# Find out if the SQL query has been cached in this request. This meta is really
# helpful to users because some spans may have 0ns of duration because the query
# is simply cached from memory, so the notification is fired with start == finish.
cached = payload[:cached] || (payload[:name] == 'CACHE')

# the span should have the query ONLY in the Resource attribute,
# so that the ``sql.query`` tag will be set in the agent with an
# obfuscated version
span.span_type = Datadog::Ext::SQL::TYPE
span.set_tag('active_record.db.vendor', adapter_name)
span.set_tag('active_record.db.name', database_name)
span.set_tag('active_record.db.cached', cached) if cached
span.set_tag('out.host', adapter_host)
span.set_tag('out.port', adapter_port)
span.start_time = start
Expand Down
53 changes: 53 additions & 0 deletions test/contrib/sinatra/tracer_activerecord_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,27 @@
require 'contrib/sinatra/tracer_test_base'

class TracerActiveRecordTest < TracerTestBase
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

class Article < ApplicationRecord
end

class TracerActiveRecordTestApp < Sinatra::Application
post '/request' do
conn = settings.datadog_test_conn
conn.connection.execute('SELECT 42')
''
end

post '/cached_request' do
Article.cache do
# Do two queries (second should cache.)
Article.count
Article.count
end
end
end

def app
Expand All @@ -32,6 +47,18 @@ def setup
super
end

def migrate_db
Article.exists?
rescue ActiveRecord::StatementInvalid
ActiveRecord::Schema.define(version: 20180101000000) do
create_table 'articles', force: :cascade do |t|
t.string 'title'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
end
end
end

def test_request
post '/request'
assert_equal(200, last_response.status)
Expand Down Expand Up @@ -66,4 +93,30 @@ def test_request
assert_equal(0, sinatra_span.status)
assert_nil(sinatra_span.parent)
end

# Testing AR query caching requires use of a model.
# Create a model, query it a few times, make sure cached tag gets set.
def test_cached_tag
# Make sure Article table exists
migrate_db

# Do query with cached query
post '/cached_request'

# Assert correct number of spans (ignoring transactions, etc.)
spans = all_spans.select { |s| s.resource == 'SELECT COUNT(*) FROM "articles"' }
assert_equal(2, spans.length)

# Assert cached flag not present on first query
assert_nil(spans.first.get_tag('active_record.db.cached'))

# Assert cached flag set correctly on second query
assert_equal('true', spans.last.get_tag('active_record.db.cached'))
end

private

def all_spans
@writer.spans(:keep)
end
end

0 comments on commit 5935934

Please sign in to comment.