Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update Net::HTTP instrumentation to no-op on untraced contexts #722

Merged
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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'opentelemetry-sdk', '~> 1.1'
spec.add_development_dependency 'opentelemetry-test-helpers', '~> 0.3'
spec.add_development_dependency 'rake', '~> 13.0.1'
spec.add_development_dependency 'rspec-mocks'
spec.add_development_dependency 'rubocop', '~> 1.56.1'
spec.add_development_dependency 'simplecov', '~> 0.17.1'
spec.add_development_dependency 'webmock', '~> 3.19'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,34 @@
_(span.attributes['net.peer.port']).must_equal(80)
end
end

describe 'untraced context' do
it 'no-ops on #request' 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.
expect(instrumentation.tracer).not_to receive(:in_span)

OpenTelemetry::Common::Utilities.untraced do
Net::HTTP.get('example.com', '/body')
end

_(exporter.finished_spans.size).must_equal 0
end

it 'no-ops on #connect' do
expect(instrumentation.tracer).not_to receive(:in_span)

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

_(exporter.finished_spans.size).must_equal 0
end
end
end

describe '#connect' do
Expand Down
1 change: 1 addition & 0 deletions instrumentation/net_http/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Bundler.require(:default, :development, :test)

require 'minitest/autorun'
require 'rspec/mocks/minitest_integration'
require 'webmock/minitest'

# global opentelemetry-sdk setup:
Expand Down