metrics_SUITE.erl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. } = Metrics,
  134. %% All good!
  135. ok
  136. after 1000 ->
  137. error(timeout)
  138. end.
  139. post_body(Config) ->
  140. doc("Confirm metrics are correct for a normal POST request."),
  141. %% Perform a POST request.
  142. ConnPid = gun_open(Config),
  143. Body = <<0:8000000>>,
  144. Ref = gun:post(ConnPid, "/full/read_body", [
  145. {<<"accept-encoding">>, <<"gzip">>},
  146. {<<"x-test-pid">>, pid_to_list(self())}
  147. ], Body),
  148. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  149. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  150. gun:close(ConnPid),
  151. %% Receive the metrics and validate them.
  152. receive
  153. {metrics, From, Metrics} ->
  154. %% Ensure the timestamps are in the expected order.
  155. #{
  156. req_start := ReqStart, req_end := ReqEnd,
  157. resp_start := RespStart, resp_end := RespEnd
  158. } = Metrics,
  159. true = (ReqStart =< RespStart)
  160. and (RespStart =< RespEnd)
  161. and (RespEnd =< ReqEnd),
  162. %% We didn't send a body.
  163. #{
  164. req_body_start := ReqBodyStart,
  165. req_body_end := ReqBodyEnd,
  166. req_body_length := ReqBodyLen
  167. } = Metrics,
  168. true = ReqBodyStart =< ReqBodyEnd,
  169. ReqBodyLen = byte_size(Body),
  170. %% We got a 200 response with a body.
  171. #{
  172. resp_status := 200,
  173. resp_headers := ExpectedRespHeaders,
  174. resp_body_length := RespBodyLen
  175. } = Metrics,
  176. ExpectedRespHeaders = maps:from_list(RespHeaders),
  177. true = byte_size(RespBody) > 0,
  178. true = RespBodyLen > 0,
  179. %% The request process executed normally.
  180. #{procs := Procs} = Metrics,
  181. [{_, #{
  182. spawn := ProcSpawn,
  183. exit := ProcExit,
  184. reason := normal
  185. }}] = maps:to_list(Procs),
  186. true = ProcSpawn =< ProcExit,
  187. %% Confirm other metadata are as expected.
  188. #{
  189. ref := _,
  190. pid := From,
  191. streamid := 1,
  192. reason := normal,
  193. req := #{}
  194. } = Metrics,
  195. %% All good!
  196. ok
  197. after 1000 ->
  198. error(timeout)
  199. end.
  200. no_resp_body(Config) ->
  201. doc("Confirm metrics are correct for a default 204 response to a GET request."),
  202. %% Perform a GET request.
  203. ConnPid = gun_open(Config),
  204. Ref = gun:get(ConnPid, "/default", [
  205. {<<"accept-encoding">>, <<"gzip">>},
  206. {<<"x-test-pid">>, pid_to_list(self())}
  207. ]),
  208. {response, fin, 204, RespHeaders} = gun:await(ConnPid, Ref),
  209. gun:close(ConnPid),
  210. %% Receive the metrics and validate them.
  211. receive
  212. {metrics, From, Metrics} ->
  213. %% Ensure the timestamps are in the expected order.
  214. #{
  215. req_start := ReqStart, req_end := ReqEnd,
  216. resp_start := RespStart, resp_end := RespEnd
  217. } = Metrics,
  218. true = (ReqStart =< RespStart)
  219. and (RespStart =< RespEnd)
  220. and (RespEnd =< ReqEnd),
  221. %% We didn't send a body.
  222. #{
  223. req_body_start := undefined,
  224. req_body_end := undefined,
  225. req_body_length := 0
  226. } = Metrics,
  227. %% We got a 200 response with a body.
  228. #{
  229. resp_status := 204,
  230. resp_headers := ExpectedRespHeaders,
  231. resp_body_length := 0
  232. } = Metrics,
  233. ExpectedRespHeaders = maps:from_list(RespHeaders),
  234. %% The request process executed normally.
  235. #{procs := Procs} = Metrics,
  236. [{_, #{
  237. spawn := ProcSpawn,
  238. exit := ProcExit,
  239. reason := normal
  240. }}] = maps:to_list(Procs),
  241. true = ProcSpawn =< ProcExit,
  242. %% Confirm other metadata are as expected.
  243. #{
  244. ref := _,
  245. pid := From,
  246. streamid := 1,
  247. reason := normal,
  248. req := #{}
  249. } = Metrics,
  250. %% All good!
  251. ok
  252. after 1000 ->
  253. error(timeout)
  254. end.
  255. early_error(Config) ->
  256. case config(protocol, Config) of
  257. http -> do_early_error(Config);
  258. http2 -> doc("The callback early_error/5 is not currently used for HTTP/2.")
  259. end.
  260. do_early_error(Config) ->
  261. doc("Confirm metrics are correct for an early_error response."),
  262. %% Perform a malformed GET request.
  263. ConnPid = gun_open(Config),
  264. Ref = gun:get(ConnPid, "/", [
  265. {<<"accept-encoding">>, <<"gzip">>},
  266. {<<"host">>, <<"host:port">>},
  267. {<<"x-test-pid">>, pid_to_list(self())}
  268. ]),
  269. {response, fin, 400, RespHeaders} = gun:await(ConnPid, Ref),
  270. gun:close(ConnPid),
  271. %% Receive the metrics and validate them.
  272. receive
  273. {metrics, From, Metrics} ->
  274. %% Confirm the metadata is there as expected.
  275. #{
  276. ref := _,
  277. pid := From,
  278. streamid := 1,
  279. reason := {stream_error, 1, protocol_error, _},
  280. partial_req := #{},
  281. resp_status := 400,
  282. resp_headers := ExpectedRespHeaders,
  283. early_error_time := _,
  284. resp_body_length := 0
  285. } = Metrics,
  286. ExpectedRespHeaders = maps:from_list(RespHeaders),
  287. %% All good!
  288. ok
  289. after 1000 ->
  290. error(timeout)
  291. end.
  292. %% This test is identical to normal GET except for the handler.
  293. stream_reply(Config) ->
  294. doc("Confirm metrics are correct for long polling."),
  295. do_get("/resp/stream_reply2/200", Config).
  296. ws(Config) ->
  297. case config(protocol, Config) of
  298. http -> do_ws(Config);
  299. http2 -> doc("It is not currently possible to switch to Websocket over HTTP/2.")
  300. end.
  301. do_ws(Config) ->
  302. doc("Confirm metrics are correct when switching to Websocket."),
  303. ConnPid = gun_open(Config),
  304. {ok, http} = gun:await_up(ConnPid),
  305. gun:ws_upgrade(ConnPid, "/ws_echo", [
  306. {<<"accept-encoding">>, <<"gzip">>},
  307. {<<"x-test-pid">>, pid_to_list(self())}
  308. ]),
  309. receive
  310. {metrics, From, Metrics} ->
  311. %% Ensure the timestamps are in the expected order.
  312. #{
  313. req_start := ReqStart,
  314. req_end := ReqEnd
  315. } = Metrics,
  316. true = ReqStart =< ReqEnd,
  317. %% We didn't send a body.
  318. #{
  319. req_body_start := undefined,
  320. req_body_end := undefined,
  321. req_body_length := 0
  322. } = Metrics,
  323. %% We didn't send a response.
  324. #{
  325. resp_start := undefined,
  326. resp_end := undefined,
  327. resp_status := undefined,
  328. resp_headers := undefined,
  329. resp_body_length := 0
  330. } = Metrics,
  331. %% The request process may not have terminated before terminate
  332. %% is called. We therefore only check when it spawned.
  333. #{procs := Procs} = Metrics,
  334. [{_, #{
  335. spawn := ProcSpawn
  336. }}] = maps:to_list(Procs),
  337. %% Confirm other metadata are as expected.
  338. #{
  339. ref := _,
  340. pid := From,
  341. streamid := 1,
  342. reason := switch_protocol,
  343. req := #{}
  344. } = Metrics,
  345. %% All good!
  346. ok
  347. after 1000 ->
  348. error(timeout)
  349. end,
  350. %% And of course the upgrade completed successfully after that.
  351. receive
  352. {gun_ws_upgrade, ConnPid, ok, _} ->
  353. ok
  354. after 1000 ->
  355. error(timeout)
  356. end,
  357. gun:close(ConnPid).