Skip to content

Commit

Permalink
Refactored: ActiveModelSerializers to use Datadog::Contrib::Integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Sep 26, 2018
1 parent cc9d368 commit f4672d8
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 50 deletions.
2 changes: 1 addition & 1 deletion lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def configure(target = configuration, opts = {})
end

require 'ddtrace/contrib/base'
require 'ddtrace/contrib/active_model_serializers/patcher'
require 'ddtrace/contrib/active_model_serializers/integration'
require 'ddtrace/contrib/active_record/integration'
require 'ddtrace/contrib/aws/integration'
require 'ddtrace/contrib/concurrent_ruby/integration'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'ddtrace/contrib/configuration/settings'
require 'ddtrace/contrib/active_model_serializers/ext'

module Datadog
module Contrib
module ActiveModelSerializers
module Configuration
# Custom settings for the ActiveModelSerializers 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
6 changes: 4 additions & 2 deletions lib/ddtrace/contrib/active_model_serializers/event.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'ddtrace/ext/http'
require 'ddtrace/contrib/active_support/notifications/event'
require 'ddtrace/contrib/active_model_serializers/ext'

module Datadog
module Contrib
Expand Down Expand Up @@ -31,12 +33,12 @@ def process(span, event, _id, payload)
# Set the resource name and serializer name
res = resource(payload[:serializer])
span.resource = res
span.set_tag('active_model_serializers.serializer', res)
span.set_tag(Ext::TAG_SERIALIZER, res)

span.span_type = Datadog::Ext::HTTP::TEMPLATE

# Will be nil in 0.9
span.set_tag('active_model_serializers.adapter', payload[:adapter].class) unless payload[:adapter].nil?
span.set_tag(Ext::TAG_ADAPTER, payload[:adapter].class) unless payload[:adapter].nil?
end

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

module Datadog
Expand All @@ -9,7 +10,6 @@ module Render
include ActiveModelSerializers::Event

EVENT_NAME = 'render.active_model_serializers'.freeze
SPAN_NAME = 'active_model_serializers.render'.freeze

module_function

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

def span_name
self::SPAN_NAME
Ext::SPAN_RENDER
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'ddtrace/contrib/active_model_serializers/ext'
require 'ddtrace/contrib/active_model_serializers/event'

module Datadog
Expand All @@ -9,7 +10,6 @@ module Serialize
include ActiveModelSerializers::Event

EVENT_NAME = '!serialize.active_model_serializers'.freeze
SPAN_NAME = 'active_model_serializers.serialize'.freeze

module_function

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

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

SPAN_RENDER = 'active_model_serializers.render'.freeze
SPAN_SERIALIZE = 'active_model_serializers.serialize'.freeze

TAG_ADAPTER = 'active_model_serializers.adapter'.freeze
TAG_SERIALIZER = 'active_model_serializers.serializer'.freeze
end
end
end
end
39 changes: 39 additions & 0 deletions lib/ddtrace/contrib/active_model_serializers/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/active_model_serializers/configuration/settings'
require 'ddtrace/contrib/active_model_serializers/patcher'

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

register_as :active_model_serializers

def self.version
Gem.loaded_specs['active_model_serializers'] \
&& Gem.loaded_specs['active_model_serializers'].version
end

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

def self.compatible?
super \
&& defined?(::ActiveSupport::Notifications) \
&& version >= Gem::Version.new('0.9.0')
end

def default_configuration
Configuration::Settings.new
end

def patcher
Patcher
end
end
end
end
end
67 changes: 24 additions & 43 deletions lib/ddtrace/contrib/active_model_serializers/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,41 @@
require 'ddtrace/contrib/patcher'
require 'ddtrace/ext/app_types'
require 'ddtrace/ext/http'
require 'ddtrace/contrib/active_model_serializers/ext'
require 'ddtrace/contrib/active_model_serializers/events'

module Datadog
module Contrib
module ActiveModelSerializers
# Provides instrumentation for ActiveModelSerializers through ActiveSupport instrumentation signals
# Patcher enables patching of 'active_model_serializers' module.
module Patcher
include Base
include Contrib::Patcher

VERSION_REQUIRED = Gem::Version.new('0.9.0')
module_function

register_as :active_model_serializers
def patched?
done?(:active_model_serializers)
end

option :service_name, default: 'active_model_serializers'
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
def patch
do_once(:active_model_serializers) do
begin
# Subscribe to ActiveModelSerializers events
Events.subscribe!

# Set service info
get_option(:tracer).set_service_info(
get_option(:service_name),
Ext::APP,
Datadog::Ext::AppTypes::WEB
)
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply ActiveModelSerializers integration: #{e}")
end
end
end

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

# Subscribe to ActiveModelSerializers events
Events.subscribe!

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

@patched = true
end

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

private

def configuration
Datadog.configuration[:active_model_serializers]
end

def compatible?
Gem.loaded_specs['active_model_serializers'] && Gem.loaded_specs['activesupport'] \
&& Gem.loaded_specs['active_model_serializers'].version >= VERSION_REQUIRED
end
def get_option(option)
Datadog.configuration[:active_model_serializers].get_option(option)
end
end
end
Expand Down

0 comments on commit f4672d8

Please sign in to comment.