Skip to content

Commit 1eaa258

Browse files
epchrisdmke
authored andcommitted
Add support for configuring HTTP Proxy
The previous behavior was to adopt the global proxy setting through the use of the Net::HTTP module. This change introduces two new configuration parameters that allow for manually specifying the HTTP Proxy to use for the InfluxDB connection. This allows for circumstances where it may be required to use a specific proxy only for the InfluxDB connection.
1 parent 588d2ae commit 1eaa258

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ url = "https://influxdb.example.com:8086/database_name?open_timeout=3"
9494
influxdb = InfluxDB::Client.new url: url, open_timeout: 10
9595
```
9696

97+
#### Using a custom HTTP Proxy
98+
99+
By default, the `Net::HTTP` proxy behavior is used (see [Net::HTTP Proxy][proxy])
100+
You can optionally set a proxy address and port via the `proxy_addr` and
101+
`proxy_port` options:
102+
103+
``` ruby
104+
influxdb = InfluxDB::Client.new database,
105+
host: "influxdb.domain.com",
106+
proxy_addr: "your.proxy.addr",
107+
proxy_port: 8080
108+
```
109+
110+
[proxy]: https://docs.ruby-lang.org/en/2.7.0/Net/HTTP.html#class-Net::HTTP-label-Proxies
111+
97112
### Writing data
98113

99114
Write some data:

lib/influxdb/client/http.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def connect_with_retry
4545
retry_count = 0
4646

4747
begin
48-
http = Net::HTTP.new(host, config.port)
48+
http = build_http(host, config.port)
4949
http.open_timeout = config.open_timeout
5050
http.read_timeout = config.read_timeout
5151

@@ -133,6 +133,16 @@ def generate_cert_store
133133
end
134134
store
135135
end
136+
137+
# Builds an http instance, taking into account any configured
138+
# proxy configuration
139+
def build_http(host, port)
140+
if config.proxy_addr
141+
Net::HTTP.new(host, port, config.proxy_addr, config.proxy_port)
142+
else
143+
Net::HTTP.new(host, port)
144+
end
145+
end
136146
end
137147
# rubocop:enable Metrics/MethodLength
138148
# rubocop:enable Metrics/AbcSize

lib/influxdb/config.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module InfluxDB
1616
open_timeout: 5,
1717
read_timeout: 300,
1818
auth_method: nil,
19+
proxy_addr: nil,
20+
proxy_port: nil,
1921

2022
# SSL options
2123
use_ssl: false,

spec/influxdb/config_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
specify { expect(conf).not_to be_udp }
2121
specify { expect(conf).not_to be_async }
2222
specify { expect(conf.epoch).to be_falsey }
23+
specify { expect(conf.proxy_addr).to be_nil }
24+
specify { expect(conf.proxy_port).to be_nil }
2325
end
2426

2527
context "with no database specified" do
@@ -205,4 +207,21 @@
205207
expect(conf).not_to be_async
206208
end
207209
end
210+
211+
context "given explicit proxy information" do
212+
let(:args) do
213+
[{
214+
host: "host",
215+
port: "port",
216+
username: "username",
217+
password: "password",
218+
time_precision: "m",
219+
proxy_addr: "my.proxy.addr",
220+
proxy_port: 8080
221+
}]
222+
end
223+
224+
specify { expect(conf.proxy_addr).to eq("my.proxy.addr") }
225+
specify { expect(conf.proxy_port).to eq(8080) }
226+
end
208227
end

0 commit comments

Comments
 (0)