metrics_SUITE.erl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. ]}
  64. ].
  65. do_metrics_callback() ->
  66. fun(Metrics) ->
  67. PidBin = case Metrics of
  68. #{req := #{headers := #{<<"x-test-pid">> := P}}} -> P;
  69. #{partial_req := #{headers := #{<<"x-test-pid">> := P}}} -> P
  70. end,
  71. Pid = list_to_pid(binary_to_list(PidBin)),
  72. Pid ! {metrics, self(), Metrics},
  73. ok
  74. end.
  75. %% Tests.
  76. hello_world(Config) ->
  77. doc("Confirm metrics are correct for a normal GET request."),
  78. do_get("/", Config).
  79. do_get(Path, Config) ->
  80. %% Perform a GET request.
  81. ConnPid = gun_open(Config),
  82. Ref = gun:get(ConnPid, Path, [
  83. {<<"accept-encoding">>, <<"gzip">>},
  84. {<<"x-test-pid">>, pid_to_list(self())}
  85. ]),
  86. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  87. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  88. gun:close(ConnPid),
  89. %% Receive the metrics and validate them.
  90. receive
  91. {metrics, From, Metrics} ->
  92. %% Ensure the timestamps are in the expected order.
  93. #{
  94. req_start := ReqStart, req_end := ReqEnd,
  95. resp_start := RespStart, resp_end := RespEnd
  96. } = Metrics,
  97. true = (ReqStart =< RespStart)
  98. and (RespStart =< RespEnd)
  99. and (RespEnd =< ReqEnd),
  100. %% We didn't send a body.
  101. #{
  102. req_body_start := undefined,
  103. req_body_end := undefined,
  104. req_body_length := 0
  105. } = Metrics,
  106. %% We got a 200 response with a body.
  107. #{
  108. resp_status := 200,
  109. resp_headers := ExpectedRespHeaders,
  110. resp_body_length := RespBodyLen
  111. } = Metrics,
  112. %% The transfer-encoding header is hidden from stream handlers.
  113. ExpectedRespHeaders = maps:remove(<<"transfer-encoding">>,
  114. maps:from_list(RespHeaders)),
  115. true = byte_size(RespBody) > 0,
  116. true = RespBodyLen > 0,
  117. %% The request process executed normally.
  118. #{procs := Procs} = Metrics,
  119. [{_, #{
  120. spawn := ProcSpawn,
  121. exit := ProcExit,
  122. reason := normal
  123. }}] = maps:to_list(Procs),
  124. true = ProcSpawn =< ProcExit,
  125. %% Confirm other metadata are as expected.
  126. #{
  127. ref := _,
  128. pid := From,
  129. streamid := 1,
  130. reason := normal,
  131. req := #{}
  132. } = Metrics,
  133. %% All good!
  134. ok
  135. after 1000 ->
  136. error(timeout)
  137. end.
  138. post_body(Config) ->
  139. doc("Confirm metrics are correct for a normal POST request."),
  140. %% Perform a POST request.
  141. ConnPid = gun_open(Config),
  142. Body = <<0:8000000>>,
  143. Ref = gun:post(ConnPid, "/full/read_body", [
  144. {<<"accept-encoding">>, <<"gzip">>},
  145. {<<"x-test-pid">>, pid_to_list(self())}
  146. ], Body),
  147. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  148. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  149. gun:close(ConnPid),
  150. %% Receive the metrics and validate them.
  151. receive
  152. {metrics, From, Metrics} ->
  153. %% Ensure the timestamps are in the expected order.
  154. #{
  155. req_start := ReqStart, req_end := ReqEnd,
  156. resp_start := RespStart, resp_end := RespEnd
  157. } = Metrics,
  158. true = (ReqStart =< RespStart)
  159. and (RespStart =< RespEnd)
  160. and (RespEnd =< ReqEnd),
  161. %% We didn't send a body.
  162. #{
  163. req_body_start := ReqBodyStart,
  164. req_body_end := ReqBodyEnd,
  165. req_body_length := ReqBodyLen
  166. } = Metrics,
  167. true = ReqBodyStart =< ReqBodyEnd,
  168. ReqBodyLen = byte_size(Body),
  169. %% We got a 200 response with a body.
  170. #{
  171. resp_status := 200,
  172. resp_headers := ExpectedRespHeaders,
  173. resp_body_length := RespBodyLen
  174. } = Metrics,
  175. ExpectedRespHeaders = maps:from_list(RespHeaders),
  176. true = byte_size(RespBody) > 0,
  177. true = RespBodyLen > 0,
  178. %% The request process executed normally.
  179. #{procs := Procs} = Metrics,
  180. [{_, #{
  181. spawn := ProcSpawn,
  182. exit := ProcExit,
  183. reason := normal
  184. }}] = maps:to_list(Procs),
  185. true = ProcSpawn =< ProcExit,
  186. %% Confirm other metadata are as expected.
  187. #{
  188. ref := _,
  189. pid := From,
  190. streamid := 1,
  191. reason := normal,
  192. req := #{}
  193. } = Metrics,
  194. %% All good!
  195. ok
  196. after 1000 ->
  197. error(timeout)
  198. end.
  199. no_resp_body(Config) ->
  200. doc("Confirm metrics are correct for a default 204 response to a GET request."),
  201. %% Perform a GET request.
  202. ConnPid = gun_open(Config),
  203. Ref = gun:get(ConnPid, "/default", [
  204. {<<"accept-encoding">>, <<"gzip">>},
  205. {<<"x-test-pid">>, pid_to_list(self())}
  206. ]),
  207. {response, fin, 204, RespHeaders} = gun:await(ConnPid, Ref),
  208. gun:close(ConnPid),
  209. %% Receive the metrics and validate them.
  210. receive
  211. {metrics, From, Metrics} ->
  212. %% Ensure the timestamps are in the expected order.
  213. #{
  214. req_start := ReqStart, req_end := ReqEnd,
  215. resp_start := RespStart, resp_end := RespEnd
  216. } = Metrics,
  217. true = (ReqStart =< RespStart)
  218. and (RespStart =< RespEnd)
  219. and (RespEnd =< ReqEnd),
  220. %% We didn't send a body.
  221. #{
  222. req_body_start := undefined,
  223. req_body_end := undefined,
  224. req_body_length := 0
  225. } = Metrics,
  226. %% We got a 200 response with a body.
  227. #{
  228. resp_status := 204,
  229. resp_headers := ExpectedRespHeaders,
  230. resp_body_length := 0
  231. } = Metrics,
  232. ExpectedRespHeaders = maps:from_list(RespHeaders),
  233. %% The request process executed normally.
  234. #{procs := Procs} = Metrics,
  235. [{_, #{
  236. spawn := ProcSpawn,
  237. exit := ProcExit,
  238. reason := normal
  239. }}] = maps:to_list(Procs),
  240. true = ProcSpawn =< ProcExit,
  241. %% Confirm other metadata are as expected.
  242. #{
  243. ref := _,
  244. pid := From,
  245. streamid := 1,
  246. reason := normal,
  247. req := #{}
  248. } = Metrics,
  249. %% All good!
  250. ok
  251. after 1000 ->
  252. error(timeout)
  253. end.
  254. early_error(Config) ->
  255. case config(protocol, Config) of
  256. http -> do_early_error(Config);
  257. http2 -> doc("The callback early_error/5 is not currently used for HTTP/2.")
  258. end.
  259. do_early_error(Config) ->
  260. doc("Confirm metrics are correct for an early_error response."),
  261. %% Perform a malformed GET request.
  262. ConnPid = gun_open(Config),
  263. Ref = gun:get(ConnPid, "/", [
  264. {<<"accept-encoding">>, <<"gzip">>},
  265. {<<"host">>, <<"host:port">>},
  266. {<<"x-test-pid">>, pid_to_list(self())}
  267. ]),
  268. {response, fin, 400, RespHeaders} = gun:await(ConnPid, Ref),
  269. gun:close(ConnPid),
  270. %% Receive the metrics and validate them.
  271. receive
  272. {metrics, From, Metrics} ->
  273. %% Confirm the metadata is there as expected.
  274. #{
  275. ref := _,
  276. pid := From,
  277. streamid := 1,
  278. reason := {stream_error, 1, protocol_error, _},
  279. partial_req := #{},
  280. resp_status := 400,
  281. resp_headers := ExpectedRespHeaders,
  282. early_error_time := _,
  283. resp_body_length := 0
  284. } = Metrics,
  285. ExpectedRespHeaders = maps:from_list(RespHeaders),
  286. %% All good!
  287. ok
  288. after 1000 ->
  289. error(timeout)
  290. end.
  291. %% This test is identical to normal GET except for the handler.
  292. stream_reply(Config) ->
  293. doc("Confirm metrics are correct for long polling."),
  294. do_get("/resp/stream_reply2/200", Config).