1010-include_lib (" kernel/include/logger.hrl" ).
1111-include_lib (" rabbit_common/include/rabbit.hrl" ).
1212-include_lib (" amqp10_common/include/amqp10_types.hrl" ).
13+ -include (" rabbit_reader.hrl" ).
1314-include (" rabbit_amqp.hrl" ).
1415
1516-export ([init /1 ,
7980 pending_recv :: boolean (),
8081 buf :: list (),
8182 buf_len :: non_neg_integer (),
82- tracked_channels :: #{channel_number () => Session :: pid ()}
83+ tracked_channels :: #{channel_number () => Session :: pid ()},
84+ stats_timer :: rabbit_event :state ()
8385 }).
8486
8587-type state () :: # v1 {}.
9092
9193unpack_from_0_9_1 (
9294 {Sock , PendingRecv , SupPid , Buf , BufLen , ProxySocket ,
93- ConnectionName , Host , PeerHost , Port , PeerPort , ConnectedAt },
95+ ConnectionName , Host , PeerHost , Port , PeerPort , ConnectedAt , StatsTimer },
9496 Parent ) ->
9597 logger :update_process_metadata (#{connection => ConnectionName }),
9698 # v1 {parent = Parent ,
@@ -106,6 +108,7 @@ unpack_from_0_9_1(
106108 tracked_channels = maps :new (),
107109 writer = none ,
108110 connection_state = received_amqp3100 ,
111+ stats_timer = StatsTimer ,
109112 connection = # v1_connection {
110113 name = ConnectionName ,
111114 container_id = none ,
@@ -201,6 +204,10 @@ mainloop(Deb, State = #v1{sock = Sock, buf = Buf, buf_len = BufLen}) ->
201204 end
202205 end .
203206
207+ handle_other (emit_stats , State ) ->
208+ emit_stats (State );
209+ handle_other (ensure_stats_timer , State ) ->
210+ ensure_stats_timer (State );
204211handle_other ({'EXIT' , Parent , Reason }, State = # v1 {parent = Parent }) ->
205212 ReasonString = rabbit_misc :format (" broker forced connection closure with reason '~w '" ,
206213 [Reason ]),
@@ -247,8 +254,17 @@ handle_other({'$gen_call', From, {info, Items}}, State) ->
247254 end ,
248255 gen_server :reply (From , Reply ),
249256 State ;
250- handle_other ({'$gen_cast' , {force_event_refresh , _Ref }}, State ) ->
251- State ;
257+ handle_other ({'$gen_cast' , {force_event_refresh , Ref }}, State ) ->
258+ case ? IS_RUNNING (State ) of
259+ true ->
260+ % %TODO test case
261+ Infos = infos (? CONNECTION_EVENT_KEYS , State ),
262+ rabbit_event :notify (connection_created , Infos , Ref ),
263+ rabbit_event :init_stats_timer (State , # v1 .stats_timer );
264+ false ->
265+ % % Ignore, we will emit a connection_created event once we start running.
266+ State
267+ end ;
252268handle_other (terminate_connection , _State ) ->
253269 stop ;
254270handle_other ({set_credential , Cred }, State ) ->
@@ -646,7 +662,8 @@ handle_input({frame_header, Mode},
646662 " frame size (~b bytes) > maximum frame size (~b bytes)" ,
647663 [Size , MaxFrameSize ]));
648664 true ->
649- switch_callback (State , {frame_body , Mode , DOff , Channel }, Size - 8 )
665+ State1 = switch_callback (State , {frame_body , Mode , DOff , Channel }, Size - 8 ),
666+ ensure_stats_timer (State1 )
650667 end ;
651668handle_input ({frame_header , _Mode }, Malformed , _State ) ->
652669 throw ({bad_1_0_header , Malformed });
@@ -1045,6 +1062,11 @@ i(channels, #v1{tracked_channels = Channels}) ->
10451062 maps :size (Channels );
10461063i (channel_max , # v1 {connection = # v1_connection {channel_max = Max }}) ->
10471064 Max ;
1065+ i (reductions = Item , _State ) ->
1066+ {Item , Reductions } = erlang :process_info (self (), Item ),
1067+ Reductions ;
1068+ i (garbage_collection , _State ) ->
1069+ rabbit_misc :get_gc_info (self ());
10481070i (Item , # v1 {}) ->
10491071 throw ({bad_argument , Item }).
10501072
@@ -1055,6 +1077,24 @@ socket_info(Get, Select, #v1{sock = Sock}) ->
10551077 {error , _ } -> ''
10561078 end .
10571079
1080+ emit_stats (State ) ->
1081+ [{_ , Pid },
1082+ {_ , RecvOct },
1083+ {_ , SendOct },
1084+ {_ , Reductions }] = infos (? SIMPLE_METRICS , State ),
1085+ Infos = infos (? OTHER_METRICS , State ),
1086+ rabbit_core_metrics :connection_stats (Pid , Infos ),
1087+ rabbit_core_metrics :connection_stats (Pid , RecvOct , SendOct , Reductions ),
1088+ % % NB: Don't call ensure_stats_timer because it becomes expensive
1089+ % % if all idle connections emit stats.
1090+ rabbit_event :reset_stats_timer (State , # v1 .stats_timer ).
1091+
1092+ ensure_stats_timer (State )
1093+ when ? IS_RUNNING (State ) ->
1094+ rabbit_event :ensure_stats_timer (State , # v1 .stats_timer , emit_stats );
1095+ ensure_stats_timer (State ) ->
1096+ State .
1097+
10581098ignore_maintenance ({map , Properties }) ->
10591099 lists :member (
10601100 {{symbol , <<" ignore-maintenance" >>}, true },
0 commit comments