Sha256: 3cb4105acb69cfdf75d8b60bc1ec84f60a490dce97dbbda4ddd512c6efcedde2

Contents?: true

Size: 1.86 KB

Versions: 13

Compression:

Stored size: 1.86 KB

Contents

require 'ddtrace/contrib/active_record/event'

module Datadog
  module Contrib
    module ActiveRecord
      module Events
        # Defines instrumentation for sql.active_record event
        module SQL
          include ActiveRecord::Event

          EVENT_NAME = 'sql.active_record'.freeze
          SPAN_NAME = 'active_record.sql'.freeze

          module_function

          def event_name
            self::EVENT_NAME
          end

          def span_name
            self::SPAN_NAME
          end

          def process(span, event, _id, payload)
            config = Utils.connection_config(payload[:connection_id])
            settings = Datadog.configuration[:active_record, config]
            adapter_name = Datadog::Utils::Database.normalize_vendor(config[:adapter])
            service_name = !settings.nil? ? settings.service_name : configuration[:service_name]

            span.name = "#{adapter_name}.query"
            span.service = service_name
            span.resource = payload.fetch(:sql)
            span.span_type = Datadog::Ext::SQL::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')

            span.set_tag('active_record.db.vendor', adapter_name)
            span.set_tag('active_record.db.name', config[:database])
            span.set_tag('active_record.db.cached', cached) if cached
            span.set_tag('out.host', config[:host]) if config[:host]
            span.set_tag('out.port', config[:port]) if config[:port]
          rescue StandardError => e
            Datadog::Tracer.log.debug(e.message)
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ddtrace-0.16.1 lib/ddtrace/contrib/active_record/events/sql.rb
ddtrace-0.15.0.internaltracinfeature1 lib/ddtrace/contrib/active_record/events/sql.rb
ddtrace-0.16.0 lib/ddtrace/contrib/active_record/events/sql.rb
ddtrace-0.14.2.disableprotocolversion4 lib/ddtrace/contrib/active_record/events/sql.rb
ddtrace-0.15.0 lib/ddtrace/contrib/active_record/events/sql.rb
ddtrace-0.14.2.withoutpriorityparsing1 lib/ddtrace/contrib/active_record/events/sql.rb
ddtrace-0.14.2 lib/ddtrace/contrib/active_record/events/sql.rb
ddtrace-0.14.1 lib/ddtrace/contrib/active_record/events/sql.rb
ddtrace-0.15.0.beta1 lib/ddtrace/contrib/active_record/events/sql.rb
ddtrace-0.14.0 lib/ddtrace/contrib/active_record/events/sql.rb
ddtrace-0.14.0.rc1 lib/ddtrace/contrib/active_record/events/sql.rb
ddtrace-0.14.0.beta2 lib/ddtrace/contrib/active_record/events/sql.rb
ddtrace-0.14.0.beta1 lib/ddtrace/contrib/active_record/events/sql.rb