-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from skisulli/active-model-serializers
Instrument ActiveModelSerializers
- Loading branch information
Showing
13 changed files
with
396 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
lib/ddtrace/contrib/active_model_serializers/events/render.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
lib/ddtrace/contrib/active_model_serializers/events/serialize.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.