tracer_SUITE.erl 13 KB

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