Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Commit

Permalink
Use user specified connection name in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Lebedeff committed Jun 1, 2016
1 parent 9010ea7 commit a3926e1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
11 changes: 10 additions & 1 deletion src/rabbit_misc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
-export([interval_operation/5]).
-export([ensure_timer/4, stop_timer/2, send_after/3, cancel_timer/1]).
-export([get_parent/0]).
-export([store_proc_name/1, store_proc_name/2]).
-export([store_proc_name/1, store_proc_name/2, get_proc_name/0]).
-export([moving_average/4]).
-export([get_env/3]).
-export([get_channel_operation_timeout/0]).
Expand Down Expand Up @@ -262,6 +262,7 @@
-spec(get_parent/0 :: () -> pid()).
-spec(store_proc_name/2 :: (atom(), rabbit_types:proc_name()) -> ok).
-spec(store_proc_name/1 :: (rabbit_types:proc_type_and_name()) -> ok).
-spec(get_proc_name/0 :: () -> {'ok', rabbit_type:proc_name()} | 'undefined').
-spec(moving_average/4 :: (float(), float(), float(), float() | 'undefined')
-> float()).
-spec(get_env/3 :: (atom(), atom(), term()) -> term()).
Expand Down Expand Up @@ -1130,6 +1131,14 @@ cancel_timer({timer, Ref}) -> {ok, cancel} = timer:cancel(Ref),
store_proc_name(Type, ProcName) -> store_proc_name({Type, ProcName}).
store_proc_name(TypeProcName) -> put(process_name, TypeProcName).

get_proc_name() ->
case get(process_name) of
undefined ->
undefined;
{_Type, Name} ->
{ok, Name}
end.

%% application:get_env/3 is only available in R16B01 or later.
get_env(Application, Key, Def) ->
case application:get_env(Application, Key) of
Expand Down
51 changes: 39 additions & 12 deletions src/rabbit_reader.erl
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ socket_op(Sock, Fun) ->
start_connection(Parent, HelperSup, Deb, Sock) ->
process_flag(trap_exit, true),
Name = case rabbit_net:connection_string(Sock, inbound) of
{ok, Str} -> Str;
{ok, Str} -> list_to_binary(Str);
{error, enotconn} -> rabbit_net:fast_close(Sock),
exit(normal);
{error, Reason} -> socket_error(Reason),
Expand All @@ -349,11 +349,11 @@ start_connection(Parent, HelperSup, Deb, Sock) ->
erlang:send_after(HandshakeTimeout, self(), handshake_timeout),
{PeerHost, PeerPort, Host, Port} =
socket_op(Sock, fun (S) -> rabbit_net:socket_ends(S, inbound) end),
?store_proc_name(list_to_binary(Name)),
?store_proc_name(Name),
State = #v1{parent = Parent,
sock = Sock,
connection = #connection{
name = list_to_binary(Name),
name = Name,
host = Host,
peer_host = PeerHost,
port = Port,
Expand Down Expand Up @@ -387,10 +387,10 @@ start_connection(Parent, HelperSup, Deb, Sock) ->
[Deb, [], 0, switch_callback(rabbit_event:init_stats_timer(
State, #v1.stats_timer),
handshake, 8)]}),
log(info, "closing AMQP connection ~p (~s)~n", [self(), Name])
log(info, "closing AMQP connection ~p (~s)~n", [self(), dynamic_connection_name(Name)])
catch
Ex ->
log_connection_exception(Name, Ex)
log_connection_exception(dynamic_connection_name(Name), Ex)
after
%% We don't call gen_tcp:close/1 here since it waits for
%% pending output to be sent, which results in unnecessary
Expand Down Expand Up @@ -1134,21 +1134,22 @@ handle_method0(#'connection.start_ok'{mechanism = Mechanism,
response = Response,
client_properties = ClientProperties},
State0 = #v1{connection_state = starting,
connection = Connection,
connection = Connection0,
sock = Sock}) ->
AuthMechanism = auth_mechanism_to_module(Mechanism, Sock),
Capabilities =
case rabbit_misc:table_lookup(ClientProperties, <<"capabilities">>) of
{table, Capabilities1} -> Capabilities1;
_ -> []
end,
Connection1 = Connection0#connection{
client_properties = ClientProperties,
capabilities = Capabilities,
auth_mechanism = {Mechanism, AuthMechanism},
auth_state = AuthMechanism:init(Sock)},
Connection2 = augment_connection_name(Connection1),
State = State0#v1{connection_state = securing,
connection =
Connection#connection{
client_properties = ClientProperties,
capabilities = Capabilities,
auth_mechanism = {Mechanism, AuthMechanism},
auth_state = AuthMechanism:init(Sock)}},
connection = Connection2},
auth_phase(Response, State);

handle_method0(#'connection.secure_ok'{response = Response},
Expand Down Expand Up @@ -1505,3 +1506,29 @@ send_error_on_channel0_and_close(Channel, Protocol, Reason, State) ->
State1 = close_connection(terminate_channels(State)),
ok = send_on_channel0(State#v1.sock, CloseMethod, Protocol),
State1.

augment_connection_name(#connection{client_properties = ClientProperties,
name = Name0} = Connection) ->
UserSpecifiedName = rabbit_misc:table_lookup(ClientProperties, <<"connection_name">>),
Name = add_user_specified_name(Name0, UserSpecifiedName),
case Name of
Name0 -> % not changed
Connection;
_ ->
log(info, "Setting custom name on AMQP connection ~p (~s)~n", [self(), Name]),
?store_proc_name(Name),
Connection#connection{name = Name}
end.

add_user_specified_name(Name, {longstr, UserSpecifiedName}) ->
<<Name/binary, " - ", UserSpecifiedName/binary>>;
add_user_specified_name(Name, _) ->
Name.

dynamic_connection_name(Default) ->
case rabbit_misc:get_proc_name() of
{ok, Name} ->
Name;
_ ->
Default
end.

0 comments on commit a3926e1

Please sign in to comment.