Skip to content

Commit

Permalink
Refactored: Racecar to use Datadog::Contrib::Integration. (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Sep 21, 2018
1 parent 54c54d4 commit 55ba1bc
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def configure(target = configuration, opts = {})
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/mongodb/patcher'
require 'ddtrace/contrib/mysql2/patcher'
require 'ddtrace/contrib/racecar/patcher'
require 'ddtrace/contrib/racecar/integration'
require 'ddtrace/contrib/rack/integration'
require 'ddtrace/contrib/rails/integration'
require 'ddtrace/contrib/rake/patcher'
Expand Down
22 changes: 22 additions & 0 deletions lib/ddtrace/contrib/racecar/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'ddtrace/contrib/configuration/settings'

module Datadog
module Contrib
module Racecar
module Configuration
# Custom settings for the Rack integration
class Settings < Contrib::Configuration::Settings
option :service_name, default: 'racecar'
option :tracer, default: Datadog.tracer do |value|
(value || Datadog.tracer).tap do |v|
# Make sure to update tracers of all subscriptions
Events.subscriptions.each do |subscription|
subscription.tracer = v
end
end
end
end
end
end
end
end
38 changes: 38 additions & 0 deletions lib/ddtrace/contrib/racecar/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/racecar/configuration/settings'
require 'ddtrace/contrib/racecar/patcher'

module Datadog
module Contrib
module Racecar
# Description of Racecar integration
class Integration
include Contrib::Integration

APP = 'racecar'.freeze

register_as :racecar, auto_patch: false

def self.version
Gem.loaded_specs['racecar'] && Gem.loaded_specs['racecar'].version
end

def self.present?
super && defined?(::Racecar)
end

def self.compatible?
super && defined?(::ActiveSupport::Notifications)
end

def default_configuration
Configuration::Settings.new
end

def patcher
Patcher
end
end
end
end
end
64 changes: 23 additions & 41 deletions lib/ddtrace/contrib/racecar/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,36 @@
require 'ddtrace/contrib/patcher'
require 'ddtrace/ext/app_types'
require 'ddtrace/contrib/racecar/events'

module Datadog
module Contrib
module Racecar
# Provides instrumentation for `racecar` through ActiveSupport instrumentation signals
# Patcher enables patching of 'racecar' module.
module Patcher
include Base
include Contrib::Patcher

register_as :racecar
option :service_name, default: 'racecar'
option :tracer, default: Datadog.tracer do |value|
(value || Datadog.tracer).tap do |v|
# Make sure to update tracers of all subscriptions
Events.subscriptions.each do |subscription|
subscription.tracer = v
end
end
end

class << self
def patch
return patched? if patched? || !compatible?

# Subscribe to Racecar events
Events.subscribe!

# Set service info
configuration[:tracer].set_service_info(
configuration[:service_name],
'racecar',
Ext::AppTypes::WORKER
)

@patched = true
end

def patched?
return @patched if defined?(@patched)
@patched = false
end

private
module_function

def configuration
Datadog.configuration[:racecar]
end
def patched?
done?(:racecar)
end

def compatible?
defined?(::Racecar) && defined?(::ActiveSupport::Notifications)
def patch
do_once(:racecar) do
begin
# Subscribe to Racecar events
Events.subscribe!

# Set service info
configuration = Datadog.configuration[:racecar]
configuration[:tracer].set_service_info(
configuration[:service_name],
Integration::APP,
Ext::AppTypes::WORKER
)
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply Racecar integration: #{e}")
end
end
end
end
Expand Down

0 comments on commit 55ba1bc

Please sign in to comment.