Module: Datadog::Contrib::Rails::ActiveRecord

Includes:
Patcher
Defined in:
lib/ddtrace/contrib/rails/active_record.rb

Overview

Code used to create and handle ‘mysql.query’, ‘postgres.query’, … spans.

Class Method Summary collapse

Methods included from Patcher

included

Methods included from Patcher::CommonMethods

#do_once, #without_warnings

Class Method Details

.instrumentObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ddtrace/contrib/rails/active_record.rb', line 12

def self.instrument
  # ActiveRecord is instrumented only if it's available
  return unless defined?(::ActiveRecord)

  do_once(:instrument) do
    # subscribe when the active record query has been processed
    ::ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
      sql(*args)
    end
  end
end

.sql(_name, start, finish, _id, payload) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ddtrace/contrib/rails/active_record.rb', line 24

def self.sql(_name, start, finish, _id, payload)
  tracer = Datadog.configuration[:rails][:tracer]
  database_service = Datadog.configuration[:rails][:database_service]
  connection_config = Datadog::Contrib::Rails::Utils.connection_config(payload[:connection_id])
  span_type = Datadog::Ext::SQL::TYPE

  span = tracer.trace(
    "#{connection_config[:adapter_name]}.query",
    resource: payload.fetch(:sql),
    service: database_service,
    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('rails.db.vendor', connection_config[:adapter_name])
  span.set_tag('rails.db.name', connection_config[:database_name])
  span.set_tag('rails.db.cached', cached) if cached
  span.set_tag('out.host', connection_config[:adapter_host])
  span.set_tag('out.port', connection_config[:adapter_port])
  span.start_time = start
  span.finish(finish)
rescue StandardError => e
  Datadog::Tracer.log.error(e.message)
end