lib/ddtrace/contrib/rails/framework.rb in ddtrace-0.12.0.beta2 vs lib/ddtrace/contrib/rails/framework.rb in ddtrace-0.12.0.rc1

- old
+ new

@@ -1,15 +1,15 @@ require 'ddtrace/pin' require 'ddtrace/ext/app_types' +require 'ddtrace/contrib/active_record/patcher' require 'ddtrace/contrib/grape/endpoint' require 'ddtrace/contrib/rack/middlewares' require 'ddtrace/contrib/rails/core_extensions' require 'ddtrace/contrib/rails/action_controller' require 'ddtrace/contrib/rails/action_view' -require 'ddtrace/contrib/rails/active_record' require 'ddtrace/contrib/rails/active_support' require 'ddtrace/contrib/rails/utils' module Datadog module Contrib @@ -19,41 +19,56 @@ # - handle configuration entries which are specific to Datadog tracing # - instrument parts of the framework when needed module Framework # configure Datadog settings def self.setup - config = Datadog.configuration[:rails] - config[:service_name] ||= Utils.app_name - tracer = config[:tracer] + config = config_with_defaults + activate_rack!(config) + activate_active_record!(config) + set_service_info!(config) + + # By default, default service would be guessed from the script + # being executed, but here we know better, get it from Rails config. + config[:tracer].default_service = config[:service_name] + end + + def self.config_with_defaults + # We set defaults here instead of in the patcher because we need to wait + # for the Rails application to be fully initialized. + Datadog.configuration[:rails].tap do |config| + config[:service_name] ||= Utils.app_name + config[:database_service] ||= "#{config[:service_name]}-#{Contrib::ActiveRecord::Utils.adapter_name}" + config[:controller_service] ||= config[:service_name] + config[:cache_service] ||= "#{config[:service_name]}-cache" + end + end + + def self.activate_rack!(config) Datadog.configuration.use( :rack, - tracer: tracer, + tracer: config[:tracer], + application: ::Rails.application, service_name: config[:service_name], + middleware_names: config[:middleware_names], distributed_tracing: config[:distributed_tracing] ) - - config[:controller_service] ||= config[:service_name] - config[:cache_service] ||= "#{config[:service_name]}-cache" - - tracer.set_service_info(config[:controller_service], 'rails', Ext::AppTypes::WEB) - tracer.set_service_info(config[:cache_service], 'rails', Ext::AppTypes::CACHE) - set_database_service - - # By default, default service would be guessed from the script - # being executed, but here we know better, get it from Rails config. - tracer.default_service = config[:service_name] end - def self.set_database_service + def self.activate_active_record!(config) return unless defined?(::ActiveRecord) - config = Datadog.configuration[:rails] - adapter_name = Utils.adapter_name - config[:database_service] ||= "#{config[:service_name]}-#{adapter_name}" - config[:tracer].set_service_info(config[:database_service], adapter_name, Ext::AppTypes::DB) - rescue => e - Tracer.log.warn("Unable to get database config (#{e}), skipping ActiveRecord instrumentation") + Datadog.configuration.use( + :active_record, + service_name: config[:database_service], + tracer: config[:tracer] + ) + end + + def self.set_service_info!(config) + tracer = config[:tracer] + tracer.set_service_info(config[:controller_service], 'rails', Ext::AppTypes::WEB) + tracer.set_service_info(config[:cache_service], 'rails', Ext::AppTypes::CACHE) end end end end end