Skip to content

Commit

Permalink
Merge pull request #340 from skisulli/active-model-serializers
Browse files Browse the repository at this point in the history
Instrument ActiveModelSerializers
  • Loading branch information
delner committed May 8, 2018
2 parents 40fd59b + 942be64 commit f30db7f
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ end

if RUBY_VERSION >= '2.2.2' && RUBY_PLATFORM != 'java'
appraise 'contrib' do
gem 'active_model_serializers', '>= 0.10.0'
gem 'elasticsearch-transport'
gem 'mongo', '< 2.5'
gem 'graphql'
Expand All @@ -136,6 +137,7 @@ if RUBY_VERSION >= '2.2.2' && RUBY_PLATFORM != 'java'
end
else
appraise 'contrib-old' do
gem 'active_model_serializers', '~> 0.9.0'
gem 'elasticsearch-transport'
gem 'mongo', '< 2.5'
gem 'redis', '< 4.0'
Expand Down
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace :spec do
end

[
:active_model_serializers,
:active_record,
:active_support,
:aws,
Expand Down Expand Up @@ -220,6 +221,7 @@ task :ci do
sh 'rvm $MRI_OLD_VERSIONS --verbose do appraisal contrib-old rake test:aws'
sh 'rvm $MRI_OLD_VERSIONS --verbose do appraisal contrib-old rake test:sucker_punch'
# RSpec
sh 'rvm $MRI_VERSIONS --verbose do appraisal contrib rake spec:active_model_serializers'
sh 'rvm $MRI_VERSIONS --verbose do appraisal contrib rake spec:active_record'
sh 'rvm $MRI_VERSIONS --verbose do appraisal contrib rake spec:active_support'
sh 'rvm $MRI_VERSIONS --verbose do appraisal contrib rake spec:dalli'
Expand All @@ -231,6 +233,7 @@ task :ci do
sh 'rvm $MRI_VERSIONS --verbose do appraisal contrib rake spec:redis'
sh 'rvm $MRI_VERSIONS --verbose do appraisal contrib rake spec:resque'
sh 'rvm $MRI_VERSIONS --verbose do appraisal contrib rake spec:sequel'
sh 'rvm $MRI_OLD_VERSIONS --verbose do appraisal contrib-old rake spec:active_model_serializers'
sh 'rvm $MRI_OLD_VERSIONS --verbose do appraisal contrib-old rake spec:active_record'
sh 'rvm $MRI_OLD_VERSIONS --verbose do appraisal contrib-old rake spec:active_support'
sh 'rvm $MRI_OLD_VERSIONS --verbose do appraisal contrib-old rake spec:dalli'
Expand Down
1 change: 1 addition & 0 deletions gemfiles/contrib.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
source "https://rubygems.org"

gem "pry-nav", git: "https://github.com/nixme/pry-nav.git", branch: "master"
gem "active_model_serializers", ">= 0.10.0"
gem "elasticsearch-transport"
gem "mongo", "< 2.5"
gem "graphql"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/contrib_old.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
source "https://rubygems.org"

gem "pry-nav", git: "https://github.com/nixme/pry-nav.git", branch: "master"
gem "active_model_serializers", "~> 0.9.0"
gem "elasticsearch-transport"
gem "mongo", "< 2.5"
gem "redis", "< 4.0"
Expand Down
1 change: 1 addition & 0 deletions lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def configure(target = configuration, opts = {})
require 'ddtrace/contrib/base'
require 'ddtrace/contrib/rack/patcher'
require 'ddtrace/contrib/rails/patcher'
require 'ddtrace/contrib/active_model_serializers/patcher'
require 'ddtrace/contrib/active_record/patcher'
require 'ddtrace/contrib/sequel/patcher'
require 'ddtrace/contrib/elasticsearch/patcher'
Expand Down
57 changes: 57 additions & 0 deletions lib/ddtrace/contrib/active_model_serializers/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'ddtrace/contrib/active_support/notifications/event'

module Datadog
module Contrib
module ActiveModelSerializers
# Defines basic behaviors for an ActiveModelSerializers event.
module Event
def self.included(base)
base.send(:include, ActiveSupport::Notifications::Event)
base.send(:extend, ClassMethods)
end

# Class methods for ActiveModelSerializers events.
# Note, they share the same process method and before_trace method.
module ClassMethods
def span_options
{ service: configuration[:service_name] }
end

def tracer
configuration[:tracer]
end

def configuration
Datadog.configuration[:active_model_serializers]
end

def process(span, event, _id, payload)
span.service = configuration[:service_name]

# Set the resource name and serializer name
res = resource(payload[:serializer])
span.resource = res
span.set_tag('active_model_serializers.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?
end

private

def resource(serializer)
# Depending on the version of ActiveModelSerializers
# serializer will be a string or an object.
if serializer.respond_to?(:name)
serializer.name
else
serializer
end
end
end
end
end
end
end
30 changes: 30 additions & 0 deletions lib/ddtrace/contrib/active_model_serializers/events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'ddtrace/contrib/active_model_serializers/events/render'
require 'ddtrace/contrib/active_model_serializers/events/serialize'

module Datadog
module Contrib
module ActiveModelSerializers
# Defines collection of instrumented ActiveModelSerializers events
module Events
ALL = [
Events::Render,
Events::Serialize
].freeze

module_function

def all
self::ALL
end

def subscriptions
all.collect(&:subscriptions).collect(&:to_a).flatten
end

def subscribe!
all.each(&:subscribe!)
end
end
end
end
end
32 changes: 32 additions & 0 deletions lib/ddtrace/contrib/active_model_serializers/events/render.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'ddtrace/contrib/active_model_serializers/event'

module Datadog
module Contrib
module ActiveModelSerializers
module Events
# Defines instrumentation for render.active_model_serializers event
module Render
include ActiveModelSerializers::Event

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

module_function

def supported?
Gem.loaded_specs['active_model_serializers'] \
&& Gem.loaded_specs['active_model_serializers'].version >= Gem::Version.new('0.10')
end

def event_name
self::EVENT_NAME
end

def span_name
self::SPAN_NAME
end
end
end
end
end
end
35 changes: 35 additions & 0 deletions lib/ddtrace/contrib/active_model_serializers/events/serialize.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'ddtrace/contrib/active_model_serializers/event'

module Datadog
module Contrib
module ActiveModelSerializers
module Events
# Defines instrumentation for !serialize.active_model_serializers event
module Serialize
include ActiveModelSerializers::Event

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

module_function

def supported?
Gem.loaded_specs['active_model_serializers'] \
&& ( \
Gem.loaded_specs['active_model_serializers'].version >= Gem::Version.new('0.9') \
&& Gem.loaded_specs['active_model_serializers'].version < Gem::Version.new('0.10') \
)
end

def event_name
self::EVENT_NAME
end

def span_name
self::SPAN_NAME
end
end
end
end
end
end
62 changes: 62 additions & 0 deletions lib/ddtrace/contrib/active_model_serializers/patcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'ddtrace/ext/app_types'
require 'ddtrace/ext/http'
require 'ddtrace/contrib/active_model_serializers/events'

module Datadog
module Contrib
module ActiveModelSerializers
# Provides instrumentation for ActiveModelSerializers through ActiveSupport instrumentation signals
module Patcher
include Base

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

register_as :active_model_serializers

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
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
end
end
end
end
end
61 changes: 61 additions & 0 deletions spec/ddtrace/contrib/active_model_serializers/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module ActiveModelSerializersHelpers
class << self
def ams_0_10_or_newer?
Gem.loaded_specs['active_model_serializers'] \
&& Gem.loaded_specs['active_model_serializers'].version >= Gem::Version.new('0.10')
end

def disable_logging
if ams_0_10_or_newer?
ActiveModelSerializers.logger.level = Logger::Severity::UNKNOWN
end
end
end
end

RSpec.shared_context 'AMS serializer' do
let(:serializer_class) do
end

if ActiveModelSerializersHelpers.ams_0_10_or_newer?
before(:each) do
stub_const('Model', Class.new(ActiveModelSerializers::Model) do
attr_writer :id
end)

stub_const('TestModel', Class.new(Model) do
attributes :name
end)

stub_const('TestModelSerializer', Class.new(ActiveModel::Serializer) do
attributes :name
end)
end
else
before(:each) do
stub_const('Model', Class.new do
attr_writer :id

def initialize(hash = {})
@attributes = hash
end

def read_attribute_for_serialization(name)
if [:id, 'id'].include?(name)
object_id
elsif respond_to?(name)
send name
else
@attributes[name]
end
end
end)

stub_const('TestModel', Class.new(Model))

stub_const('TestModelSerializer', Class.new(ActiveModel::Serializer) do
attributes :name
end)
end
end
end
Loading

0 comments on commit f30db7f

Please sign in to comment.