diff --git a/instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb b/instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb index da685cb37..ab52b6fc1 100644 --- a/instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb +++ b/instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb @@ -49,9 +49,15 @@ def clear_cached_config def initialize(app) @app = app + @untraced_endpoints = config[:untraced_endpoints].is_a?(Array) ? config[:untraced_endpoints] : [] end def call(env) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength + if untraced_request?(env) + OpenTelemetry::Common::Utilities.untraced do + return @app.call(env) + end + end original_env = env.dup extracted_context = OpenTelemetry.propagation.extract( env, @@ -79,6 +85,10 @@ def call(env) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength private + def untraced_request?(env) + @untraced_endpoints.include?(env['PATH_INFO']) + end + # return Context with the frontend span as the current span def create_frontend_span(env, extracted_context) request_start_time = OpenTelemetry::Instrumentation::Rack::Util::QueueTime.get_request_start(env) diff --git a/instrumentation/rack/test/opentelemetry/instrumentation/rack/middlewares/tracer_middleware_test.rb b/instrumentation/rack/test/opentelemetry/instrumentation/rack/middlewares/tracer_middleware_test.rb index 4f12ef915..6aac42ec2 100644 --- a/instrumentation/rack/test/opentelemetry/instrumentation/rack/middlewares/tracer_middleware_test.rb +++ b/instrumentation/rack/test/opentelemetry/instrumentation/rack/middlewares/tracer_middleware_test.rb @@ -23,6 +23,7 @@ let(:rack_builder) { Rack::Builder.new } let(:exporter) { EXPORTER } + let(:finished_spans) { exporter.finished_spans } let(:first_span) { exporter.finished_spans.first } let(:default_config) { {} } @@ -71,6 +72,50 @@ _(first_span.parent_span_id).must_equal OpenTelemetry::Trace::INVALID_SPAN_ID end + describe 'config[:untraced_endpoints]' do + describe 'when an array is passed in' do + let(:config) { { untraced_endpoints: ['/ping'] } } + + it 'does not trace paths listed in the array' do + Rack::MockRequest.new(rack_builder).get('/ping', env) + + ping_span = finished_spans.find { |s| s.name == '/ping' } + _(ping_span).must_be_nil + + root_span = finished_spans.find { |s| s.name == '/' } + _(root_span).wont_be_nil + end + end + + describe 'when a string is passed in' do + let(:config) { { untraced_endpoints: '/ping' } } + + it 'traces everything' do + Rack::MockRequest.new(rack_builder).get('/ping', env) + + ping_span = finished_spans.find { |s| s.name == '/ping' } + _(ping_span).wont_be_nil + + root_span = finished_spans.find { |s| s.name == '/' } + _(root_span).wont_be_nil + end + end + + describe 'when nil is passed in' do + let(:config) { { untraced_endpoints: nil } } + + it 'traces everything' do + Rack::MockRequest.new(rack_builder).get('/ping', env) + + ping_span = finished_spans.find { |s| s.name == '/ping' } + _(ping_span).wont_be_nil + + root_span = finished_spans.find { |s| s.name == '/' } + _(root_span).wont_be_nil + end + end + end + describe 'config[:allowed_request_headers]' do let(:env) { Hash('HTTP_FOO_BAR' => 'http foo bar value') }