Skip to content

Commit 168b43a

Browse files
natikgadzhist0012
andauthored
Correctly parse IPv6 addresses in Net::HTTP instrumentation (#2180)
* 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]>
1 parent bebedc4 commit 168b43a

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- Fixed a deprecation in `sidekiq-ruby` error handler [#2160](https://github.com/getsentry/sentry-ruby/pull/2160)
2626
- Avoid invoking ActiveSupport::BroadcastLogger if not defined [#2169](https://github.com/getsentry/sentry-ruby/pull/2169)
2727
- Respect custom `Delayed::Job.max_attempts` if it's defined [#2176](https://github.com/getsentry/sentry-ruby/pull/2176)
28+
- Fixed a bug where `Net::HTTP` instrumentation won't work for some IPv6 addresses [#2180](https://github.com/getsentry/sentry-ruby/pull/2180)
2829
- Allow non-string error message to be reported to sentry ([#2137](https://github.com/getsentry/sentry-ruby/pull/2137))
2930

3031
## 5.13.0

sentry-ruby/lib/sentry/net/http.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "net/http"
4+
require "resolv"
45

56
module Sentry
67
# @api private
@@ -77,7 +78,10 @@ def from_sentry_sdk?
7778
end
7879

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

8387
result = { method: req.method, url: url }

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

+24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@
99
::Logger.new(string_io)
1010
end
1111

12+
context "with IPv6 addresses" do
13+
before do
14+
perform_basic_setup do |config|
15+
config.traces_sample_rate = 1.0
16+
end
17+
end
18+
19+
it "correctly parses the short-hand IPv6 addresses" do
20+
stub_normal_response
21+
22+
transaction = Sentry.start_transaction
23+
Sentry.get_current_scope.set_span(transaction)
24+
25+
_ = Net::HTTP.get("::1", "/path", 8080)
26+
27+
expect(transaction.span_recorder.spans.count).to eq(2)
28+
29+
request_span = transaction.span_recorder.spans.last
30+
expect(request_span.data).to eq(
31+
{ "url" => "http://[::1]/path", "http.request.method" => "GET", "http.response.status_code" => 200 }
32+
)
33+
end
34+
end
35+
1236
context "with tracing enabled" do
1337
before do
1438
perform_basic_setup do |config|

0 commit comments

Comments
 (0)