Skip to content

Commit

Permalink
Move xmpp_stanza_size_sent into mongoose_c2s
Browse files Browse the repository at this point in the history
Add c2s_message_processing_time metric
Init mongoose_c2s for host types
  • Loading branch information
arcusfelis committed Jun 18, 2024
1 parent de88a13 commit 4f2bd26
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 37 deletions.
6 changes: 4 additions & 2 deletions big_tests/tests/bosh_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ create_and_terminate_session(Config) ->

% Assert that correct events have been executed
[instrument_helper:assert(Event, Label, fun(#{byte_size := BS}) -> BS > 0 end)
|| {Event, Label} <- instrumentation_events()],
|| {Event, Label} <- instrumentation_events(), Event =/= c2s_message_processing_time],

%% Verify C2S listener is not used
instrument_helper:assert_not_emitted(negative_instrumentation_events(), true),
Expand Down Expand Up @@ -948,7 +948,9 @@ wait_for_zero_bosh_sessions() ->
#{name => get_bosh_sessions}).

instrumentation_events() ->
instrument_helper:declared_events(mod_bosh, []).
instrument_helper:declared_events(mod_bosh, [])
++ instrument_helper:declared_events(mongoose_c2s, [global])
++ instrument_helper:declared_events(mongoose_c2s). %% For host_type()

negative_instrumentation_events() ->
[{Name, #{}} || Name <- negative_instrumentation_events_names()].
Expand Down
10 changes: 7 additions & 3 deletions big_tests/tests/connect_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,10 @@ metrics_test(Config) ->
tls_authenticate(Config),

% Assert that correct events have been executed
[instrument_helper:assert(Event, Label, fun(#{byte_size := BS}) -> BS > 0 end)
|| {Event, Label} <- instrumentation_events()].
[instrument_helper:assert(Event, Label, fun(#{byte_size := BS}) -> BS > 0;
(#{time := Time}) -> Time > 0 end)
|| {Event, Label} <- instrumentation_events(),
Event =/= c2s_message_processing_time].

tls_authenticate(Config) ->
%% Given
Expand Down Expand Up @@ -814,4 +816,6 @@ proxy_info() ->
}.

instrumentation_events() ->
instrument_helper:declared_events(mongoose_c2s_listener, [#{}]).
instrument_helper:declared_events(mongoose_c2s_listener, [#{}])
++ instrument_helper:declared_events(mongoose_c2s, [global])
++ instrument_helper:declared_events(mongoose_c2s). %% For host_type()
16 changes: 11 additions & 5 deletions big_tests/tests/mim_c2s_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ two_users_can_log_and_chat(Config) ->
AliceHost = escalus_users:get_server(Config, alice),
HostType = domain_helper:domain_to_host_type(mim(), AliceHost),
HostTypePrefix = domain_helper:make_metrics_prefix(HostType),
MongooseMetrics = [{[HostTypePrefix, data, xmpp, c2s, message, processing_time], changed}],
escalus:fresh_story([{mongoose_metrics, MongooseMetrics} | Config],
escalus:fresh_story(Config,
[{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
escalus_client:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi!">>)),
escalus:assert(is_chat_message, [<<"Hi!">>], escalus_client:wait_for_stanza(Bob)),
escalus_client:send(Bob, escalus_stanza:chat_to(Alice, <<"Hi!">>)),
escalus:assert(is_chat_message, [<<"Hi!">>], escalus_client:wait_for_stanza(Alice)),

% Assert that correct events have been executed
[instrument_helper:assert(Event, Label, fun(#{byte_size := BS}) -> BS > 0 end)
|| {Event, Label} <- tcp_instrumentation_events()],
[instrument_helper:assert(Event, Label, fun(#{byte_size := BS}) -> BS > 0;
(#{time := Time}) -> Time > 0 end)
|| {Event, Label} <- tcp_instrumentation_events() ++ common_instrumentation_events()],
instrument_helper:assert_not_emitted(tls_instrumentation_events(), true)
end).

Expand Down Expand Up @@ -239,7 +239,9 @@ escalus_start(Cfg, FlatCDs) ->
Clients.

instrumentation_events() ->
instrument_helper:declared_events(mongoose_c2s_listener, [#{}]).
instrument_helper:declared_events(mongoose_c2s_listener, [#{}])
++ instrument_helper:declared_events(mongoose_c2s, [global])
++ instrument_helper:declared_events(mongoose_c2s). %% For host_type()

tcp_instrumentation_events() ->
[{c2s_tcp_data_sent, #{}},
Expand All @@ -248,3 +250,7 @@ tcp_instrumentation_events() ->
tls_instrumentation_events() ->
[{c2s_tls_data_sent, #{}},
{c2s_tls_data_received, #{}}].

common_instrumentation_events() ->
HostType = domain_helper:host_type(),
[{c2s_message_processing_time, #{host_type => HostType}}].
7 changes: 5 additions & 2 deletions big_tests/tests/websockets_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ metrics_test(Config) ->
escalus:assert(is_chat_message, [<<"Hello!">>], escalus_client:wait_for_stanza(GeraltS)),

% Assert that correct events have been executed
[instrument_helper:assert(Event, Label, fun(#{byte_size := BS}) -> BS > 0 end)
[instrument_helper:assert(Event, Label, fun(#{byte_size := BS}) -> BS > 0;
(#{time := Time}) -> Time > 0 end)
|| {Event, Label} <- instrumentation_events()],

%% Verify C2S listener is not used
Expand Down Expand Up @@ -168,7 +169,9 @@ escape_attrs(Config) ->
end).

instrumentation_events() ->
instrument_helper:declared_events(mod_websockets, []).
instrument_helper:declared_events(mod_websockets, [])
++ instrument_helper:declared_events(mongoose_c2s, [global])
++ instrument_helper:declared_events(mongoose_c2s). %% For host_type()

negative_instrumentation_events() ->
[{Name, #{}} || Name <- negative_instrumentation_events_names()].
Expand Down
2 changes: 1 addition & 1 deletion doc/operation-and-maintenance/MongooseIM-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Metrics specific to an extension, e.g. Message Archive Management, are described
| `[global, data, xmpp, sent, s2s]` | spiral | A size (in bytes) of a data sent via TCP and TLS (before encryption) Server-to-Server connections. |
| `[global, data, xmpp, received, component]` | spiral | A size (in bytes) of a data received from XMPP component. |
| `[global, data, xmpp, sent, component]` | spiral | A size (in bytes) of a data sent to XMPP component. |
| `[HostType, data, xmpp, c2s, message, processing_time`] | histogram | Processing time for incomming c2s stanzas. |
| `[HostType, c2s_message_processing_time`] | histogram | Processing time for incomming c2s stanzas. |
| `[global, data, dist]` | proplist | Network stats for an Erlang distributed communication. A proplist with values: `recv_oct`, `recv_cnt`, `recv_max`, `send_oct`, `send_max`, `send_cnt`, `send_pend`, `connections`. |
| `[global, data, rdbms, PoolName]` | proplist | For every RDBMS pool defined, an instance of this metric is available. It is a proplist with values `workers`, `recv_oct`, `recv_cnt`, `recv_max`, `send_oct`, `send_max`, `send_cnt`, `send_pend`. |

Expand Down
21 changes: 19 additions & 2 deletions src/c2s/mongoose_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
-define(AUTH_RETRIES, 3).
-define(BIND_RETRIES, 5).

-export([instrumentation/0, instrumentation/1]).

%% gen_statem callbacks
-export([callback_mode/0, init/1, handle_event/4, terminate/3]).

Expand All @@ -24,7 +26,7 @@
-export([replace_resource/2, generate_random_resource/0]).
-export([verify_user/4, maybe_open_session/3]).

-ignore_xref([get_ip/1, get_socket/1]).
-ignore_xref([get_ip/1, get_socket/1, instrumentation/1]).

-record(c2s_data, {
host_type :: undefined | mongooseim:host_type(),
Expand Down Expand Up @@ -70,6 +72,21 @@

-export_type([packet/0, data/0, state/0, state/1, fsm_res/0, fsm_res/1, retries/0, listener_opts/0]).

%% mongoose_http_handler instrumentation
-spec instrumentation() -> [mongoose_instrument:spec()].
instrumentation() ->
lists:flatmap(fun instrumentation/1, [global | ?ALL_HOST_TYPES]).

-spec instrumentation(global | mongooseim:host_type()) -> [mongoose_instrument:spec()].
instrumentation(global) ->
[{xmpp_stanza_size_sent, #{},
#{metrics => #{byte_size => histogram}}},
{xmpp_stanza_size_received, #{},
#{metrics => #{byte_size => histogram}}}];
instrumentation(HostType) ->
[{c2s_message_processing_time, #{host_type => HostType},
#{metrics => #{byte_size => histogram}}}].

%%%----------------------------------------------------------------------
%%% gen_statem
%%%----------------------------------------------------------------------
Expand Down Expand Up @@ -773,7 +790,7 @@ handle_stanza_from_client(#c2s_data{host_type = HostType}, HookParams, Acc, <<"m
Acc1 = mongoose_c2s_hooks:user_send_message(HostType, Acc, HookParams),
Acc2 = maybe_route(Acc1),
TS1 = erlang:system_time(microsecond),
mongoose_metrics:update(HostType, [data, xmpp, c2s, message, processing_time], (TS1 - TS0)),
mongoose_instrument:execute(c2s_message_processing_time, #{host_type => HostType}, #{time => (TS1 - TS0)}),
Acc2;
handle_stanza_from_client(#c2s_data{host_type = HostType}, HookParams, Acc, <<"iq">>) ->
Acc1 = mongoose_c2s_hooks:user_send_iq(HostType, Acc, HookParams),
Expand Down
7 changes: 1 addition & 6 deletions src/c2s/mongoose_c2s_listener.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ instrumentation(_Opts) ->
{c2s_tcp_data_received, #{},
#{metrics => #{byte_size => spiral}}},
{c2s_tls_data_received, #{},
#{metrics => #{byte_size => spiral}}},
%% Shared between different types of C2S listeners
{xmpp_stanza_size_sent, #{},
#{metrics => #{byte_size => histogram}}},
{xmpp_stanza_size_received, #{},
#{metrics => #{byte_size => histogram}}}].
#{metrics => #{byte_size => spiral}}}].

%% mongoose_listener
-spec start_listener(options()) -> ok.
Expand Down
1 change: 0 additions & 1 deletion src/metrics/mongoose_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ create_host_type_metrics() ->
-spec create_host_type_metrics(mongooseim:host_type()) -> 'ok'.
create_host_type_metrics(HostType) ->
lists:foreach(fun(Name) -> ensure_metric(HostType, Name, spiral) end, ?GENERAL_SPIRALS),
lists:foreach(fun(Name) -> ensure_metric(HostType, Name, histogram) end, ?GENERAL_HISTOGRAMS),
lists:foreach(fun(Name) -> ensure_metric(HostType, Name, counter) end, ?TOTAL_COUNTERS).

-spec create_host_type_hook_metrics() -> ok.
Expand Down
2 changes: 0 additions & 2 deletions src/metrics/mongoose_metrics_definitions.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@
{[erlang, memory], [function, erlang, memory, ['$dp'], value],
[total, processes_used, atom_used, binary, ets, system]}]).

-define(GENERAL_HISTOGRAMS, [[data, xmpp, c2s, message, processing_time]]).

-define(DATA_FUN_METRICS,
[{[data, dist],
{function, mongoose_metrics, get_dist_data_stats, [], proplist, [connections | ?INET_STATS]}}]).
Expand Down
7 changes: 1 addition & 6 deletions src/mod_bosh.erl
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,7 @@ instrumentation() ->
[{mod_bosh_data_sent, #{},
#{metrics => #{byte_size => spiral}}},
{mod_bosh_data_received, #{},
#{metrics => #{byte_size => spiral}}},
%% Shared between different types of C2S listeners
{xmpp_stanza_size_sent, #{},
#{metrics => #{byte_size => histogram}}},
{xmpp_stanza_size_received, #{},
#{metrics => #{byte_size => histogram}}}].
#{metrics => #{byte_size => spiral}}}].

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
Expand Down
7 changes: 1 addition & 6 deletions src/mod_websockets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,7 @@ instrumentation() ->
[{mod_websocket_data_sent, #{},
#{metrics => #{byte_size => spiral}}},
{mod_websocket_data_received, #{},
#{metrics => #{byte_size => spiral}}},
%% Shared between different types of C2S listeners
{xmpp_stanza_size_sent, #{},
#{metrics => #{byte_size => histogram}}},
{xmpp_stanza_size_received, #{},
#{metrics => #{byte_size => histogram}}}].
#{metrics => #{byte_size => spiral}}}].

%%--------------------------------------------------------------------
%% Common callbacks for all cowboy behaviours
Expand Down
3 changes: 2 additions & 1 deletion src/mongoose_listener.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ stop_listener(Opts) ->
-spec instrumentation([options()]) -> [mongoose_instrument:spec()].
instrumentation(Listeners) ->
%% c2s instrumentation is shared between Bosh, Websockets and TCP listeners
lists:usort([Spec || Listener <- Listeners, Spec <- listener_instrumentation(Listener)]).
lists:usort([Spec || Listener <- Listeners, Spec <- listener_instrumentation(Listener)])
++ mongoose_c2s:instrumentation().

-spec listener_instrumentation(options()) -> [mongoose_instrument:spec()].
listener_instrumentation(Opts = #{module := Module}) ->
Expand Down

0 comments on commit 4f2bd26

Please sign in to comment.