metrics_SUITE.erl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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_http(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_http(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. {"/full/:key", echo_h, []}
  61. ]}
  62. ].
  63. do_metrics_callback() ->
  64. fun(Metrics=#{req := #{headers := #{<<"x-test-pid">> := PidBin}}}) ->
  65. Pid = list_to_pid(binary_to_list(PidBin)),
  66. Pid ! {metrics, self(), Metrics},
  67. ok
  68. end.
  69. %% Tests.
  70. hello_world(Config) ->
  71. doc("Confirm metrics are correct for a normal GET request."),
  72. %% Perform a GET request.
  73. ConnPid = gun_open(Config),
  74. Ref = gun:get(ConnPid, "/", [
  75. {<<"accept-encoding">>, <<"gzip">>},
  76. {<<"x-test-pid">>, pid_to_list(self())}
  77. ]),
  78. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  79. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  80. gun:close(ConnPid),
  81. %% Receive the metrics and validate them.
  82. receive
  83. {metrics, From, Metrics} ->
  84. %% Ensure the timestamps are in the expected order.
  85. #{
  86. req_start := ReqStart, req_end := ReqEnd,
  87. resp_start := RespStart, resp_end := RespEnd
  88. } = Metrics,
  89. true = (ReqStart =< RespStart)
  90. and (RespStart =< RespEnd)
  91. and (RespEnd =< ReqEnd),
  92. %% We didn't send a body.
  93. #{
  94. req_body_start := undefined,
  95. req_body_end := undefined,
  96. req_body_length := 0
  97. } = Metrics,
  98. %% We got a 200 response with a body.
  99. #{
  100. resp_status := 200,
  101. resp_headers := ExpectedRespHeaders,
  102. resp_body_length := RespBodyLen
  103. } = Metrics,
  104. ExpectedRespHeaders = maps:from_list(RespHeaders),
  105. true = byte_size(RespBody) > 0,
  106. true = RespBodyLen > 0,
  107. %% The request process executed normally.
  108. #{procs := Procs} = Metrics,
  109. [{_, #{
  110. spawn := ProcSpawn,
  111. exit := ProcExit,
  112. reason := normal
  113. }}] = maps:to_list(Procs),
  114. true = ProcSpawn =< ProcExit,
  115. %% Confirm other metadata are as expected.
  116. #{
  117. ref := _,
  118. pid := From,
  119. streamid := 1,
  120. reason := normal,
  121. req := #{}
  122. } = Metrics,
  123. %% All good!
  124. ok
  125. after 1000 ->
  126. error(timeout)
  127. end.
  128. post_body(Config) ->
  129. doc("Confirm metrics are correct for a normal POST request."),
  130. %% Perform a POST request.
  131. ConnPid = gun_open(Config),
  132. Body = <<0:8000000>>,
  133. Ref = gun:post(ConnPid, "/full/read_body", [
  134. {<<"accept-encoding">>, <<"gzip">>},
  135. {<<"x-test-pid">>, pid_to_list(self())}
  136. ], Body),
  137. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  138. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  139. gun:close(ConnPid),
  140. %% Receive the metrics and validate them.
  141. receive
  142. {metrics, From, Metrics} ->
  143. %% Ensure the timestamps are in the expected order.
  144. #{
  145. req_start := ReqStart, req_end := ReqEnd,
  146. resp_start := RespStart, resp_end := RespEnd
  147. } = Metrics,
  148. true = (ReqStart =< RespStart)
  149. and (RespStart =< RespEnd)
  150. and (RespEnd =< ReqEnd),
  151. %% We didn't send a body.
  152. #{
  153. req_body_start := ReqBodyStart,
  154. req_body_end := ReqBodyEnd,
  155. req_body_length := ReqBodyLen
  156. } = Metrics,
  157. true = ReqBodyStart =< ReqBodyEnd,
  158. ReqBodyLen = byte_size(Body),
  159. %% We got a 200 response with a body.
  160. #{
  161. resp_status := 200,
  162. resp_headers := ExpectedRespHeaders,
  163. resp_body_length := RespBodyLen
  164. } = Metrics,
  165. ExpectedRespHeaders = maps:from_list(RespHeaders),
  166. true = byte_size(RespBody) > 0,
  167. true = RespBodyLen > 0,
  168. %% The request process executed normally.
  169. #{procs := Procs} = Metrics,
  170. [{_, #{
  171. spawn := ProcSpawn,
  172. exit := ProcExit,
  173. reason := normal
  174. }}] = maps:to_list(Procs),
  175. true = ProcSpawn =< ProcExit,
  176. %% Confirm other metadata are as expected.
  177. #{
  178. ref := _,
  179. pid := From,
  180. streamid := 1,
  181. reason := normal,
  182. req := #{}
  183. } = Metrics,
  184. %% All good!
  185. ok
  186. after 1000 ->
  187. error(timeout)
  188. end.