Skip to content

Commit

Permalink
Add instrumentation callback for modules
Browse files Browse the repository at this point in the history
It returns a list of instrument specs, just like hooks/1 returns hooks.

Instrumentation is set up before hooks, and torn down after them.
  • Loading branch information
chrzaszcz committed Feb 15, 2024
1 parent 93368c8 commit 9302206
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/gen_mod.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
-callback hooks(HostType :: host_type()) -> gen_hook:hook_list().
-callback supported_features() -> [module_feature()].
-callback config_spec() -> mongoose_config_spec:config_section().
-callback instrumentation(host_type()) -> [mongoose_instrument:spec()].

%% Optional callback specifying module dependencies.
%% The dependent module can specify parameters with which the dependee should be
Expand All @@ -86,7 +87,7 @@
%% function).
-callback deps(host_type(), module_opts()) -> gen_mod_deps:deps().

-optional_callbacks([hooks/1, config_spec/0, supported_features/0, deps/2]).
-optional_callbacks([hooks/1, config_spec/0, supported_features/0, instrumentation/1, deps/2]).

%% @doc This function should be called by mongoose_modules only.
%% To start a new module at runtime, use mongoose_modules:ensure_module/3 instead.
Expand All @@ -101,6 +102,7 @@ start_module_for_host_type(HostType, Module, Opts) ->
lists:map(fun mongoose_service:assert_loaded/1,
get_required_services(HostType, Module, Opts)),
check_dynamic_domains_support(HostType, Module),
run_for_instrumentation(HostType, fun mongoose_instrument:set_up/1, Module),
Res = Module:start(HostType, Opts),
run_for_hooks(HostType, fun gen_hook:add_handlers/1, Module),
{links, LinksAfter} = erlang:process_info(self(), links),
Expand Down Expand Up @@ -152,6 +154,12 @@ run_for_hooks(HostType, Fun, Module) ->
false -> ok
end.

run_for_instrumentation(HostType, Fun, Module) ->
case erlang:function_exported(Module, instrumentation, 1) of
true -> Fun(Module:instrumentation(HostType));
false -> ok
end.

check_dynamic_domains_support(HostType, Module) ->
case lists:member(HostType, ?MYHOSTS) of
true -> ok;
Expand Down Expand Up @@ -197,7 +205,7 @@ stop_module_for_host_type(HostType, Module) ->
{wait, Process} ->
wait_for_process(Process);
_ ->
ok
run_for_instrumentation(HostType, fun mongoose_instrument:tear_down/1, Module)
catch Class:Reason:Stacktrace ->
?LOG_ERROR(#{what => module_stopping_failed,
host_type => HostType, stop_module => Module,
Expand Down

0 comments on commit 9302206

Please sign in to comment.