From 4a5e3131215550928d5c6faa42e4554e3bd30a3c Mon Sep 17 00:00:00 2001 From: David Elner Date: Wed, 26 Sep 2018 16:09:06 -0400 Subject: [PATCH] Implement Mysql2 integration configuration (#559) * Refactored: Mysql2 to use Datadog::Contrib::Integration. * Fixed: Mysql2 tests not working locally. --- docker-compose.yml | 1 + lib/ddtrace.rb | 2 +- lib/ddtrace/contrib/mysql2/client.rb | 14 ++++---- .../contrib/mysql2/configuration/settings.rb | 16 +++++++++ lib/ddtrace/contrib/mysql2/ext.rb | 15 ++++++++ lib/ddtrace/contrib/mysql2/integration.rb | 32 +++++++++++++++++ lib/ddtrace/contrib/mysql2/patcher.rb | 34 +++++++------------ spec/ddtrace/contrib/mysql2/patcher_spec.rb | 2 +- 8 files changed, 86 insertions(+), 30 deletions(-) create mode 100644 lib/ddtrace/contrib/mysql2/configuration/settings.rb create mode 100644 lib/ddtrace/contrib/mysql2/ext.rb create mode 100644 lib/ddtrace/contrib/mysql2/integration.rb diff --git a/docker-compose.yml b/docker-compose.yml index d1dcd7b4c0..5215531fe8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -217,6 +217,7 @@ services: mysql: image: mysql:5.6 environment: + - MYSQL_DATABASE=$TEST_MYSQL_DB - MYSQL_ROOT_PASSWORD=$TEST_MYSQL_ROOT_PASSWORD - MYSQL_PASSWORD=$TEST_MYSQL_PASSWORD - MYSQL_USER=$TEST_MYSQL_USER diff --git a/lib/ddtrace.rb b/lib/ddtrace.rb index 50e71b36d6..51aee4dc2b 100644 --- a/lib/ddtrace.rb +++ b/lib/ddtrace.rb @@ -59,7 +59,7 @@ def configure(target = configuration, opts = {}) require 'ddtrace/contrib/http/integration' require 'ddtrace/contrib/integration' require 'ddtrace/contrib/mongodb/patcher' -require 'ddtrace/contrib/mysql2/patcher' +require 'ddtrace/contrib/mysql2/integration' require 'ddtrace/contrib/racecar/integration' require 'ddtrace/contrib/rack/integration' require 'ddtrace/contrib/rails/integration' diff --git a/lib/ddtrace/contrib/mysql2/client.rb b/lib/ddtrace/contrib/mysql2/client.rb index 91a8498dba..0a1ea48746 100644 --- a/lib/ddtrace/contrib/mysql2/client.rb +++ b/lib/ddtrace/contrib/mysql2/client.rb @@ -1,5 +1,7 @@ -require 'ddtrace/ext/sql' require 'ddtrace/ext/app_types' +require 'ddtrace/ext/net' +require 'ddtrace/ext/sql' +require 'ddtrace/contrib/mysql2/ext' module Datadog module Contrib @@ -34,13 +36,13 @@ module InstanceMethods end def query(sql, options = {}) - datadog_pin.tracer.trace('mysql2.query') do |span| + datadog_pin.tracer.trace(Ext::SPAN_QUERY) do |span| span.resource = sql span.service = datadog_pin.service span.span_type = Datadog::Ext::SQL::TYPE - span.set_tag('mysql2.db.name', query_options[:database]) - span.set_tag('out.host', query_options[:host]) - span.set_tag('out.port', query_options[:port]) + span.set_tag(Ext::TAG_DB_NAME, query_options[:database]) + span.set_tag(Datadog::Ext::NET::TARGET_HOST, query_options[:host]) + span.set_tag(Datadog::Ext::NET::TARGET_PORT, query_options[:port]) super(sql, options) end end @@ -48,7 +50,7 @@ def query(sql, options = {}) def datadog_pin @datadog_pin ||= Datadog::Pin.new( Datadog.configuration[:mysql2][:service_name], - app: 'mysql2', + app: Ext::APP, app_type: Datadog::Ext::AppTypes::DB, tracer: Datadog.configuration[:mysql2][:tracer] ) diff --git a/lib/ddtrace/contrib/mysql2/configuration/settings.rb b/lib/ddtrace/contrib/mysql2/configuration/settings.rb new file mode 100644 index 0000000000..27cde8bb78 --- /dev/null +++ b/lib/ddtrace/contrib/mysql2/configuration/settings.rb @@ -0,0 +1,16 @@ +require 'ddtrace/contrib/configuration/settings' +require 'ddtrace/contrib/mysql2/ext' + +module Datadog + module Contrib + module Mysql2 + module Configuration + # Custom settings for the Mysql2 integration + class Settings < Contrib::Configuration::Settings + option :service_name, default: Ext::SERVICE_NAME + option :tracer, default: Datadog.tracer + end + end + end + end +end diff --git a/lib/ddtrace/contrib/mysql2/ext.rb b/lib/ddtrace/contrib/mysql2/ext.rb new file mode 100644 index 0000000000..1e6cfc5055 --- /dev/null +++ b/lib/ddtrace/contrib/mysql2/ext.rb @@ -0,0 +1,15 @@ +module Datadog + module Contrib + module Mysql2 + # Mysql2 integration constants + module Ext + APP = 'mysql2'.freeze + SERVICE_NAME = 'mysql2'.freeze + + SPAN_QUERY = 'mysql2.query'.freeze + + TAG_DB_NAME = 'mysql2.db.name'.freeze + end + end + end +end diff --git a/lib/ddtrace/contrib/mysql2/integration.rb b/lib/ddtrace/contrib/mysql2/integration.rb new file mode 100644 index 0000000000..fd7b81aa91 --- /dev/null +++ b/lib/ddtrace/contrib/mysql2/integration.rb @@ -0,0 +1,32 @@ +require 'ddtrace/contrib/integration' +require 'ddtrace/contrib/mysql2/configuration/settings' +require 'ddtrace/contrib/mysql2/patcher' + +module Datadog + module Contrib + module Mysql2 + # Description of Mysql2 integration + class Integration + include Contrib::Integration + + register_as :mysql2 + + def self.version + Gem.loaded_specs['mysql2'] && Gem.loaded_specs['mysql2'].version + end + + def self.present? + super && defined?(::Mysql2) + end + + def default_configuration + Configuration::Settings.new + end + + def patcher + Patcher + end + end + end + end +end diff --git a/lib/ddtrace/contrib/mysql2/patcher.rb b/lib/ddtrace/contrib/mysql2/patcher.rb index 27dbe65a39..24f9aa9e53 100644 --- a/lib/ddtrace/contrib/mysql2/patcher.rb +++ b/lib/ddtrace/contrib/mysql2/patcher.rb @@ -1,37 +1,27 @@ +require 'ddtrace/contrib/patcher' require 'ddtrace/contrib/mysql2/client' module Datadog module Contrib module Mysql2 - # Mysql2 patcher + # Patcher enables patching of 'mysql2' module. module Patcher - include Base - - register_as :mysql2 - option :service_name, default: 'mysql2' - option :tracer, default: Datadog.tracer - - @patched = false + include Contrib::Patcher module_function - def patch - return @patched if patched? || !compatible? - - patch_mysql2_client - - @patched = true - rescue StandardError => e - Tracer.log.error("Unable to apply mysql2 integration: #{e}") - @patched - end - def patched? - @patched + done?(:mysql2) end - def compatible? - defined?(::Mysql2) + def patch + do_once(:mysql2) do + begin + patch_mysql2_client + rescue StandardError => e + Datadog::Tracer.log.error("Unable to apply mysql2 integration: #{e}") + end + end end def patch_mysql2_client diff --git a/spec/ddtrace/contrib/mysql2/patcher_spec.rb b/spec/ddtrace/contrib/mysql2/patcher_spec.rb index 8d883da6f3..44924f9421 100644 --- a/spec/ddtrace/contrib/mysql2/patcher_spec.rb +++ b/spec/ddtrace/contrib/mysql2/patcher_spec.rb @@ -19,7 +19,7 @@ let(:host) { ENV.fetch('TEST_MYSQL_HOST') { '127.0.0.1' } } let(:port) { ENV.fetch('TEST_MYSQL_PORT') { '3306' } } let(:database) { ENV.fetch('TEST_MYSQL_DB') { 'mysql' } } - let(:username) { ENV.fetch('TEST_MYSQL_USERNAME') { 'root' } } + let(:username) { ENV.fetch('TEST_MYSQL_USER') { 'root' } } let(:password) { ENV.fetch('TEST_MYSQL_PASSWORD') { 'root' } } let(:pin) { client.datadog_pin }