Skip to content

Commit 914409f

Browse files
committed
feat!: Remove retain_middleware_names Rack Option
This configuration option sets a rack environment variable that isn't used in the instrumentation or its dependencies. dd-trace-rb used this option to set the span name to the Rack application Name: https://github.com/DataDog/dd-trace-rb/blob/d76de947d66fe50f5ac6c7a6016ebbfa5c0de986/lib/datadog/tracing/contrib/rack/middlewares.rb#L158 It looks like some of the functionality was ported over but applying it to the span name was never completed: open-telemetry/opentelemetry-ruby#166
1 parent 757d9e1 commit 914409f

4 files changed

Lines changed: 35 additions & 78 deletions

File tree

instrumentation/rack/lib/opentelemetry/instrumentation/rack/instrumentation.rb

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ module Rack
1212
# The Instrumentation class contains logic to detect and install the Rack
1313
# instrumentation
1414
class Instrumentation < OpenTelemetry::Instrumentation::Base
15-
install do |config|
15+
install do |_config|
16+
# TODO: move logic that configures allow lists here
1617
require_dependencies
17-
18-
retain_middleware_names if config[:retain_middleware_names]
1918
end
2019

2120
present do
@@ -26,7 +25,6 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
2625
option :allowed_response_headers, default: [], validate: :array
2726
option :application, default: nil, validate: :callable
2827
option :record_frontend_span, default: false, validate: :boolean
29-
option :retain_middleware_names, default: false, validate: :boolean
3028
option :untraced_endpoints, default: [], validate: :array
3129
option :url_quantization, default: nil, validate: :callable
3230
option :untraced_requests, default: nil, validate: :callable
@@ -37,30 +35,6 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
3735
def require_dependencies
3836
require_relative 'middlewares/tracer_middleware'
3937
end
40-
41-
MissingApplicationError = Class.new(StandardError)
42-
43-
# intercept all middleware-compatible calls, retain class name
44-
def retain_middleware_names
45-
next_middleware = config[:application]
46-
raise MissingApplicationError unless next_middleware
47-
48-
while next_middleware
49-
if next_middleware.respond_to?(:call)
50-
next_middleware.singleton_class.class_eval do
51-
alias_method :__call, :call
52-
53-
def call(env)
54-
env['RESPONSE_MIDDLEWARE'] = self.class.to_s
55-
__call(env)
56-
end
57-
end
58-
end
59-
60-
next_middleware = next_middleware.instance_variable_defined?('@app') &&
61-
next_middleware.instance_variable_get('@app')
62-
end
63-
end
6438
end
6539
end
6640
end

instrumentation/rack/opentelemetry-instrumentation-rack.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
3838
spec.add_development_dependency 'rack', '~> 2.0.8'
3939
spec.add_development_dependency 'rack-test', '~> 1.1.0'
4040
spec.add_development_dependency 'rake', '~> 13.0'
41+
spec.add_development_dependency 'rspec-mocks'
4142
spec.add_development_dependency 'rubocop', '~> 1.41.1'
4243
spec.add_development_dependency 'simplecov', '~> 0.17.1'
4344
spec.add_development_dependency 'webmock', '~> 3.7.6'

instrumentation/rack/test/opentelemetry/instrumentation/rack/instrumentation_test.rb

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,64 +11,45 @@
1111
let(:instrumentation) { instrumentation_class.instance }
1212
let(:config) { {} }
1313

14-
after do
14+
before do
1515
# simulate a fresh install:
1616
instrumentation.instance_variable_set('@installed', false)
17-
instrumentation.install({})
1817
end
1918

20-
describe 'config[:retain_middleware_names]' do
21-
let(:config) { Hash(retain_middleware_names: true) }
22-
23-
describe 'without config[:application]' do
24-
it 'raises error' do
25-
# allow for re-installation with new config:
26-
instrumentation.instance_variable_set('@installed', false)
27-
28-
assert_raises instrumentation_class::MissingApplicationError do
29-
instrumentation.install(config)
30-
end
31-
end
19+
describe 'given default config options' do
20+
before do
21+
instrumentation.install(config)
3222
end
3323

34-
describe 'default' do
35-
class MyAppClass
36-
attr_reader :env
37-
38-
def use(*); end
39-
40-
def call(env)
41-
@env = env
42-
[200, { 'Content-Type' => 'text/plain' }, ['OK']]
43-
end
44-
end
45-
46-
let(:app) { MyAppClass.new }
47-
48-
describe 'without config' do
49-
it 'does not set RESPONSE_MIDDLEWARE' do
50-
app.call({})
51-
52-
_(app.env['RESPONSE_MIDDLEWARE']).must_be_nil
53-
end
54-
end
55-
56-
describe 'with config[:application]' do
57-
let(:config) do
58-
{ retain_middleware_names: true,
59-
application: app }
60-
end
61-
62-
it 'retains RESPONSE_MIDDLEWARE after .call' do
63-
# allow for re-installation with new config:
64-
instrumentation.instance_variable_set('@installed', false)
24+
it 'is installed with default settings' do
25+
_(instrumentation).must_be :installed?
26+
_(instrumentation.config[:allowed_request_headers]).must_be_empty
27+
_(instrumentation.config[:allowed_response_headers]).must_be_empty
28+
_(instrumentation.config[:application]).must_be_nil
29+
_(instrumentation.config[:record_frontend_span]).must_equal false
30+
_(instrumentation.config[:untraced_endpoints]).must_be_empty
31+
_(instrumentation.config[:url_quantization]).must_be_nil
32+
_(instrumentation.config[:untraced_requests]).must_be_nil
33+
_(instrumentation.config[:response_propagators]).must_be_empty
34+
end
35+
end
6536

66-
instrumentation.install(config)
67-
app.call({})
37+
describe 'when rack gem does not exist' do
38+
before do
39+
hide_const('Rack')
40+
instrumentation.install(config)
41+
end
6842

69-
_(app.env['RESPONSE_MIDDLEWARE']).must_equal 'MyAppClass'
70-
end
71-
end
43+
it 'skips installation' do
44+
_(instrumentation).wont_be :installed?
45+
_(instrumentation.config[:allowed_request_headers]).must_be_empty
46+
_(instrumentation.config[:allowed_response_headers]).must_be_empty
47+
_(instrumentation.config[:application]).must_be_nil
48+
_(instrumentation.config[:record_frontend_span]).must_equal false
49+
_(instrumentation.config[:untraced_endpoints]).must_be_empty
50+
_(instrumentation.config[:url_quantization]).must_be_nil
51+
_(instrumentation.config[:untraced_requests]).must_be_nil
52+
_(instrumentation.config[:response_propagators]).must_be_empty
7253
end
7354
end
7455
end

instrumentation/rack/test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Bundler.require(:default, :development, :test)
99

1010
require 'minitest/autorun'
11+
require 'rspec/mocks/minitest_integration'
1112
require 'webmock/minitest'
1213

1314
require 'opentelemetry-instrumentation-rack'

0 commit comments

Comments
 (0)