-
Notifications
You must be signed in to change notification settings - Fork 428
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
Changes from 3 commits
0eeac5d
41e2b88
79742d3
d9c72a3
2bf64f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
remove_host_type_metrics/1, get_report_interval/0, | ||
sample_metric/1]). | ||
|
||
-define(PREFIXES, {?MODULE, prefixes}). | ||
-define(DEFAULT_REPORT_INTERVAL, 60000). %%60s | ||
|
||
-type use_or_skip() :: use | skip. | ||
|
@@ -62,6 +63,7 @@ | |
|
||
-spec init() -> ok. | ||
init() -> | ||
prepare_prefixes(), | ||
create_global_metrics(), | ||
lists:foreach( | ||
fun(HostType) -> | ||
|
@@ -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() -> | ||
|
@@ -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) -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this atom always There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I think it is always |
||
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) -> | ||
|
@@ -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]). |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🤷🏽 🤔