Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrumenting rendering of resources #1322

Merged
merged 8 commits into from
Nov 19, 2015
Merged

Conversation

bf4
Copy link
Member

@bf4 bf4 commented Nov 8, 2015

Building on the very excellent PR by @maurogeorge (all squashed)

#1291

@bf4 bf4 force-pushed the maurogeorge-patch-10 branch from 600974d to 2f4bb4a Compare November 8, 2015 22:09
ActiveSupport.on_load(:action_controller) do
ActiveModelSerializers.logger = ActionController::Base.logger
ActiveSupport.on_load(:active_model_serializers) do
self.logger = ActionController::Base.logger
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bonus code, out of scope... but shouldn't be that controversial.. pattern copied from active job

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty cool 👍

@beauby
Copy link
Contributor

beauby commented Nov 8, 2015

Looks cool 👍

require 'active_model'
require 'active_support'
require 'active_support/tagged_logging'
require 'active_support/logger'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove these 2 requires, since we already require active_support

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, for this I followed the pattern in active job

It's always nice to be explicit

You're right that it just so happens require 'active_support' requires logging and autoloads TaggedLogging, but there's no guarantee https://github.com/rails/rails/blob/4-0-stable/activesupport/lib/active_support.rb#L27

That said, if you feel strongly I'll take it out

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we have tests covering this we are good to go, to remove.

maurogeorge and others added 6 commits November 10, 2015 03:09
Squashed commits:

Add Logging

Generates logging when renders a serializer.

Tunning performance on notify_active_support

- Use yield over block.call
- Freeze the event name string

Organize the logger architeture

* Keep only the `ActiveModel::Serializer.logger` to follow the same public API we
  have for example to config, like `ActiveModel::Serializer.config.adapter` and
  remove the `ActiveModelSerializers.logger` API.
* Define the logger on the load of the AMS, following the Rails convention on
  Railties [1], [2] and [3].

This way on non Rails apps we have a default logger and on Rails apps we will
use the `Rails.logger` the same way that Active Job do [4].

[1]: https://github.com/rails/rails/blob/2ad9afe4ff2c12d1a9e4a00d9009d040e636c9b0/activejob/lib/active_job/railtie.rb#L9-L11
[2]: https://github.com/rails/rails/blob/2ad9afe4ff2c12d1a9e4a00d9009d040e636c9b0/activerecord/lib/active_record/railtie.rb#L75-L77
[3]: https://github.com/rails/rails/blob/2ad9afe4ff2c12d1a9e4a00d9009d040e636c9b0/actionview/lib/action_view/railtie.rb#L19-L21
[4]: https://github.com/rails/rails/blob/2ad9afe4ff2c12d1a9e4a00d9009d040e636c9b0/activejob/lib/active_job/logging.rb#L10-L11

Performance tunning on LogSubscriber#render

Move the definition of locals to inside the `info` block this way the code is
executed only when the logger is called.

Remove not needed check on SerializableResource

Use SerializableResource on ActionController integration

On the ActionController was using a adapter, and since the instrumentation is
made on the SerializableResource we need to use the SerializableResource over
the adapter directly. Otherwise the logger is not called on a Rails app.

Use SerializableResource on the ActionController, since this is the main
interface to create and call a serializer.

Using always the SerializableResource we can keep the adapter code more easy to
mantain since no Adapter will need to call the instrumentation, only the
SerializableResource care about this.

Add docs about logging

Add a CHANGELOG entry

Keep the ActiveModelSerializers.logger

Better wording on Logging docs

[ci skip]

Add doc about instrumentation

[ci skip]

Use ActiveModel::Callbacks on the SerializableResource
@bf4 bf4 force-pushed the maurogeorge-patch-10 branch from fcb8b05 to d4fc811 Compare November 10, 2015 09:28
@bf4
Copy link
Member Author

bf4 commented Nov 10, 2015

@maurogeorge updated with two incremental commits responding to your feedback, for easy review

@maurogeorge
Copy link
Member

@bf4 I am ok with that 👍

@@ -0,0 +1,19 @@
# Instrumentation

ActiveModelSerializers uses the ActiveSupport::Notification API, which
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there be an example here of how to tie this in to the Notification API?

end
end

# Macros that can be used to customize the logging of class or instance methods,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is some solid documentation :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied pretty much :)

B mobile phone

On Nov 10, 2015, at 8:22 AM, L. Preston Sego III notifications@github.com wrote:

In lib/active_model_serializers/logging.rb:

  •  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,

this is some solid documentation :-)


Reply to this email directly or view it on GitHub.

@bf4
Copy link
Member Author

bf4 commented Nov 13, 2015

Appveyour failure is transitory

@wagenet
Copy link
Contributor

wagenet commented Nov 17, 2015

Is this waiting on anything? Would love to get to work on Skylight integration once this is official :)

@bf4
Copy link
Member Author

bf4 commented Nov 19, 2015

@wagenet not sure. Have you tried dogfooding using it? @NullVoxPopuli ok? @maurogeorge ?

Otherwise, I just need to rebase it

@maurogeorge
Copy link
Member

@bf4 I am ok with that :shipit:
Please just keep my single commit bf4@5142496 ❤️

@bf4 bf4 force-pushed the maurogeorge-patch-10 branch from d4fc811 to 733f5bc Compare November 19, 2015 18:44
bf4 added a commit that referenced this pull request Nov 19, 2015
Instrumenting rendering of resources
@bf4 bf4 merged commit aa43848 into rails-api:master Nov 19, 2015
@bf4 bf4 deleted the maurogeorge-patch-10 branch November 19, 2015 19:01
@bf4
Copy link
Member Author

bf4 commented Nov 19, 2015

Merged

@maurogeorge
Copy link
Member

Thanks 🎉 🎊

@wagenet
Copy link
Contributor

wagenet commented Nov 23, 2015

Thanks! Haven't had a chance to use it yet, but hope to soon :)

@wagenet
Copy link
Contributor

wagenet commented Nov 24, 2015

Started to play around with this some. Looks good to me so far!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants