tracer_SUITE.erl 14 KB

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