Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/hackney_response.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ expect_response(Client) ->
end.


wait_status(#client{buffer=Buf, parser=Parser}=Client) ->
wait_status(#client{buffer=Buf, parser=Parser, mod_metrics=Metrics}=Client) ->
case hackney_http:execute(Parser, Buf) of
{more, NParser} ->
case recv(Client) of
Expand All @@ -82,6 +82,7 @@ wait_status(#client{buffer=Buf, parser=Parser}=Client) ->
Error
end;
{response, Version, Status, _Reason, NParser} ->
_ = metrics:update_histogram(Metrics, [hackney, Client#client.host, status_code], Status),
wait_headers(Client#client{parser=NParser,
buffer = <<>>,
version=Version}, Status);
Expand Down
52 changes: 52 additions & 0 deletions test/hackney_metrics_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-module(hackney_metrics_tests).
-include_lib("eunit/include/eunit.hrl").
-include("hackney_lib.hrl").

%% Metrics callbacks
-export([new/2, increment_counter/1, increment_counter/2, decrement_counter/1, decrement_counter/2, update_histogram/2, update_meter/2, update_gauge/2]).

all_tests() ->
[fun test_host_status_metrics/0].

http_requests_test_() ->
{setup,
fun start/0,
fun stop/1,
fun(ok) ->
{inorder, all_tests()}
end}.

start() ->
application:set_env(hackney, mod_metrics, ?MODULE),
{ok, _} = application:ensure_all_started(hackney),
ok.

stop(ok) ->
application:unset_env(hackney, mod_metrics),
ok.

test_host_status_metrics() ->
URL = <<"http://localhost:8000/status/401">>,
Headers = [{<<"Host">>, <<"myhost.com">>}],
Options = [with_body],
{ok, 401, _H, _JsonBody} = hackney:get(URL, Headers, <<>>, Options),
receive
{update_histogram, [hackney, "localhost", status_code], 401} ->
ok
after
0 -> throw(metric_not_received)
end.


new(_Type, _) -> ok.

increment_counter(Metric) -> increment_counter(Metric, 1).
increment_counter(Metric, Value) -> self() ! {increment_counter, Metric, Value}.

decrement_counter(Metric) -> decrement_counter(Metric, 1).
decrement_counter(Metric, Value) -> self() ! {decrement_counter, Metric, Value}.

update_meter(Metric, Value) -> self() ! {update_meter, Metric, Value}.
update_gauge(Metric, Value) -> self() ! {update_gauge, Metric, Value}.

update_histogram(Metric, Value) -> self() ! {update_histogram, Metric, Value}.