feat: add `live_connections` field for some HTTP APIs

i.e:
- `/monitor_current`, `/monitor_current/nodes/{node}`
- `/monitor/nodes/{node}`, `/monitor`
- `/node/{node}`, `/nodes`
This commit is contained in:
JianBo He 2023-06-06 16:57:55 +08:00
parent 4e9eeb489d
commit fcef456174
7 changed files with 33 additions and 1 deletions

View File

@ -61,7 +61,8 @@
-define(GAUGE_SAMPLER_LIST, [ -define(GAUGE_SAMPLER_LIST, [
subscriptions, subscriptions,
topics, topics,
connections connections,
live_connections
]). ]).
-define(SAMPLER_LIST, ?GAUGE_SAMPLER_LIST ++ ?DELTA_SAMPLER_LIST). -define(SAMPLER_LIST, ?GAUGE_SAMPLER_LIST ++ ?DELTA_SAMPLER_LIST).

View File

@ -401,6 +401,7 @@ getstats(Key) ->
end. end.
stats(connections) -> emqx_stats:getstat('connections.count'); stats(connections) -> emqx_stats:getstat('connections.count');
stats(live_connections) -> emqx_stats:getstat('live_connections.count');
stats(topics) -> emqx_stats:getstat('topics.count'); stats(topics) -> emqx_stats:getstat('topics.count');
stats(subscriptions) -> emqx_stats:getstat('subscriptions.count'); stats(subscriptions) -> emqx_stats:getstat('subscriptions.count');
stats(received) -> emqx_metrics:val('messages.received'); stats(received) -> emqx_metrics:val('messages.received');

View File

@ -171,6 +171,11 @@ swagger_desc(topics) ->
" Can only represent the approximate state" " Can only represent the approximate state"
>>; >>;
swagger_desc(connections) -> swagger_desc(connections) ->
<<
"Sessions at the time of sampling."
" Can only represent the approximate state"
>>;
swagger_desc(live_connections) ->
<< <<
"Connections at the time of sampling." "Connections at the time of sampling."
" Can only represent the approximate state" " Can only represent the approximate state"

View File

@ -90,6 +90,20 @@ t_monitor_current_api(_) ->
], ],
ok. ok.
t_monitor_current_api_live_connections(_) ->
process_flag(trap_exit, true),
ClientId = <<"live_conn_tests">>,
{ok, C} = emqtt:start_link([{clean_start, false}, {clientid, ClientId}]),
{ok, _} = emqtt:connect(C),
ok = emqtt:disconnect(C),
{ok, Rate} = request(["monitor_current"]),
?assertEqual(0, maps:get(<<"live_connections">>, Rate)),
?assertEqual(1, maps:get(<<"connections">>, Rate)),
%% clears
{ok, C1} = emqtt:start_link([{clean_start, true}, {clientid, ClientId}]),
{ok, _} = emqtt:connect(C1),
ok = emqtt:disconnect(C1).
t_monitor_reset(_) -> t_monitor_reset(_) ->
restart_monitor(), restart_monitor(),
{ok, Rate} = request(["monitor_current"]), {ok, Rate} = request(["monitor_current"]),

View File

@ -142,6 +142,7 @@ node_info() ->
max_fds, lists:usort(lists:flatten(erlang:system_info(check_io))) max_fds, lists:usort(lists:flatten(erlang:system_info(check_io)))
), ),
connections => ets:info(?CHAN_TAB, size), connections => ets:info(?CHAN_TAB, size),
live_connections => ets:info(?CHAN_LIVE_TAB, size),
node_status => 'running', node_status => 'running',
uptime => proplists:get_value(uptime, BrokerInfo), uptime => proplists:get_value(uptime, BrokerInfo),
version => iolist_to_binary(proplists:get_value(version, BrokerInfo)), version => iolist_to_binary(proplists:get_value(version, BrokerInfo)),

View File

@ -151,6 +151,11 @@ fields(node_info) ->
#{desc => <<"Node name">>, example => <<"emqx@127.0.0.1">>} #{desc => <<"Node name">>, example => <<"emqx@127.0.0.1">>}
)}, )},
{connections, {connections,
mk(
non_neg_integer(),
#{desc => <<"Number of clients session in this node">>, example => 0}
)},
{live_connections,
mk( mk(
non_neg_integer(), non_neg_integer(),
#{desc => <<"Number of clients currently connected to this node">>, example => 0} #{desc => <<"Number of clients currently connected to this node">>, example => 0}

View File

@ -60,6 +60,11 @@ t_nodes_api(_) ->
Edition = maps:get(<<"edition">>, LocalNodeInfo), Edition = maps:get(<<"edition">>, LocalNodeInfo),
?assertEqual(emqx_release:edition_longstr(), Edition), ?assertEqual(emqx_release:edition_longstr(), Edition),
Conns = maps:get(<<"connections">>, LocalNodeInfo),
?assertEqual(0, Conns),
LiveConns = maps:get(<<"live_connections">>, LocalNodeInfo),
?assertEqual(0, LiveConns),
NodePath = emqx_mgmt_api_test_util:api_path(["nodes", atom_to_list(node())]), NodePath = emqx_mgmt_api_test_util:api_path(["nodes", atom_to_list(node())]),
{ok, NodeInfo} = emqx_mgmt_api_test_util:request_api(get, NodePath), {ok, NodeInfo} = emqx_mgmt_api_test_util:request_api(get, NodePath),
NodeNameResponse = NodeNameResponse =