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

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

Class Method Details

.instrumentObject



10
11
12
13
14
15
16
17
18
# File 'lib/ddtrace/contrib/rails/active_record.rb', line 10

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

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

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



20
21
22
23
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
# File 'lib/ddtrace/contrib/rails/active_record.rb', line 20

def self.sql(_name, start, finish, _id, payload)
  tracer = ::Rails.configuration.datadog_trace.fetch(:tracer)
  database_service = ::Rails.configuration.datadog_trace.fetch(:default_database_service)
  adapter_name = ::ActiveRecord::Base.connection_config[:adapter]
  adapter_name = Datadog::Contrib::Rails::Utils.normalize_vendor(adapter_name)
  span_type = Datadog::Ext::SQL::TYPE

  span = tracer.trace(
    "#{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.
  # TODO[manu]: this feature has been merged into master but has not yet released.
  # We're supporting this action as a best effort, but we should add a test after
  # a new version of Rails is out.
  cached = payload[:cached]

  # 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', adapter_name)
  span.set_tag('rails.db.cached', cached) if cached
  span.start_time = start
  span.finish(finish)
rescue StandardError => e
  Datadog::Tracer.log.error(e.message)
end