metrics_SUITE.erl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. %% Copyright (c) 2017, Loïc Hoguin <essen@ninenines.eu>
  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. -module(metrics_SUITE).
  15. -compile(export_all).
  16. -import(ct_helper, [config/2]).
  17. -import(ct_helper, [doc/1]).
  18. -import(cowboy_test, [gun_open/1]).
  19. -import(cowboy_test, [gun_down/1]).
  20. %% ct.
  21. all() ->
  22. cowboy_test:common_all().
  23. groups() ->
  24. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  25. init_per_group(Name = http, Config) ->
  26. cowboy_test:init_http(Name, init_plain_opts(Config), Config);
  27. init_per_group(Name = https, Config) ->
  28. cowboy_test:init_http(Name, init_plain_opts(Config), Config);
  29. init_per_group(Name = h2, Config) ->
  30. cowboy_test:init_http2(Name, init_plain_opts(Config), Config);
  31. init_per_group(Name = h2c, Config) ->
  32. Config1 = cowboy_test:init_http(Name, init_plain_opts(Config), Config),
  33. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  34. init_per_group(Name = http_compress, Config) ->
  35. cowboy_test:init_http(Name, init_compress_opts(Config), Config);
  36. init_per_group(Name = https_compress, Config) ->
  37. cowboy_test:init_http(Name, init_compress_opts(Config), Config);
  38. init_per_group(Name = h2_compress, Config) ->
  39. cowboy_test:init_http2(Name, init_compress_opts(Config), Config);
  40. init_per_group(Name = h2c_compress, Config) ->
  41. Config1 = cowboy_test:init_http(Name, init_compress_opts(Config), Config),
  42. lists:keyreplace(protocol, 1, Config1, {protocol, http2}).
  43. end_per_group(Name, _) ->
  44. cowboy:stop_listener(Name).
  45. init_plain_opts(Config) ->
  46. #{
  47. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  48. metrics_callback => do_metrics_callback(),
  49. stream_handlers => [cowboy_metrics_h, cowboy_stream_h]
  50. }.
  51. init_compress_opts(Config) ->
  52. #{
  53. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  54. metrics_callback => do_metrics_callback(),
  55. stream_handlers => [cowboy_metrics_h, cowboy_compress_h, cowboy_stream_h]
  56. }.
  57. init_routes(_) -> [
  58. {"localhost", [
  59. {"/", hello_h, []},
  60. {"/default", default_h, []},
  61. {"/full/:key", echo_h, []},
  62. {"/resp/:key[/:arg]", resp_h, []},
  63. {"/ws_echo", ws_echo, []}
  64. ]}
  65. ].
  66. do_metrics_callback() ->
  67. fun(Metrics) ->
  68. PidBin = case Metrics of
  69. #{req := #{headers := #{<<"x-test-pid">> := P}}} -> P;
  70. #{partial_req := #{headers := #{<<"x-test-pid">> := P}}} -> P
  71. end,
  72. Pid = list_to_pid(binary_to_list(PidBin)),
  73. Pid ! {metrics, self(), Metrics},
  74. ok
  75. end.
  76. %% Tests.
  77. hello_world(Config) ->
  78. doc("Confirm metrics are correct for a normal GET request."),
  79. do_get("/", Config).
  80. do_get(Path, Config) ->
  81. %% Perform a GET request.
  82. ConnPid = gun_open(Config),
  83. Ref = gun:get(ConnPid, Path, [
  84. {<<"accept-encoding">>, <<"gzip">>},
  85. {<<"x-test-pid">>, pid_to_list(self())}
  86. ]),
  87. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  88. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  89. gun:close(ConnPid),
  90. %% Receive the metrics and validate them.
  91. receive
  92. {metrics, From, Metrics} ->
  93. %% Ensure the timestamps are in the expected order.
  94. #{
  95. req_start := ReqStart, req_end := ReqEnd,
  96. resp_start := RespStart, resp_end := RespEnd
  97. } = Metrics,
  98. true = (ReqStart =< RespStart)
  99. and (RespStart =< RespEnd)
  100. and (RespEnd =< ReqEnd),
  101. %% We didn't send a body.
  102. #{
  103. req_body_start := undefined,
  104. req_body_end := undefined,
  105. req_body_length := 0
  106. } = Metrics,
  107. %% We got a 200 response with a body.
  108. #{
  109. resp_status := 200,
  110. resp_headers := ExpectedRespHeaders,
  111. resp_body_length := RespBodyLen
  112. } = Metrics,
  113. %% The transfer-encoding header is hidden from stream handlers.
  114. ExpectedRespHeaders = maps:remove(<<"transfer-encoding">>,
  115. maps:from_list(RespHeaders)),
  116. true = byte_size(RespBody) > 0,
  117. true = RespBodyLen > 0,
  118. %% The request process executed normally.
  119. #{procs := Procs} = Metrics,
  120. [{_, #{
  121. spawn := ProcSpawn,
  122. exit := ProcExit,
  123. reason := normal
  124. }}] = maps:to_list(Procs),
  125. true = ProcSpawn =< ProcExit,
  126. %% Confirm other metadata are as expected.
  127. #{
  128. ref := _,
  129. pid := From,
  130. streamid := 1,
  131. reason := normal,
  132. req := #{},
  133. informational := []
  134. } = Metrics,
  135. %% All good!
  136. ok
  137. after 1000 ->
  138. error(timeout)
  139. end.
  140. post_body(Config) ->
  141. doc("Confirm metrics are correct for a normal POST request."),
  142. %% Perform a POST request.
  143. ConnPid = gun_open(Config),
  144. Body = <<0:8000000>>,
  145. Ref = gun:post(ConnPid, "/full/read_body", [
  146. {<<"accept-encoding">>, <<"gzip">>},
  147. {<<"x-test-pid">>, pid_to_list(self())}
  148. ], Body),
  149. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  150. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  151. gun:close(ConnPid),
  152. %% Receive the metrics and validate them.
  153. receive
  154. {metrics, From, Metrics} ->
  155. %% Ensure the timestamps are in the expected order.
  156. #{
  157. req_start := ReqStart, req_end := ReqEnd,
  158. resp_start := RespStart, resp_end := RespEnd
  159. } = Metrics,
  160. true = (ReqStart =< RespStart)
  161. and (RespStart =< RespEnd)
  162. and (RespEnd =< ReqEnd),
  163. %% We didn't send a body.
  164. #{
  165. req_body_start := ReqBodyStart,
  166. req_body_end := ReqBodyEnd,
  167. req_body_length := ReqBodyLen
  168. } = Metrics,
  169. true = ReqBodyStart =< ReqBodyEnd,
  170. ReqBodyLen = byte_size(Body),
  171. %% We got a 200 response with a body.
  172. #{
  173. resp_status := 200,
  174. resp_headers := ExpectedRespHeaders,
  175. resp_body_length := RespBodyLen
  176. } = Metrics,
  177. ExpectedRespHeaders = maps:from_list(RespHeaders),
  178. true = byte_size(RespBody) > 0,
  179. true = RespBodyLen > 0,
  180. %% The request process executed normally.
  181. #{procs := Procs} = Metrics,
  182. [{_, #{
  183. spawn := ProcSpawn,
  184. exit := ProcExit,
  185. reason := normal
  186. }}] = maps:to_list(Procs),
  187. true = ProcSpawn =< ProcExit,
  188. %% Confirm other metadata are as expected.
  189. #{
  190. ref := _,
  191. pid := From,
  192. streamid := 1,
  193. reason := normal,
  194. req := #{},
  195. informational := []
  196. } = Metrics,
  197. %% All good!
  198. ok
  199. after 1000 ->
  200. error(timeout)
  201. end.
  202. no_resp_body(Config) ->
  203. doc("Confirm metrics are correct for a default 204 response to a GET request."),
  204. %% Perform a GET request.
  205. ConnPid = gun_open(Config),
  206. Ref = gun:get(ConnPid, "/default", [
  207. {<<"accept-encoding">>, <<"gzip">>},
  208. {<<"x-test-pid">>, pid_to_list(self())}
  209. ]),
  210. {response, fin, 204, RespHeaders} = gun:await(ConnPid, Ref),
  211. gun:close(ConnPid),
  212. %% Receive the metrics and validate them.
  213. receive
  214. {metrics, From, Metrics} ->
  215. %% Ensure the timestamps are in the expected order.
  216. #{
  217. req_start := ReqStart, req_end := ReqEnd,
  218. resp_start := RespStart, resp_end := RespEnd
  219. } = Metrics,
  220. true = (ReqStart =< RespStart)
  221. and (RespStart =< RespEnd)
  222. and (RespEnd =< ReqEnd),
  223. %% We didn't send a body.
  224. #{
  225. req_body_start := undefined,
  226. req_body_end := undefined,
  227. req_body_length := 0
  228. } = Metrics,
  229. %% We got a 200 response with a body.
  230. #{
  231. resp_status := 204,
  232. resp_headers := ExpectedRespHeaders,
  233. resp_body_length := 0
  234. } = Metrics,
  235. ExpectedRespHeaders = maps:from_list(RespHeaders),
  236. %% The request process executed normally.
  237. #{procs := Procs} = Metrics,
  238. [{_, #{
  239. spawn := ProcSpawn,
  240. exit := ProcExit,
  241. reason := normal
  242. }}] = maps:to_list(Procs),
  243. true = ProcSpawn =< ProcExit,
  244. %% Confirm other metadata are as expected.
  245. #{
  246. ref := _,
  247. pid := From,
  248. streamid := 1,
  249. reason := normal,
  250. req := #{},
  251. informational := []
  252. } = Metrics,
  253. %% All good!
  254. ok
  255. after 1000 ->
  256. error(timeout)
  257. end.
  258. early_error(Config) ->
  259. case config(protocol, Config) of
  260. http -> do_early_error(Config);
  261. http2 -> doc("The callback early_error/5 is not currently used for HTTP/2.")
  262. end.
  263. do_early_error(Config) ->
  264. doc("Confirm metrics are correct for an early_error response."),
  265. %% Perform a malformed GET request.
  266. ConnPid = gun_open(Config),
  267. Ref = gun:get(ConnPid, "/", [
  268. {<<"accept-encoding">>, <<"gzip">>},
  269. {<<"host">>, <<"host:port">>},
  270. {<<"x-test-pid">>, pid_to_list(self())}
  271. ]),
  272. {response, fin, 400, RespHeaders} = gun:await(ConnPid, Ref),
  273. gun:close(ConnPid),
  274. %% Receive the metrics and validate them.
  275. receive
  276. {metrics, From, Metrics} ->
  277. %% Confirm the metadata is there as expected.
  278. #{
  279. ref := _,
  280. pid := From,
  281. streamid := 1,
  282. reason := {stream_error, 1, protocol_error, _},
  283. partial_req := #{},
  284. resp_status := 400,
  285. resp_headers := ExpectedRespHeaders,
  286. early_error_time := _,
  287. resp_body_length := 0
  288. } = Metrics,
  289. ExpectedRespHeaders = maps:from_list(RespHeaders),
  290. %% All good!
  291. ok
  292. after 1000 ->
  293. error(timeout)
  294. end.
  295. %% This test is identical to normal GET except for the handler.
  296. stream_reply(Config) ->
  297. doc("Confirm metrics are correct for long polling."),
  298. do_get("/resp/stream_reply2/200", Config).
  299. ws(Config) ->
  300. case config(protocol, Config) of
  301. http -> do_ws(Config);
  302. http2 -> doc("It is not currently possible to switch to Websocket over HTTP/2.")
  303. end.
  304. do_ws(Config) ->
  305. doc("Confirm metrics are correct when switching to Websocket."),
  306. ConnPid = gun_open(Config),
  307. {ok, http} = gun:await_up(ConnPid),
  308. gun:ws_upgrade(ConnPid, "/ws_echo", [
  309. {<<"accept-encoding">>, <<"gzip">>},
  310. {<<"x-test-pid">>, pid_to_list(self())}
  311. ]),
  312. receive
  313. {metrics, From, Metrics} ->
  314. %% Ensure the timestamps are in the expected order.
  315. #{
  316. req_start := ReqStart,
  317. req_end := ReqEnd
  318. } = Metrics,
  319. true = ReqStart =< ReqEnd,
  320. %% We didn't send a body.
  321. #{
  322. req_body_start := undefined,
  323. req_body_end := undefined,
  324. req_body_length := 0
  325. } = Metrics,
  326. %% We didn't send a response.
  327. #{
  328. resp_start := undefined,
  329. resp_end := undefined,
  330. resp_status := undefined,
  331. resp_headers := undefined,
  332. resp_body_length := 0
  333. } = Metrics,
  334. %% The request process may not have terminated before terminate
  335. %% is called. We therefore only check when it spawned.
  336. #{procs := Procs} = Metrics,
  337. [{_, #{
  338. spawn := _
  339. }}] = maps:to_list(Procs),
  340. %% Confirm other metadata are as expected.
  341. #{
  342. ref := _,
  343. pid := From,
  344. streamid := 1,
  345. reason := switch_protocol,
  346. req := #{},
  347. %% A 101 upgrade response was sent.
  348. informational := [#{
  349. status := 101,
  350. headers := #{
  351. <<"connection">> := <<"Upgrade">>,
  352. <<"upgrade">> := <<"websocket">>,
  353. <<"sec-websocket-accept">> := _
  354. },
  355. time := _
  356. }]
  357. } = Metrics,
  358. %% All good!
  359. ok
  360. after 1000 ->
  361. error(timeout)
  362. end,
  363. %% And of course the upgrade completed successfully after that.
  364. receive
  365. {gun_ws_upgrade, ConnPid, ok, _} ->
  366. ok
  367. after 1000 ->
  368. error(timeout)
  369. end,
  370. gun:close(ConnPid).