From c2f9e060e636e6a48262b51a954bcc3840508420 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Sun, 23 Jan 2022 16:37:12 +0000 Subject: [PATCH] Don't reuse Net::HTTP objects in `HTTPTransport` (#1696) * Don't reused Net::HTTP objects * Update changelog --- CHANGELOG.md | 4 +++ .../lib/sentry/transport/http_transport.rb | 9 +++---- .../sentry/transport/http_transport_spec.rb | 27 ++++++++++++++++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d1b3f061..fa87dbee0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 5.0.1 + +- Don't reuse Net::HTTP objects in `HTTPTransport` [#1696](https://github.com/getsentry/sentry-ruby/pull/1696) + ## 5.0.0 ### Breaking Change - Goodbye `faraday` 👋 diff --git a/sentry-ruby/lib/sentry/transport/http_transport.rb b/sentry-ruby/lib/sentry/transport/http_transport.rb index 886a48ce6..e32a835e6 100644 --- a/sentry-ruby/lib/sentry/transport/http_transport.rb +++ b/sentry-ruby/lib/sentry/transport/http_transport.rb @@ -14,12 +14,11 @@ class HTTPTransport < Transport RATE_LIMIT_HEADER = "x-sentry-rate-limits" USER_AGENT = "sentry-ruby/#{Sentry::VERSION}" - attr_reader :conn - def initialize(*args) super - @conn = set_conn @endpoint = @dsn.envelope_endpoint + + log_debug("Sentry HTTP Transport will connect to #{@dsn.server}") end def send_data(data) @@ -127,11 +126,9 @@ def should_compress?(data) @transport_configuration.encoding == GZIP_ENCODING && data.bytesize >= GZIP_THRESHOLD end - def set_conn + def conn server = URI(@dsn.server) - log_debug("Sentry HTTP Transport connecting to #{server}") - use_ssl = server.scheme == "https" port = use_ssl ? 443 : 80 diff --git a/sentry-ruby/spec/sentry/transport/http_transport_spec.rb b/sentry-ruby/spec/sentry/transport/http_transport_spec.rb index 2b44f1452..11c548517 100644 --- a/sentry-ruby/spec/sentry/transport/http_transport_spec.rb +++ b/sentry-ruby/spec/sentry/transport/http_transport_spec.rb @@ -16,15 +16,36 @@ subject.encode(event.to_hash) end - subject { described_class.new(configuration) } + subject { client.transport } - it "logs a debug message during initialization" do + it "logs a debug message only during initialization" do + stub_request(build_fake_response("200")) string_io = StringIO.new configuration.logger = Logger.new(string_io) subject - expect(string_io.string).to include("sentry: Sentry HTTP Transport connecting to http://sentry.localdomain") + expect(string_io.string).to include("sentry: Sentry HTTP Transport will connect to http://sentry.localdomain") + + string_io.string = "" + expect(string_io.string).to eq("") + + subject.send_data(data) + + expect(string_io.string).not_to include("sentry: Sentry HTTP Transport will connect to http://sentry.localdomain") + end + + it "initializes new Net::HTTP instance for every request" do + stub_request(build_fake_response("200")) do |request| + expect(request["User-Agent"]).to eq("sentry-ruby/#{Sentry::VERSION}") + end + + subject + + expect(Net::HTTP).to receive(:new).and_call_original.exactly(2) + + subject.send_data(data) + subject.send_data(data) end describe "customizations" do