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

Pre-build metric prefixes and fetch-or-rebuild when needed #3649

Merged
merged 5 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 2 additions & 4 deletions big_tests/tests/metrics_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ get_counter_value(CounterName) ->
get_counter_value(domain_helper:host_type(mim), CounterName).

get_counter_value(HostType, Metric) ->
HostTypeName = make_host_type_name(HostType),
case rpc(mim(), mongoose_metrics, get_metric_value, [HostTypeName, Metric]) of
case rpc(mim(), mongoose_metrics, get_metric_value, [HostType, Metric]) of
{ok, [{count, Total}, {one, _}]} ->
{value, Total};
{ok, [{value, Value} | _]} when is_integer(Value) ->
Expand All @@ -34,8 +33,7 @@ assert_counter(Value, CounterName) ->
assert_counter(domain_helper:host_type(mim), Value, CounterName).

assert_counter(HostType, Value, CounterName) ->
HostTypeName = make_host_type_name(HostType),
{value, Value} = get_counter_value(HostTypeName, CounterName).
{value, Value} = get_counter_value(HostType, CounterName).

-spec prepare_by_all_metrics_are_global(Config :: list(), UseAllMetricsAreGlobal :: boolean()) ->
list().
Expand Down
38 changes: 29 additions & 9 deletions src/metrics/mongoose_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
remove_host_type_metrics/1, get_report_interval/0,
sample_metric/1]).

-define(PREFIXES, {?MODULE, prefixes}).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think an atom mongoose_metrics_prefixes would be marginally faster but still faster.
Unless to have a tuple is some kind of convention to follow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, might be faster, but for once I didn't think about that, I kinda assumed from somewhere we have that convention 🤷🏽 🤔

-define(DEFAULT_REPORT_INTERVAL, 60000). %%60s

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

-spec init() -> ok.
init() ->
prepare_prefixes(),
create_global_metrics(),
lists:foreach(
fun(HostType) ->
Expand Down Expand Up @@ -138,7 +140,7 @@ sample_metric(Metric) ->
exometer:sample(Metric).

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 @@ -182,24 +184,47 @@ 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() ->
Prebuilt = maps:from_list([begin
Prefix = make_host_type_prefix(HT),
{Prefix, Prefix}
end || HT <- ?ALL_HOST_TYPES ]),
Prefixes = maps:from_list([ {HT, make_host_type_prefix(HT)}
|| HT <- ?ALL_HOST_TYPES ]),
persistent_term:put(?PREFIXES, maps:merge(Prebuilt, Prefixes)).

-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) ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this atom always global? If so, maybe we could match on it explicitly and add a type spec with mongooseim:host_type_or_global()? It would help with type checking and understanding the code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I think it is always global indeed, will double-check and try to fix 👍🏽

HostType;
get_host_type_prefix(HostType) when is_binary(HostType) ->
case persistent_term:get(?PREFIXES, #{}) of
#{HostType := HostTypePrefix} -> HostTypePrefix;
#{} -> 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 @@ -469,11 +494,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]).