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