-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for probes, which are called periodically
- New bahaviour: mongoose_instrument_probe - Use timer:apply_interval/4 to call Module:probe/3 There is timer:apply_periodically in OTP 26, which protects against executing the function in parallel, but we need to drop OTP 25 first. - Store probe timers in the state - The provided probe function returns measurements, which are then passed to mongoose_instrument:execute/4 - Failing probes cause error logs, but are not removed to allow recovery from transient issues. - Probe interval can be set per event, and is 15 seconds by default. Also: add missing 'loglevel' key to the config() type. Add a module allowing periodic execution of instrumentation events
- Loading branch information
Showing
2 changed files
with
123 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
-module(mongoose_instrument_probe). | ||
|
||
-export([start_probe_timer/3, call/3]). | ||
|
||
-callback probe(mongoose_instrument:event_name(), mongoose_instrument:labels()) -> | ||
mongoose_instrument:measurements(). | ||
|
||
-ignore_xref([call/3]). | ||
|
||
-spec start_probe_timer(mongoose_instrument:event_name(), | ||
mongoose_instrument:labels(), | ||
mongoose_instrument:probe_config()) -> timer:tref(). | ||
start_probe_timer(EventName, Labels, #{module := Module} = ProbeConfig) -> | ||
Interval = timer:seconds(get_probe_interval(ProbeConfig)), | ||
%% TODO: when dropping support for OTP25, consider changing this to apply_repeatedly | ||
{ok, TRef} = timer:apply_interval(Interval, ?MODULE, call, [Module, EventName, Labels]), | ||
TRef. | ||
|
||
call(ProbeMod, EventName, Labels) -> | ||
case safely:apply_and_log(ProbeMod, probe, [EventName, Labels], | ||
#{what => probe_failed, probe_mod => ProbeMod, | ||
event_name => EventName, labels => Labels}) of | ||
{exception, _} -> | ||
ok; % Already logged | ||
Measurements = #{} -> | ||
mongoose_instrument:execute(EventName, Labels, Measurements) | ||
end. | ||
|
||
-spec get_probe_interval(mongoose_instrument:probe_config()) -> pos_integer(). | ||
get_probe_interval(#{interval := Interval}) when is_integer(Interval), Interval > 0 -> | ||
Interval; | ||
get_probe_interval(#{}) -> | ||
mongoose_config:get_opt([instrumentation, probe_interval]). |