Skip to content

Commit 3b50bc2

Browse files
committed
Add redbug tracing support
1 parent 0181cb7 commit 3b50bc2

File tree

3 files changed

+155
-1
lines changed

3 files changed

+155
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
%% -------------------------------------------------------------------
2+
%%
3+
%%
4+
%% This file is provided to you under the Apache License,
5+
%% Version 2.0 (the "License"); you may not use this file
6+
%% except in compliance with the License. You may obtain
7+
%% a copy of the License at
8+
%%
9+
%% http://www.apache.org/licenses/LICENSE-2.0
10+
%%
11+
%% Unless required by applicable law or agreed to in writing,
12+
%% software distributed under the License is distributed on an
13+
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
%% KIND, either express or implied. See the License for the
15+
%% specific language governing permissions and limitations
16+
%% under the License.
17+
%%
18+
%%-------------------------------------------------------------------
19+
20+
-module(riak_core_vnode_proxy_intercepts).
21+
-compile(export_all).
22+
-include("intercept.hrl").
23+
-define(M, riak_core_vnode_proxy_orig).
24+
25+
%% @doc force a soft-loaded response for the proxy
26+
soft_load_mbox(_, CRI) ->
27+
?I_INFO("returning a soft loaded mbox\n"),
28+
{soft_loaded, CRI*2, CRI}.
29+

src/riak_test_escript.erl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ cli_options() ->
4545
{keep, undefined, "keep", boolean, "do not teardown cluster"},
4646
{batch, undefined, "batch", undefined, "running a batch, always teardown, even on failure"},
4747
{report, $r, "report", string, "you're reporting an official test run, provide platform info (e.g. ubuntu-1204-64)\nUse 'config' if you want to pull from ~/.riak_test.config"},
48-
{file, $F, "file", string, "use the specified file instead of ~/.riak_test.config"}
48+
{file, $F, "file", string, "use the specified file instead of ~/.riak_test.config"},
49+
{apply_traces,undefined, "trace", undefined, "Apply traces to the target node, defined in the SUITEs"}
4950
].
5051

5152
print_help() ->
@@ -211,6 +212,9 @@ parse_command_line_tests(ParsedArgs) ->
211212
[] -> [undefined];
212213
UpgradeList -> UpgradeList
213214
end,
215+
216+
rt_redbug:set_tracing_applied(proplists:is_defined(apply_traces, ParsedArgs)),
217+
214218
%% Parse Command Line Tests
215219
{CodePaths, SpecificTests} =
216220
lists:foldl(fun extract_test_names/2,

src/rt_redbug.erl

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
%% -------------------------------------------------------------------
2+
%%
3+
%% Copyright (c) 2016 Basho Technologies, Inc.
4+
%%
5+
%% This file is provided to you under the Apache License,
6+
%% Version 2.0 (the "License"); you may not use this file
7+
%% except in compliance with the License. You may obtain
8+
%% a copy of the License at
9+
%%
10+
%% http://www.apache.org/licenses/LICENSE-2.0
11+
%%
12+
%% Unless required by applicable law or agreed to in writing,
13+
%% software distributed under the License is distributed on an
14+
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
%% KIND, either express or implied. See the License for the
16+
%% specific language governing permissions and limitations
17+
%% under the License.
18+
%%
19+
%% Enable and disable tracing from the riak_test command line,
20+
%% add `--tracing` to the test run to execute any calls to
21+
%% `rt_redbug:trace/2` that are in the test suites. The traces
22+
%% are printed to the test.log
23+
%%
24+
%% -------------------------------------------------------------------
25+
26+
-module(rt_redbug).
27+
28+
-export([is_tracing_applied/0]).
29+
-export([set_tracing_applied/1]).
30+
-export([stop/1]).
31+
-export([trace/2, trace/3]).
32+
33+
set_tracing_applied(TracingApplied) when is_boolean(TracingApplied) ->
34+
35+
%% Enable redbug tracing if enabled via command-line or config file
36+
Enabled = case TracingApplied of
37+
true ->
38+
TracingApplied;
39+
_ -> rt_config:get(apply_traces, false)
40+
end,
41+
case Enabled of
42+
true ->
43+
lager:warning("Will enable any redbug traces contained in the test");
44+
_ ->
45+
lager:warning("Will not enable any redbug traces contained in the test")
46+
end,
47+
rt_config:set(apply_traces, Enabled).
48+
49+
is_tracing_applied() ->
50+
rt_config:get(apply_traces, false).
51+
52+
%% Apply traces to one or more nodes using redbug tracing and syntax.
53+
%%
54+
%% eper documentation for the redbug trace string and options is here:
55+
%%
56+
%% https://github.com/massemanet/eper/blob/master/doc/redbug.txt
57+
%%
58+
%% docs on profiling using redbug (for test timeouts) is here:
59+
%%
60+
%% http://roberto-aloi.com/erlang/profiling-erlang-applications-using-redbug
61+
%%
62+
%% Set a trace on a function
63+
%% rt_redbug:trace(Node, "riak_kv_qry_compiler:compile").
64+
%% rt_redbug:trace(Node, ["riak_kv_qry_compiler:compile", "riak_ql_parser:parse"]).
65+
%%
66+
%% Multiple traces can be set in one call. Calling the `rt_redbug:trace`
67+
%% function a second time stops the traces started by the first call.
68+
%%
69+
%% Traces can be stopped by calling `rt_redbug:stop(Nodes)` with the nodes in
70+
%% the test cluster. This is important if there are multiple tests in the same
71+
%% suite, but not if the cluster is torn down at the end of the suite.
72+
trace(Nodes, TraceStrings) ->
73+
trace(Nodes, TraceStrings, []).
74+
75+
trace(Nodes, TraceStrings, Options1) when (is_atom(Nodes) orelse is_list(Nodes)) ->
76+
case is_tracing_applied() of
77+
true ->
78+
Options2 = apply_options_to_defaults(Options1),
79+
[apply_trace(N, TraceStrings, Options2) || N <- ensure_list(Nodes)],
80+
ok;
81+
false ->
82+
ok
83+
end.
84+
85+
%%
86+
apply_trace(Node, TraceString, Options) ->
87+
rpc:call(Node, redbug, start, [TraceString, Options]).
88+
89+
%%
90+
apply_options_to_defaults(Options) ->
91+
lists:foldl(
92+
fun({K,_} = E,Acc) ->
93+
lists:keystore(K, 1, Acc, E)
94+
end, default_trace_options(), Options).
95+
96+
%%
97+
default_trace_options() ->
98+
[
99+
%% default ct timeout of 30 minutes
100+
%% http://erlang.org/doc/apps/common_test/write_test_chapter.html#id77922
101+
{time,(30*60*1000)},
102+
%% raise the threshold for the number of traces that can be received by
103+
%% redbug before tracing is suspended
104+
{msgs, 1000},
105+
%% print milliseconds
106+
{print_msec, true}
107+
].
108+
109+
%% Stop redbug tracing on a node or list of nodes
110+
stop(Nodes) ->
111+
case is_tracing_applied() of
112+
true ->
113+
[rpc:call(N, redbug, stop, []) || N <- ensure_list(Nodes)],
114+
ok;
115+
false ->
116+
ok
117+
end.
118+
119+
%% Doesn't work on lists of strings!
120+
ensure_list(V) ->
121+
lists:flatten([V]).

0 commit comments

Comments
 (0)