Skip to content

Commit edc24f5

Browse files
Merge pull request #1610 from rabbitmq/rabbitmq-server-1596-connection-name-to-connection-closed-event-to-3-7
Add client properties to connection.closed events
2 parents f6b942b + 506fb81 commit edc24f5

File tree

1 file changed

+55
-12
lines changed

1 file changed

+55
-12
lines changed

src/rabbit_reader.erl

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,24 @@ start_connection(Parent, HelperSup, Deb, Sock) ->
386386
%% socket w/o delay before termination.
387387
rabbit_net:fast_close(RealSocket),
388388
rabbit_networking:unregister_connection(self()),
389-
rabbit_core_metrics:connection_closed(self()),
390-
rabbit_event:notify(connection_closed, [{name, Name},
391-
{pid, self()},
392-
{node, node()}])
389+
rabbit_core_metrics:connection_closed(self()),
390+
ClientProperties = case get(client_properties) of
391+
undefined ->
392+
[];
393+
Properties ->
394+
Properties
395+
end,
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]
405+
end,
406+
rabbit_event:notify(connection_closed, EventProperties1)
393407
end,
394408
done.
395409

@@ -606,7 +620,9 @@ handle_other({'$gen_cast', {force_event_refresh, Ref}}, State)
606620
when ?IS_RUNNING(State) ->
607621
rabbit_event:notify(
608622
connection_created,
609-
[{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),
610626
rabbit_event:init_stats_timer(State, #v1.stats_timer);
611627
handle_other({'$gen_cast', {force_event_refresh, _Ref}}, State) ->
612628
%% Ignore, we will emit a created event once we start running.
@@ -1129,6 +1145,15 @@ handle_method0(#'connection.start_ok'{mechanism = Mechanism,
11291145
Connection2 = augment_connection_log_name(Connection1),
11301146
State = State0#v1{connection_state = securing,
11311147
connection = Connection2},
1148+
% adding client properties to process dictionary to send them later
1149+
% in the connection_closed event
1150+
put(client_properties, ClientProperties),
1151+
case user_provided_connection_name(Connection2) of
1152+
undefined ->
1153+
undefined;
1154+
UserProvidedConnectionName ->
1155+
put(connection_user_provided_name, UserProvidedConnectionName)
1156+
end,
11321157
auth_phase(Response, State);
11331158

11341159
handle_method0(#'connection.secure_ok'{response = Response},
@@ -1201,7 +1226,10 @@ handle_method0(#'connection.open'{virtual_host = VHost},
12011226
connection = NewConnection,
12021227
channel_sup_sup_pid = ChannelSupSupPid,
12031228
throttle = Throttle1}),
1204-
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+
),
12051233
rabbit_core_metrics:connection_created(proplists:get_value(pid, Infos),
12061234
Infos),
12071235
rabbit_event:notify(connection_created, Infos),
@@ -1660,16 +1688,31 @@ control_throttle(State = #v1{connection_state = CS,
16601688
_ -> State1
16611689
end.
16621690

1663-
augment_connection_log_name(#connection{client_properties = ClientProperties,
1664-
name = Name} = Connection) ->
1665-
case rabbit_misc:table_lookup(ClientProperties, <<"connection_name">>) of
1666-
{longstr, UserSpecifiedName} ->
1691+
augment_connection_log_name(#connection{name = Name} = Connection) ->
1692+
case user_provided_connection_name(Connection) of
1693+
undefined ->
1694+
Connection;
1695+
UserSpecifiedName ->
16671696
LogName = <<Name/binary, " - ", UserSpecifiedName/binary>>,
16681697
rabbit_log_connection:info("Connection ~p (~s) has a client-provided name: ~s~n", [self(), Name, UserSpecifiedName]),
16691698
?store_proc_name(LogName),
1670-
Connection#connection{log_name = LogName};
1699+
Connection#connection{log_name = LogName}
1700+
end.
1701+
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+
1710+
user_provided_connection_name(#connection{client_properties = ClientProperties}) ->
1711+
case rabbit_misc:table_lookup(ClientProperties, <<"connection_name">>) of
1712+
{longstr, UserSpecifiedName} ->
1713+
UserSpecifiedName;
16711714
_ ->
1672-
Connection
1715+
undefined
16731716
end.
16741717

16751718
dynamic_connection_name(Default) ->

0 commit comments

Comments
 (0)