tracer_SUITE.erl 14 KB

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