tracer_SUITE.erl 13 KB

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