1818 connection_stats /2 ,
1919 connection_stats /4 ]).
2020
21+ -export ([session_begun /1 ,
22+ session_ended /0 ,
23+ session_pids /0 ,
24+ session_infos /1 ,
25+ session_stats /1
26+ % session_stats/4
27+ ]).
28+
2129-export ([channel_created /2 ,
2230 channel_closed /1 ,
2331 channel_stats /2 ,
3846 queue_deleted /1 ,
3947 queues_deleted /1 ]).
4048
49+ -export ([exchange_stats /3 ]).
50+
4151-export ([node_stats /2 ]).
4252
4353-export ([node_node_stats /2 ]).
5464 get_auth_attempts /0 ,
5565 get_auth_attempts_by_source /0 ]).
5666
67+ % -define(QUEUE_METRICS_DEFAULT(QName),
68+ % %% Last field is delete marker.
69+ % {QName, 0, 0, 0, 0, 0, 0, 0, 0}).
70+
71+ % -define(SESSION_EXCHANGE_METRICS_DEFAULT(Key),
72+ % %% Last field is delete marker.
73+ % {Key, 0, 0, 0, 0, 0}).
74+
75+ % -define(SESSION_QUEUE_METRICS_DEFAULT(Key),
76+ % %% Last field is delete marker.
77+ % {Key, 0, 0, 0, 0, 0, 0, 0, 0}).
78+
5779% %----------------------------------------------------------------------------
5880% % Types
5981% %----------------------------------------------------------------------------
60- -type (channel_stats_id () :: pid () |
61- {pid (),
62- {rabbit_types :rabbit_amqqueue_name (), rabbit_types :exchange_name ()}} |
63- {pid (), rabbit_types :rabbit_amqqueue_name ()} |
64- {pid (), rabbit_types :exchange_name ()}).
82+
83+ % -type(session_stats_key() :: {pid(), rabbit_types:r(exchange | queue)}).
84+ % -type(session_stats_type() :: exchange_stats | queue_stats).
85+
86+ -type (channel_stats_id () ::
87+ pid () |
88+ {pid (),
89+ {rabbit_types :rabbit_amqqueue_name (), rabbit_types :exchange_name ()} |
90+ rabbit_types :r (exchange | queue )}).
6591
6692-type (channel_stats_type () :: queue_exchange_stats | queue_stats |
6793 exchange_stats | reductions ).
6894
95+ -type (exchange_operation () :: publish | confirm | return_unroutable | drop_unroutable ).
96+
6997-type (activity_status () :: up | single_active | waiting | suspected_down ).
7098% %----------------------------------------------------------------------------
7199% % Specs
107135% %----------------------------------------------------------------------------
108136
109137create_table ({Table , Type }) ->
110- ets :new (Table , [Type , public , named_table , {write_concurrency , true },
111- {read_concurrency , true }]).
138+ ets :new (Table , [Type , public , named_table , {write_concurrency , true }]).
112139
113140init () ->
114141 Tables = ? CORE_TABLES ++ ? CORE_EXTRA_TABLES ++ ? CORE_NON_CHANNEL_TABLES ,
@@ -146,6 +173,92 @@ connection_stats(Pid, Recv_oct, Send_oct, Reductions) ->
146173 ets :insert (connection_coarse_metrics , {Pid , Recv_oct , Send_oct , Reductions , 0 }),
147174 ok .
148175
176+ -spec session_begun (rabbit_types :infos ()) -> ok .
177+ session_begun (ImmutableInfos ) ->
178+ ets :insert (session_metrics , {self (), ImmutableInfos , _MutableInfos = []}),
179+ ok .
180+
181+ -spec session_ended () -> ok .
182+ session_ended () ->
183+ ets :delete (session_metrics , self ()),
184+ ok .
185+
186+ -spec session_pids () -> [pid ()].
187+ session_pids () ->
188+ lists :map (fun ([Pid ]) ->
189+ Pid
190+ end , ets :match (session_metrics , {'$1' , '_' , '_' })).
191+
192+ -spec session_infos (rabbit_types :info_keys ()) -> [rabbit_types :infos ()].
193+ session_infos (Items ) ->
194+ lists :map (fun ({Pid , ImmutableInfos , MutableInfos }) ->
195+ Infos = maps :from_list ([{pid , Pid }] ++ ImmutableInfos ++ MutableInfos ),
196+ lists :map (fun (Item ) ->
197+ {Item , maps :get (Item , Infos )}
198+ end , Items )
199+ end , ets :tab2list (session_metrics )).
200+
201+ -spec session_stats (rabbit_types :infos ()) -> ok .
202+ session_stats (MutableInfos ) ->
203+ ets :update_element (session_metrics , self (), {3 , MutableInfos }),
204+ ok .
205+
206+ % -spec session_stats(session_stats_type(), atom(), session_stats_key(), pos_integer()) -> ok.
207+ % session_stats(exchange_stats, publish, {_SessionPid, XName} = Key, Value) ->
208+ % _ = ets:update_counter(session_exchange_metrics, Key, {2, Value}, ?SESSION_EXCHANGE_METRICS_DEFAULT(Key)),
209+ % _ = ets:update_counter(exchange_metrics, XName, {2, Value}, ?EXCHANGE_METRICS_DEFAULT(XName)),
210+ % ok;
211+ % session_stats(exchange_stats, accept, {_SessionPid, XName} = Key, Value) ->
212+ % _ = ets:update_counter(session_exchange_metrics, Key, {3, Value}, ?SESSION_EXCHANGE_METRICS_DEFAULT(Key)),
213+ % _ = ets:update_counter(exchange_metrics, XName, {3, Value}, ?EXCHANGE_METRICS_DEFAULT(XName)),
214+ % ok;
215+ % session_stats(exchange_stats, return_unroutable, {_SessionPid, XName} = Key, Value) ->
216+ % _ = ets:update_counter(session_exchange_metrics, Key, {4, Value}, ?SESSION_EXCHANGE_METRICS_DEFAULT(Key)),
217+ % _ = ets:update_counter(exchange_metrics, XName, {4, Value}, ?EXCHANGE_METRICS_DEFAULT(XName)),
218+ % ok;
219+ % session_stats(exchange_stats, drop_unroutable, {_SessionPid, XName} = Key, Value) ->
220+ % _ = ets:update_counter(session_exchange_metrics, Key, {5, Value}, ?SESSION_EXCHANGE_METRICS_DEFAULT(Key)),
221+ % _ = ets:update_counter(exchange_metrics, XName, {5, Value}, ?EXCHANGE_METRICS_DEFAULT(XName)),
222+ % ok;
223+
224+ % session_stats(queue_stats, deliver_unsettled, {_SessionPid, QName} = Key, Value) ->
225+ % _ = ets:update_counter(session_queue_metrics, Key, {2, Value}, ?SESSION_QUEUE_METRICS_DEFAULT(Key)),
226+ % _ = ets:update_counter(queue_delivery_metrics, QName, {4, Value}, ?QUEUE_METRICS_DEFAULT(QName)),
227+ % ok;
228+ % session_stats(queue_stats, deliver_settled, {_SessionPid, QName} = Key, Value) ->
229+ % _ = ets:update_counter(session_queue_metrics, Key, {3, Value}, ?SESSION_QUEUE_METRICS_DEFAULT(Key)),
230+ % _ = ets:update_counter(queue_delivery_metrics, QName, {5, Value}, ?QUEUE_METRICS_DEFAULT(QName)),
231+ % ok;
232+ % session_stats(queue_stats, redeliver, {_SessionPid, QName} = Key, Value) ->
233+ % _ = ets:update_counter(session_queue_metrics, Key, {4, Value}, ?SESSION_QUEUE_METRICS_DEFAULT(Key)),
234+ % _ = ets:update_counter(queue_delivery_metrics, QName, {6, Value}, ?QUEUE_METRICS_DEFAULT(QName)),
235+ % ok;
236+ % session_stats(queue_stats, accept, {_SessionPid, QName} = Key, Value) ->
237+ % _ = ets:update_counter(session_queue_metrics, Key, {5, Value}, ?SESSION_QUEUE_METRICS_DEFAULT(Key)),
238+ % _ = ets:update_counter(queue_delivery_metrics, QName, {7, Value}, ?QUEUE_METRICS_DEFAULT(QName)),
239+ % ok;
240+ % session_stats(queue_stats, reject, {_SessionPid, _QName} = Key, Value) ->
241+ % _ = ets:update_counter(session_queue_metrics, Key, {6, Value}, ?SESSION_QUEUE_METRICS_DEFAULT(Key)),
242+ % ok;
243+ % session_stats(queue_stats, release, {_SessionPid, _QName} = Key, Value) ->
244+ % _ = ets:update_counter(session_queue_metrics, Key, {7, Value}, ?SESSION_QUEUE_METRICS_DEFAULT(Key)),
245+ % ok;
246+ % session_stats(queue_stats, modify, {_SessionPid, _QName} = Key, Value) ->
247+ % _ = ets:update_counter(session_queue_metrics, Key, {8, Value}, ?SESSION_QUEUE_METRICS_DEFAULT(Key)),
248+ % ok.
249+
250+ -spec exchange_stats (exchange_operation (), rabbit_types :exchange_name (), pos_integer ()) -> ok .
251+ exchange_stats (Operation , XName , Incr ) ->
252+ Pos = case Operation of
253+ publish -> 2 ;
254+ confirm -> 3 ;
255+ return_unroutable -> 4 ;
256+ drop_unroutable -> 5
257+ end ,
258+ % % Last field is delete marker.
259+ _ = ets :update_counter (exchange_metrics , XName , {Pos , Incr }, {XName , 0 , 0 , 0 , 0 , 0 }),
260+ ok .
261+
149262channel_created (Pid , Infos ) ->
150263 ets :insert (channel_created , {Pid , Infos }),
151264 _ = ets :update_counter (connection_churn_metrics , node (), {4 , 1 },
@@ -168,26 +281,22 @@ channel_stats(reductions, Id, Value) ->
168281 ets :insert (channel_process_metrics , {Id , Value }),
169282 ok .
170283
171- channel_stats (exchange_stats , publish , {_ChannelPid , XName } = Id , Value ) ->
284+ channel_stats (exchange_stats , Op = publish , {_ChannelPid , XName } = Id , Value ) ->
172285 % % Includes delete marker
173286 _ = ets :update_counter (channel_exchange_metrics , Id , {2 , Value }, {Id , 0 , 0 , 0 , 0 , 0 }),
174- _ = ets :update_counter (exchange_metrics , XName , {2 , Value }, {XName , 0 , 0 , 0 , 0 , 0 }),
175- ok ;
176- channel_stats (exchange_stats , confirm , {_ChannelPid , XName } = Id , Value ) ->
287+ exchange_stats (Op , XName , Value );
288+ channel_stats (exchange_stats , Op = confirm , {_ChannelPid , XName } = Id , Value ) ->
177289 % % Includes delete marker
178290 _ = ets :update_counter (channel_exchange_metrics , Id , {3 , Value }, {Id , 0 , 0 , 0 , 0 , 0 }),
179- _ = ets :update_counter (exchange_metrics , XName , {3 , Value }, {XName , 0 , 0 , 0 , 0 , 0 }),
180- ok ;
181- channel_stats (exchange_stats , return_unroutable , {_ChannelPid , XName } = Id , Value ) ->
291+ exchange_stats (Op , XName , Value );
292+ channel_stats (exchange_stats , Op = return_unroutable , {_ChannelPid , XName } = Id , Value ) ->
182293 % % Includes delete marker
183294 _ = ets :update_counter (channel_exchange_metrics , Id , {4 , Value }, {Id , 0 , 0 , 0 , 0 , 0 }),
184- _ = ets :update_counter (exchange_metrics , XName , {4 , Value }, {XName , 0 , 0 , 0 , 0 , 0 }),
185- ok ;
186- channel_stats (exchange_stats , drop_unroutable , {_ChannelPid , XName } = Id , Value ) ->
295+ exchange_stats (Op , XName , Value );
296+ channel_stats (exchange_stats , Op = drop_unroutable , {_ChannelPid , XName } = Id , Value ) ->
187297 % % Includes delete marker
188298 _ = ets :update_counter (channel_exchange_metrics , Id , {5 , Value }, {Id , 0 , 0 , 0 , 0 , 0 }),
189- _ = ets :update_counter (exchange_metrics , XName , {5 , Value }, {XName , 0 , 0 , 0 , 0 , 0 }),
190- ok ;
299+ exchange_stats (Op , XName , Value );
191300channel_stats (queue_exchange_stats , publish , {_ChannelPid , QueueExchange } = Id , Value ) ->
192301 % % Includes delete marker
193302 _ = ets :update_counter (channel_queue_exchange_metrics , Id , Value , {Id , 0 , 0 }),
0 commit comments