Skip to content

Commit 11072fb

Browse files
feat: introduce new metric active_mqtt_connections (#81)
* feat: introduce new metric active_mqtt_connections * fix: add new araity for listener
1 parent 13d3c41 commit 11072fb

6 files changed

Lines changed: 101 additions & 60 deletions

File tree

apps/vmq_server/src/vmq_health_http.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ cluster_status() ->
7171
-spec listeners_status() -> ok | {error, Reason :: string()}.
7272
listeners_status() ->
7373
NotRunningListeners = lists:filtermap(
74-
fun({Type, _, _, Status, _, _, _}) ->
74+
fun({Type, _, _, Status, _, _, _, _, _}) ->
7575
case Status of
7676
running ->
7777
false;

apps/vmq_server/src/vmq_listener_cli.erl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ vmq_listener_show_cmd() ->
312312
fun(_, [], []) ->
313313
Table =
314314
lists:foldl(
315-
fun({Type, Ip, Port, Status, MP, MaxConns, PP}, Acc) ->
315+
fun({Type, Ip, Port, Status, MP, MaxConns, PP, ActiveConns, AllConns}, Acc) ->
316316
[
317317
[
318318
{type, Type},
@@ -321,7 +321,9 @@ vmq_listener_show_cmd() ->
321321
{port, Port},
322322
{mountpoint, MP},
323323
{max_conns, MaxConns},
324-
{proxy_protocol, PP}
324+
{proxy_protocol, PP},
325+
{active_conns, ActiveConns},
326+
{all_conns, AllConns}
325327
]
326328
| Acc
327329
]

apps/vmq_server/src/vmq_metrics.erl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,13 +2153,19 @@ fetch_external_metric(Mod, Fun, Default) ->
21532153
misc_statistics() ->
21542154
{NrOfSubs, SMemory} = fetch_external_metric(vmq_reg_trie, stats, {0, 0}),
21552155
{NrOfRetain, RMemory} = fetch_external_metric(vmq_retain_srv, stats, {0, 0}),
2156+
{NrOfMQTTConnections, NrOfMQTTWSConnections} = fetch_external_metric(
2157+
vmq_ranch_sup, active_mqtt_connections, {0, 0}
2158+
),
21562159
[
21572160
{router_subscriptions, NrOfSubs},
21582161
{router_memory, SMemory},
21592162
{retain_messages, NrOfRetain},
21602163
{retain_memory, RMemory},
21612164
{queue_processes, fetch_external_metric(vmq_queue_sup_sup, nr_of_queues, 0)},
2162-
{offline_messages, fetch_external_metric(vmq_message_store, nr_of_offline_messages, 0)}
2165+
{offline_messages, fetch_external_metric(vmq_message_store, nr_of_offline_messages, 0)},
2166+
{active_mqttws_connections, NrOfMQTTWSConnections},
2167+
{active_mqtt_connections, NrOfMQTTConnections},
2168+
{total_active_connections, NrOfMQTTWSConnections + NrOfMQTTConnections}
21632169
].
21642170

21652171
-spec misc_stats_def() -> [metric_def()].
@@ -2194,7 +2200,28 @@ misc_stats_def() ->
21942200
<<"The number of bytes used for storing retained messages.">>
21952201
),
21962202
m(gauge, [], queue_processes, queue_processes, <<"The number of MQTT queue processes.">>),
2197-
m(gauge, [], offline_messages, offline_messages, <<"The number of offline messages">>)
2203+
m(gauge, [], offline_messages, offline_messages, <<"The number of offline messages">>),
2204+
m(
2205+
gauge,
2206+
[],
2207+
active_mqtt_connections,
2208+
active_mqtt_connections,
2209+
<<"The number of active MQTT(S) connections.">>
2210+
),
2211+
m(
2212+
gauge,
2213+
[],
2214+
active_mqttws_connections,
2215+
active_mqttws_connections,
2216+
<<"The number of active MQTT WS(S) connections.">>
2217+
),
2218+
m(
2219+
gauge,
2220+
[],
2221+
total_active_connections,
2222+
total_active_connections,
2223+
<<"The total number of active MQTT and MQTTWS connections.">>
2224+
)
21982225
].
21992226

22002227
-spec system_statistics() -> [{metric_id(), any()}].

apps/vmq_server/src/vmq_ranch_config.erl

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ start_link() ->
5959
stop_all_mqtt_listeners(KillSessions) ->
6060
lists:foreach(
6161
fun
62-
({mqtt, Addr, Port, _, _, _, _}) -> stop_listener(Addr, Port, KillSessions);
63-
({mqtts, Addr, Port, _, _, _, _}) -> stop_listener(Addr, Port, KillSessions);
64-
({mqttws, Addr, Port, _, _, _, _}) -> stop_listener(Addr, Port, KillSessions);
65-
({mqttwss, Addr, Port, _, _, _, _}) -> stop_listener(Addr, Port, KillSessions);
62+
({mqtt, Addr, Port, _, _, _, _, _, _}) -> stop_listener(Addr, Port, KillSessions);
63+
({mqtts, Addr, Port, _, _, _, _, _, _}) -> stop_listener(Addr, Port, KillSessions);
64+
({mqttws, Addr, Port, _, _, _, _, _, _}) -> stop_listener(Addr, Port, KillSessions);
65+
({mqttwss, Addr, Port, _, _, _, _, _, _}) -> stop_listener(Addr, Port, KillSessions);
6666
(_) -> ignore
6767
end,
6868
listeners()
@@ -135,51 +135,38 @@ start_listener(Type, Addr, Port, {TransportOpts, Opts}) ->
135135
end.
136136

137137
listeners() ->
138-
lists:foldl(
139-
fun
140-
({ranch_server, _, _, _}, Acc) ->
141-
Acc;
142-
({{ranch_listener_sup, {Ip, Port}}, Status, supervisor, _}, Acc) ->
143-
{ok, {Type, Opts}} = get_listener_config(Ip, Port),
144-
MountPoint = proplists:get_value(mountpoint, Opts, ""),
145-
MaxConnections = proplists:get_value(
146-
max_connections,
147-
Opts,
148-
vmq_config:get_env(max_connections)
149-
),
150-
Status1 =
151-
case Status of
152-
restarting ->
153-
restarting;
154-
undefined ->
155-
stopped;
156-
Pid when is_pid(Pid) ->
157-
case
158-
lists:keyfind(
159-
ranch_acceptors_sup, 1, supervisor:which_children(Pid)
160-
)
161-
of
162-
false ->
163-
not_found;
164-
{_, restarting, supervisor, _} ->
165-
restarting;
166-
{_, undefined, supervisor, _} ->
167-
stopped;
168-
{_, AcceptorPid, supervisor, _} when is_pid(AcceptorPid) ->
169-
running
170-
end
171-
end,
172-
StrIp = inet:ntoa(Ip),
173-
StrPort = integer_to_list(Port),
174-
ProxyProtocol = proplists:get_value(
175-
proxy_protocol,
176-
Opts,
177-
false
178-
),
179-
[{Type, StrIp, StrPort, Status1, MountPoint, MaxConnections, ProxyProtocol} | Acc]
138+
maps:fold(
139+
fun({Ip, Port}, ConfigMap, Acc) ->
140+
{ok, {Type, Opts}} = get_listener_config(Ip, Port),
141+
MountPoint = proplists:get_value(mountpoint, Opts, ""),
142+
MaxConnections = proplists:get_value(
143+
max_connections,
144+
Opts,
145+
vmq_config:get_env(max_connections)
146+
),
147+
ActiveConnections = maps:get(active_connections, ConfigMap),
148+
% the highest number of connections seen
149+
AllConnections = maps:get(all_connections, ConfigMap),
150+
Status = maps:get(status, ConfigMap),
151+
StrIp =
152+
case Ip of
153+
{local, FS} -> {local, FS};
154+
_ -> inet:ntoa(Ip)
155+
end,
156+
StrPort = integer_to_list(Port),
157+
ProxyProtocol = proplists:get_value(
158+
proxy_protocol,
159+
Opts,
160+
false
161+
),
162+
[
163+
{Type, StrIp, StrPort, Status, MountPoint, MaxConnections, ProxyProtocol,
164+
ActiveConnections, AllConnections}
165+
| Acc
166+
]
180167
end,
181168
[],
182-
supervisor:which_children(ranch_sup)
169+
ranch:info()
183170
).
184171

185172
get_listener_config(Addr, Port) ->

apps/vmq_server/src/vmq_ranch_sup.erl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
-behaviour(supervisor).
1717

1818
%% API
19-
-export([start_link/0]).
19+
-export([start_link/0, active_mqtt_connections/0]).
2020

2121
%% Supervisor callbacks
2222
-export([init/1]).
@@ -27,6 +27,26 @@
2727
%%% API functions
2828
%%%===================================================================
2929

30+
-spec active_mqtt_connections() -> {non_neg_integer(), non_neg_integer()}.
31+
active_mqtt_connections() ->
32+
lists:foldl(
33+
fun
34+
% Unix sockets will also be counted under "mqtt"
35+
({mqtt, _, _, _, _, _, _, Active, _}, {MQTT, WS}) ->
36+
{MQTT + Active, WS};
37+
({mqtts, _, _, _, _, _, _, Active, _}, {MQTT, WS}) ->
38+
{MQTT + Active, WS};
39+
({mqttws, _, _, _, _, _, _, Active, _}, {MQTT, WS}) ->
40+
{MQTT, WS + Active};
41+
({mqttwss, _, _, _, _, _, _, Active, _}, {MQTT, WS}) ->
42+
{MQTT, WS + Active};
43+
(_, Sum) ->
44+
Sum
45+
end,
46+
{0, 0},
47+
vmq_ranch_config:listeners()
48+
).
49+
3050
%%--------------------------------------------------------------------
3151
%% @doc
3252
%% Starts the supervisor

apps/vmq_server/src/vmq_status_http.erl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ cluster_status() ->
6969

7070
node_status() ->
7171
% Total Connections
72-
SocketOpen = counter_val(?METRIC_SOCKET_OPEN),
73-
SocketClose = counter_val(?METRIC_SOCKET_CLOSE),
74-
TotalConnections = SocketOpen - SocketClose,
72+
TotalActiveMqttConnections = lists:sum(
73+
tuple_to_list(vmq_ranch_sup:active_mqtt_connections())
74+
),
7575
% Total Online Queues
7676
TotalQueues = vmq_queue_sup_sup:nr_of_queues(),
77-
TotalOfflineQueues = TotalQueues - TotalConnections,
77+
TotalOfflineQueues = TotalQueues - TotalActiveMqttConnections,
7878
TotalPublishIn =
7979
counter_val(?MQTT4_PUBLISH_RECEIVED) +
8080
counter_val(?MQTT5_PUBLISH_RECEIVED),
@@ -90,7 +90,7 @@ node_status() ->
9090
{NrOfSubs, _SMemory} = vmq_reg_trie:stats(),
9191
{NrOfRetain, _RMemory} = vmq_retain_srv:stats(),
9292
{ok, [
93-
{<<"num_online">>, TotalConnections},
93+
{<<"num_online">>, TotalActiveMqttConnections},
9494
{<<"num_offline">>, TotalOfflineQueues},
9595
{<<"msg_in">>, TotalPublishIn},
9696
{<<"msg_out">>, TotalPublishOut},
@@ -118,12 +118,17 @@ counter_val(C) ->
118118

119119
listeners() ->
120120
lists:foldl(
121-
fun({Type, Ip, Port, Status, MP, MaxConns, PP}, Acc) ->
121+
fun({Type, Ip, Port, Status, MP, MaxConns, PP, _, _}, Acc) ->
122+
Ip1 =
123+
case Ip of
124+
{local, FS} -> list_to_binary(FS);
125+
Any -> list_to_binary(Any)
126+
end,
122127
[
123128
[
124129
{type, Type},
125130
{status, Status},
126-
{ip, list_to_binary(Ip)},
131+
{ip, Ip1},
127132
{port, list_to_integer(Port)},
128133
{mountpoint, MP},
129134
{max_conns, MaxConns},

0 commit comments

Comments
 (0)