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

Add active storage instrumentation #1290

Closed
wants to merge 10 commits into from
Closed

Conversation

ericmustin
Copy link
Contributor

Summary

This PR adds instrumentation for the Rails ActiveStorage gem, using the ActiveSupport Notifications that it emits.

ActiveStorage is available in both rails 5 and 6 for ruby versions >=2.5. By adding ActiveStorage instrumentation it allows the rails integration to have better coverage over possible rails deployments.

Notes

  • The tests here are quite cumbersome as I wanted to actually mock the full ActiveStorage action lifecycle instead of just mocking an activesupport notification. It required an additional appraisal block and a lot of boilerplate. There are some lifecycle hooks that handle appending the ActiveStorage methods/macros to ActiveRecord as well as establishing/creating migrations for the underlying db tables that power ActiveStorage's file management. These lifecycle hooks changed pretty dramatically, including changes to file structure, between rails 5 and 6, so I had to mock all of this, and it can be a little distracting in the tests.

  • I wasn't sure about the value of some of the instrumented events, but i added all of them as they each seemed to represent unique actions related to file management.

@ericmustin ericmustin requested a review from a team December 17, 2020 20:54
end)

stub_const('Article', Class.new(ApplicationRecord) do
# active storage initializers have changed from 5 => 6 so we need to mimic both approaches
Copy link
Member

Choose a reason for hiding this comment

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

We have different application files for different versions of Rails: https://github.com/DataDog/dd-trace-rb/tree/master/test/contrib/rails/apps

Do you think it makes sense to move this to rails5.rb and rails6.rb respectively?

It could be something like declaring a global method, like:

stub_const('Article', new_application_record)

or a mixin:

stub_const('Article', Class.new(ApplicationRecord) do
	include Datadog::Testing::ApplicationRecord
end)

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense, can clean this up

Comment on lines +123 to +128
# Rails.configuration.active_storage.service_configurations = {
# local: {
# service: 'Disk',
# root: '/tmp/dd-trace-rb/storage'
# }
# }
Copy link
Member

Choose a reason for hiding this comment

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

Is this block supposed to be commented out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

err, i think it should've been deleted, will update

# https://github.com/rails/rails/blob/2a3f8a29e5d7d17a0448fa47fd756c8d11b175b1/activestorage/app/models/active_storage/blob.rb#L64
# https://github.com/rails/rails/blob/06c94818d640d13efecc722c4ec8c9c5ea2a99b0/activestorage/app/models/active_storage/blob.rb#L104
def create_blob(data: 'Hello world!', filename: 'hello.txt', content_type: 'text/plain', identify: true, record: nil)
if Rails.version >= '6.0'
Copy link
Member

Choose a reason for hiding this comment

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

Similar question/suggestion about possibly moving this to https://github.com/DataDog/dd-trace-rb/tree/master/test/contrib/rails/apps.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea i think that's reasonable

module Events
# Defines instrumentation for 'service_delete.active_storage' event.
# From: https://edgeguides.rubyonrails.org/active_support_instrumentation.html#active-storage
module Delete
Copy link
Member

Choose a reason for hiding this comment

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

The events are not all the same, but I see quite a few similarities between them.
Would it be possible to create a shared component for these?
I understand that not all the fields are the same, and would require custom treatment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes probably, i can take an approach similar to our kafka instrumentation

@delner delner added feature Involves a product feature integrations Involves tracing integrations labels Apr 1, 2021
@ericmustin ericmustin marked this pull request as draft August 5, 2021 19:00
@ericmustin ericmustin closed this Sep 30, 2023
@GustavoCaso GustavoCaso deleted the add_active_storage branch October 11, 2023 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Involves a product feature integrations Involves tracing integrations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants