tracer_SUITE.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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(tracer_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [gun_open/1]).
  20. -import(cowboy_test, [gun_down/1]).
  21. %% ct.
  22. suite() ->
  23. [{timetrap, 30000}].
  24. %% We initialize trace patterns here. Appropriate would be in
  25. %% init_per_suite/1, but this works just as well.
  26. all() ->
  27. cowboy_tracer_h:set_trace_patterns(),
  28. %% @todo Implement these tests for HTTP/3.
  29. cowboy_test:common_all() -- [{group, h3}, {group, h3_compress}].
  30. %% We want tests for each group to execute sequentially
  31. %% because we need to modify the protocol options. Groups
  32. %% can run in parallel however.
  33. groups() ->
  34. Tests = ct_helper:all(?MODULE),
  35. [
  36. {http, [], Tests},
  37. {https, [], Tests},
  38. {h2, [], Tests},
  39. {h2c, [], Tests},
  40. {http_compress, [], Tests},
  41. {https_compress, [], Tests},
  42. {h2_compress, [], Tests},
  43. {h2c_compress, [], Tests}
  44. ].
  45. init_per_group(Name = http, Config) ->
  46. cowboy_test:init_http(Name, init_plain_opts(Config), Config);
  47. init_per_group(Name = https, Config) ->
  48. cowboy_test:init_http(Name, init_plain_opts(Config), Config);
  49. init_per_group(Name = h2, Config) ->
  50. cowboy_test:init_http2(Name, init_plain_opts(Config), Config);
  51. init_per_group(Name = h2c, Config) ->
  52. Config1 = cowboy_test:init_http(Name, init_plain_opts(Config), Config),
  53. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  54. init_per_group(Name = http_compress, Config) ->
  55. cowboy_test:init_http(Name, init_compress_opts(Config), Config);
  56. init_per_group(Name = https_compress, Config) ->
  57. cowboy_test:init_http(Name, init_compress_opts(Config), Config);
  58. init_per_group(Name = h2_compress, Config) ->
  59. cowboy_test:init_http2(Name, init_compress_opts(Config), Config);
  60. init_per_group(Name = h2c_compress, Config) ->
  61. Config1 = cowboy_test:init_http(Name, init_compress_opts(Config), Config),
  62. lists:keyreplace(protocol, 1, Config1, {protocol, http2}).
  63. end_per_group(Name, _) ->
  64. cowboy:stop_listener(Name).
  65. init_plain_opts(Config) ->
  66. #{
  67. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  68. stream_handlers => [cowboy_tracer_h, cowboy_stream_h]
  69. }.
  70. init_compress_opts(Config) ->
  71. #{
  72. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  73. stream_handlers => [cowboy_tracer_h, cowboy_compress_h, cowboy_stream_h]
  74. }.
  75. init_routes(_) -> [
  76. {"localhost", [
  77. {"/", hello_h, []},
  78. {"/longer/hello/path", hello_h, []}
  79. ]}
  80. ].
  81. do_get(Path, Config) ->
  82. %% Perform a GET request.
  83. ConnPid = gun_open(Config),
  84. Ref = gun:get(ConnPid, Path, [
  85. {<<"accept-encoding">>, <<"gzip">>},
  86. {<<"x-test-pid">>, pid_to_list(self())}
  87. ]),
  88. {response, nofin, 200, _Headers} = gun:await(ConnPid, Ref),
  89. {ok, _Body} = gun:await_body(ConnPid, Ref),
  90. gun:close(ConnPid).
  91. %% We only care about cowboy_req:reply/4 calls and init/terminate events.
  92. do_tracer_callback(Pid) ->
  93. fun
  94. (Event, _) when Event =:= init; Event =:= terminate ->
  95. Pid ! Event,
  96. 0;
  97. (Event={trace_ts, _, call, {cowboy_req, reply, _}, _}, State) ->
  98. Pid ! Event,
  99. Pid ! {state, State},
  100. State + 1;
  101. (_, State) ->
  102. State + 1
  103. end.
  104. %% Tests.
  105. init(Config) ->
  106. doc("Ensure the init event is triggered."),
  107. Ref = config(ref, Config),
  108. Opts = ranch:get_protocol_options(Ref),
  109. ranch:set_protocol_options(Ref, Opts#{
  110. tracer_callback => do_tracer_callback(self()),
  111. tracer_match_specs => [fun(_,_,_) -> true end]
  112. }),
  113. do_get("/", Config),
  114. receive
  115. init ->
  116. ok
  117. end.
  118. terminate(Config) ->
  119. doc("Ensure the terminate event is triggered."),
  120. Ref = config(ref, Config),
  121. Opts = ranch:get_protocol_options(Ref),
  122. ranch:set_protocol_options(Ref, Opts#{
  123. tracer_callback => do_tracer_callback(self()),
  124. tracer_match_specs => [fun(_,_,_) -> true end]
  125. }),
  126. do_get("/", Config),
  127. receive
  128. terminate ->
  129. ok
  130. end.
  131. state(Config) ->
  132. doc("Ensure the returned state is used."),
  133. Ref = config(ref, Config),
  134. Opts = ranch:get_protocol_options(Ref),
  135. ranch:set_protocol_options(Ref, Opts#{
  136. tracer_callback => do_tracer_callback(self()),
  137. tracer_match_specs => [fun(_,_,_) -> true end]
  138. }),
  139. do_get("/", Config),
  140. receive
  141. {state, St} ->
  142. true = St > 0,
  143. ok
  144. end.
  145. empty(Config) ->
  146. doc("Empty match specs unconditionally enable tracing."),
  147. Ref = config(ref, Config),
  148. Opts = ranch:get_protocol_options(Ref),
  149. ranch:set_protocol_options(Ref, Opts#{
  150. tracer_callback => do_tracer_callback(self()),
  151. tracer_match_specs => []
  152. }),
  153. do_get("/", Config),
  154. receive
  155. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  156. ok
  157. end.
  158. predicate_true(Config) ->
  159. doc("Predicate function returns true, unconditionally enable tracing."),
  160. Ref = config(ref, Config),
  161. Opts = ranch:get_protocol_options(Ref),
  162. ranch:set_protocol_options(Ref, Opts#{
  163. tracer_callback => do_tracer_callback(self()),
  164. tracer_match_specs => [fun(_,_,_) -> true end]
  165. }),
  166. do_get("/", Config),
  167. receive
  168. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  169. ok
  170. end.
  171. predicate_false(Config) ->
  172. doc("Predicate function returns false, unconditionally disable tracing."),
  173. Ref = config(ref, Config),
  174. Opts = ranch:get_protocol_options(Ref),
  175. ranch:set_protocol_options(Ref, Opts#{
  176. tracer_callback => do_tracer_callback(self()),
  177. tracer_match_specs => [fun(_,_,_) -> false end]
  178. }),
  179. do_get("/", Config),
  180. receive
  181. Msg when element(1, Msg) =:= trace_ts ->
  182. error(Msg)
  183. after 100 ->
  184. ok
  185. end.
  186. method(Config) ->
  187. doc("Method is the same as the request's, enable tracing."),
  188. Ref = config(ref, Config),
  189. Opts = ranch:get_protocol_options(Ref),
  190. ranch:set_protocol_options(Ref, Opts#{
  191. tracer_callback => do_tracer_callback(self()),
  192. tracer_match_specs => [{method, <<"GET">>}]
  193. }),
  194. do_get("/", Config),
  195. receive
  196. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  197. ok
  198. end.
  199. method_no_match(Config) ->
  200. doc("Method is different from the request's, disable tracing."),
  201. Ref = config(ref, Config),
  202. Opts = ranch:get_protocol_options(Ref),
  203. ranch:set_protocol_options(Ref, Opts#{
  204. tracer_callback => do_tracer_callback(self()),
  205. tracer_match_specs => [{method, <<"POST">>}]
  206. }),
  207. do_get("/", Config),
  208. receive
  209. Msg when element(1, Msg) =:= trace_ts ->
  210. error(Msg)
  211. after 100 ->
  212. ok
  213. end.
  214. host(Config) ->
  215. doc("Host is the same as the request's, enable tracing."),
  216. Ref = config(ref, Config),
  217. Opts = ranch:get_protocol_options(Ref),
  218. ranch:set_protocol_options(Ref, Opts#{
  219. tracer_callback => do_tracer_callback(self()),
  220. tracer_match_specs => [{host, <<"localhost">>}]
  221. }),
  222. do_get("/", Config),
  223. receive
  224. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  225. ok
  226. end.
  227. host_no_match(Config) ->
  228. doc("Host is different from the request's, disable tracing."),
  229. Ref = config(ref, Config),
  230. Opts = ranch:get_protocol_options(Ref),
  231. ranch:set_protocol_options(Ref, Opts#{
  232. tracer_callback => do_tracer_callback(self()),
  233. tracer_match_specs => [{host, <<"ninenines.eu">>}]
  234. }),
  235. do_get("/", Config),
  236. receive
  237. Msg when element(1, Msg) =:= trace_ts ->
  238. error(Msg)
  239. after 100 ->
  240. ok
  241. end.
  242. path(Config) ->
  243. doc("Path is the same as the request's, enable tracing."),
  244. Ref = config(ref, Config),
  245. Opts = ranch:get_protocol_options(Ref),
  246. ranch:set_protocol_options(Ref, Opts#{
  247. tracer_callback => do_tracer_callback(self()),
  248. tracer_match_specs => [{path, <<"/longer/hello/path">>}]
  249. }),
  250. do_get("/longer/hello/path", Config),
  251. receive
  252. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  253. ok
  254. end.
  255. path_no_match(Config) ->
  256. doc("Path is different from the request's, disable tracing."),
  257. Ref = config(ref, Config),
  258. Opts = ranch:get_protocol_options(Ref),
  259. ranch:set_protocol_options(Ref, Opts#{
  260. tracer_callback => do_tracer_callback(self()),
  261. tracer_match_specs => [{path, <<"/some/other/path">>}]
  262. }),
  263. do_get("/longer/hello/path", Config),
  264. receive
  265. Msg when element(1, Msg) =:= trace_ts ->
  266. error(Msg)
  267. after 100 ->
  268. ok
  269. end.
  270. path_start(Config) ->
  271. doc("Start of path is the same as request's, enable tracing."),
  272. Ref = config(ref, Config),
  273. Opts = ranch:get_protocol_options(Ref),
  274. ranch:set_protocol_options(Ref, Opts#{
  275. tracer_callback => do_tracer_callback(self()),
  276. tracer_match_specs => [{path_start, <<"/longer/hello">>}]
  277. }),
  278. do_get("/longer/hello/path", Config),
  279. receive
  280. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  281. ok
  282. end.
  283. path_start_no_match(Config) ->
  284. doc("Start of path is different from the request's, disable tracing."),
  285. Ref = config(ref, Config),
  286. Opts = ranch:get_protocol_options(Ref),
  287. ranch:set_protocol_options(Ref, Opts#{
  288. tracer_callback => do_tracer_callback(self()),
  289. tracer_match_specs => [{path_start, <<"/shorter/hello">>}]
  290. }),
  291. do_get("/longer/hello/path", Config),
  292. receive
  293. Msg when element(1, Msg) =:= trace_ts ->
  294. error(Msg)
  295. after 100 ->
  296. ok
  297. end.
  298. header_defined(Config) ->
  299. doc("Header is defined in the request, enable tracing."),
  300. Ref = config(ref, Config),
  301. Opts = ranch:get_protocol_options(Ref),
  302. ranch:set_protocol_options(Ref, Opts#{
  303. tracer_callback => do_tracer_callback(self()),
  304. tracer_match_specs => [{header, <<"accept-encoding">>}]
  305. }),
  306. do_get("/", Config),
  307. receive
  308. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  309. ok
  310. end.
  311. header_defined_no_match(Config) ->
  312. doc("Header is not defined in the request, disable tracing."),
  313. Ref = config(ref, Config),
  314. Opts = ranch:get_protocol_options(Ref),
  315. ranch:set_protocol_options(Ref, Opts#{
  316. tracer_callback => do_tracer_callback(self()),
  317. tracer_match_specs => [{header, <<"accept-language">>}]
  318. }),
  319. do_get("/", Config),
  320. receive
  321. Msg when element(1, Msg) =:= trace_ts ->
  322. error(Msg)
  323. after 100 ->
  324. ok
  325. end.
  326. header_value(Config) ->
  327. doc("Header value is the same as the request's, enable tracing."),
  328. Ref = config(ref, Config),
  329. Opts = ranch:get_protocol_options(Ref),
  330. ranch:set_protocol_options(Ref, Opts#{
  331. tracer_callback => do_tracer_callback(self()),
  332. tracer_match_specs => [{header, <<"accept-encoding">>, <<"gzip">>}]
  333. }),
  334. do_get("/", Config),
  335. receive
  336. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  337. ok
  338. end.
  339. header_value_no_match(Config) ->
  340. doc("Header value is different from the request's, disable tracing."),
  341. Ref = config(ref, Config),
  342. Opts = ranch:get_protocol_options(Ref),
  343. ranch:set_protocol_options(Ref, Opts#{
  344. tracer_callback => do_tracer_callback(self()),
  345. tracer_match_specs => [{header, <<"accept-encoding">>, <<"nope">>}]
  346. }),
  347. do_get("/", Config),
  348. receive
  349. Msg when element(1, Msg) =:= trace_ts ->
  350. error(Msg)
  351. after 100 ->
  352. ok
  353. end.
  354. peer_ip(Config) ->
  355. doc("Peer IP is the same as the request's, enable tracing."),
  356. Ref = config(ref, Config),
  357. Opts = ranch:get_protocol_options(Ref),
  358. ranch:set_protocol_options(Ref, Opts#{
  359. tracer_callback => do_tracer_callback(self()),
  360. tracer_match_specs => [{peer_ip, {127, 0, 0, 1}}]
  361. }),
  362. do_get("/", Config),
  363. receive
  364. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  365. ok
  366. end.
  367. peer_ip_no_match(Config) ->
  368. doc("Peer IP is different from the request's, disable tracing."),
  369. Ref = config(ref, Config),
  370. Opts = ranch:get_protocol_options(Ref),
  371. ranch:set_protocol_options(Ref, Opts#{
  372. tracer_callback => do_tracer_callback(self()),
  373. tracer_match_specs => [{peer_ip, {8, 8, 8, 8}}]
  374. }),
  375. do_get("/", Config),
  376. receive
  377. Msg when element(1, Msg) =:= trace_ts ->
  378. error(Msg)
  379. after 100 ->
  380. ok
  381. end.
  382. missing_callback(Config) ->
  383. doc("Ensure the request is still processed if the callback is not provided."),
  384. Ref = config(ref, Config),
  385. Opts0 = ranch:get_protocol_options(Ref),
  386. Opts = maps:remove(tracer_callback, Opts0),
  387. ranch:set_protocol_options(Ref, Opts#{
  388. tracer_match_specs => [{method, <<"GET">>}]
  389. }),
  390. do_get("/", Config),
  391. receive
  392. Msg when element(1, Msg) =:= trace_ts ->
  393. error(Msg)
  394. after 100 ->
  395. ok
  396. end.
  397. missing_match_specs(Config) ->
  398. doc("Ensure the request is still processed if match specs are not provided."),
  399. Ref = config(ref, Config),
  400. Opts0 = ranch:get_protocol_options(Ref),
  401. Opts = maps:remove(tracer_match_specs, Opts0),
  402. ranch:set_protocol_options(Ref, Opts#{
  403. tracer_callback => do_tracer_callback(self())
  404. }),
  405. do_get("/", Config),
  406. receive
  407. Msg when element(1, Msg) =:= trace_ts ->
  408. error(Msg)
  409. after 100 ->
  410. ok
  411. end.
  412. two_matching_requests(Config) ->
  413. doc("Perform two requests that enable tracing on the same connection."),
  414. Ref = config(ref, Config),
  415. Opts = ranch:get_protocol_options(Ref),
  416. ranch:set_protocol_options(Ref, Opts#{
  417. tracer_callback => do_tracer_callback(self()),
  418. tracer_match_specs => [fun(_,_,_) -> true end]
  419. }),
  420. %% Perform a GET request.
  421. ConnPid = gun_open(Config),
  422. Ref1 = gun:get(ConnPid, "/", []),
  423. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  424. {ok, _} = gun:await_body(ConnPid, Ref1),
  425. receive
  426. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  427. ok
  428. end,
  429. %% Perform a second GET request on the same connection.
  430. Ref2 = gun:get(ConnPid, "/", []),
  431. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  432. {ok, _} = gun:await_body(ConnPid, Ref2),
  433. receive
  434. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  435. ok
  436. end,
  437. gun:close(ConnPid).