diff --git a/instrumentation/net_http/lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb b/instrumentation/net_http/lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb index 32a67122cf..ba64b247d8 100644 --- a/instrumentation/net_http/lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb +++ b/instrumentation/net_http/lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb @@ -86,12 +86,20 @@ def tracer end def untraced? + untraced_context? || untraced_host? + end + + def untraced_host? return true if Net::HTTP::Instrumentation.instance.config[:untraced_hosts]&.any? do |host| host.is_a?(Regexp) ? host.match?(@address) : host == @address end false end + + def untraced_context? + OpenTelemetry::Common::Utilities.untraced? + end end end end diff --git a/instrumentation/net_http/test/opentelemetry/instrumentation/net/http/instrumentation_test.rb b/instrumentation/net_http/test/opentelemetry/instrumentation/net/http/instrumentation_test.rb index 2ab764299a..fb35a349f1 100644 --- a/instrumentation/net_http/test/opentelemetry/instrumentation/net/http/instrumentation_test.rb +++ b/instrumentation/net_http/test/opentelemetry/instrumentation/net/http/instrumentation_test.rb @@ -178,6 +178,45 @@ _(span.attributes['net.peer.port']).must_equal(80) end end + + describe 'untraced context' do + let(:tracer_stub) { Object.new } + let(:expect_in_span_not_to_be_called) do + # Calling `tracer.in_span` within an untraced context causes the logging of "called + # finish on an ended Span" messages. To avoid log noise, the instrumentation must + # no-op (i.e., not call `tracer.in_span`) when the context is untraced. + def tracer_stub.in_span(...) + raise 'tracer.in_span should not have been called' + end + end + + it 'no-ops on #request' do + expect_in_span_not_to_be_called + + instrumentation.stub :tracer, tracer_stub do + OpenTelemetry::Common::Utilities.untraced do + Net::HTTP.get('example.com', '/body') + end + end + + _(exporter.finished_spans.size).must_equal 0 + end + + it 'no-ops on #connect' do + expect_in_span_not_to_be_called + + instrumentation.stub :tracer, tracer_stub do + OpenTelemetry::Common::Utilities.untraced do + uri = URI.parse('http://example.com/body') + http = Net::HTTP.new(uri.host, uri.port) + http.send(:connect) + http.send(:do_finish) + end + end + + _(exporter.finished_spans.size).must_equal 0 + end + end end describe '#connect' do