diff --git a/src/gen_mod.erl b/src/gen_mod.erl index 4263d8719c..30a79bfcd7 100644 --- a/src/gen_mod.erl +++ b/src/gen_mod.erl @@ -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 @@ -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. @@ -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), @@ -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; @@ -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,