hackney icon indicating copy to clipboard operation
hackney copied to clipboard

Integrate prometheus metrics in hackney

Open silverblaze404 opened this issue 8 months ago • 1 comments

Hi @benoitc @deadtrickster

I am thinking of adding prometheus metrics in hackney wherever we are using erlang-metrics (metrics) lib.


do_connect(Host, Port, Transport, #client{mod_metrics=Metrics,
                                          options=ClientOptions}=Client0, Type) ->
  Begin = os:timestamp(),
  {_RequestRef, Client} = case Type of
                            pool ->
                              {Client0#client.request_ref, Client0};
                            direct ->
                              hackney_manager:new_request(Client0)
                          end,

  ConnectTimeout = connect_timeout(Client),
  ConnectOpts = hackney_connection:connect_options(Transport, Host, ClientOptions),

  case Transport:connect(Host, Port, ConnectOpts, ConnectTimeout) of
    {ok, Skt} ->
      ?report_trace("new connection", []),
      ConnectTime = timer:now_diff(os:timestamp(), Begin)/1000,
      _ = metrics:update_histogram(Metrics, [hackney, Host, connect_time], ConnectTime),
      _ = metrics:increment_counter(Metrics, [hackney_pool, Host, new_connection]),
      Client1 = Client#client{socket=Skt,
                              state = connected},
      hackney_manager:update_state(Client1),
      {ok, Client1};
    {error, timeout} ->
      ?report_trace("connect timeout", []),
      _ = metrics:increment_counter(Metrics, [hackney, Host, connect_timeout]),
      hackney_manager:cancel_request(Client),
      {error, connect_timeout};
    Error ->
      ?report_trace("connect error", []),
      _ = metrics:increment_counter(Metrics, [hackney, Host, connect_error]),
      hackney_manager:cancel_request(Client),
      Error
  end.


Any thoughts or things i need to consider while doing this change, I can raise PR for this if there are no challenges/concerns...

silverblaze404 avatar Jun 10 '25 08:06 silverblaze404

I'm open to adding Prometheus support, but we should ensure the solution doesn't restrict us from integrating with other systems. This flexibility is exactly why we have the metrics library. Could you elaborate more on your proposed approach and how it would fit alongside other metric systems?

benoitc avatar Jun 24 '25 20:06 benoitc