Skip to content

Commit d655452

Browse files
committed
Extract XML element measurements to a separate module
1 parent 178bf52 commit d655452

File tree

4 files changed

+47
-86
lines changed

4 files changed

+47
-86
lines changed

src/c2s/mongoose_c2s.erl

+4-22
Original file line numberDiff line numberDiff line change
@@ -1270,32 +1270,14 @@ merge_states(S0 = #c2s_data{}, S1 = #c2s_data{}) ->
12701270
%% Instrumentation helpers
12711271

12721272
-spec execute_element_event(mongoose_instrument:event_name(), exml:element(), data()) -> ok.
1273-
execute_element_event(EventName, El, #c2s_data{host_type = HostType, jid = Jid}) ->
1274-
Metrics = measure_element(El#xmlel.name, exml_query:attr(El, <<"type">>)),
1273+
execute_element_event(EventName, Element, #c2s_data{host_type = HostType, jid = Jid}) ->
1274+
Metrics = mongoose_measurements:measure_element(Element),
12751275
Measurements = case Jid of
1276-
undefined -> Metrics#{element => El};
1277-
_ -> Metrics#{element => El, jid => Jid}
1276+
undefined -> Metrics;
1277+
_ -> Metrics#{Jid => Jid}
12781278
end,
12791279
mongoose_instrument:execute(EventName, #{host_type => HostType}, Measurements).
12801280

1281-
-spec measure_element(binary(), binary() | undefined) -> mongoose_instrument:measurements().
1282-
measure_element(<<"message">>, <<"error">>) ->
1283-
#{count => 1, stanza_count => 1, error_count => 1, message_error_count => 1};
1284-
measure_element(<<"iq">>, <<"error">>) ->
1285-
#{count => 1, stanza_count => 1, error_count => 1, iq_error_count => 1};
1286-
measure_element(<<"presence">>, <<"error">>) ->
1287-
#{count => 1, stanza_count => 1, error_count => 1, presence_error_count => 1};
1288-
measure_element(<<"message">>, _Type) ->
1289-
#{count => 1, stanza_count => 1, message_count => 1};
1290-
measure_element(<<"iq">>, _Type) ->
1291-
#{count => 1, stanza_count => 1, iq_count => 1};
1292-
measure_element(<<"presence">>, _Type) ->
1293-
#{count => 1, stanza_count => 1, presence_count => 1};
1294-
measure_element(<<"stream:error">>, _Type) ->
1295-
#{count => 1, error_count => 1};
1296-
measure_element(_Name, _Type) ->
1297-
#{count => 1}.
1298-
12991281
-spec labels() -> mongoose_instrument:labels().
13001282
labels() ->
13011283
#{connection_type => c2s}.

src/component/mongoose_component_connection.erl

+11-39
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ handle_event(internal, #xmlel{name = <<"handshake">>} = El, wait_for_handshake,
8686
handle_event(internal, #xmlel{} = El, stream_established, StateData) ->
8787
execute_element_event(component_element_in, El, StateData),
8888
handle_stream_established(StateData, El);
89-
handle_event(internal, #xmlstreamend{name = Name}, _, StateData) ->
90-
StreamEnd = #xmlel{name = Name},
91-
execute_element_event(component_element_in, StreamEnd, StateData),
89+
handle_event(internal, #xmlstreamend{}, _, StateData) ->
9290
send_trailer(StateData),
9391
{stop, {shutdown, stream_end}};
9492
handle_event(internal, #xmlstreamstart{}, _, StateData) ->
@@ -203,7 +201,6 @@ handle_stream_start(S0, Attrs) ->
203201
IsSubdomain = <<"true">> =:= maps:get(<<"is_subdomain">>, Attrs, <<>>),
204202
S1 = S0#component_data{lserver = LServer, is_subdomain = IsSubdomain},
205203
send_header(S1),
206-
execute_element_event(component_element_in, Attrs, S1),
207204
{next_state, wait_for_handshake, S1, state_timeout(S1)};
208205
{?NS_COMPONENT_ACCEPT, error} ->
209206
stream_start_error(S0, mongoose_xmpp_errors:host_unknown());
@@ -408,12 +405,12 @@ close_parser(#component_data{parser = Parser}) ->
408405
-spec send_xml(data(), exml_stream:element() | [exml_stream:element()]) -> maybe_ok().
409406
send_xml(Data, XmlElement) when is_tuple(XmlElement) ->
410407
send_xml(Data, [XmlElement]);
411-
send_xml(#component_data{lserver = LServer, socket = Socket} = StateData, XmlElements) when is_list(XmlElements) ->
412-
[ begin
413-
execute_element_event(component_element_out, El, StateData),
414-
mongoose_instrument:execute(
415-
xmpp_element_size_out, labels(), #{byte_size => exml:xml_size(El), lserver => LServer})
416-
end || El <- XmlElements],
408+
send_xml(#component_data{lserver = LServer, socket = Socket}, XmlElements) when is_list(XmlElements) ->
409+
[begin
410+
execute_element_event(component_element_out, Element, LServer),
411+
mongoose_instrument:execute(
412+
xmpp_element_size_out, labels(), #{byte_size => exml:xml_size(Element), lserver => LServer})
413+
end || Element = #xmlel{} <- XmlElements],
417414
mongoose_xmpp_socket:send_xml(Socket, XmlElements).
418415

419416
state_timeout(#component_data{listener_opts = LOpts}) ->
@@ -443,35 +440,10 @@ unregister_routes(#component_data{component = Component}) ->
443440
mongoose_component:unregister_component(Component).
444441

445442
%%% Instrumentation helpers
446-
-spec execute_element_event(
447-
mongoose_instrument:event_name(), exml:attrs() | exml_stream:element(), data()) -> ok.
448-
execute_element_event(EventName, #xmlel{name = Name} = El, #component_data{lserver = LServer}) ->
449-
Metrics = measure_element(Name, exml_query:attr(El, <<"type">>)),
450-
Measurements = Metrics#{element => El, lserver => LServer},
451-
mongoose_instrument:execute(EventName, #{}, Measurements);
452-
execute_element_event(EventName, El, #component_data{lserver = LServer}) ->
453-
Measurements = #{count => 1, element => El, lserver => LServer},
454-
mongoose_instrument:execute(EventName, #{}, Measurements);
455-
execute_element_event(_, _, _) ->
456-
ok.
457-
458-
-spec measure_element(binary(), binary() | undefined) -> mongoose_instrument:measurements().
459-
measure_element(<<"message">>, <<"error">>) ->
460-
#{count => 1, stanza_count => 1, error_count => 1, message_error_count => 1};
461-
measure_element(<<"iq">>, <<"error">>) ->
462-
#{count => 1, stanza_count => 1, error_count => 1, iq_error_count => 1};
463-
measure_element(<<"presence">>, <<"error">>) ->
464-
#{count => 1, stanza_count => 1, error_count => 1, presence_error_count => 1};
465-
measure_element(<<"message">>, _Type) ->
466-
#{count => 1, stanza_count => 1, message_count => 1};
467-
measure_element(<<"iq">>, _Type) ->
468-
#{count => 1, stanza_count => 1, iq_count => 1};
469-
measure_element(<<"presence">>, _Type) ->
470-
#{count => 1, stanza_count => 1, presence_count => 1};
471-
measure_element(<<"stream:error">>, _Type) ->
472-
#{count => 1, error_count => 1};
473-
measure_element(_Name, _Type) ->
474-
#{count => 1}.
443+
-spec execute_element_event(mongoose_instrument:event_name(), exml:element(), jid:lserver()) -> ok.
444+
execute_element_event(EventName, Element, LServer) ->
445+
Measurements = mongoose_measurements:measure_element(Element),
446+
mongoose_instrument:execute(EventName, #{}, Measurements#{lserver => LServer}).
475447

476448
-spec labels() -> mongoose_instrument:labels().
477449
labels() ->
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-module(mongoose_measurements).
2+
3+
-export([measure_element/1]).
4+
5+
-include_lib("exml/include/exml.hrl").
6+
7+
-spec measure_element(exml:element()) -> mongoose_instrument:measurements().
8+
measure_element(#xmlel{name = Name} = Element) ->
9+
Metrics = measure_element(Name, exml_query:attr(Element, <<"type">>)),
10+
Metrics#{element => Element}.
11+
12+
-spec measure_element(binary(), binary() | undefined) -> mongoose_instrument:measurements().
13+
measure_element(<<"message">>, <<"error">>) ->
14+
#{count => 1, stanza_count => 1, error_count => 1, message_error_count => 1};
15+
measure_element(<<"iq">>, <<"error">>) ->
16+
#{count => 1, stanza_count => 1, error_count => 1, iq_error_count => 1};
17+
measure_element(<<"presence">>, <<"error">>) ->
18+
#{count => 1, stanza_count => 1, error_count => 1, presence_error_count => 1};
19+
measure_element(<<"message">>, _Type) ->
20+
#{count => 1, stanza_count => 1, message_count => 1};
21+
measure_element(<<"iq">>, _Type) ->
22+
#{count => 1, stanza_count => 1, iq_count => 1};
23+
measure_element(<<"presence">>, _Type) ->
24+
#{count => 1, stanza_count => 1, presence_count => 1};
25+
measure_element(<<"stream:error">>, _Type) ->
26+
#{count => 1, error_count => 1};
27+
measure_element(_Name, _Type) ->
28+
#{count => 1}.

src/s2s/mongoose_s2s_in.erl

+4-25
Original file line numberDiff line numberDiff line change
@@ -528,31 +528,10 @@ send_xml(#s2s_data{myname = LServer, socket = Socket}, Elem) ->
528528
#{byte_size => exml:xml_size(Elem), lserver => LServer, pid => self(), module => ?MODULE}),
529529
mongoose_xmpp_socket:send_xml(Socket, Elem).
530530

531-
-spec execute_element_event(data(), exml_stream:element(), mongoose_instrument:event_name()) -> ok.
532-
execute_element_event(#s2s_data{myname = LServer}, #xmlel{name = Name} = El, EventName) ->
533-
Metrics = measure_element(Name, exml_query:attr(El, <<"type">>)),
534-
Measurements = Metrics#{element => El, lserver => LServer},
535-
mongoose_instrument:execute(EventName, #{}, Measurements);
536-
execute_element_event(_, _, _) ->
537-
ok.
538-
539-
-spec measure_element(binary(), binary() | undefined) -> mongoose_instrument:measurements().
540-
measure_element(<<"message">>, <<"error">>) ->
541-
#{count => 1, stanza_count => 1, error_count => 1, message_error_count => 1};
542-
measure_element(<<"iq">>, <<"error">>) ->
543-
#{count => 1, stanza_count => 1, error_count => 1, iq_error_count => 1};
544-
measure_element(<<"presence">>, <<"error">>) ->
545-
#{count => 1, stanza_count => 1, error_count => 1, presence_error_count => 1};
546-
measure_element(<<"message">>, _Type) ->
547-
#{count => 1, stanza_count => 1, message_count => 1};
548-
measure_element(<<"iq">>, _Type) ->
549-
#{count => 1, stanza_count => 1, iq_count => 1};
550-
measure_element(<<"presence">>, _Type) ->
551-
#{count => 1, stanza_count => 1, presence_count => 1};
552-
measure_element(<<"stream:error">>, _Type) ->
553-
#{count => 1, error_count => 1};
554-
measure_element(_Name, _Type) ->
555-
#{count => 1}.
531+
-spec execute_element_event(data(), exml:element(), mongoose_instrument:event_name()) -> ok.
532+
execute_element_event(#s2s_data{myname = LServer}, Element, EventName) ->
533+
Measurements = mongoose_measurements:measure_element(Element),
534+
mongoose_instrument:execute(EventName, #{}, Measurements#{lserver => LServer}).
556535

557536
check_auth_domain(error, _) ->
558537
false;

0 commit comments

Comments
 (0)