Skip to content

Commit 506fb81

Browse files
committed
Add connection name to connection created/closed events if necessary
Fixes #1596 [#157500358] (cherry picked from commit e48ac19)
1 parent 33d0e12 commit 506fb81

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/rabbit_reader.erl

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
peer_cert_validity, auth_mechanism, ssl_protocol,
135135
ssl_key_exchange, ssl_cipher, ssl_hash, protocol, user, vhost,
136136
timeout, frame_max, channel_max, client_properties, connected_at,
137-
node, user_who_performed_action, connection_user_provided_name]).
137+
node, user_who_performed_action]).
138138

139139
-define(INFO_KEYS, ?CREATION_EVENT_KEYS ++ ?STATISTICS_KEYS -- [pid]).
140140

@@ -393,17 +393,17 @@ start_connection(Parent, HelperSup, Deb, Sock) ->
393393
Properties ->
394394
Properties
395395
end,
396-
ConnectionUserProvidedName = case get(connection_user_provided_name) of
397-
undefined ->
398-
'';
399-
ConnectionName ->
400-
ConnectionName
396+
EventProperties = [{name, Name},
397+
{pid, self()},
398+
{node, node()},
399+
{client_properties, ClientProperties}],
400+
EventProperties1 = case get(connection_user_provided_name) of
401+
undefined ->
402+
EventProperties;
403+
ConnectionUserProvidedName ->
404+
[{user_provided_name, ConnectionUserProvidedName} | EventProperties]
401405
end,
402-
rabbit_event:notify(connection_closed, [{name, Name},
403-
{pid, self()},
404-
{node, node()},
405-
{client_properties, ClientProperties},
406-
{connection_user_provided_name, ConnectionUserProvidedName}])
406+
rabbit_event:notify(connection_closed, EventProperties1)
407407
end,
408408
done.
409409

@@ -620,7 +620,9 @@ handle_other({'$gen_cast', {force_event_refresh, Ref}}, State)
620620
when ?IS_RUNNING(State) ->
621621
rabbit_event:notify(
622622
connection_created,
623-
[{type, network} | infos(?CREATION_EVENT_KEYS, State)], Ref),
623+
augment_infos_with_user_provided_connection_name(
624+
[{type, network} | infos(?CREATION_EVENT_KEYS, State)], State),
625+
Ref),
624626
rabbit_event:init_stats_timer(State, #v1.stats_timer);
625627
handle_other({'$gen_cast', {force_event_refresh, _Ref}}, State) ->
626628
%% Ignore, we will emit a created event once we start running.
@@ -1146,7 +1148,12 @@ handle_method0(#'connection.start_ok'{mechanism = Mechanism,
11461148
% adding client properties to process dictionary to send them later
11471149
% in the connection_closed event
11481150
put(client_properties, ClientProperties),
1149-
put(connection_user_provided_name, user_provided_connection_name(Connection2)),
1151+
case user_provided_connection_name(Connection2) of
1152+
undefined ->
1153+
undefined;
1154+
UserProvidedConnectionName ->
1155+
put(connection_user_provided_name, UserProvidedConnectionName)
1156+
end,
11501157
auth_phase(Response, State);
11511158

11521159
handle_method0(#'connection.secure_ok'{response = Response},
@@ -1219,7 +1226,10 @@ handle_method0(#'connection.open'{virtual_host = VHost},
12191226
connection = NewConnection,
12201227
channel_sup_sup_pid = ChannelSupSupPid,
12211228
throttle = Throttle1}),
1222-
Infos = [{type, network} | infos(?CREATION_EVENT_KEYS, State1)],
1229+
Infos = augment_infos_with_user_provided_connection_name(
1230+
[{type, network} | infos(?CREATION_EVENT_KEYS, State1)],
1231+
State1
1232+
),
12231233
rabbit_core_metrics:connection_created(proplists:get_value(pid, Infos),
12241234
Infos),
12251235
rabbit_event:notify(connection_created, Infos),
@@ -1476,13 +1486,6 @@ ic(client_properties, #connection{client_properties = CP}) -> CP;
14761486
ic(auth_mechanism, #connection{auth_mechanism = none}) -> none;
14771487
ic(auth_mechanism, #connection{auth_mechanism = {Name, _Mod}}) -> Name;
14781488
ic(connected_at, #connection{connected_at = T}) -> T;
1479-
ic(connection_user_provided_name, C) ->
1480-
case user_provided_connection_name(C) of
1481-
undefined ->
1482-
'';
1483-
ConnectionUserProvidedName ->
1484-
ConnectionUserProvidedName
1485-
end;
14861489
ic(Item, #connection{}) -> throw({bad_argument, Item}).
14871490

14881491
socket_info(Get, Select, #v1{sock = Sock}) ->
@@ -1696,6 +1699,14 @@ augment_connection_log_name(#connection{name = Name} = Connection) ->
16961699
Connection#connection{log_name = LogName}
16971700
end.
16981701

1702+
augment_infos_with_user_provided_connection_name(Infos, #v1{connection = Connection}) ->
1703+
case user_provided_connection_name(Connection) of
1704+
undefined ->
1705+
Infos;
1706+
UserProvidedConnectionName ->
1707+
[{user_provided_name, UserProvidedConnectionName} | Infos]
1708+
end.
1709+
16991710
user_provided_connection_name(#connection{client_properties = ClientProperties}) ->
17001711
case rabbit_misc:table_lookup(ClientProperties, <<"connection_name">>) of
17011712
{longstr, UserSpecifiedName} ->

0 commit comments

Comments
 (0)