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 24, 2018
1 parent 9089bb1 commit 41f1621
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 54 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
23 changes: 23 additions & 0 deletions lib/ddtrace/contrib/racecar/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'ddtrace/contrib/configuration/settings'
require 'ddtrace/contrib/racecar/ext'

module Datadog
module Contrib
module Racecar
module Configuration
# Custom settings for the Rack integration
class Settings < Contrib::Configuration::Settings
option :service_name, default: Ext::SERVICE_NAME
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
13 changes: 7 additions & 6 deletions lib/ddtrace/contrib/racecar/event.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'ddtrace/contrib/active_support/notifications/event'
require 'ddtrace/contrib/racecar/ext'

module Datadog
module Contrib
Expand Down Expand Up @@ -35,12 +36,12 @@ def process(span, event, _id, payload)
span.service = configuration[:service_name]
span.resource = payload[:consumer_class]

span.set_tag('kafka.topic', payload[:topic])
span.set_tag('kafka.consumer', payload[:consumer_class])
span.set_tag('kafka.partition', payload[:partition])
span.set_tag('kafka.offset', payload[:offset]) if payload.key?(:offset)
span.set_tag('kafka.first_offset', payload[:first_offset]) if payload.key?(:first_offset)
span.set_tag('kafka.message_count', payload[:message_count]) if payload.key?(:message_count)
span.set_tag(Ext::TAG_TOPIC, payload[:topic])
span.set_tag(Ext::TAG_CONSUMER, payload[:consumer_class])
span.set_tag(Ext::TAG_PARTITION, payload[:partition])
span.set_tag(Ext::TAG_OFFSET, payload[:offset]) if payload.key?(:offset)
span.set_tag(Ext::TAG_FIRST_OFFSET, payload[:first_offset]) if payload.key?(:first_offset)
span.set_tag(Ext::TAG_MESSAGE_COUNT, payload[:message_count]) if payload.key?(:message_count)
span.set_error(payload[:exception_object]) if payload[:exception_object]
end

Expand Down
4 changes: 2 additions & 2 deletions lib/ddtrace/contrib/racecar/events/batch.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'ddtrace/contrib/racecar/ext'
require 'ddtrace/contrib/racecar/event'

module Datadog
Expand All @@ -9,7 +10,6 @@ module Batch
include Racecar::Event

EVENT_NAME = 'process_batch.racecar'.freeze
SPAN_NAME = 'racecar.batch'.freeze

module_function

Expand All @@ -18,7 +18,7 @@ def event_name
end

def span_name
self::SPAN_NAME
Ext::SPAN_BATCH
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ddtrace/contrib/racecar/events/message.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'ddtrace/contrib/racecar/ext'
require 'ddtrace/contrib/racecar/event'

module Datadog
Expand All @@ -9,7 +10,6 @@ module Message
include Racecar::Event

EVENT_NAME = 'process_message.racecar'.freeze
SPAN_NAME = 'racecar.message'.freeze

module_function

Expand All @@ -18,7 +18,7 @@ def event_name
end

def span_name
self::SPAN_NAME
Ext::SPAN_MESSAGE
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions lib/ddtrace/contrib/racecar/ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Datadog
module Contrib
module Racecar
# Racecar integration constants
module Ext
APP = 'racecar'.freeze
SERVICE_NAME = 'racecar'.freeze

SPAN_BATCH = 'racecar.batch'.freeze
SPAN_MESSAGE = 'racecar.message'.freeze

TAG_CONSUMER = 'kafka.consumer'.freeze
TAG_FIRST_OFFSET = 'kafka.first_offset'.freeze
TAG_MESSAGE_COUNT = 'kafka.message_count'.freeze
TAG_OFFSET = 'kafka.offset'.freeze
TAG_PARTITION = 'kafka.partition'.freeze
TAG_TOPIC = 'kafka.topic'.freeze
end
end
end
end
36 changes: 36 additions & 0 deletions lib/ddtrace/contrib/racecar/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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

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
65 changes: 24 additions & 41 deletions lib/ddtrace/contrib/racecar/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,37 @@
require 'ddtrace/contrib/patcher'
require 'ddtrace/ext/app_types'
require 'ddtrace/contrib/racecar/ext'
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],
Ext::APP,
Datadog::Ext::AppTypes::WORKER
)
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply Racecar integration: #{e}")
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/ddtrace/contrib/racecar/patcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def all_spans
end

let(:racecar_span) do
all_spans.select { |s| s.name == Datadog::Contrib::Racecar::Events::Message::SPAN_NAME }.first
all_spans.select { |s| s.name == Datadog::Contrib::Racecar::Ext::SPAN_MESSAGE }.first
end

context 'that doesn\'t raise an error' do
Expand Down Expand Up @@ -100,7 +100,7 @@ def all_spans
end

let(:racecar_span) do
all_spans.select { |s| s.name == Datadog::Contrib::Racecar::Events::Batch::SPAN_NAME }.first
all_spans.select { |s| s.name == Datadog::Contrib::Racecar::Ext::SPAN_BATCH }.first
end

context 'that doesn\'t raise an error' do
Expand Down

0 comments on commit 41f1621

Please sign in to comment.