Skip to content

Commit

Permalink
Correctly parse IPv6 addresses in Net::HTTP instrumentation (#2180)
Browse files Browse the repository at this point in the history
* Correctly parse IPv6 addresses in Net::HTTP instrumentation

* Update sentry-ruby/spec/sentry/net/http_spec.rb

Co-authored-by: Stan Lo <[email protected]>

* Require resolv before using it

* require resolv in http patch, not in http_transport

* Update sentry-ruby/spec/sentry/net/http_spec.rb

Co-authored-by: Stan Lo <[email protected]>

---------

Co-authored-by: Stan Lo <[email protected]>
  • Loading branch information
natikgadzhi and st0012 authored Nov 25, 2023
1 parent bebedc4 commit 168b43a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
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

0 comments on commit 168b43a

Please sign in to comment.