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

feat: Add ActiveStorage instrumentation #1313

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

ymtdzzz
Copy link
Contributor

@ymtdzzz ymtdzzz commented Dec 20, 2024

Description

Adding support for instrumenting ActiveStorage using ActiveSupport::Notification.

related: #1307

Test

Example script

$ cd instrumentation/active_storage/example
$ ruby trace_demonstration.rb

#<struct OpenTelemetry::SDK::Trace::SpanData
 name="service_upload.active_storage",
 kind=:internal,
 status=#<OpenTelemetry::Trace::Status:0x000000012b2df308 @code=1, @description="">,
 parent_span_id="\x00\x00\x00\x00\x00\x00\x00\x00",
 total_recorded_attributes=2,
 total_recorded_events=0,
 total_recorded_links=0,
 start_timestamp=1736671214398000000,
 end_timestamp=1736671214398395000,
 attributes={"active_storage.checksum"=>"x4UGDIZnlswqFwjJlxVMjg==", "active_storage.service"=>"Disk"},
 links=nil,
 events=nil,
 resource=
  #<OpenTelemetry::SDK::Resources::Resource:0x000000011f5bd810
   @attributes=
    {"service.name"=>"unknown_service",
     "process.pid"=>98542,
     "process.command"=>"trace_demonstration.rb",
     "process.runtime.name"=>"ruby",
     "process.runtime.version"=>"3.2.5",
     "process.runtime.description"=>"ruby 3.2.5 (2024-07-26 revision 31d0f1a2e7) [arm64-darwin23]",
     "telemetry.sdk.name"=>"opentelemetry",
     "telemetry.sdk.language"=>"ruby",
     "telemetry.sdk.version"=>"1.6.0"}>,
 instrumentation_scope=#<struct OpenTelemetry::SDK::InstrumentationScope name="OpenTelemetry::Instrumentation::ActiveStorage", version="0.0.0">,
 span_id="\xCA$\xA1\xD6\x01RV\x95",
 trace_id="\x13~\xDF\x02\x1F\x8C\xF3\x8B\x96\xA0\x97\xC3)}\x9A\xFA",
 trace_flags=#<OpenTelemetry::Trace::TraceFlags:0x000000012a539050 @flags=1>,
 tracestate=#<OpenTelemetry::Trace::Tracestate:0x000000012a536508 @hash={}>>

With other instrumentations in your Rails app

Without ActiveStorage instrumentation

# config/initializers/opentelemetry.rb
require "opentelemetry/sdk"

# install all compatible instrumentation with default configuration
OpenTelemetry::SDK.configure do |c|
  c.service_name = "otel-ruby-development"
  c.use "OpenTelemetry::Instrumentation::ActionPack"
  c.use "OpenTelemetry::Instrumentation::ActiveRecord"
  # c.use "OpenTelemetry::Instrumentation::ActiveStorage"
  c.use "OpenTelemetry::Instrumentation::ActiveJob"
end

image

With ActiveStorage instrumentation

# config/initializers/opentelemetry.rb
require "opentelemetry/sdk"

# install all compatible instrumentation with default configuration
OpenTelemetry::SDK.configure do |c|
  c.service_name = "otel-ruby-development"
  c.use "OpenTelemetry::Instrumentation::ActionPack"
  c.use "OpenTelemetry::Instrumentation::ActiveRecord"
  c.use "OpenTelemetry::Instrumentation::ActiveStorage", { key: :include, url: :include }
  c.use "OpenTelemetry::Instrumentation::ActiveJob"
end

image
image

@ymtdzzz ymtdzzz force-pushed the feature/active_storage_support branch 6 times, most recently from 7a91acc to 641f823 Compare January 13, 2025 01:58
@ymtdzzz ymtdzzz changed the title [WIP] feat: Add ActiveStorage instrumentation feat: Add ActiveStorage instrumentation Jan 14, 2025
@ymtdzzz ymtdzzz force-pushed the feature/active_storage_support branch 2 times, most recently from 721c056 to 5959af8 Compare January 14, 2025 12:43
@ymtdzzz ymtdzzz force-pushed the feature/active_storage_support branch from 5959af8 to cf7ee82 Compare January 14, 2025 14:23
@ymtdzzz ymtdzzz marked this pull request as ready for review January 14, 2025 14:45
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 3.0'

spec.add_dependency 'opentelemetry-instrumentation-active_model_serializers', '~> 0.21.0'
spec.add_dependency 'opentelemetry-instrumentation-active_storage', '~> 0.0.0'
Copy link
Contributor

@xuan-cao-swi xuan-cao-swi Jan 16, 2025

Choose a reason for hiding this comment

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

I think it's best to include this instrumentation to -all and -rails once this pr gets merged and released (other part of code that is related to this applies)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that's a good call. However, if this is added to -rails, I don't think it also has to be added to -all, because -all requires -rails.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make sense. In d6979a7 , I removed -active_storage from -all .
I'll create another PR to -active_storage this to -rails after this PR is relesed.

when /^7\./
ActiveStorage::Current.url_options = { host: 'http://example.com' }
when /^8\./
ActiveStorage::Current.url_options = { host: 'http://example.com' }
Copy link
Contributor

