Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,9 @@ module ActionPack
class Railtie < ::Rails::Railtie
config.before_initialize do |app|
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.install({})

stability_opt_in = ENV.fetch('OTEL_SEMCONV_STABILITY_OPT_IN', '')
values = stability_opt_in.split(',').map(&:strip)

rack_middleware_args = if values.include?('http/dup')
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args_dup
elsif values.include?('http')
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args_stable
else
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args
end

app.middleware.insert_before(
0,
*rack_middleware_args
*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
# This option is only valid for applications using Rack 2.0 or greater
option :use_rack_events, default: true, validate: :boolean

def middleware_args
patch_type = determine_semconv
send(:"middleware_args_#{patch_type}")
end

# Temporary Helper for Sinatra and ActionPack middleware to use during installation
#
# @example Default usage
Expand All @@ -40,16 +45,14 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
# run lambda { |_arg| [200, { 'Content-Type' => 'text/plain' }, body] }
# end
# @return [Array] consisting of a middleware and arguments used in rack builders
def middleware_args
def middleware_args_old
if config.fetch(:use_rack_events, false) == true && defined?(OpenTelemetry::Instrumentation::Rack::Middlewares::Old::EventHandler)
[::Rack::Events, [OpenTelemetry::Instrumentation::Rack::Middlewares::Old::EventHandler.new]]
else
[OpenTelemetry::Instrumentation::Rack::Middlewares::Old::TracerMiddleware]
end
end

alias middleware_args_old middleware_args

def middleware_args_dup
if config.fetch(:use_rack_events, false) == true && defined?(OpenTelemetry::Instrumentation::Rack::Middlewares::Dup::EventHandler)
[::Rack::Events, [OpenTelemetry::Instrumentation::Rack::Middlewares::Dup::EventHandler.new]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
end

def install_middleware(app)
if config[:install_rack]
stability_opt_in = ENV.fetch('OTEL_SEMCONV_STABILITY_OPT_IN', '')
values = stability_opt_in.split(',').map(&:strip)

if values.include?('http/dup')
app.use(*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args_dup)
elsif values.include?('http')
app.use(*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args_stable)
else
app.use(*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args)
end
end

app.use(*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args) if config[:install_rack]
Copy link
Contributor

Choose a reason for hiding this comment

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

This all looks so great. Thank you for working on this.

Two Sinatra tests are using the direct stable/dup methods. It would be great to update those tests to better match the changes you've made by replacing middleware_args_stable and middleware_args_dup with middleware_args.

The files are:

  • sinatra/test/opentelemetry/instrumentation/sinatra_dup_http_test
  • sinatra/test/opentelemetry/instrumentation/sinatra_stable_http_test

app.use(Middlewares::TracerMiddleware)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class CustomError < StandardError; end
let(:app) do
apps_to_build = apps
Rack::Builder.new do
use(*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args_dup)
use(*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args)

apps_to_build.each do |root, app|
map root do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class CustomError < StandardError; end
let(:app) do
apps_to_build = apps
Rack::Builder.new do
use(*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args_stable)
use(*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args)

apps_to_build.each do |root, app|
map root do
Expand Down
Loading