From 039a4fc8af4014fc80c21a23181bb1519461c288 Mon Sep 17 00:00:00 2001 From: Jared Knipp Date: Fri, 8 Apr 2022 16:01:56 -0500 Subject: [PATCH 1/2] Allow config.transport.proxy to be a String or URI (#1788) Fixes #1782 Update `config.transport.proxy` to allow String and URI values as previously supported by `sentry-ruby` versions <= 4.8 using Faraday. --- CHANGELOG.md | 6 +++-- .../lib/sentry/transport/http_transport.rb | 16 +++++++++++- .../sentry/transport/http_transport_spec.rb | 26 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7102c9c4..1eef0bdfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ - [Discussion thread and explanation on the decision](https://github.com/rails/rails/pull/43625#issuecomment-1072514175) - Check if ActiveRecord connection exists before calling AR connection pool [#1769](https://github.com/getsentry/sentry-ruby/pull/1769) - Fixes [#1745](https://github.com/getsentry/sentry-ruby/issues/1745) +- Update `config.transport.proxy` to allow String and URI values as previously supported by `sentry-ruby` versions <= 4.8 using Faraday + - Fixes [#1782](https://github.com/getsentry/sentry-ruby/issues/1782) ### Refactoring @@ -79,8 +81,8 @@ - The SDK now supports [automatic session tracking / release health](https://docs.sentry.io/product/releases/health/) by default in Rack based applications. - Aggregate statistics on successful / errored requests are collected and sent to the server every minute. + The SDK now supports [automatic session tracking / release health](https://docs.sentry.io/product/releases/health/) by default in Rack based applications. + Aggregate statistics on successful / errored requests are collected and sent to the server every minute. To use this feature, make sure the SDK can detect your app's release. Or you have set it with: ```ruby diff --git a/sentry-ruby/lib/sentry/transport/http_transport.rb b/sentry-ruby/lib/sentry/transport/http_transport.rb index 1e0a78264..9c6dd459f 100644 --- a/sentry-ruby/lib/sentry/transport/http_transport.rb +++ b/sentry-ruby/lib/sentry/transport/http_transport.rb @@ -130,7 +130,7 @@ def conn server = URI(@dsn.server) connection = - if proxy = @transport_configuration.proxy + if proxy = normalize_proxy(@transport_configuration.proxy) ::Net::HTTP.new(server.hostname, server.port, proxy[:uri].hostname, proxy[:uri].port, proxy[:user], proxy[:password]) else ::Net::HTTP.new(server.hostname, server.port, nil) @@ -148,6 +148,20 @@ def conn connection end + def normalize_proxy(proxy) + return proxy unless proxy + + case proxy + when String + uri = URI(proxy) + { uri: uri, user: uri.user, password: uri.password } + when URI + { uri: proxy, user: proxy.user, password: proxy.password } + when Hash + proxy + end + end + def ssl_configuration configuration = { verify: @transport_configuration.ssl_verification, diff --git a/sentry-ruby/spec/sentry/transport/http_transport_spec.rb b/sentry-ruby/spec/sentry/transport/http_transport_spec.rb index eaffd863d..c33999c84 100644 --- a/sentry-ruby/spec/sentry/transport/http_transport_spec.rb +++ b/sentry-ruby/spec/sentry/transport/http_transport_spec.rb @@ -103,6 +103,32 @@ subject.send_data(data) end + it "accepts a custom proxy string" do + configuration.transport.proxy = "https://stan:foobar@example.com:8080" + + stub_request(fake_response) do |_, http_obj| + expect(http_obj.proxy_address).to eq("example.com") + expect(http_obj.proxy_user).to eq("stan") + expect(http_obj.proxy_pass).to eq("foobar") + expect(http_obj.proxy_port).to eq(8080) + end + + subject.send_data(data) + end + + it "accepts a custom proxy URI" do + configuration.transport.proxy = URI("https://stan:foobar@example.com:8080") + + stub_request(fake_response) do |_, http_obj| + expect(http_obj.proxy_address).to eq("example.com") + expect(http_obj.proxy_user).to eq("stan") + expect(http_obj.proxy_pass).to eq("foobar") + expect(http_obj.proxy_port).to eq(8080) + end + + subject.send_data(data) + end + it "accepts custom timeout" do configuration.transport.timeout = 10 From 72396ce4eed95d704eab59c439f07a8a3d59a207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Parent-L=C3=A9vesque?= Date: Sat, 9 Apr 2022 04:59:43 -0400 Subject: [PATCH 2/2] Register SentryContextClientMiddleware on sidekiq workers (#1774) --- sentry-sidekiq/lib/sentry-sidekiq.rb | 3 +++ sentry-sidekiq/spec/sentry/sidekiq_spec.rb | 1 + 2 files changed, 4 insertions(+) diff --git a/sentry-sidekiq/lib/sentry-sidekiq.rb b/sentry-sidekiq/lib/sentry-sidekiq.rb index 7694ebcfd..2ff2816bd 100644 --- a/sentry-sidekiq/lib/sentry-sidekiq.rb +++ b/sentry-sidekiq/lib/sentry-sidekiq.rb @@ -29,6 +29,9 @@ class Railtie < ::Rails::Railtie config.server_middleware do |chain| chain.add Sentry::Sidekiq::SentryContextServerMiddleware end + config.client_middleware do |chain| + chain.add Sentry::Sidekiq::SentryContextClientMiddleware + end end Sidekiq.configure_client do |config| diff --git a/sentry-sidekiq/spec/sentry/sidekiq_spec.rb b/sentry-sidekiq/spec/sentry/sidekiq_spec.rb index 0ae02a177..9879448a1 100644 --- a/sentry-sidekiq/spec/sentry/sidekiq_spec.rb +++ b/sentry-sidekiq/spec/sentry/sidekiq_spec.rb @@ -39,6 +39,7 @@ it "registers error handlers and middlewares" do expect(Sidekiq.error_handlers).to include(described_class::ErrorHandler) expect(Sidekiq.server_middleware.entries.first.klass).to eq(described_class::SentryContextServerMiddleware) + expect(Sidekiq.client_middleware.entries.first.klass).to eq(described_class::SentryContextClientMiddleware) end it "captues exception raised in the worker" do