tracer_SUITE.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. 0;
  90. (Event={trace_ts, _, call, {cowboy_req, reply, _}, _}, State) ->
  91. Pid ! Event,
  92. Pid ! {state, State},
  93. State + 1;
  94. (_, State) ->
  95. State + 1
  96. end.
  97. %% Tests.
  98. init(Config) ->
  99. doc("Ensure the init event is triggered."),
  100. Ref = config(ref, Config),
  101. Opts = ranch:get_protocol_options(Ref),
  102. ranch:set_protocol_options(Ref, Opts#{
  103. tracer_callback => do_tracer_callback(self()),
  104. tracer_match_specs => [fun(_,_,_) -> true end]
  105. }),
  106. do_get("/", Config),
  107. receive
  108. init ->
  109. ok
  110. after 100 ->
  111. error(timeout)
  112. end.
  113. terminate(Config) ->
  114. doc("Ensure the terminate event is triggered."),
  115. Ref = config(ref, Config),
  116. Opts = ranch:get_protocol_options(Ref),
  117. ranch:set_protocol_options(Ref, Opts#{
  118. tracer_callback => do_tracer_callback(self()),
  119. tracer_match_specs => [fun(_,_,_) -> true end]
  120. }),
  121. do_get("/", Config),
  122. receive
  123. terminate ->
  124. ok
  125. after 100 ->
  126. error(timeout)
  127. end.
  128. state(Config) ->
  129. doc("Ensure the returned state is used."),
  130. Ref = config(ref, Config),
  131. Opts = ranch:get_protocol_options(Ref),
  132. ranch:set_protocol_options(Ref, Opts#{
  133. tracer_callback => do_tracer_callback(self()),
  134. tracer_match_specs => [fun(_,_,_) -> true end]
  135. }),
  136. do_get("/", Config),
  137. receive
  138. {state, St} ->
  139. true = St > 0,
  140. ok
  141. after 100 ->
  142. error(timeout)
  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. after 100 ->
  157. error(timeout)
  158. end.
  159. predicate_true(Config) ->
  160. doc("Predicate function returns true, unconditionally enable tracing."),
  161. Ref = config(ref, Config),
  162. Opts = ranch:get_protocol_options(Ref),
  163. ranch:set_protocol_options(Ref, Opts#{
  164. tracer_callback => do_tracer_callback(self()),
  165. tracer_match_specs => [fun(_,_,_) -> true end]
  166. }),
  167. do_get("/", Config),
  168. receive
  169. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  170. ok
  171. after 100 ->
  172. error(timeout)
  173. end.
  174. predicate_false(Config) ->
  175. doc("Predicate function returns false, unconditionally disable tracing."),
  176. Ref = config(ref, Config),
  177. Opts = ranch:get_protocol_options(Ref),
  178. ranch:set_protocol_options(Ref, Opts#{
  179. tracer_callback => do_tracer_callback(self()),
  180. tracer_match_specs => [fun(_,_,_) -> false end]
  181. }),
  182. do_get("/", Config),
  183. receive
  184. Msg when element(1, Msg) =:= trace_ts ->
  185. error(Msg)
  186. after 100 ->
  187. ok
  188. end.
  189. method(Config) ->
  190. doc("Method is the same as the request's, enable tracing."),
  191. Ref = config(ref, Config),
  192. Opts = ranch:get_protocol_options(Ref),
  193. ranch:set_protocol_options(Ref, Opts#{
  194. tracer_callback => do_tracer_callback(self()),
  195. tracer_match_specs => [{method, <<"GET">>}]
  196. }),
  197. do_get("/", Config),
  198. receive
  199. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  200. ok
  201. after 100 ->
  202. error(timeout)
  203. end.
  204. method_no_match(Config) ->
  205. doc("Method is different from the request's, disable tracing."),
  206. Ref = config(ref, Config),
  207. Opts = ranch:get_protocol_options(Ref),
  208. ranch:set_protocol_options(Ref, Opts#{
  209. tracer_callback => do_tracer_callback(self()),
  210. tracer_match_specs => [{method, <<"POST">>}]
  211. }),
  212. do_get("/", Config),
  213. receive
  214. Msg when element(1, Msg) =:= trace_ts ->
  215. error(Msg)
  216. after 100 ->
  217. ok
  218. end.
  219. host(Config) ->
  220. doc("Host is the same as the request's, enable tracing."),
  221. Ref = config(ref, Config),
  222. Opts = ranch:get_protocol_options(Ref),
  223. ranch:set_protocol_options(Ref, Opts#{
  224. tracer_callback => do_tracer_callback(self()),
  225. tracer_match_specs => [{host, <<"localhost">>}]
  226. }),
  227. do_get("/", Config),
  228. receive
  229. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  230. ok
  231. after 100 ->
  232. error(timeout)
  233. end.
  234. host_no_match(Config) ->
  235. doc("Host is different from the request's, disable tracing."),
  236. Ref = config(ref, Config),
  237. Opts = ranch:get_protocol_options(Ref),
  238. ranch:set_protocol_options(Ref, Opts#{
  239. tracer_callback => do_tracer_callback(self()),
  240. tracer_match_specs => [{host, <<"ninenines.eu">>}]
  241. }),
  242. do_get("/", Config),
  243. receive
  244. Msg when element(1, Msg) =:= trace_ts ->
  245. error(Msg)
  246. after 100 ->
  247. ok
  248. end.
  249. path(Config) ->
  250. doc("Path is the same as the request's, enable tracing."),
  251. Ref = config(ref, Config),
  252. Opts = ranch:get_protocol_options(Ref),
  253. ranch:set_protocol_options(Ref, Opts#{
  254. tracer_callback => do_tracer_callback(self()),
  255. tracer_match_specs => [{path, <<"/longer/hello/path">>}]
  256. }),
  257. do_get("/longer/hello/path", Config),
  258. receive
  259. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  260. ok
  261. after 100 ->
  262. error(timeout)
  263. end.
  264. path_no_match(Config) ->
  265. doc("Path is different from the request's, disable tracing."),
  266. Ref = config(ref, Config),
  267. Opts = ranch:get_protocol_options(Ref),
  268. ranch:set_protocol_options(Ref, Opts#{
  269. tracer_callback => do_tracer_callback(self()),
  270. tracer_match_specs => [{path, <<"/some/other/path">>}]
  271. }),
  272. do_get("/longer/hello/path", Config),
  273. receive
  274. Msg when element(1, Msg) =:= trace_ts ->
  275. error(Msg)
  276. after 100 ->
  277. ok
  278. end.
  279. path_start(Config) ->
  280. doc("Start of path is the same as request's, enable tracing."),
  281. Ref = config(ref, Config),
  282. Opts = ranch:get_protocol_options(Ref),
  283. ranch:set_protocol_options(Ref, Opts#{
  284. tracer_callback => do_tracer_callback(self()),
  285. tracer_match_specs => [{path_start, <<"/longer/hello">>}]
  286. }),
  287. do_get("/longer/hello/path", Config),
  288. receive
  289. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  290. ok
  291. after 100 ->
  292. error(timeout)
  293. end.
  294. path_start_no_match(Config) ->
  295. doc("Start of path is different from the request's, disable tracing."),
  296. Ref = config(ref, Config),
  297. Opts = ranch:get_protocol_options(Ref),
  298. ranch:set_protocol_options(Ref, Opts#{
  299. tracer_callback => do_tracer_callback(self()),
  300. tracer_match_specs => [{path_start, <<"/shorter/hello">>}]
  301. }),
  302. do_get("/longer/hello/path", Config),
  303. receive
  304. Msg when element(1, Msg) =:= trace_ts ->
  305. error(Msg)
  306. after 100 ->
  307. ok
  308. end.
  309. header_defined(Config) ->
  310. doc("Header is defined in the request, enable tracing."),
  311. Ref = config(ref, Config),
  312. Opts = ranch:get_protocol_options(Ref),
  313. ranch:set_protocol_options(Ref, Opts#{
  314. tracer_callback => do_tracer_callback(self()),
  315. tracer_match_specs => [{header, <<"accept-encoding">>}]
  316. }),
  317. do_get("/", Config),
  318. receive
  319. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  320. ok
  321. after 100 ->
  322. error(timeout)
  323. end.
  324. header_defined_no_match(Config) ->
  325. doc("Header is not defined in the request, disable tracing."),
  326. Ref = config(ref, Config),
  327. Opts = ranch:get_protocol_options(Ref),
  328. ranch:set_protocol_options(Ref, Opts#{
  329. tracer_callback => do_tracer_callback(self()),
  330. tracer_match_specs => [{header, <<"accept-language">>}]
  331. }),
  332. do_get("/", Config),
  333. receive
  334. Msg when element(1, Msg) =:= trace_ts ->
  335. error(Msg)
  336. after 100 ->
  337. ok
  338. end.
  339. header_value(Config) ->
  340. doc("Header value is the same as the request's, enable 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">>, <<"gzip">>}]
  346. }),
  347. do_get("/", Config),
  348. receive
  349. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  350. ok
  351. after 100 ->
  352. error(timeout)
  353. end.
  354. header_value_no_match(Config) ->
  355. doc("Header value is different from the request's, disable 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 => [{header, <<"accept-encoding">>, <<"nope">>}]
  361. }),
  362. do_get("/", Config),
  363. receive
  364. Msg when element(1, Msg) =:= trace_ts ->
  365. error(Msg)
  366. after 100 ->
  367. ok
  368. end.
  369. peer_ip(Config) ->
  370. doc("Peer IP is the same as the request's, enable tracing."),
  371. Ref = config(ref, Config),
  372. Opts = ranch:get_protocol_options(Ref),
  373. ranch:set_protocol_options(Ref, Opts#{
  374. tracer_callback => do_tracer_callback(self()),
  375. tracer_match_specs => [{peer_ip, {127, 0, 0, 1}}]
  376. }),
  377. do_get("/", Config),
  378. receive
  379. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  380. ok
  381. after 100 ->
  382. error(timeout)
  383. end.
  384. peer_ip_no_match(Config) ->
  385. doc("Peer IP is different from the request's, disable tracing."),
  386. Ref = config(ref, Config),
  387. Opts = ranch:get_protocol_options(Ref),
  388. ranch:set_protocol_options(Ref, Opts#{
  389. tracer_callback => do_tracer_callback(self()),
  390. tracer_match_specs => [{peer_ip, {8, 8, 8, 8}}]
  391. }),
  392. do_get("/", Config),
  393. receive
  394. Msg when element(1, Msg) =:= trace_ts ->
  395. error(Msg)
  396. after 100 ->
  397. ok
  398. end.
  399. missing_callback(Config) ->
  400. doc("Ensure the request is still processed if the callback is not provided."),
  401. Ref = config(ref, Config),
  402. Opts0 = ranch:get_protocol_options(Ref),
  403. Opts = maps:remove(tracer_callback, Opts0),
  404. ranch:set_protocol_options(Ref, Opts#{
  405. tracer_match_specs => [{method, <<"GET">>}]
  406. }),
  407. do_get("/", Config),
  408. receive
  409. Msg when element(1, Msg) =:= trace_ts ->
  410. error(Msg)
  411. after 100 ->
  412. ok
  413. end.
  414. missing_match_specs(Config) ->
  415. doc("Ensure the request is still processed if match specs are not provided."),
  416. Ref = config(ref, Config),
  417. Opts0 = ranch:get_protocol_options(Ref),
  418. Opts = maps:remove(tracer_match_specs, Opts0),
  419. ranch:set_protocol_options(Ref, Opts#{
  420. tracer_callback => do_tracer_callback(self())
  421. }),
  422. do_get("/", Config),
  423. receive
  424. Msg when element(1, Msg) =:= trace_ts ->
  425. error(Msg)
  426. after 100 ->
  427. ok
  428. end.
  429. two_matching_requests(Config) ->
  430. doc("Perform two requests that enable tracing on the same connection."),
  431. Ref = config(ref, Config),
  432. Opts = ranch:get_protocol_options(Ref),
  433. ranch:set_protocol_options(Ref, Opts#{
  434. tracer_callback => do_tracer_callback(self()),
  435. tracer_match_specs => [fun(_,_,_) -> true end]
  436. }),
  437. %% Perform a GET request.
  438. ConnPid = gun_open(Config),
  439. Ref1 = gun:get(ConnPid, "/", []),
  440. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  441. {ok, _} = gun:await_body(ConnPid, Ref1),
  442. receive
  443. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  444. ok
  445. after 100 ->
  446. error(timeout)
  447. end,
  448. %% Perform a second GET request on the same connection.
  449. Ref2 = gun:get(ConnPid, "/", []),
  450. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  451. {ok, _} = gun:await_body(ConnPid, Ref2),
  452. receive
  453. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  454. ok
  455. after 100 ->
  456. error(timeout)
  457. end,
  458. gun:close(ConnPid).