| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 2 |
%% |
| 3 |
%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
%% purpose with or without fee is hereby granted, provided that the above |
| 5 |
%% copyright notice and this permission notice appear in all copies. |
| 6 |
%% |
| 7 |
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
-module(metrics_SUITE). |
| 16 |
-compile(export_all). |
| 17 |
-compile(nowarn_export_all). |
| 18 |
|
| 19 |
-import(ct_helper, [config/2]). |
| 20 |
-import(ct_helper, [doc/1]). |
| 21 |
-import(cowboy_test, [gun_open/1]). |
| 22 |
-import(cowboy_test, [gun_down/1]). |
| 23 |
-import(cowboy_test, [raw_open/1]). |
| 24 |
-import(cowboy_test, [raw_send/2]). |
| 25 |
-import(cowboy_test, [raw_recv_head/1]). |
| 26 |
|
| 27 |
%% ct. |
| 28 |
|
| 29 |
suite() -> |
| 30 |
[{timetrap, 30000}]. |
| 31 |
|
| 32 |
all() -> |
| 33 |
cowboy_test:common_all(). |
| 34 |
|
| 35 |
groups() -> |
| 36 |
cowboy_test:common_groups(ct_helper:all(?MODULE)). |
| 37 |
|
| 38 |
init_per_group(Name = http, Config) -> |
| 39 |
cowboy_test:init_http(Name, init_plain_opts(Config), Config); |
| 40 |
init_per_group(Name = https, Config) -> |
| 41 |
cowboy_test:init_http(Name, init_plain_opts(Config), Config); |
| 42 |
init_per_group(Name = h2, Config) -> |
| 43 |
cowboy_test:init_http2(Name, init_plain_opts(Config), Config); |
| 44 |
init_per_group(Name = h2c, Config) -> |
| 45 |
Config1 = cowboy_test:init_http(Name, init_plain_opts(Config), Config), |
| 46 |
lists:keyreplace(protocol, 1, Config1, {protocol, http2}); |
| 47 |
init_per_group(Name = h3, Config) -> |
| 48 |
cowboy_test:init_http3(Name, init_plain_opts(Config), Config); |
| 49 |
init_per_group(Name = http_compress, Config) -> |
| 50 |
cowboy_test:init_http(Name, init_compress_opts(Config), Config); |
| 51 |
init_per_group(Name = https_compress, Config) -> |
| 52 |
cowboy_test:init_http(Name, init_compress_opts(Config), Config); |
| 53 |
init_per_group(Name = h2_compress, Config) -> |
| 54 |
cowboy_test:init_http2(Name, init_compress_opts(Config), Config); |
| 55 |
init_per_group(Name = h2c_compress, Config) -> |
| 56 |
Config1 = cowboy_test:init_http(Name, init_compress_opts(Config), Config), |
| 57 |
lists:keyreplace(protocol, 1, Config1, {protocol, http2}); |
| 58 |
init_per_group(Name = h3_compress, Config) -> |
| 59 |
cowboy_test:init_http3(Name, init_compress_opts(Config), Config). |
| 60 |
|
| 61 |
end_per_group(Name, _) -> |
| 62 |
cowboy_test:stop_group(Name). |
| 63 |
|
| 64 |
init_plain_opts(Config) -> |
| 65 |
#{ |
| 66 |
env => #{dispatch => cowboy_router:compile(init_routes(Config))}, |
| 67 |
metrics_callback => do_metrics_callback(), |
| 68 |
stream_handlers => [cowboy_metrics_h, cowboy_stream_h] |
| 69 |
}. |
| 70 |
|
| 71 |
init_compress_opts(Config) -> |
| 72 |
#{ |
| 73 |
env => #{dispatch => cowboy_router:compile(init_routes(Config))}, |
| 74 |
metrics_callback => do_metrics_callback(), |
| 75 |
stream_handlers => [cowboy_metrics_h, cowboy_compress_h, cowboy_stream_h] |
| 76 |
}. |
| 77 |
|
| 78 |
init_routes(_) -> [ |
| 79 |
{"localhost", [ |
| 80 |
{"/", hello_h, []}, |
| 81 |
{"/crash/no_reply", crash_h, no_reply}, |
| 82 |
{"/crash/reply", crash_h, reply}, |
| 83 |
{"/default", default_h, []}, |
| 84 |
{"/full/:key", echo_h, []}, |
| 85 |
{"/resp/:key[/:arg]", resp_h, []}, |
| 86 |
{"/set_options/:key", set_options_h, []}, |
| 87 |
{"/ws_echo", ws_echo, []} |
| 88 |
]} |
| 89 |
]. |
| 90 |
|
| 91 |
do_metrics_callback() -> |
| 92 |
fun(Metrics) -> |
| 93 |
Pid = case Metrics of |
| 94 |
#{req := #{headers := #{<<"x-test-pid">> := P}}} -> |
| 95 |
list_to_pid(binary_to_list(P)); |
| 96 |
#{partial_req := #{headers := #{<<"x-test-pid">> := P}}} -> |
| 97 |
list_to_pid(binary_to_list(P)); |
| 98 |
_ -> |
| 99 |
whereis(early_error_metrics) |
| 100 |
end, |
| 101 |
Pid ! {metrics, self(), Metrics}, |
| 102 |
ok |
| 103 |
end. |
| 104 |
|
| 105 |
%% Tests. |
| 106 |
|
| 107 |
hello_world(Config) -> |
| 108 |
doc("Confirm metrics are correct for a normal GET request."), |
| 109 |
do_get("/", #{}, Config). |
| 110 |
|
| 111 |
user_data(Config) -> |
| 112 |
doc("Confirm user data can be attached to metrics."), |
| 113 |
do_get("/set_options/metrics_user_data", #{handler => set_options_h}, Config). |
| 114 |
|
| 115 |
do_get(Path, UserData, Config) -> |
| 116 |
%% Perform a GET request. |
| 117 |
ConnPid = gun_open(Config), |
| 118 |
Ref = gun:get(ConnPid, Path, [ |
| 119 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 120 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 121 |
]), |
| 122 |
{response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref, infinity), |
| 123 |
{ok, RespBody} = gun:await_body(ConnPid, Ref, infinity), |
| 124 |
%% Receive the metrics and validate them. |
| 125 |
receive |
| 126 |
{metrics, From, Metrics} -> |
| 127 |
%% Ensure the timestamps are in the expected order. |
| 128 |
#{ |
| 129 |
req_start := ReqStart, req_end := ReqEnd, |
| 130 |
resp_start := RespStart, resp_end := RespEnd |
| 131 |
} = Metrics, |
| 132 |
true = (ReqStart =< RespStart) |
| 133 |
and (RespStart =< RespEnd) |
| 134 |
and (RespEnd =< ReqEnd), |
| 135 |
%% We didn't send a body. |
| 136 |
#{ |
| 137 |
req_body_start := undefined, |
| 138 |
req_body_end := undefined, |
| 139 |
req_body_length := 0 |
| 140 |
} = Metrics, |
| 141 |
%% We got a 200 response with a body. |
| 142 |
#{ |
| 143 |
resp_status := 200, |
| 144 |
resp_headers := ExpectedRespHeaders, |
| 145 |
resp_body_length := RespBodyLen |
| 146 |
} = Metrics, |
| 147 |
%% The transfer-encoding header is hidden from stream handlers. |
| 148 |
ExpectedRespHeaders = maps:remove(<<"transfer-encoding">>, |
| 149 |
maps:from_list(RespHeaders)), |
| 150 |
true = byte_size(RespBody) > 0, |
| 151 |
true = RespBodyLen > 0, |
| 152 |
%% The request process executed normally. |
| 153 |
#{procs := Procs} = Metrics, |
| 154 |
[{_, #{ |
| 155 |
spawn := ProcSpawn, |
| 156 |
exit := ProcExit, |
| 157 |
reason := normal |
| 158 |
}}] = maps:to_list(Procs), |
| 159 |
true = ProcSpawn =< ProcExit, |
| 160 |
%% Confirm other metadata are as expected. |
| 161 |
#{ |
| 162 |
ref := _, |
| 163 |
pid := From, |
| 164 |
streamid := StreamID, |
| 165 |
reason := normal, %% @todo Getting h3_no_error here. |
| 166 |
req := #{}, |
| 167 |
informational := [], |
| 168 |
user_data := UserData |
| 169 |
} = Metrics, |
| 170 |
do_check_streamid(StreamID, Config), |
| 171 |
%% All good! |
| 172 |
gun:close(ConnPid) |
| 173 |
end. |
| 174 |
|
| 175 |
do_check_streamid(StreamID, Config) -> |
| 176 |
case config(protocol, Config) of |
| 177 |
http -> 1 = StreamID; |
| 178 |
http2 -> 1 = StreamID; |
| 179 |
http3 -> 0 = StreamID |
| 180 |
end. |
| 181 |
|
| 182 |
post_body(Config) -> |
| 183 |
doc("Confirm metrics are correct for a normal POST request."), |
| 184 |
%% Perform a POST request. |
| 185 |
ConnPid = gun_open(Config), |
| 186 |
Body = <<0:8000000>>, |
| 187 |
Ref = gun:post(ConnPid, "/full/read_body", [ |
| 188 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 189 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 190 |
], Body), |
| 191 |
{response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref, infinity), |
| 192 |
{ok, RespBody} = gun:await_body(ConnPid, Ref, infinity), |
| 193 |
%% Receive the metrics and validate them. |
| 194 |
receive |
| 195 |
{metrics, From, Metrics} -> |
| 196 |
%% Ensure the timestamps are in the expected order. |
| 197 |
#{ |
| 198 |
req_start := ReqStart, req_end := ReqEnd, |
| 199 |
resp_start := RespStart, resp_end := RespEnd |
| 200 |
} = Metrics, |
| 201 |
true = (ReqStart =< RespStart) |
| 202 |
and (RespStart =< RespEnd) |
| 203 |
and (RespEnd =< ReqEnd), |
| 204 |
%% We didn't send a body. |
| 205 |
#{ |
| 206 |
req_body_start := ReqBodyStart, |
| 207 |
req_body_end := ReqBodyEnd, |
| 208 |
req_body_length := ReqBodyLen |
| 209 |
} = Metrics, |
| 210 |
true = ReqBodyStart =< ReqBodyEnd, |
| 211 |
ReqBodyLen = byte_size(Body), |
| 212 |
%% We got a 200 response with a body. |
| 213 |
#{ |
| 214 |
resp_status := 200, |
| 215 |
resp_headers := ExpectedRespHeaders, |
| 216 |
resp_body_length := RespBodyLen |
| 217 |
} = Metrics, |
| 218 |
ExpectedRespHeaders = maps:from_list(RespHeaders), |
| 219 |
true = byte_size(RespBody) > 0, |
| 220 |
true = RespBodyLen > 0, |
| 221 |
%% The request process executed normally. |
| 222 |
#{procs := Procs} = Metrics, |
| 223 |
[{_, #{ |
| 224 |
spawn := ProcSpawn, |
| 225 |
exit := ProcExit, |
| 226 |
reason := normal |
| 227 |
}}] = maps:to_list(Procs), |
| 228 |
true = ProcSpawn =< ProcExit, |
| 229 |
%% Confirm other metadata are as expected. |
| 230 |
#{ |
| 231 |
ref := _, |
| 232 |
pid := From, |
| 233 |
streamid := StreamID, |
| 234 |
reason := normal, |
| 235 |
req := #{}, |
| 236 |
informational := [], |
| 237 |
user_data := #{} |
| 238 |
} = Metrics, |
| 239 |
do_check_streamid(StreamID, Config), |
| 240 |
%% All good! |
| 241 |
gun:close(ConnPid) |
| 242 |
end. |
| 243 |
|
| 244 |
no_resp_body(Config) -> |
| 245 |
doc("Confirm metrics are correct for a default 204 response to a GET request."), |
| 246 |
%% Perform a GET request. |
| 247 |
ConnPid = gun_open(Config), |
| 248 |
Ref = gun:get(ConnPid, "/default", [ |
| 249 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 250 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 251 |
]), |
| 252 |
{response, fin, 204, RespHeaders} = gun:await(ConnPid, Ref, infinity), |
| 253 |
%% Receive the metrics and validate them. |
| 254 |
receive |
| 255 |
{metrics, From, Metrics} -> |
| 256 |
%% Ensure the timestamps are in the expected order. |
| 257 |
#{ |
| 258 |
req_start := ReqStart, req_end := ReqEnd, |
| 259 |
resp_start := RespStart, resp_end := RespEnd |
| 260 |
} = Metrics, |
| 261 |
true = (ReqStart =< RespStart) |
| 262 |
and (RespStart =< RespEnd) |
| 263 |
and (RespEnd =< ReqEnd), |
| 264 |
%% We didn't send a body. |
| 265 |
#{ |
| 266 |
req_body_start := undefined, |
| 267 |
req_body_end := undefined, |
| 268 |
req_body_length := 0 |
| 269 |
} = Metrics, |
| 270 |
%% We got a 200 response with a body. |
| 271 |
#{ |
| 272 |
resp_status := 204, |
| 273 |
resp_headers := ExpectedRespHeaders, |
| 274 |
resp_body_length := 0 |
| 275 |
} = Metrics, |
| 276 |
ExpectedRespHeaders = maps:from_list(RespHeaders), |
| 277 |
%% The request process executed normally. |
| 278 |
#{procs := Procs} = Metrics, |
| 279 |
[{_, #{ |
| 280 |
spawn := ProcSpawn, |
| 281 |
exit := ProcExit, |
| 282 |
reason := normal |
| 283 |
}}] = maps:to_list(Procs), |
| 284 |
true = ProcSpawn =< ProcExit, |
| 285 |
%% Confirm other metadata are as expected. |
| 286 |
#{ |
| 287 |
ref := _, |
| 288 |
pid := From, |
| 289 |
streamid := StreamID, |
| 290 |
reason := normal, |
| 291 |
req := #{}, |
| 292 |
informational := [], |
| 293 |
user_data := #{} |
| 294 |
} = Metrics, |
| 295 |
do_check_streamid(StreamID, Config), |
| 296 |
%% All good! |
| 297 |
gun:close(ConnPid) |
| 298 |
end. |
| 299 |
|
| 300 |
early_error(Config) -> |
| 301 |
doc("Confirm metrics are correct for an early_error response."), |
| 302 |
%% Perform a malformed GET request. |
| 303 |
ConnPid = gun_open(Config), |
| 304 |
%% We must use different solutions to hit early_error with a stream_error |
| 305 |
%% reason in both protocols. |
| 306 |
{Method, Headers, Status, Error} = case config(protocol, Config) of |
| 307 |
http -> {<<"GET">>, [{<<"host">>, <<"host:port">>}], 400, protocol_error}; |
| 308 |
http2 -> {<<"TRACE">>, [], 501, no_error}; |
| 309 |
http3 -> {<<"TRACE">>, [], 501, h3_no_error} |
| 310 |
end, |
| 311 |
Ref = gun:request(ConnPid, Method, "/", [ |
| 312 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 313 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 314 |
|Headers], <<>>), |
| 315 |
{response, fin, Status, RespHeaders} = gun:await(ConnPid, Ref, infinity), |
| 316 |
%% Receive the metrics and validate them. |
| 317 |
receive |
| 318 |
{metrics, From, Metrics} -> |
| 319 |
%% Confirm the metadata is there as expected. |
| 320 |
#{ |
| 321 |
ref := _, |
| 322 |
pid := From, |
| 323 |
streamid := StreamID, |
| 324 |
reason := {stream_error, Error, _}, |
| 325 |
partial_req := #{}, |
| 326 |
resp_status := Status, |
| 327 |
resp_headers := ExpectedRespHeaders, |
| 328 |
early_error_time := _, |
| 329 |
resp_body_length := 0 |
| 330 |
} = Metrics, |
| 331 |
do_check_streamid(StreamID, Config), |
| 332 |
ExpectedRespHeaders = maps:from_list(RespHeaders), |
| 333 |
%% All good! |
| 334 |
gun:close(ConnPid) |
| 335 |
end. |
| 336 |
|
| 337 |
early_error_request_line(Config) -> |
| 338 |
case config(protocol, Config) of |
| 339 |
http -> do_early_error_request_line(Config); |
| 340 |
http2 -> doc("There are no request lines in HTTP/2."); |
| 341 |
http3 -> doc("There are no request lines in HTTP/3.") |
| 342 |
end. |
| 343 |
|
| 344 |
do_early_error_request_line(Config) -> |
| 345 |
doc("Confirm metrics are correct for an early_error response " |
| 346 |
"that occurred on the request-line."), |
| 347 |
%% Register the process in order to receive the metrics event. |
| 348 |
register(early_error_metrics, self()), |
| 349 |
%% Send a malformed request-line. |
| 350 |
Client = raw_open(Config), |
| 351 |
ok = raw_send(Client, <<"FOO bar\r\n">>), |
| 352 |
{'HTTP/1.1', 400, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)), |
| 353 |
{RespHeaders, _} = cow_http:parse_headers(Rest), |
| 354 |
%% Receive the metrics and validate them. |
| 355 |
receive |
| 356 |
{metrics, From, Metrics} -> |
| 357 |
%% Confirm the metadata is there as expected. |
| 358 |
#{ |
| 359 |
ref := _, |
| 360 |
pid := From, |
| 361 |
streamid := StreamID, |
| 362 |
reason := {connection_error, protocol_error, _}, |
| 363 |
partial_req := #{}, |
| 364 |
resp_status := 400, |
| 365 |
resp_headers := ExpectedRespHeaders, |
| 366 |
early_error_time := _, |
| 367 |
resp_body_length := 0 |
| 368 |
} = Metrics, |
| 369 |
do_check_streamid(StreamID, Config), |
| 370 |
ExpectedRespHeaders = maps:from_list(RespHeaders), |
| 371 |
%% All good! |
| 372 |
ok |
| 373 |
end. |
| 374 |
|
| 375 |
%% This test is identical to normal GET except for the handler. |
| 376 |
stream_reply(Config) -> |
| 377 |
doc("Confirm metrics are correct for long polling."), |
| 378 |
do_get("/resp/stream_reply2/200", #{}, Config). |
| 379 |
|
| 380 |
ws(Config) -> |
| 381 |
case config(protocol, Config) of |
| 382 |
http -> do_ws(Config); |
| 383 |
%% @todo The test can be implemented for HTTP/2. |
| 384 |
http2 -> doc("It is not currently possible to switch to Websocket over HTTP/2."); |
| 385 |
http3 -> {skip, "Gun does not currently support Websocket over HTTP/3."} |
| 386 |
end. |
| 387 |
|
| 388 |
do_ws(Config) -> |
| 389 |
doc("Confirm metrics are correct when switching to Websocket."), |
| 390 |
ConnPid = gun_open(Config), |
| 391 |
{ok, http} = gun:await_up(ConnPid, infinity), |
| 392 |
StreamRef = gun:ws_upgrade(ConnPid, "/ws_echo", [ |
| 393 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 394 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 395 |
]), |
| 396 |
receive |
| 397 |
{metrics, From, Metrics} -> |
| 398 |
%% Ensure the timestamps are in the expected order. |
| 399 |
#{ |
| 400 |
req_start := ReqStart, |
| 401 |
req_end := ReqEnd |
| 402 |
} = Metrics, |
| 403 |
true = ReqStart =< ReqEnd, |
| 404 |
%% We didn't send a body. |
| 405 |
#{ |
| 406 |
req_body_start := undefined, |
| 407 |
req_body_end := undefined, |
| 408 |
req_body_length := 0 |
| 409 |
} = Metrics, |
| 410 |
%% We didn't send a response. |
| 411 |
#{ |
| 412 |
resp_start := undefined, |
| 413 |
resp_end := undefined, |
| 414 |
resp_status := undefined, |
| 415 |
resp_headers := undefined, |
| 416 |
resp_body_length := 0 |
| 417 |
} = Metrics, |
| 418 |
%% The request process may not have terminated before terminate |
| 419 |
%% is called. We therefore only check when it spawned. |
| 420 |
#{procs := Procs} = Metrics, |
| 421 |
[{_, #{ |
| 422 |
spawn := _ |
| 423 |
}}] = maps:to_list(Procs), |
| 424 |
%% Confirm other metadata are as expected. |
| 425 |
#{ |
| 426 |
ref := _, |
| 427 |
pid := From, |
| 428 |
streamid := StreamID, |
| 429 |
reason := switch_protocol, |
| 430 |
req := #{}, |
| 431 |
%% A 101 upgrade response was sent. |
| 432 |
informational := [#{ |
| 433 |
status := 101, |
| 434 |
headers := #{ |
| 435 |
<<"connection">> := <<"Upgrade">>, |
| 436 |
<<"upgrade">> := <<"websocket">>, |
| 437 |
<<"sec-websocket-accept">> := _ |
| 438 |
}, |
| 439 |
time := _ |
| 440 |
}], |
| 441 |
user_data := #{} |
| 442 |
} = Metrics, |
| 443 |
do_check_streamid(StreamID, Config), |
| 444 |
%% All good! |
| 445 |
ok |
| 446 |
end, |
| 447 |
%% And of course the upgrade completed successfully after that. |
| 448 |
receive |
| 449 |
{gun_upgrade, ConnPid, StreamRef, _, _} -> |
| 450 |
ok |
| 451 |
end, |
| 452 |
gun:close(ConnPid). |
| 453 |
|
| 454 |
error_response(Config) -> |
| 455 |
doc("Confirm metrics are correct when an error_response command is returned."), |
| 456 |
%% Perform a GET request. |
| 457 |
ConnPid = gun_open(Config), |
| 458 |
Ref = gun:get(ConnPid, "/crash/no_reply", [ |
| 459 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 460 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 461 |
]), |
| 462 |
Protocol = config(protocol, Config), |
| 463 |
RespHeaders = case gun:await(ConnPid, Ref, infinity) of |
| 464 |
{response, fin, 500, RespHeaders0} -> |
| 465 |
RespHeaders0; |
| 466 |
%% The RST_STREAM arrived before the start of the response. |
| 467 |
%% See maybe_h3_error comment for details. |
| 468 |
{error, {stream_error, {stream_error, h3_internal_error, _}}} when Protocol =:= http3 -> |
| 469 |
unknown |
| 470 |
end, |
| 471 |
timer:sleep(100), |
| 472 |
%% Receive the metrics and validate them. |
| 473 |
receive |
| 474 |
{metrics, From, Metrics} -> |
| 475 |
%% Ensure the timestamps are in the expected order. |
| 476 |
#{ |
| 477 |
req_start := ReqStart, req_end := ReqEnd, |
| 478 |
resp_start := RespStart, resp_end := RespEnd |
| 479 |
} = Metrics, |
| 480 |
true = (ReqStart =< RespStart) |
| 481 |
and (RespStart =< RespEnd) |
| 482 |
and (RespEnd =< ReqEnd), |
| 483 |
%% We didn't send a body. |
| 484 |
#{ |
| 485 |
req_body_start := undefined, |
| 486 |
req_body_end := undefined, |
| 487 |
req_body_length := 0 |
| 488 |
} = Metrics, |
| 489 |
%% We got a 500 response without a body. |
| 490 |
#{ |
| 491 |
resp_status := 500, |
| 492 |
resp_headers := ExpectedRespHeaders, |
| 493 |
resp_body_length := 0 |
| 494 |
} = Metrics, |
| 495 |
case RespHeaders of |
| 496 |
%% The HTTP/3 stream has reset too early so we can't |
| 497 |
%% verify the response headers. |
| 498 |
unknown -> |
| 499 |
ok; |
| 500 |
_ -> |
| 501 |
ExpectedRespHeaders = maps:from_list(RespHeaders) |
| 502 |
end, |
| 503 |
%% The request process executed normally. |
| 504 |
#{procs := Procs} = Metrics, |
| 505 |
[{_, #{ |
| 506 |
spawn := ProcSpawn, |
| 507 |
exit := ProcExit, |
| 508 |
reason := {crash, StackTrace} |
| 509 |
}}] = maps:to_list(Procs), |
| 510 |
true = ProcSpawn =< ProcExit, |
| 511 |
%% Confirm other metadata are as expected. |
| 512 |
#{ |
| 513 |
ref := _, |
| 514 |
pid := From, |
| 515 |
streamid := StreamID, |
| 516 |
reason := {internal_error, {'EXIT', _Pid, {crash, StackTrace}}, 'Stream process crashed.'}, |
| 517 |
req := #{}, |
| 518 |
informational := [], |
| 519 |
user_data := #{} |
| 520 |
} = Metrics, |
| 521 |
do_check_streamid(StreamID, Config), |
| 522 |
%% All good! |
| 523 |
gun:close(ConnPid) |
| 524 |
end. |
| 525 |
|
| 526 |
error_response_after_reply(Config) -> |
| 527 |
doc("Confirm metrics are correct when an error_response command is returned " |
| 528 |
"after a response was sent."), |
| 529 |
%% Perform a GET request. |
| 530 |
ConnPid = gun_open(Config), |
| 531 |
Ref = gun:get(ConnPid, "/crash/reply", [ |
| 532 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 533 |
{<<"x-test-pid">>, pid_to_list(self())} |
| 534 |
]), |
| 535 |
Protocol = config(protocol, Config), |
| 536 |
RespHeaders = case gun:await(ConnPid, Ref, infinity) of |
| 537 |
{response, fin, 200, RespHeaders0} -> |
| 538 |
RespHeaders0; |
| 539 |
%% The RST_STREAM arrived before the start of the response. |
| 540 |
%% See maybe_h3_error comment for details. |
| 541 |
{error, {stream_error, {stream_error, h3_internal_error, _}}} when Protocol =:= http3 -> |
| 542 |
unknown |
| 543 |
end, |
| 544 |
timer:sleep(100), |
| 545 |
%% Receive the metrics and validate them. |
| 546 |
receive |
| 547 |
{metrics, From, Metrics} -> |
| 548 |
%% Ensure the timestamps are in the expected order. |
| 549 |
#{ |
| 550 |
req_start := ReqStart, req_end := ReqEnd, |
| 551 |
resp_start := RespStart, resp_end := RespEnd |
| 552 |
} = Metrics, |
| 553 |
true = (ReqStart =< RespStart) |
| 554 |
and (RespStart =< RespEnd) |
| 555 |
and (RespEnd =< ReqEnd), |
| 556 |
%% We didn't send a body. |
| 557 |
#{ |
| 558 |
req_body_start := undefined, |
| 559 |
req_body_end := undefined, |
| 560 |
req_body_length := 0 |
| 561 |
} = Metrics, |
| 562 |
%% We got a 200 response without a body. |
| 563 |
#{ |
| 564 |
resp_status := 200, |
| 565 |
resp_headers := ExpectedRespHeaders, |
| 566 |
resp_body_length := 0 |
| 567 |
} = Metrics, |
| 568 |
case RespHeaders of |
| 569 |
%% The HTTP/3 stream has reset too early so we can't |
| 570 |
%% verify the response headers. |
| 571 |
unknown -> |
| 572 |
ok; |
| 573 |
_ -> |
| 574 |
ExpectedRespHeaders = maps:from_list(RespHeaders) |
| 575 |
end, |
| 576 |
%% The request process executed normally. |
| 577 |
#{procs := Procs} = Metrics, |
| 578 |
[{_, #{ |
| 579 |
spawn := ProcSpawn, |
| 580 |
exit := ProcExit, |
| 581 |
reason := {crash, StackTrace} |
| 582 |
}}] = maps:to_list(Procs), |
| 583 |
true = ProcSpawn =< ProcExit, |
| 584 |
%% Confirm other metadata are as expected. |
| 585 |
#{ |
| 586 |
ref := _, |
| 587 |
pid := From, |
| 588 |
streamid := StreamID, |
| 589 |
reason := {internal_error, {'EXIT', _Pid, {crash, StackTrace}}, 'Stream process crashed.'}, |
| 590 |
req := #{}, |
| 591 |
informational := [], |
| 592 |
user_data := #{} |
| 593 |
} = Metrics, |
| 594 |
do_check_streamid(StreamID, Config), |
| 595 |
%% All good! |
| 596 |
gun:close(ConnPid) |
| 597 |
end. |