aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/cowboy_metrics_h.erl10
-rw-r--r--test/handlers/crash_h.erl16
-rw-r--r--test/metrics_SUITE.erl119
3 files changed, 145 insertions, 0 deletions
diff --git a/src/cowboy_metrics_h.erl b/src/cowboy_metrics_h.erl
index 3a2a8f0..f9a1363 100644
--- a/src/cowboy_metrics_h.erl
+++ b/src/cowboy_metrics_h.erl
@@ -222,6 +222,16 @@ fold([{response, Status, Headers, Body}|Tail],
resp_end=Resp,
resp_body_length=resp_body_length(Body)
});
+fold([{error_response, Status, Headers, Body}|Tail],
+ State=#state{resp_status=RespStatus}) ->
+ %% The error_response command only results in a response
+ %% if no response was sent before.
+ case RespStatus of
+ undefined ->
+ fold([{response, Status, Headers, Body}|Tail], State);
+ _ ->
+ fold(Tail, State)
+ end;
fold([{headers, Status, Headers}|Tail],
State=#state{resp_headers_filter=RespHeadersFilter}) ->
RespStart = erlang:monotonic_time(),
diff --git a/test/handlers/crash_h.erl b/test/handlers/crash_h.erl
new file mode 100644
index 0000000..b687aba
--- /dev/null
+++ b/test/handlers/crash_h.erl
@@ -0,0 +1,16 @@
+%% This module crashes immediately.
+
+-module(crash_h).
+
+-behaviour(cowboy_handler).
+
+-export([init/2]).
+
+-spec init(_, _) -> no_return().
+init(_, no_reply) ->
+ ct_helper:ignore(?MODULE, init, 2),
+ error(crash);
+init(Req, reply) ->
+ _ = cowboy_req:reply(200, Req),
+ ct_helper:ignore(?MODULE, init, 2),
+ error(crash).
diff --git a/test/metrics_SUITE.erl b/test/metrics_SUITE.erl
index 92f8016..4ead60c 100644
--- a/test/metrics_SUITE.erl
+++ b/test/metrics_SUITE.erl
@@ -71,6 +71,8 @@ init_compress_opts(Config) ->
init_routes(_) -> [
{"localhost", [
{"/", hello_h, []},
+ {"/crash/no_reply", crash_h, no_reply},
+ {"/crash/reply", crash_h, reply},
{"/default", default_h, []},
{"/full/:key", echo_h, []},
{"/resp/:key[/:arg]", resp_h, []},
@@ -434,3 +436,120 @@ do_ws(Config) ->
error(timeout)
end,
gun:close(ConnPid).
+
+error_response(Config) ->
+ doc("Confirm metrics are correct when an error_response command is returned."),
+ %% Perform a GET request.
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/crash/no_reply", [
+ {<<"accept-encoding">>, <<"gzip">>},
+ {<<"x-test-pid">>, pid_to_list(self())}
+ ]),
+ {response, fin, 500, RespHeaders} = gun:await(ConnPid, Ref),
+ timer:sleep(100),
+ gun:close(ConnPid),
+ %% Receive the metrics and validate them.
+ receive
+ {metrics, From, Metrics} ->
+ %% Ensure the timestamps are in the expected order.
+ #{
+ req_start := ReqStart, req_end := ReqEnd,
+ resp_start := RespStart, resp_end := RespEnd
+ } = Metrics,
+ true = (ReqStart =< RespStart)
+ and (RespStart =< RespEnd)
+ and (RespEnd =< ReqEnd),
+ %% We didn't send a body.
+ #{
+ req_body_start := undefined,
+ req_body_end := undefined,
+ req_body_length := 0
+ } = Metrics,
+ %% We got a 500 response without a body.
+ #{
+ resp_status := 500,
+ resp_headers := ExpectedRespHeaders,
+ resp_body_length := 0
+ } = Metrics,
+ ExpectedRespHeaders = maps:from_list(RespHeaders),
+ %% The request process executed normally.
+ #{procs := Procs} = Metrics,
+ [{_, #{
+ spawn := ProcSpawn,
+ exit := ProcExit,
+ reason := {crash, _StackTrace}
+ }}] = maps:to_list(Procs),
+ true = ProcSpawn =< ProcExit,
+ %% Confirm other metadata are as expected.
+ #{
+ ref := _,
+ pid := From,
+ streamid := 1,
+ reason := {internal_error, {'EXIT', _Pid, {crash, _StackTrace}}, 'Stream process crashed.'},
+ req := #{},
+ informational := []
+ } = Metrics,
+ %% All good!
+ ok
+ after 1000 ->
+ error(timeout)
+ end.
+
+error_response_after_reply(Config) ->
+ doc("Confirm metrics are correct when an error_response command is returned "
+ "after a response was sent."),
+ %% Perform a GET request.
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/crash/reply", [
+ {<<"accept-encoding">>, <<"gzip">>},
+ {<<"x-test-pid">>, pid_to_list(self())}
+ ]),
+ {response, fin, 200, RespHeaders} = gun:await(ConnPid, Ref),
+ timer:sleep(100),
+ gun:close(ConnPid),
+ %% Receive the metrics and validate them.
+ receive
+ {metrics, From, Metrics} ->
+ %% Ensure the timestamps are in the expected order.
+ #{
+ req_start := ReqStart, req_end := ReqEnd,
+ resp_start := RespStart, resp_end := RespEnd
+ } = Metrics,
+ true = (ReqStart =< RespStart)
+ and (RespStart =< RespEnd)
+ and (RespEnd =< ReqEnd),
+ %% We didn't send a body.
+ #{
+ req_body_start := undefined,
+ req_body_end := undefined,
+ req_body_length := 0
+ } = Metrics,
+ %% We got a 200 response without a body.
+ #{
+ resp_status := 200,
+ resp_headers := ExpectedRespHeaders,
+ resp_body_length := 0
+ } = Metrics,
+ ExpectedRespHeaders = maps:from_list(RespHeaders),
+ %% The request process executed normally.
+ #{procs := Procs} = Metrics,
+ [{_, #{
+ spawn := ProcSpawn,
+ exit := ProcExit,
+ reason := {crash, _StackTrace}
+ }}] = maps:to_list(Procs),
+ true = ProcSpawn =< ProcExit,
+ %% Confirm other metadata are as expected.
+ #{
+ ref := _,
+ pid := From,
+ streamid := 1,
+ reason := {internal_error, {'EXIT', _Pid, {crash, _StackTrace}}, 'Stream process crashed.'},
+ req := #{},
+ informational := []
+ } = Metrics,
+ %% All good!
+ ok
+ after 1000 ->
+ error(timeout)
+ end.