From e75e6d93c10a2e4ba191afa70d082bdc3b69194c Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Thu, 26 May 2022 17:41:00 -0600 Subject: [PATCH] wip: fixing up railtie middleware install --- Gemfile | 1 + .../otel/instrumentation/action_pack.rb | 10 ++++++ .../action_pack/instrumentation.rb | 36 +++++++++++++++++++ .../instrumentation/action_pack/railtie.rb | 18 +++++----- .../instrumentation/action_pack/version.rb | 9 +++++ test/splunk/instrumentation/rails_test.rb | 6 +++- 6 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 lib/splunk/otel/instrumentation/action_pack/instrumentation.rb create mode 100644 lib/splunk/otel/instrumentation/action_pack/version.rb diff --git a/Gemfile b/Gemfile index 36447c8..869b67b 100644 --- a/Gemfile +++ b/Gemfile @@ -15,3 +15,4 @@ gem "rack-test", "~> 1.1" # for testing the rack middleware gem "opentelemetry-instrumentation-rack", "0.20.2" +gem "opentelemetry-instrumentation-action_pack", "0.1.4" diff --git a/lib/splunk/otel/instrumentation/action_pack.rb b/lib/splunk/otel/instrumentation/action_pack.rb index 9a8c033..e1a7485 100644 --- a/lib/splunk/otel/instrumentation/action_pack.rb +++ b/lib/splunk/otel/instrumentation/action_pack.rb @@ -1,3 +1,13 @@ # frozen_string_literal: true +module Splunk + module Otel + module Instrumentation + # Contains the RUM instrumentation for the ActionPack gem + module ActionPack + end + end + end +end + require_relative "./action_pack/railtie" diff --git a/lib/splunk/otel/instrumentation/action_pack/instrumentation.rb b/lib/splunk/otel/instrumentation/action_pack/instrumentation.rb new file mode 100644 index 0000000..53eb598 --- /dev/null +++ b/lib/splunk/otel/instrumentation/action_pack/instrumentation.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Splunk + module Otel + module ActionPack + # The Instrumentation class contains logic to detect and install the ActionPack instrumentation + class Instrumentation < OpenTelemetry::Instrumentation::Base + MINIMUM_VERSION = Gem::Version.new('5.2.0') + + install do |_config| + require_railtie + end + + present do + defined?(::ActionController) + end + + compatible do + gem_version >= MINIMUM_VERSION + end + + option :enable_recognize_route, default: false, validate: :boolean + + private + + def gem_version + ::ActionPack.version + end + + def require_railtie + require_relative 'railtie' + end + end + end + end +end diff --git a/lib/splunk/otel/instrumentation/action_pack/railtie.rb b/lib/splunk/otel/instrumentation/action_pack/railtie.rb index d2e0de8..003dd8d 100644 --- a/lib/splunk/otel/instrumentation/action_pack/railtie.rb +++ b/lib/splunk/otel/instrumentation/action_pack/railtie.rb @@ -2,14 +2,16 @@ module Splunk module Otel - module ActionPack - # Install the Rack middleware for RUM responses - class Railtie < ::Rails::Railtie - config.before_initialize do |app| - app.middleware.insert_before( - 0, - Splunk::Otel::Rack::RumMiddleware - ) + module Instrumentation + module ActionPack + # Install the Rack middleware for RUM responses + class Railtie < ::Rails::Railtie + config.before_initialize do |app| + app.middleware.insert_before( + 0, + Splunk::Otel::Rack::RumMiddleware + ) + end end end end diff --git a/lib/splunk/otel/instrumentation/action_pack/version.rb b/lib/splunk/otel/instrumentation/action_pack/version.rb new file mode 100644 index 0000000..6ee2010 --- /dev/null +++ b/lib/splunk/otel/instrumentation/action_pack/version.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Splunk + module Otel + module ActionPack + VERSION = '0.1.0' + end + end +end diff --git a/test/splunk/instrumentation/rails_test.rb b/test/splunk/instrumentation/rails_test.rb index 75448c5..1ce9673 100644 --- a/test/splunk/instrumentation/rails_test.rb +++ b/test/splunk/instrumentation/rails_test.rb @@ -5,8 +5,11 @@ require "test_helper" require "opentelemetry/sdk" require "opentelemetry/instrumentation/rack" +require "opentelemetry/instrumentation/action_pack" require "splunk/otel" require "splunk/otel/instrumentation/rack" +require "splunk/otel/instrumentation/action_pack" +require "splunk/otel/instrumentation/action_pack/railtie" require "rack/test" require 'test_helpers/app_config.rb' @@ -20,7 +23,8 @@ class RumRailsTest < Test::Unit::TestCase def setup with_env("OTEL_SERVICE_NAME" => "test-service") do Splunk::Otel.configure do |c| - c.use 'OpenTelemetry::Instrumentation::ActionPack' + c.use "OpenTelemetry::Instrumentation::ActionPack" + # c.use "Splunk::Otel::Instrumentation::ActionPack" end end end