Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global metrics only switch #940

Merged
merged 9 commits into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions apps/ejabberd/src/ejabberd_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
%% TODO: TBH this name smells.
-type passwordlike() :: binary() | scram:scram_tuple().

-define(METRIC(Host, Name), [backends, auth, Host, Name]).
-define(METRIC(Name), [backends, auth, Name]).

%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
Expand Down Expand Up @@ -608,13 +609,13 @@ auth_modules(LServer) ->

ensure_metrics(Host) ->
Metrics = [check_password, try_register, does_user_exist],
[mongoose_metrics:ensure_metric(?METRIC(Host, Metric), histogram)
[mongoose_metrics:ensure_metric(Host, ?METRIC(Metric), histogram)
|| Metric <- Metrics].

-spec timed_call(ejabberd:lserver(), term(), fun(), list()) -> term().
timed_call(LServer, Metric, Fun, Args) ->
{Time, Result} = timer:tc(Fun, Args),
mongoose_metrics:update(?METRIC(LServer, Metric), Time),
mongoose_metrics:update(LServer, ?METRIC(Metric), Time),
Result.

%% Library functions for reuse in ejabberd_auth_* modules
Expand Down
6 changes: 3 additions & 3 deletions apps/ejabberd/src/ejabberd_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -867,13 +867,13 @@ session_established({xmlstreamelement,
session_established, StateData);
session_established({xmlstreamelement,
#xmlel{name = <<"inactive">>} = El}, State) ->
mongoose_metrics:update([State#state.server, modCSIInactive], 1),
mongoose_metrics:update(State#state.server, modCSIInactive, 1),

maybe_inactivate_session(xml:get_tag_attr_s(<<"xmlns">>, El), State);

session_established({xmlstreamelement,
#xmlel{name = <<"active">>} = El}, State) ->
mongoose_metrics:update([State#state.server, modCSIActive], 1),
mongoose_metrics:update(State#state.server, modCSIActive, 1),

maybe_activate_session(xml:get_tag_attr_s(<<"xmlns">>, El), State);

Expand Down Expand Up @@ -1526,7 +1526,7 @@ change_shaper(StateData, JID) ->
send_text(StateData, Text) ->
?DEBUG("Send XML on stream = ~p", [Text]),
Size = size(Text),
mongoose_metrics:update([data, xmpp, sent, xml_stanza_size], Size),
mongoose_metrics:update(global, [data, xmpp, sent, xml_stanza_size], Size),
(StateData#state.sockmod):send(StateData#state.socket, Text).

-spec maybe_send_element_safe(state(), El :: jlib:xmlel()) -> any().
Expand Down
6 changes: 4 additions & 2 deletions apps/ejabberd/src/ejabberd_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ process_term(Term, State) ->
add_option(sasl_mechanisms, Mechanisms, State);
{http_connections, HttpConnections} ->
add_option(http_connections, HttpConnections, State);
{all_metrics_are_global, Value} ->
add_option(all_metrics_are_global, Value, State);
{_Opt, _Val} ->
lists:foldl(fun(Host, S) -> process_host_term(Term, Host, S) end,
State, State#state.hosts)
Expand Down Expand Up @@ -1009,7 +1011,7 @@ handle_local_config_add(#local_config{key = {cassandra_server,_,_}}) ->
mongoose_cassandra:stop(),
mongoose_cassandra:start();
handle_local_config_add(#local_config{key=Key} = El) ->
case Key of
case can_be_ignored(Key) of
true ->
ok;
false ->
Expand Down Expand Up @@ -1168,7 +1170,7 @@ add_virtual_host(Host) ->

-spec can_be_ignored(Key :: atom()) -> boolean().
can_be_ignored(Key) when is_atom(Key) ->
L = [domain_certfile, s2s],
L = [domain_certfile, s2s, all_metrics_are_global],
lists:member(Key,L).

-spec remove_virtual_host(ejabberd:server()) -> any().
Expand Down
6 changes: 3 additions & 3 deletions apps/ejabberd/src/ejabberd_receiver.erl
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ handle_info({Tag, _TCPSocket, Data},
when (Tag == tcp) or (Tag == ssl) ->
case SockMod of
ejabberd_tls ->
mongoose_metrics:update([data, xmpp, received, encrypted_size], size(Data)),
mongoose_metrics:update(global, [data, xmpp, received, encrypted_size], size(Data)),
case ejabberd_tls:recv_data(Socket, Data) of
{ok, TLSData} ->
{noreply, process_data(TLSData, State),
Expand All @@ -213,7 +213,7 @@ handle_info({Tag, _TCPSocket, Data},
{stop, normal, State}
end;
ejabberd_zlib ->
mongoose_metrics:update([data, xmpp, received, compressed_size], size(Data)),
mongoose_metrics:update(global, [data, xmpp, received, compressed_size], size(Data)),
case ejabberd_zlib:recv_data(Socket, Data) of
{ok, ZlibData} ->
{noreply, process_data(ZlibData, State),
Expand Down Expand Up @@ -322,7 +322,7 @@ process_data(Data, #state{parser = Parser,
c2s_pid = C2SPid} = State) ->
?DEBUG("Received XML on stream = \"~s\"", [Data]),
Size = size(Data),
mongoose_metrics:update([data, xmpp, received, xml_stanza_size], Size),
mongoose_metrics:update(global, [data, xmpp, received, xml_stanza_size], Size),

maybe_run_keep_alive_hook(Size, State),
case exml_stream:parse(Parser, Data) of
Expand Down
4 changes: 2 additions & 2 deletions apps/ejabberd/src/ejabberd_router.erl
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ init([]) ->
{record_name, external_component}]),
mnesia:add_table_copy(external_component_global, node(), ram_copies),
compile_routing_module(),
mongoose_metrics:ensure_metric([global, routingErrors], spiral),
mongoose_metrics:ensure_metric(global, routingErrors, spiral),

{ok, #state{}}.

Expand Down Expand Up @@ -440,7 +440,7 @@ route(From, To, Packet, []) ->
?ERROR_MSG("error routing from=~ts to=~ts, packet=~ts, reason: no more routing modules",
[jid:to_binary(From), jid:to_binary(To),
exml:to_binary(Packet)]),
mongoose_metrics:update([global, routingErrors], 1),
mongoose_metrics:update(global, routingErrors, 1),
ok;
route(OrigFrom, OrigTo, OrigPacket, [M|Tail]) ->
?DEBUG("Using module ~p", [M]),
Expand Down
6 changes: 3 additions & 3 deletions apps/ejabberd/src/ejabberd_sm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
%%--------------------------------------------------------------------
-spec start_link() -> 'ignore' | {'error',_} | {'ok',pid()}.
start_link() ->
mongoose_metrics:ensure_metric(?UNIQUE_COUNT_CACHE, gauge),
mongoose_metrics:ensure_metric(global, ?UNIQUE_COUNT_CACHE, gauge),
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).


Expand Down Expand Up @@ -338,7 +338,7 @@ get_session_pid(User, Server, Resource) ->
get_unique_sessions_number() ->
try
C = ?SM_BACKEND:unique_count(),
mongoose_metrics:update(?UNIQUE_COUNT_CACHE, C),
mongoose_metrics:update(global, ?UNIQUE_COUNT_CACHE, C),
C
catch
_:_ ->
Expand Down Expand Up @@ -958,7 +958,7 @@ sm_backend(Backend) ->

-spec get_cached_unique_count() -> non_neg_integer().
get_cached_unique_count() ->
case mongoose_metrics:get_metric_value(?UNIQUE_COUNT_CACHE) of
case mongoose_metrics:get_metric_value(global, ?UNIQUE_COUNT_CACHE) of
{ok, DataPoints} ->
proplists:get_value(value, DataPoints);
_ ->
Expand Down
3 changes: 2 additions & 1 deletion apps/ejabberd/src/ejabberd_tls.erl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ send(#tlssock{tcpsock = TCPSocket, tlsport = Port} = TLSSock, Packet) ->
%?PRINT("OUT: ~p~n", [{TCPSocket, lists:flatten(Packet)}]),
case port_control(Port, ?GET_ENCRYPTED_OUTPUT, []) of
<<0, Out/binary>> ->
mongoose_metrics:update([data, xmpp, sent, encrypted_size], size(Out)),
mongoose_metrics:update(
global, [data, xmpp, sent, encrypted_size], size(Out)),
gen_tcp:send(TCPSocket, Out);
<<1, Error/binary>> ->
{error, binary_to_list(Error)}
Expand Down
2 changes: 1 addition & 1 deletion apps/ejabberd/src/ejabberd_zlib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ send(#zlibsock{sockmod = SockMod, socket = Socket, zlibport = Port},
Packet) ->
case port_control(Port, ?DEFLATE, Packet) of
<<0, Out/binary>> ->
mongoose_metrics:update([data, xmpp, sent, compressed_size], size(Out)),
mongoose_metrics:update(global, [data, xmpp, sent, compressed_size], size(Out)),
SockMod:send(Socket, Out);
<<1, Error/binary>> ->
{error, erlang:binary_to_existing_atom(Error, utf8)};
Expand Down
13 changes: 4 additions & 9 deletions apps/ejabberd/src/gen_mod.erl
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,18 @@ generate_fun_body(true, BaseModule, RealBackendModule, F, Args) ->
FS = atom_to_list(F),
%% returned is the following
%% {Time, Result} = timer:tc(Backend, F, Args),
%% mongoose_metrics:update(?METRIC(Backend, F), Time),
%% mongoose_metrics:update(global, ?METRIC(Backend, F), Time),
%% Result.
[" {Time, Result} = timer:tc(",RealBackendModule,", ",FS,", [",Args,"]),\n",
" mongoose_metrics:update(",
" mongoose_metrics:update(global, ",
io_lib:format("~p", [?METRIC(BaseModule, F)]),
", Time),\n",
" Result.\n"].

ensure_backend_metrics(Module, Ops) ->
EnsureFun = fun(Op) ->
case exometer:info(?METRIC(Module, Op), type) of
undefined ->
exometer:new(?METRIC(Module, Op), histogram);
_ ->
ok
end
end,
mongoose_metrics:ensure_metric(global, ?METRIC(Module, Op), histogram)
end,
lists:foreach(EnsureFun, Ops).

-spec is_app_running(_) -> boolean().
Expand Down
16 changes: 10 additions & 6 deletions apps/ejabberd/src/mod_http_notification.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
-define(DEFAULT_POOL_NAME, http_pool).
-define(DEFAULT_PATH, "").

-define(SENT_METRIC, [mod_http_notifications, sent]).
-define(FAILED_METRIC, [mod_http_notifications, failed]).
-define(RESPONSE_METRIC, [mod_http_notifications, response_time]).

start(Host, _Opts) ->
ensure_metrics(Host),
ejabberd_hooks:add(user_send_packet, Host, ?MODULE, on_user_send_packet, 100),
Expand Down Expand Up @@ -77,16 +81,16 @@ make_req(Host, Sender, Receiver, Message) ->
ok.

ensure_metrics(Host) ->
mongoose_metrics:ensure_metric([Host, mod_http_notifications, sent], spiral),
mongoose_metrics:ensure_metric([Host, mod_http_notifications, failed], spiral),
mongoose_metrics:ensure_metric([Host, mod_http_notifications, response_time], histogram),
mongoose_metrics:ensure_metric(Host, ?SENT_METRIC, spiral),
mongoose_metrics:ensure_metric(Host, ?FAILED_METRIC, spiral),
mongoose_metrics:ensure_metric(Host, ?RESPONSE_METRIC, histogram),
ok.
record_result(Host, ok, Elapsed) ->
mongoose_metrics:update([Host, mod_http_notifications, sent], 1),
mongoose_metrics:update([Host, mod_http_notifications, response_time], Elapsed),
mongoose_metrics:update(Host, ?SENT_METRIC, 1),
mongoose_metrics:update(Host, ?RESPONSE_METRIC, Elapsed),
ok;
record_result(Host, {error, Reason}, _) ->
mongoose_metrics:update([Host, mod_http_notifications, failed], 1),
mongoose_metrics:update(Host, ?FAILED_METRIC, 1),
?WARNING_MSG("Sending http notification failed: ~p", [Reason]),
ok.

Expand Down
10 changes: 5 additions & 5 deletions apps/ejabberd/src/mod_mam.erl
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ start(Host, Opts) ->
ejabberd_hooks:add(remove_user, Host, ?MODULE, remove_user, 50),
ejabberd_hooks:add(anonymous_purge_hook, Host, ?MODULE, remove_user, 50),
ejabberd_hooks:add(amp_determine_strategy, Host, ?MODULE, determine_amp_strategy, 20),
mongoose_metrics:create([backends, ?MODULE, lookup], histogram),
mongoose_metrics:create([Host, modMamLookups, simple], spiral),
mongoose_metrics:create([backends, ?MODULE, archive], histogram),
mongoose_metrics:ensure_metric(global, [backends, ?MODULE, lookup], histogram),
mongoose_metrics:ensure_metric(Host, [modMamLookups, simple], spiral),
mongoose_metrics:ensure_metric(global, [backends, ?MODULE, archive], histogram),
ok.


Expand Down Expand Up @@ -742,7 +742,7 @@ lookup_messages(Host, ArcID, ArcJID, RSM, Borders, Start, End, Now,
Start, End, Now, WithJID,
PageSize, LimitPassed, MaxResultLimit, IsSimple]),
Diff = timer:now_diff(os:timestamp(), StartT),
mongoose_metrics:update([backends, ?MODULE, lookup], Diff),
mongoose_metrics:update(global, [backends, ?MODULE, lookup], Diff),
R.


Expand All @@ -755,7 +755,7 @@ archive_message(Host, MessID, ArcID, LocJID, RemJID, SrcJID, Dir, Packet) ->
R = ejabberd_hooks:run_fold(mam_archive_message, Host, ok,
[Host, MessID, ArcID, LocJID, RemJID, SrcJID, Dir, Packet]),
Diff = timer:now_diff(os:timestamp(), StartT),
mongoose_metrics:update([backends, ?MODULE, archive], Diff),
mongoose_metrics:update(global, [backends, ?MODULE, archive], Diff),
R.

-spec purge_single_message(Host :: ejabberd:server(),
Expand Down
2 changes: 2 additions & 0 deletions apps/ejabberd/src/mongoose_api_json.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ do_serialize(Data) ->

prepare_struct({Key, Value}) ->
{struct, [{Key, prepare_struct(Value)}]};
prepare_struct([]) ->
[];
prepare_struct(List) when is_list(List) ->
case is_proplist(List) of
true ->
Expand Down
6 changes: 3 additions & 3 deletions apps/ejabberd/src/mongoose_api_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ host_metric(Bindings) ->
{metric, Metric} = lists:keyfind(metric, 1, Bindings),
try
MetricAtom = binary_to_existing_atom(Metric, utf8),
{ok, Value} = mongoose_metrics:get_metric_value({Host, MetricAtom}),
{ok, Value} = mongoose_metrics:get_metric_value([Host, MetricAtom]),
{ok, {metric, Value}}
catch error:badarg ->
{error, not_found}
Expand All @@ -109,7 +109,7 @@ host_metrics(Bindings) ->
global_metric(Bindings) ->
{metric, Metric} = lists:keyfind(metric, 1, Bindings),
MetricAtom = binary_to_existing_atom(Metric, utf8),
case mongoose_metrics:get_metric_value({global, MetricAtom}) of
case mongoose_metrics:get_metric_value(global, MetricAtom) of
{ok, Value} ->
{ok, {metric, Value}};
_Other ->
Expand Down Expand Up @@ -161,4 +161,4 @@ get_host_metrics(Host) ->

prep_name(NameParts) ->
ToStrings = [atom_to_list(NamePart) || NamePart <- NameParts],
string:join(ToStrings, "_").
string:join(ToStrings, ".").
Loading