Skip to content

Commit

Permalink
Implement Mysql2 integration configuration (#559)
Browse files Browse the repository at this point in the history
* Refactored: Mysql2 to use Datadog::Contrib::Integration.

* Fixed: Mysql2 tests not working locally.
  • Loading branch information
delner authored Sep 26, 2018
1 parent 57c29ff commit 4a5e313
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 30 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
14 changes: 8 additions & 6 deletions lib/ddtrace/contrib/mysql2/client.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -34,21 +36,21 @@ 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

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]
)
Expand Down
16 changes: 16 additions & 0 deletions lib/ddtrace/contrib/mysql2/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions lib/ddtrace/contrib/mysql2/ext.rb
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions lib/ddtrace/contrib/mysql2/integration.rb
Original file line number Diff line number Diff line change
@@ -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
34 changes: 12 additions & 22 deletions lib/ddtrace/contrib/mysql2/patcher.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/ddtrace/contrib/mysql2/patcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 4a5e313

Please sign in to comment.