metrics_SUITE.erl 7.5 KB

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