Skip to content

Commit

Permalink
Restore riaknostic output to console
Browse files Browse the repository at this point in the history
When riaknostic became part of Riak instead of a separate app, its
output (through lager) ended up in the node's console.log instead of
being output by 'riak-admin diag'. Among other things, this broke the
riaknostic_rt riak test. This adds a layer on top of lager, so messages
can be directed to the console again, simply by using io:format. This
way, messages are sent to the group_leader instead of the user process,
which is what the lager backend does. When riaknostic is invoked through
RPC by riak-admin, the caller becomes the group leader and picks up
those messages. I wish there was a cleaner way to do this leveraging
something in lager, but I couldn't find any.
  • Loading branch information
engelsanchez committed Jun 24, 2013
1 parent fa6e707 commit a48cf9f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/riaknostic.erl
Expand Up @@ -129,7 +129,6 @@ run(InputChecks) ->
[] ->
io:format("No diagnostic messages to report.~n");
_ ->
io:format("Check ~s for diagnostic output.~n",[proplists:get_value(lager_file_backend,gen_event:which_handlers(lager_event))]),
lists:foreach(fun riaknostic_check:print/1, SortedMessages)
end
end.
Expand Down
6 changes: 4 additions & 2 deletions src/riaknostic_check.erl
Expand Up @@ -94,7 +94,9 @@ modules() ->
print({Level, Mod, Data}) ->
case Mod:format(Data) of
{Format, Terms} ->
lager:log(Level, self(), Format, Terms);
riaknostic_util:log(Level, Format, Terms);
String ->
lager:log(Level, self(), String)
riaknostic_util:log(Level, String)
end.


12 changes: 6 additions & 6 deletions src/riaknostic_node.erl
Expand Up @@ -60,7 +60,7 @@ local_command(Module, Function, Args) ->
%% @see can_connect/0
-spec local_command(Module::atom(), Function::atom(), Args::[term()], Timeout::integer()) -> term().
local_command(Module, Function, Args, Timeout) ->
lager:debug("Local RPC: ~p:~p(~p) [~p]", [Module, Function, Args, Timeout]),
riaknostic_util:log(debug, "Local RPC: ~p:~p(~p) [~p]", [Module, Function, Args, Timeout]),
rpc:call(nodename(), Module, Function, Args, Timeout).

%% @doc Calls the given 0-arity module and function on all members of
Expand All @@ -86,7 +86,7 @@ cluster_command(Module, Function, Args) ->
%% @see can_connect/0
-spec cluster_command(Module::atom(), Function::atom(), Args::[term()], Timeout::integer()) -> term().
cluster_command(Module, Function, Args, Timeout) ->
lager:debug("Cluster RPC: ~p:~p(~p) [~p]", [Module, Function, Args, Timeout]),
riaknostic_util:log(debug, "Cluster RPC: ~p:~p(~p) [~p]", [Module, Function, Args, Timeout]),
Stats = stats(),
{ring_members, RingMembers} = lists:keyfind(ring_members, 1, Stats),
rpc:multicall(RingMembers, Module, Function, Args, Timeout).
Expand All @@ -106,7 +106,7 @@ can_connect() ->
case is_connected() of
true -> true;
false ->
lager:debug("Not connected to the local Riak node, trying to connect. alive:~p connect_failed:~p", [is_alive(), connect_failed()]),
riaknostic_util:log(debug, "Not connected to the local Riak node, trying to connect. alive:~p connect_failed:~p", [is_alive(), connect_failed()]),
maybe_connect()
end.

Expand Down Expand Up @@ -149,7 +149,7 @@ try_connect() ->
case {net_kernel:hidden_connect_node(TargetNode), net_adm:ping(TargetNode)} of
{true, pong} ->
application:set_env(riaknostic, connect_failed, false),
lager:debug("Connected to local Riak node ~p.", [TargetNode]),
riaknostic_util:log(debug, "Connected to local Riak node ~p.", [TargetNode]),
true;
_ ->
application:set_env(riaknostic, connect_failed, true),
Expand All @@ -165,7 +165,7 @@ connect_failed() ->
end.

start_net() ->
lager:debug("Starting distributed Erlang."),
riaknostic_util:log(debug, "Starting distributed Erlang."),
{Type, RiakName} = riaknostic_config:node_name(),
ThisNode = append_node_suffix(RiakName, "_diag"),
{ok, _} = net_kernel:start([ThisNode, Type]),
Expand Down Expand Up @@ -198,7 +198,7 @@ has_stats() ->
end.

fetch_stats() ->
lager:debug("Fetching local riak_kv_status."),
riaknostic_util:log(debug, "Fetching local riak_kv_status."),
case local_command(riak_kv_status, statistics) of
[] -> [];
PList ->
Expand Down
31 changes: 29 additions & 2 deletions src/riaknostic_util.erl
Expand Up @@ -25,6 +25,7 @@
-module(riaknostic_util).
-export([short_name/1,
run_command/1,
log/2,log/3,
binary_to_float/1]).

%% @doc Converts a check module name into a short name that can be
Expand All @@ -38,14 +39,14 @@ short_name(Mod) when is_atom(Mod) ->
%% redirected to stdout so its output will be included.
-spec run_command(Command::iodata()) -> StdOut::iodata().
run_command(Command) ->
lager:debug("Running shell command: ~s", [Command]),
riaknostic_util:log(debug, "Running shell command: ~s", [Command]),
Port = erlang:open_port({spawn,Command},[exit_status, stderr_to_stdout]),
do_read(Port, []).

do_read(Port, Acc) ->
receive
{Port, {data, StdOut}} ->
lager:debug("Shell command output: ~n~s~n",[StdOut]),
riaknostic_util:log(debug, "Shell command output: ~n~s~n",[StdOut]),
do_read(Port, Acc ++ StdOut);
{Port, {exit_status, _}} ->
%%port_close(Port),
Expand All @@ -60,3 +61,29 @@ do_read(Port, Acc) ->
-spec binary_to_float(binary()) -> float().
binary_to_float(Bin) ->
list_to_float(binary_to_list(Bin)).

log(Level, Format, Terms) ->
case should_log(Level) of
true ->
io:format(lists:concat(["[", Level, "] ", Format, "~n"]), Terms);
false ->
ok
end,
lager:log(Level, self(), Format, Terms).

log(Level, String) ->
case should_log(Level) of
true ->
io:format(lists:concat(["[", Level, "] ", String, "~n"]));
false ->
ok
end,
lager:log(Level, self(), String).

should_log(Level) ->
AppLevel = case application:get_env(riaknostic, log_level) of
undefined -> info;
{ok, L0} -> L0
end,
lager_util:level_to_num(AppLevel) >= lager_util:level_to_num(Level).

0 comments on commit a48cf9f

Please sign in to comment.