Skip to content

Correctly parse IPv6 addresses in Net::HTTP instrumentation #2180

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

Merged
merged 6 commits into from
Nov 25, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Fixed a deprecation in `sidekiq-ruby` error handler [#2160](https://github.com/getsentry/sentry-ruby/pull/2160)
- Avoid invoking ActiveSupport::BroadcastLogger if not defined [#2169](https://github.com/getsentry/sentry-ruby/pull/2169)
- Respect custom `Delayed::Job.max_attempts` if it's defined [#2176](https://github.com/getsentry/sentry-ruby/pull/2176)
- Fixed a bug where `Net::HTTP` instrumentation won't work for some IPv6 addresses [#2180](https://github.com/getsentry/sentry-ruby/pull/2180)
- Allow non-string error message to be reported to sentry ([#2137](https://github.com/getsentry/sentry-ruby/pull/2137))

## 5.13.0
Expand Down
6 changes: 5 additions & 1 deletion sentry-ruby/lib/sentry/net/http.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "net/http"
require "resolv"

module Sentry
# @api private
Expand Down Expand Up @@ -77,7 +78,10 @@ def from_sentry_sdk?
end

def extract_request_info(req)
uri = req.uri || URI.parse("#{use_ssl? ? 'https' : 'http'}://#{address}#{req.path}")
# IPv6 url could look like '::1/path', and that won't parse without
# wrapping it in square brackets.
hostname = address =~ Resolv::IPv6::Regex ? "[#{address}]" : address
uri = req.uri || URI.parse("#{use_ssl? ? 'https' : 'http'}://#{hostname}#{req.path}")
url = "#{uri.scheme}://#{uri.host}#{uri.path}" rescue uri.to_s

result = { method: req.method, url: url }
Expand Down
24 changes: 24 additions & 0 deletions sentry-ruby/spec/sentry/net/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@
::Logger.new(string_io)
end

context "with IPv6 addresses" do
before do
perform_basic_setup do |config|
config.traces_sample_rate = 1.0
end
end

it "correctly parses the short-hand IPv6 addresses" do
stub_normal_response

transaction = Sentry.start_transaction
Sentry.get_current_scope.set_span(transaction)

_ = Net::HTTP.get("::1", "/path", 8080)

expect(transaction.span_recorder.spans.count).to eq(2)

request_span = transaction.span_recorder.spans.last
expect(request_span.data).to eq(
{ "url" => "http://[::1]/path", "http.request.method" => "GET", "http.response.status_code" => 200 }
)
end
end

context "with tracing enabled" do
before do
perform_basic_setup do |config|
Expand Down