Skip to content

Commit

Permalink
Precompute host_type prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Apr 22, 2022
1 parent 660f4dd commit a7453fa
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/metrics/mongoose_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
get_rdbms_data_stats/0, get_rdbms_data_stats/1, get_up_time/0,
remove_host_type_metrics/1, get_report_interval/0]).

-define(PREFIXES, {?MODULE, prefixes}).
-define(DEFAULT_REPORT_INTERVAL, 60000). %%60s

-type use_or_skip() :: use | skip.
Expand All @@ -60,6 +61,7 @@

-spec init() -> ok.
init() ->
prepare_prefixes(),
create_global_metrics(),
lists:foreach(
fun(HostType) ->
Expand Down Expand Up @@ -132,7 +134,7 @@ get_metric_values(HostType) ->
exometer:get_values([HostType]).

get_host_type_metric_names(HostType) ->
HostTypeName = make_host_type_name(HostType),
HostTypeName = get_host_type_prefix(HostType),
[MetricName || {[_HostTypeName | MetricName], _, _} <- exometer:find_entries([HostTypeName])].

get_global_metric_names() ->
Expand Down Expand Up @@ -176,24 +178,43 @@ get_mnesia_running_db_nodes_count() ->
{value, length(mnesia:system_info(running_db_nodes))}.

remove_host_type_metrics(HostType) ->
HostTypeName = make_host_type_name(HostType),
HostTypeName = get_host_type_prefix(HostType),
lists:foreach(fun remove_metric/1, exometer:find_entries([HostTypeName])).

remove_all_metrics() ->
persistent_term:erase(?PREFIXES),
lists:foreach(fun remove_metric/1, exometer:find_entries([])).

%% ---------------------------------------------------------------------
%% Internal functions
%% ---------------------------------------------------------------------

prepare_prefixes() ->
PrefixesList = [ {HT, make_host_type_prefix(HT)} || HT <- ?ALL_HOST_TYPES ],
PrefixesMap = maps:from_list(PrefixesList),
persistent_term:put(?PREFIXES, PrefixesMap).

-spec all_metrics_are_global() -> boolean().
all_metrics_are_global() ->
mongoose_config:get_opt(all_metrics_are_global).

get_host_type_prefix(HostType) when is_atom(HostType) ->
HostType;
get_host_type_prefix(HostType) when is_binary(HostType) ->
case persistent_term:get(?PREFIXES, undefined) of
#{HostType := HostTypePrefix} -> HostTypePrefix;
undefined -> make_host_type_prefix(HostType)
end.

make_host_type_prefix(HT) when is_atom(HT) ->
HT;
make_host_type_prefix(HT) when is_binary(HT) ->
binary:replace(HT, <<" ">>, <<"_">>, [global]).

pick_prefix_by_all_metrics_are_global(HostType) ->
case all_metrics_are_global() of
true -> global;
false -> make_host_type_name(HostType)
false -> get_host_type_prefix(HostType)
end.

pick_by_all_metrics_are_global(WhenGlobal, WhenNot) ->
Expand Down Expand Up @@ -463,11 +484,6 @@ subscribe_to_all(Reporter, Interval) ->
HostTypePrefixes = pick_by_all_metrics_are_global([], ?ALL_HOST_TYPES),
lists:foreach(
fun(Prefix) ->
UnspacedPrefix = make_host_type_name(Prefix),
UnspacedPrefix = get_host_type_prefix(Prefix),
start_metrics_subscriptions(Reporter, [UnspacedPrefix], Interval)
end, [global | HostTypePrefixes]).

make_host_type_name(HT) when is_atom(HT) ->
HT;
make_host_type_name(HT) when is_binary(HT) ->
binary:replace(HT, <<" ">>, <<"_">>, [global]).

0 comments on commit a7453fa

Please sign in to comment.