metrics_SUITE.erl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. ]}
  61. ].
  62. do_metrics_callback() ->
  63. fun(Metrics=#{req := #{headers := #{<<"x-test-pid">> := PidBin}}}) ->
  64. Pid = list_to_pid(binary_to_list(PidBin)),
  65. Pid ! {metrics, self(), Metrics},
  66. ok
  67. end.
  68. %% Tests.
  69. hello_world(Config) ->
  70. %% Perform a request.
  71. ConnPid = gun_open(Config),
  72. Ref = gun:get(ConnPid, "/", [{<<"x-test-pid">>, pid_to_list(self())}]),
  73. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  74. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  75. gun:close(ConnPid),
  76. %% Receive the metrics and print them.
  77. receive
  78. {metrics, From, Metrics} ->
  79. %% Ensure the timestamps are in the expected order.
  80. #{
  81. req_start := ReqStart, req_end := ReqEnd,
  82. resp_start := RespStart, resp_end := RespEnd
  83. } = Metrics,
  84. true = (ReqStart =< RespStart)
  85. and (RespStart =< RespEnd)
  86. and (RespEnd =< ReqEnd),
  87. %% We didn't send a body.
  88. #{
  89. req_body_start := undefined,
  90. req_body_end := undefined,
  91. req_body_length := 0
  92. } = Metrics,
  93. %% We got a 200 response with a body.
  94. #{
  95. resp_status := 200,
  96. resp_headers := ExpectedRespHeaders,
  97. resp_body_length := RespBodyLen
  98. } = Metrics,
  99. ExpectedRespHeaders = maps:from_list(RespHeaders),
  100. true = RespBodyLen > 0,
  101. %% The request process executed normally.
  102. #{procs := Procs} = Metrics,
  103. [{_, #{
  104. spawn := ProcSpawn,
  105. exit := ProcExit,
  106. reason := normal
  107. }}] = maps:to_list(Procs),
  108. true = ProcSpawn =< ProcExit,
  109. %% Confirm other metadata are as expected.
  110. #{
  111. ref := _,
  112. pid := From,
  113. streamid := 1,
  114. reason := normal,
  115. req := #{}
  116. } = Metrics,
  117. %% All good!
  118. ok
  119. after 1000 ->
  120. error(timeout)
  121. end.