metrics_SUITE.erl 8.7 KB

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