tracer_SUITE.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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.
  85. do_tracer_callback(Pid) ->
  86. fun
  87. (init, _) -> undefined;
  88. (Event={trace_ts, _, call, {cowboy_req, reply, _}, _}, State) -> Pid ! Event, State;
  89. (_, State) -> State
  90. end.
  91. %% Tests.
  92. predicate_true(Config) ->
  93. doc("Predicate function returns true, unconditionally enable tracing."),
  94. Ref = config(ref, Config),
  95. Opts = ranch:get_protocol_options(Ref),
  96. ranch:set_protocol_options(Ref, Opts#{
  97. tracer_callback => do_tracer_callback(self()),
  98. tracer_match_specs => [fun(_,_,_) -> true end]
  99. }),
  100. do_get("/", Config),
  101. receive
  102. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  103. ok
  104. after 100 ->
  105. error(timeout)
  106. end.
  107. predicate_false(Config) ->
  108. doc("Predicate function returns false, unconditionally disable tracing."),
  109. Ref = config(ref, Config),
  110. Opts = ranch:get_protocol_options(Ref),
  111. ranch:set_protocol_options(Ref, Opts#{
  112. tracer_callback => do_tracer_callback(self()),
  113. tracer_match_specs => [fun(_,_,_) -> false end]
  114. }),
  115. do_get("/", Config),
  116. receive
  117. Msg when element(1, Msg) =:= trace_ts ->
  118. error(Msg)
  119. after 100 ->
  120. ok
  121. end.
  122. method(Config) ->
  123. doc("Method is the same as the request's, enable tracing."),
  124. Ref = config(ref, Config),
  125. Opts = ranch:get_protocol_options(Ref),
  126. ranch:set_protocol_options(Ref, Opts#{
  127. tracer_callback => do_tracer_callback(self()),
  128. tracer_match_specs => [{method, <<"GET">>}]
  129. }),
  130. do_get("/", Config),
  131. receive
  132. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  133. ok
  134. after 100 ->
  135. error(timeout)
  136. end.
  137. method_no_match(Config) ->
  138. doc("Method is different from the request's, disable tracing."),
  139. Ref = config(ref, Config),
  140. Opts = ranch:get_protocol_options(Ref),
  141. ranch:set_protocol_options(Ref, Opts#{
  142. tracer_callback => do_tracer_callback(self()),
  143. tracer_match_specs => [{method, <<"POST">>}]
  144. }),
  145. do_get("/", Config),
  146. receive
  147. Msg when element(1, Msg) =:= trace_ts ->
  148. error(Msg)
  149. after 100 ->
  150. ok
  151. end.
  152. host(Config) ->
  153. doc("Host is the same as the request's, 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 => [{host, <<"localhost">>}]
  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. host_no_match(Config) ->
  168. doc("Host is different from the request's, disable 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 => [{host, <<"ninenines.eu">>}]
  174. }),
  175. do_get("/", Config),
  176. receive
  177. Msg when element(1, Msg) =:= trace_ts ->
  178. error(Msg)
  179. after 100 ->
  180. ok
  181. end.
  182. path(Config) ->
  183. doc("Path is the same as the request's, enable 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 => [{path, <<"/longer/hello/path">>}]
  189. }),
  190. do_get("/longer/hello/path", Config),
  191. receive
  192. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  193. ok
  194. after 100 ->
  195. error(timeout)
  196. end.
  197. path_no_match(Config) ->
  198. doc("Path is different from the request's, disable 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 => [{path, <<"/some/other/path">>}]
  204. }),
  205. do_get("/longer/hello/path", Config),
  206. receive
  207. Msg when element(1, Msg) =:= trace_ts ->
  208. error(Msg)
  209. after 100 ->
  210. ok
  211. end.
  212. path_start(Config) ->
  213. doc("Start of path is the same as request's, enable 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 => [{path_start, <<"/longer/hello">>}]
  219. }),
  220. do_get("/longer/hello/path", Config),
  221. receive
  222. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  223. ok
  224. after 100 ->
  225. error(timeout)
  226. end.
  227. path_start_no_match(Config) ->
  228. doc("Start of path is different from the request's, disable 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 => [{path_start, <<"/shorter/hello">>}]
  234. }),
  235. do_get("/longer/hello/path", Config),
  236. receive
  237. Msg when element(1, Msg) =:= trace_ts ->
  238. error(Msg)
  239. after 100 ->
  240. ok
  241. end.
  242. header_defined(Config) ->
  243. doc("Header is defined in the request, enable 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 => [{header, <<"accept-encoding">>}]
  249. }),
  250. do_get("/", Config),
  251. receive
  252. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  253. ok
  254. after 100 ->
  255. error(timeout)
  256. end.
  257. header_defined_no_match(Config) ->
  258. doc("Header is not defined in the request, disable 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 => [{header, <<"accept-language">>}]
  264. }),
  265. do_get("/", Config),
  266. receive
  267. Msg when element(1, Msg) =:= trace_ts ->
  268. error(Msg)
  269. after 100 ->
  270. ok
  271. end.
  272. header_value(Config) ->
  273. doc("Header value is the same as the request's, enable 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 => [{header, <<"accept-encoding">>, <<"gzip">>}]
  279. }),
  280. do_get("/", Config),
  281. receive
  282. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  283. ok
  284. after 100 ->
  285. error(timeout)
  286. end.
  287. header_value_no_match(Config) ->
  288. doc("Header value is different from the request's, disable 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 => [{header, <<"accept-encoding">>, <<"nope">>}]
  294. }),
  295. do_get("/", Config),
  296. receive
  297. Msg when element(1, Msg) =:= trace_ts ->
  298. error(Msg)
  299. after 100 ->
  300. ok
  301. end.
  302. peer_ip(Config) ->
  303. doc("Peer IP is the same as the request's, enable 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 => [{peer_ip, {127, 0, 0, 1}}]
  309. }),
  310. do_get("/", Config),
  311. receive
  312. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  313. ok
  314. after 100 ->
  315. error(timeout)
  316. end.
  317. peer_ip_no_match(Config) ->
  318. doc("Peer IP is different from the request's, disable 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 => [{peer_ip, {8, 8, 8, 8}}]
  324. }),
  325. do_get("/", Config),
  326. receive
  327. Msg when element(1, Msg) =:= trace_ts ->
  328. error(Msg)
  329. after 100 ->
  330. ok
  331. end.
  332. missing_callback(Config) ->
  333. doc("Ensure the request is still processed if the callback is not provided."),
  334. Ref = config(ref, Config),
  335. Opts0 = ranch:get_protocol_options(Ref),
  336. Opts = maps:remove(tracer_callback, Opts0),
  337. ranch:set_protocol_options(Ref, Opts#{
  338. tracer_match_specs => [{method, <<"GET">>}]
  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. missing_match_specs(Config) ->
  348. doc("Ensure the request is still processed if match specs are not provided."),
  349. Ref = config(ref, Config),
  350. Opts0 = ranch:get_protocol_options(Ref),
  351. Opts = maps:remove(tracer_match_specs, Opts0),
  352. ranch:set_protocol_options(Ref, Opts#{
  353. tracer_callback => do_tracer_callback(self())
  354. }),
  355. do_get("/", Config),
  356. receive
  357. Msg when element(1, Msg) =:= trace_ts ->
  358. error(Msg)
  359. after 100 ->
  360. ok
  361. end.
  362. two_matching_requests(Config) ->
  363. doc("Perform two requests that enable tracing on the same connection."),
  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 => [fun(_,_,_) -> true end]
  369. }),
  370. %% Perform a GET request.
  371. ConnPid = gun_open(Config),
  372. Ref1 = gun:get(ConnPid, "/", []),
  373. {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
  374. {ok, _} = gun:await_body(ConnPid, Ref1),
  375. receive
  376. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  377. ok
  378. after 100 ->
  379. error(timeout)
  380. end,
  381. %% Perform a second GET request on the same connection.
  382. Ref2 = gun:get(ConnPid, "/", []),
  383. {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
  384. {ok, _} = gun:await_body(ConnPid, Ref2),
  385. receive
  386. {trace_ts, _, call, {cowboy_req, reply, [200, _, _, _]}, _} ->
  387. ok
  388. after 100 ->
  389. error(timeout)
  390. end,
  391. gun:close(ConnPid).