kesc
How to configure inets (httpc) default profile at startup to connect via a proxy?
Has anyone configured inets default profile (to use a proxy) on startup?
I am using the new relic elixir agent dependency and :inets is started by that (as part of extra applications in that project). My primary aim is to have the :httpc client used by the new relic agent, connect via a proxy (that exists in my company’s production environment) when connecting to the new relic servers.
I tried following the example in the inets user guide which said to add this to the configuration: [{inets, [{services, [{httpc, PropertyList}]}]}] I translated that to
config :inets, services: [ httpc: [ proxy: {{'http://proxy.com', 1234}, []} ] ]
But that doesnt work.
iex>:httpc.get_options(:all)
{:ok, [ proxy: {:undefined, []}, https_proxy: {:undefined, []}, pipeline_timeout: 0, max_pipeline_length: 2,...
Please do correct me if I’m approaching this the wrong way, folks.
Most Liked
kip
I think you should be able to put it in your application.ex file. Something like the foilowing (which is just editing the generated application.ex for an application called ppp):
def start(_type, _args) do
children = [
# Start the Telemetry supervisor
PppWeb.Telemetry,
# Start the Ecto repository
Ppp.Repo,
# Start the PubSub system
{Phoenix.PubSub, name: Ppp.PubSub},
# Start Finch
{Finch, name: Ppp.Finch},
# Start the Endpoint (http/https)
PppWeb.Endpoint
# Start a worker by calling: Ppp.Worker.start_link(arg)
# {Ppp.Worker, arg}
]
# Add these lines
Application.start(:inets)
:ok = :httpc.set_options([{:proxy, {{'http://proxy.com', 1234},[]}}])
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Ppp.Supervisor]
Supervisor.start_link(children, opts)
end
I’m sure this is not the most sophisticated solution but it might be a path forward.
kip
A couple of thoughts (not tested):
- You can tell OTP not to start
new_relic_agentautomatically by configuringruntime: false. For example:
defp deps do
[
{:new_relic_agent, "~> 1.0", runtime: false}
]
end
Then you can start it up whenever you want. For example, using my previous code:
Application.start(:inets)
:ok = :httpc.set_options([{:proxy, {{'http://proxy.com', 1234},[]}}])
Application.start(:new_relic_agent)
I also see that you can configure Seems :httpc_request_options so its possible that you could simply do the following (untested)::proxy is not a valid http_option.







