-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 #1322 from bf4/maurogeorge-patch-10
Instrumenting rendering of resources
- Loading branch information
Showing
15 changed files
with
340 additions
and
9 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Instrumentation | ||
|
||
ActiveModelSerializers uses the | ||
[ActiveSupport::Notification API](http://guides.rubyonrails.org/active_support_instrumentation.html#subscribing-to-an-event), | ||
which allows for subscribing to events, such as for logging. | ||
|
||
## Events | ||
|
||
Name: | ||
|
||
`render.active_model_serializers` | ||
|
||
Payload (example): | ||
|
||
```ruby | ||
{ | ||
serializer: PostSerializer, | ||
adapter: ActiveModel::Serializer::Adapter::Attributes | ||
} | ||
``` | ||
|
||
Subscribing: | ||
|
||
```ruby | ||
ActiveSupport::Notifications.subscribe 'render.active_model_serializers' do |name, started, finished, unique_id, data| | ||
# whatever | ||
end | ||
ActiveSupport::Notifications.subscribe 'render.active_model_serializers' do |*args| | ||
event = ActiveSupport::Notifications::Event.new(*args) | ||
# event.payload | ||
# whatever | ||
end | ||
|
||
## [LogSubscriber](http://api.rubyonrails.org/classes/ActiveSupport/LogSubscriber.html) | ||
|
||
ActiveModelSerializers includes an `ActiveModelSerializers::LogSubscriber` that attaches to | ||
`render.active_model_serializers`. |
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,12 @@ | ||
# Logging | ||
|
||
If we are using ActiveModel::Serializers on Rails app by default the `Rails.logger` will be used. | ||
|
||
On a non Rails enviroment by default the `ActiveSupport::TaggedLogging` will be | ||
used. | ||
|
||
If we need to customize the logger we can define this in an initializer: | ||
|
||
```ruby | ||
ActiveModelSerializers.logger = Logger.new(STDOUT) | ||
``` |
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,55 @@ | ||
# Adapted from | ||
# https://github.com/rails/rails/blob/7f18ea14c8/activejob/lib/active_job/callbacks.rb | ||
require 'active_support/callbacks' | ||
|
||
module ActiveModelSerializers | ||
# = ActiveModelSerializers Callbacks | ||
# | ||
# ActiveModelSerializers provides hooks during the life cycle of serialization and | ||
# allow you to trigger logic. Available callbacks are: | ||
# | ||
# * <tt>around_render</tt> | ||
# | ||
module Callbacks | ||
extend ActiveSupport::Concern | ||
include ActiveSupport::Callbacks | ||
|
||
included do | ||
define_callbacks :render | ||
end | ||
|
||
# These methods will be included into any ActiveModelSerializers object, adding | ||
# callbacks for +render+. | ||
module ClassMethods | ||
# Defines a callback that will get called around the render method, | ||
# whether it is as_json, to_json, or serializable_hash | ||
# | ||
# class ActiveModel::SerializableResource | ||
# include ActiveModelSerializers::Callbacks | ||
# | ||
# around_render do |args, block| | ||
# tag_logger do | ||
# notify_render do | ||
# block.call(args) | ||
# end | ||
# end | ||
# end | ||
# | ||
# def as_json | ||
# run_callbacks :render do | ||
# adapter.as_json | ||
# end | ||
# end | ||
# # Note: So that we can re-use the instrumenter for as_json, to_json, and | ||
# # serializable_hash, we aren't using the usual format, which would be: | ||
# # def render(args) | ||
# # adapter.as_json | ||
# # end | ||
# end | ||
# | ||
def around_render(*filters, &blk) | ||
set_callback(:render, :around, *filters, &blk) | ||
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,118 @@ | ||
## | ||
# ActiveModelSerializers::Logging | ||
# | ||
# https://github.com/rails/rails/blob/280654ef88/activejob/lib/active_job/logging.rb | ||
# | ||
module ActiveModelSerializers | ||
module Logging | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
include ActiveModelSerializers::Callbacks | ||
extend Macros | ||
instrument_rendering | ||
end | ||
|
||
module ClassMethods | ||
def instrument_rendering | ||
around_render do |args, block| | ||
tag_logger do | ||
notify_render do | ||
block.call(args) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
# Macros that can be used to customize the logging of class or instance methods, | ||
# by extending the class or its singleton. | ||
# | ||
# Adapted from: | ||
# https://github.com/rubygems/rubygems/blob/cb28f5e991/lib/rubygems/deprecate.rb | ||
# | ||
# Provides a single method +notify+ to be used to declare when | ||
# something a method notifies, with the argument +callback_name+ of the notification callback. | ||
# | ||
# class Adapter | ||
# def self.klass_method | ||
# # ... | ||
# end | ||
# | ||
# def instance_method | ||
# # ... | ||
# end | ||
# | ||
# include ActiveModelSerializers::Logging::Macros | ||
# notify :instance_method, :render | ||
# | ||
# class << self | ||
# extend ActiveModelSerializers::Logging::Macros | ||
# notify :klass_method, :render | ||
# end | ||
# end | ||
module Macros | ||
## | ||
# Simple notify method that wraps up +name+ | ||
# in a dummy method. It notifies on with the +callback_name+ notifier on | ||
# each call to the dummy method, telling what the current serializer and adapter | ||
# are being rendered. | ||
# Adapted from: | ||
# https://github.com/rubygems/rubygems/blob/cb28f5e991/lib/rubygems/deprecate.rb | ||
def notify(name, callback_name) | ||
class_eval do | ||
old = "_notifying_#{callback_name}_#{name}" | ||
alias_method old, name | ||
define_method name do |*args, &block| | ||
run_callbacks callback_name do | ||
send old, *args, &block | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def notify_render(*) | ||
event_name = 'render.active_model_serializers'.freeze | ||
ActiveSupport::Notifications.instrument(event_name, notify_render_payload) do | ||
yield | ||
end | ||
end | ||
|
||
def notify_render_payload | ||
{ serializer: serializer, adapter: adapter } | ||
end | ||
|
||
private | ||
|
||
def tag_logger(*tags) | ||
if ActiveModelSerializers.logger.respond_to?(:tagged) | ||
tags.unshift 'active_model_serializsers'.freeze unless logger_tagged_by_active_model_serializers? | ||
ActiveModelSerializers.logger.tagged(*tags) { yield } | ||
else | ||
yield | ||
end | ||
end | ||
|
||
def logger_tagged_by_active_model_serializers? | ||
ActiveModelSerializers.logger.formatter.current_tags.include?('active_model_serializsers'.freeze) | ||
end | ||
|
||
class LogSubscriber < ActiveSupport::LogSubscriber | ||
def render(event) | ||
info do | ||
serializer = event.payload[:serializer] | ||
adapter = event.payload[:adapter] | ||
duration = event.duration.round(2) | ||
"Rendered #{serializer.name} with #{adapter.class} (#{duration}ms)" | ||
end | ||
end | ||
|
||
def logger | ||
ActiveModelSerializers.logger | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActiveModelSerializers::Logging::LogSubscriber.attach_to :active_model_serializers |
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
Oops, something went wrong.