1 |
|
-module(mongoose_instrument_probe). |
2 |
|
|
3 |
|
-export([start_probe_timer/3, call/3]). |
4 |
|
|
5 |
|
-callback probe(mongoose_instrument:event_name(), mongoose_instrument:labels()) -> |
6 |
|
mongoose_instrument:measurements(). |
7 |
|
|
8 |
|
-ignore_xref([call/3]). |
9 |
|
|
10 |
|
-spec start_probe_timer(mongoose_instrument:event_name(), |
11 |
|
mongoose_instrument:labels(), |
12 |
|
mongoose_instrument:probe_config()) -> timer:tref(). |
13 |
|
start_probe_timer(EventName, Labels, #{module := Module} = ProbeConfig) -> |
14 |
29 |
Interval = timer:seconds(get_probe_interval(ProbeConfig)), |
15 |
|
%% TODO: when dropping support for OTP25, consider changing this to apply_repeatedly |
16 |
29 |
{ok, TRef} = timer:apply_interval(Interval, ?MODULE, call, [Module, EventName, Labels]), |
17 |
29 |
TRef. |
18 |
|
|
19 |
|
call(ProbeMod, EventName, Labels) -> |
20 |
71 |
case safely:apply_and_log(ProbeMod, probe, [EventName, Labels], |
21 |
|
#{what => probe_failed, probe_mod => ProbeMod, |
22 |
|
event_name => EventName, labels => Labels}) of |
23 |
|
{exception, _} -> |
24 |
:-( |
ok; % Already logged |
25 |
|
Measurements = #{} -> |
26 |
71 |
mongoose_instrument:execute(EventName, Labels, Measurements) |
27 |
|
end. |
28 |
|
|
29 |
|
-spec get_probe_interval(mongoose_instrument:probe_config()) -> pos_integer(). |
30 |
|
get_probe_interval(#{interval := Interval}) when is_integer(Interval), Interval > 0 -> |
31 |
:-( |
Interval; |
32 |
|
get_probe_interval(#{}) -> |
33 |
29 |
mongoose_config:get_opt([instrumentation, probe_interval]). |