@xuan-cao-swi xuan-cao-swi Jan 16, 2025

Choose a reason for hiding this comment

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

nit: why they need to be on separate line?

Copy link
Contributor Author

@ymtdzzz ymtdzzz Jan 18, 2025

Choose a reason for hiding this comment

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

Thank you for your advice, I fixed unnecessary conditions in bfae8c7 !

module OpenTelemetry
module Instrumentation
module ActiveStorage
VERSION = '0.0.0'
Copy link
Contributor

Choose a reason for hiding this comment

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

let's make it '0.1.0' as initial release version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed the version in d6979a7

Copy link
Contributor

@kaylareopelle kaylareopelle left a comment

Choose a reason for hiding this comment

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

Thank you, @ymtdzzz! I'm looking forward to OTel to offering ActiveStorage support.

#
# SPDX-License-Identifier: Apache-2.0

%w[6.1.0 7.0.0 7.1.0].each do |version|
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
%w[6.1.0 7.0.0 7.1.0].each do |version|
%w[7.0.0 7.1.0].each do |version|

We recently removed support for Rails 6.1. See: #1231

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for pointing that out! I fixed in c6ea05f

module OpenTelemetry
module Instrumentation
module ActiveStorage
# The {OpenTelemetry::Instrumentation::ActiveStorage::Instrumentation} class contains logic to detect and install the ActiveStorage instrumentation
Copy link
Contributor

Choose a reason for hiding this comment

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

Excellent docs! Thank you!!

# })
# end
class Instrumentation < OpenTelemetry::Instrumentation::Base
MINIMUM_VERSION = Gem::Version.new('6.1.0')
Copy link
Contributor

Choose a reason for hiding this comment

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

Like I mentioned above, our new minimum Rails version is 7.0 :)

Suggested change
MINIMUM_VERSION = Gem::Version.new('6.1.0')
MINIMUM_VERSION = Gem::Version.new('7.0.0')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is also fixed in c6ea05f

# ## Configuration keys and options
#
# ### `:disallowed_notification_payload_keys`
#
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like the default is missing on just this config:

Suggested change
#
#
# - `array` **default** `[]`
#

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, fixed in 613e1b6

spec.add_development_dependency 'opentelemetry-sdk', '~> 1.1'
spec.add_development_dependency 'opentelemetry-test-helpers', '~> 0.3'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rubocop', '~> 1.69.1'
Copy link
Contributor

Choose a reason for hiding this comment

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

We're about to merge a PR that'll bump Rubocop to 1.70.0:

Suggested change
spec.add_development_dependency 'rubocop', '~> 1.69.1'
spec.add_development_dependency 'rubocop', '~> 1.70.0'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for letting me know, I've matched the version with other packages!
b98fadf

# SPDX-License-Identifier: Apache-2.0

ENV['RAILS_ENV'] = 'test'

Copy link
Contributor

Choose a reason for hiding this comment

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

We recently had to add explicit requires to 'logger' in the test helpers for some Rails-related libraries to avoid uninitialized constant errors: #1345

Rails added logger as a dependency in Rails 7.1 and above, but since we still support 7.0, we need to require it here.

Suggested change
require 'logger'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, I added explicit logger require. da97949

Comment on lines 84 to 85
when /^6\.1/
ActiveStorage::Current.host = 'http://example.com'
Copy link
Contributor

Choose a reason for hiding this comment

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

Cleanup related to dropping 6.1 support:

Suggested change
when /^6\.1/
ActiveStorage::Current.host = 'http://example.com'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, this is also fixed in c6ea05f !

@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 3.0'

spec.add_dependency 'opentelemetry-instrumentation-active_model_serializers', '~> 0.21.0'
spec.add_dependency 'opentelemetry-instrumentation-active_storage', '~> 0.0.0'
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that's a good call. However, if this is added to -rails, I don't think it also has to be added to -all, because -all requires -rails.

Comment on lines +68 to +71
- name: Install ImageMagick for active_storage testing
if: "${{ inputs.gem == 'opentelemetry-instrumentation-active_storage' }}"
shell: bash
run: sudo apt update && sudo apt install -y imagemagick
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding this service here! We also have a docker-compose.yml to allow developers to run services locally. Would you mind adding ImageMagick to that file too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As you said, the test for ActiveStorage instrumentation did not pass due to the lack of ImageMagick inside the container. So I added it to the Dockerfile and confirmed that the test passed. ed63c88

❯ docker compose run app /bin/ash
/app $ cd instrumentation/active_storage/
/app/instrumentation/active_storage $ bundle install
/app/instrumentation/active_storage $ bundle exec appraisal install
/app/instrumentation/active_storage $ bundle exec appraisal rake test
...

# Running:
                                                                                                                                                                                                                                                                                                                     
..........................
                                                                                                                                                                                                                                                                                                                     
Finished in 0.976136s, 26.6356 runs/s, 101.4203 assertions/s.
26 runs, 99 assertions, 0 failures, 0 errors, 0 skips

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

Successfully merging this pull request may close these issues.

3 participants