Skip to content
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

Allow proxy configuration via HTTP_PROXY env var #2161

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -18,6 +18,7 @@
- Fix breadcrumbs with `warn` level not being ingested [#2150](https://github.com/getsentry/sentry-ruby/pull/2150)
- Fixes [#2145](https://github.com/getsentry/sentry-ruby/issues/2145)
- Don't send negative line numbers in profiles [#2158](https://github.com/getsentry/sentry-ruby/pull/2158)
- Allow transport proxy configuration to be set with `HTTP_PROXY` environment variable [#2161](https://github.com/getsentry/sentry-ruby/pull/2161)

## 5.12.0

Expand Down
10 changes: 8 additions & 2 deletions sentry-ruby/lib/sentry/transport/http_transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,15 @@

def conn
server = URI(@dsn.server)


# connection respects proxy setting from @transport_configuration, or environment variables (HTTP_PROXY, HTTPS_PROXY, NO_PROXY)
# Net::HTTP will automatically read the env vars.
# See https://ruby-doc.org/3.2.2/stdlibs/net/Net/HTTP.html#class-Net::HTTP-label-Proxies
connection =
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)
::Net::HTTP.new(server.hostname, server.port)

Check warning on line 139 in sentry-ruby/lib/sentry/transport/http_transport.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/transport/http_transport.rb#L139

Added line #L139 was not covered by tests
end

connection.use_ssl = server.scheme == "https"
Expand All @@ -148,6 +151,9 @@
connection
end

# @param proxy [String, URI, Hash] Proxy config value passed into `config.transport`.
# Accepts either a URI formatted string, URI, or a hash with the `uri`, `user`, and `password` keys.
# @return [Hash] Normalized proxy config that will be passed into `Net::HTTP`
st0012 marked this conversation as resolved.
Show resolved Hide resolved
def normalize_proxy(proxy)
return proxy unless proxy

Expand Down
17 changes: 16 additions & 1 deletion sentry-ruby/spec/sentry/transport/http_transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
expect(subject.send(:conn).port).to eq(80)
end
end
context "with http DSN" do
context "with https DSN" do
st0012 marked this conversation as resolved.
Show resolved Hide resolved
let(:dsn) { "https://12345:[email protected]/sentry/42" }

it "sets port to 443" do
Expand Down Expand Up @@ -129,6 +129,21 @@
subject.send_data(data)
end

it "accepts a proxy from ENV[HTTP_PROXY]" do
ENV["http_proxy"] = "https://stan:[email protected]: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)

ENV["http_proxy"] = nil
natikgadzhi marked this conversation as resolved.
Show resolved Hide resolved
end

it "accepts custom timeout" do
configuration.transport.timeout = 10

Expand Down
Loading