Skip to content

Commit 22e7f02

Browse files
Merge pull request #10989 from rabbitmq/md/boot-avoid-module-scan
Avoid loading all modules during boot
2 parents f6d4fc2 + 692b6f6 commit 22e7f02

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

deps/rabbit/src/rabbit.erl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,20 @@ do_run_postlaunch_phase(Plugins) ->
10141014
?LOG_DEBUG(""),
10151015
?LOG_DEBUG("== Plugins (postlaunch phase) =="),
10161016

1017+
%% Before loading plugins, set the prometheus collectors and
1018+
%% instrumenters to the empty list. By default, prometheus will attempt
1019+
%% to find all implementers of its collector and instrumenter
1020+
%% behaviours by scanning all available modules during application
1021+
%% start. This can take significant time (on the order of seconds) due
1022+
%% to the large number of modules available.
1023+
%%
1024+
%% * Collectors: the `rabbitmq_prometheus' plugin explicitly registers
1025+
%% all collectors.
1026+
%% * Instrumenters: no instrumenters are used.
1027+
_ = application:load(prometheus),
1028+
ok = application:set_env(prometheus, collectors, [default]),
1029+
ok = application:set_env(prometheus, instrumenters, []),
1030+
10171031
%% However, we want to run their boot steps and actually start
10181032
%% them one by one, to ensure a dependency is fully started
10191033
%% before a plugin which depends on it gets a chance to start.

deps/rabbitmq_management/src/rabbit_mgmt_dispatcher.erl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,18 @@ build_module_routes(Ignore) ->
6363
Routes = [Module:dispatcher() || Module <- modules(Ignore)],
6464
[{"/api" ++ Path, Mod, Args} || {Path, Mod, Args} <- lists:append(Routes)].
6565

66-
modules(IgnoreApps) ->
67-
[Module || {App, Module, Behaviours} <-
66+
modules(IgnoreApps0) ->
67+
Apps0 = rabbit_misc:rabbitmq_related_apps(),
68+
Apps = case IgnoreApps0 of
69+
[] ->
70+
Apps0;
71+
_ ->
72+
IgnoreApps = sets:from_list(IgnoreApps0, [{version, 2}]),
73+
lists:filter(
74+
fun(App) -> not sets:is_element(App, IgnoreApps) end,
75+
Apps0)
76+
end,
77+
[Module || {_App, Module, Behaviours} <-
6878
%% Sort rabbitmq_management modules first. This is
6979
%% a microoptimization because most files belong to
7080
%% this application. Making it first avoids several
@@ -76,8 +86,7 @@ modules(IgnoreApps) ->
7686
(_, {rabbitmq_management, _, _}) -> false;
7787
({A, _, _}, {B, _, _}) -> A =< B
7888
end,
79-
rabbit_misc:all_module_attributes(behaviour)),
80-
not lists:member(App, IgnoreApps),
89+
rabbit_misc:module_attributes_from_apps(behaviour, Apps)),
8190
lists:member(rabbit_mgmt_extension, Behaviours)].
8291

8392
module_app(Module) ->

0 commit comments

Comments
 (0)