./ct_report/coverage/mongoose_instrument.COVER.html

1 -module(mongoose_instrument).
2
3 -behaviour(gen_server).
4
5 %% API
6 -export([config_spec/0,
7 start_link/0, persist/0,
8 set_up/1, set_up/3,
9 tear_down/1, tear_down/2,
10 span/4, span/5,
11 execute/3]).
12
13 %% Test API
14 -export([add_handler/2, remove_handler/1]).
15
16 %% gen_server callbacks
17 -export([init/1, handle_call/3, handle_cast/2, code_change/3, handle_info/2, terminate/2]).
18
19 -ignore_xref([start_link/0, set_up/3, tear_down/2, span/4, add_handler/2, remove_handler/1]).
20
21 -include("mongoose.hrl").
22 -include("mongoose_config_spec.hrl").
23
24 -type event_name() :: atom().
25 -type labels() :: #{host_type => mongooseim:host_type()}. % to be extended
26 -type metrics() :: #{atom() => spiral | histogram}. % to be extended
27 -type measurements() :: #{atom() => term()}.
28 -type spec() :: {event_name(), labels(), config()}.
29 -type config() :: #{metrics => metrics()}. % to be extended
30 -type handler_key() :: atom(). % key in the `instrumentation' section of the config file
31 -type handler_fun() :: fun((event_name(), labels(), config(), measurements()) -> any()).
32 -type handlers() :: {[handler_fun()], config()}.
33 -type execution_time() :: integer().
34 -type measure_fun(Result) :: fun((execution_time(), Result) -> measurements()).
35
36 -callback config_spec() -> mongoose_config_spec:config_section().
37 -callback set_up(event_name(), labels(), config()) -> boolean().
38 -callback handle_event(event_name(), labels(), config(), measurements()) -> any().
39
40 -optional_callbacks([config_spec/0]).
41
42 -export_type([event_name/0, labels/0, config/0, measurements/0, spec/0, handlers/0]).
43
44 %% API
45
46 %% @doc Specifies the `instrumentation' section of the config file
47 -spec config_spec() -> mongoose_config_spec:config_section().
48 config_spec() ->
49 93 Items = [{atom_to_binary(Key), config_spec(Key)} || Key <- all_handler_keys()],
50 93 #section{items = maps:from_list(Items),
51 wrap = global_config,
52 include = always}.
53
54 -spec start_link() -> gen_server:start_ret().
55 start_link() ->
56 93 gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
57
58 %% @doc Saves the state to a persistent term, improving performance of `execute' and `span'.
59 %% On the other hand, future calls to `set_up' or `tear_down' will update the persistent term,
60 %% which makes them less performant.
61 %% You should call this function only once - after the initial setup, but before handling any load.
62 -spec persist() -> ok.
63 persist() ->
64 93 gen_server:call(?MODULE, persist).
65
66 %% @doc Sets up instrumentation for multiple events.
67 %% @see set_up/3
68 -spec set_up([spec()]) -> ok.
69 set_up(Specs) ->
70 252 lists:foreach(fun({EventName, Labels, Config}) -> set_up(EventName, Labels, Config) end, Specs).
71
72 %% @doc Tears down instrumentation for multiple events.
73 %% @see tear_down/2
74 -spec tear_down([spec()]) -> ok.
75 tear_down(Specs) ->
76 252 lists:foreach(fun({EventName, Labels, _Config}) -> tear_down(EventName, Labels) end, Specs).
77
78 %% @doc Sets up instrumentation for an event identified by `EventName' and `Labels'
79 %% according to `Config'. Fails if the event is already registered, or if the keys of `Labels'
80 %% are different than for already registered events with `EventName'.
81 -spec set_up(event_name(), labels(), config()) -> ok.
82 set_up(EventName, Labels, Config) ->
83 1446 case gen_server:call(?MODULE, {set_up, EventName, Labels, Config}) of
84 1446 ok -> ok;
85
:-(
{error, ErrorMap} -> error(ErrorMap)
86 end.
87
88 %% @doc Tears down instrumentation for an event identified by `EventName' and `Labels'.
89 %% This operation is idempotent.
90 -spec tear_down(event_name(), labels()) -> ok.
91 tear_down(EventName, Labels) ->
92 1446 gen_server:call(?MODULE, {tear_down, EventName, Labels}).
93
94 %% @doc Calls `F', measuring its result with `MeasureF', and calls attached event handlers.
95 %% @see span/5
96 -spec span(event_name(), labels(), fun(() -> Result), measure_fun(Result)) -> Result.
97 span(EventName, Labels, F, MeasureF) ->
98
:-(
span(EventName, Labels, F, [], MeasureF).
99
100 %% @doc Calls `F' with `Args', measuring its execution time.
101 %% The time and the result are passed to `MeasureF', which returns measurements.
102 %% The measurements are then passed to all handlers attached to
103 %% the event identified by `EventName' and `Labels'.
104 %% Fails without calling `F' if the event is not registered.
105 -spec span(event_name(), labels(), fun((...) -> Result), list(), measure_fun(Result)) -> Result.
106 span(EventName, Labels, F, Args, MeasureF) ->
107 13208 Handlers = get_handlers(EventName, Labels),
108 13208 {Time, Result} = timer:tc(F, Args),
109 13208 handle_event(EventName, Labels, MeasureF(Time, Result), Handlers),
110 13208 Result.
111
112 %% @doc Executes all handlers attached to the event identified by `EventName' and `Labels',
113 %% passing `Measurements' to them. Fails if the event is not registered.
114 -spec execute(event_name(), labels(), measurements()) -> ok.
115 execute(EventName, Labels, Measurements) ->
116 1613 Handlers = get_handlers(EventName, Labels),
117 1613 handle_event(EventName, Labels, Measurements, Handlers).
118
119 %% Test API
120
121 -spec add_handler(handler_key(), mongoose_config:value()) -> ok.
122 add_handler(Key, ConfigVal) ->
123 1 case gen_server:call(?MODULE, {add_handler, Key, ConfigVal}) of
124 1 ok -> ok;
125
:-(
{error, ErrorMap} -> error(ErrorMap)
126 end.
127
128 -spec remove_handler(handler_key()) -> ok.
129 remove_handler(Key) ->
130 1 case gen_server:call(?MODULE, {remove_handler, Key}) of
131 1 ok -> ok;
132
:-(
{error, ErrorMap} -> error(ErrorMap)
133 end.
134
135 %% gen_server callbacks
136
137 -type state() :: #{event_name() => #{labels() => handlers()}}.
138
139 -spec init([]) -> {ok, state()}.
140 init([]) ->
141 93 erlang:process_flag(trap_exit, true), % Make sure that terminate is called
142 93 persistent_term:erase(?MODULE), % Prevent inconsistency when restarted after a kill
143 93 {ok, #{}}.
144
145 -spec handle_call(any(), gen_server:from(), state()) ->
146 {reply, ok | {ok, handlers()} | {error, map()}, state()}.
147 handle_call({set_up, EventName, Labels, Config}, _From, State) ->
148 1446 case set_up_and_register(EventName, Labels, Config, State) of
149 {error, _} = Error ->
150
:-(
{reply, Error, State};
151 NewState = #{} ->
152 1446 update_if_persisted(State, NewState),
153 1446 {reply, ok, NewState}
154 end;
155 handle_call({tear_down, EventName, Labels}, _From, State) ->
156 1446 NewState = deregister(EventName, Labels, State),
157 1446 update_if_persisted(State, NewState),
158 1446 {reply, ok, NewState};
159 handle_call({add_handler, Key, ConfigVal}, _From, State) ->
160 1 case mongoose_config:lookup_opt([instrumentation, Key]) of
161 {error, not_found} ->
162 1 mongoose_config:set_opt([instrumentation, Key], ConfigVal),
163 1 NewState = update_handlers(State, [], [handler_module(Key)]),
164 1 update_if_persisted(State, NewState),
165 1 {reply, ok, NewState};
166 {ok, ExistingConfig} ->
167
:-(
{reply, {error, #{what => handler_already_configured, handler_key => Key,
168 existing_config => ExistingConfig}},
169 State}
170 end;
171 handle_call({remove_handler, Key}, _From, State) ->
172 1 case mongoose_config:lookup_opt([instrumentation, Key]) of
173 {error, not_found} ->
174
:-(
{reply, {error, #{what => handler_not_configured, handler_key => Key}}, State};
175 {ok, _} ->
176 1 mongoose_config:unset_opt([instrumentation, Key]),
177 1 NewState = update_handlers(State, [handler_module(Key)], []),
178 1 update_if_persisted(State, NewState),
179 1 {reply, ok, NewState}
180 end;
181 handle_call(persist, _From, State) ->
182 93 persistent_term:put(?MODULE, State),
183 93 {reply, ok, State};
184 handle_call({lookup, EventName, Labels}, _From, State) ->
185
:-(
{reply, lookup(EventName, Labels, State), State};
186 handle_call(Request, From, State) ->
187
:-(
?UNEXPECTED_CALL(Request, From),
188
:-(
{reply, {error, #{what => unexpected_call, request => Request}}, State}.
189
190 -spec handle_cast(any(), state()) -> {noreply, state()}.
191 handle_cast(Msg, State) ->
192
:-(
?UNEXPECTED_CAST(Msg),
193
:-(
{noreply, State}.
194
195 -spec handle_info(any(), state()) -> {noreply, state()}.
196 handle_info(Info, State) ->
197
:-(
?UNEXPECTED_INFO(Info),
198
:-(
{noreply, State}.
199
200 -spec terminate(any(), state()) -> ok.
201 terminate(_Reason, _State) ->
202 93 persistent_term:erase(?MODULE),
203 93 ok.
204
205 -spec code_change(any(), state(), any()) -> {ok, state()}.
206 code_change(_OldVsn, State, _Extra) ->
207
:-(
{ok, State}.
208
209 %% Internal functions
210
211 -spec update_if_persisted(state(), state()) -> ok.
212 update_if_persisted(State, NewState) ->
213 2894 try persistent_term:get(?MODULE) of
214 2894 State -> persistent_term:put(?MODULE, NewState)
215 catch
216
:-(
error:badarg -> ok
217 end.
218
219 -spec set_up_and_register(event_name(), labels(), config(), state()) -> state() | {error, map()}.
220 set_up_and_register(EventName, Labels, Config, State) ->
221 1446 LabelKeys = label_keys(Labels),
222 1446 case State of
223 #{EventName := #{Labels := _}} ->
224
:-(
{error, #{what => event_already_registered,
225 event_name => EventName, labels => Labels}};
226 #{EventName := HandlerMap} ->
227
:-(
{ExistingLabels, _, _} = maps:next(maps:iterator(HandlerMap)),
228
:-(
case label_keys(ExistingLabels) of
229 LabelKeys ->
230
:-(
Handlers = do_set_up(EventName, Labels, Config),
231
:-(
State#{EventName := HandlerMap#{Labels => Handlers}};
232 ExistingKeys ->
233
:-(
{error, #{what => inconsistent_labels,
234 event_name => EventName, labels => Labels,
235 existing_label_keys => ExistingKeys}}
236 end;
237 #{} ->
238 1446 Handlers = do_set_up(EventName, Labels, Config),
239 1446 State#{EventName => #{Labels => Handlers}}
240 end.
241
242 -spec do_set_up(event_name(), labels(), config()) -> handlers().
243 do_set_up(EventName, Labels, Config) ->
244 1446 HandlerFuns = set_up_handlers(EventName, Labels, Config, handler_modules()),
245 1446 {HandlerFuns, Config}.
246
247 -spec update_handlers(state(), [module()], [module()]) -> state().
248 update_handlers(State, ToRemove, ToAdd) ->
249 2 maps:map(fun(EventName, HandlerMap) ->
250
:-(
maps:map(fun(Labels, Handlers) ->
251
:-(
update_event_handlers(EventName, Labels, Handlers,
252 ToRemove, ToAdd)
253 end, HandlerMap)
254 end, State).
255
256 -spec update_event_handlers(event_name(), labels(), handlers(), [module()], [module()]) ->
257 handlers().
258 update_event_handlers(EventName, Labels, {HandlerFuns, Config}, ToRemove, ToAdd) ->
259
:-(
FunsToRemove = modules_to_funs(ToRemove),
260
:-(
FunsToAdd = set_up_handlers(EventName, Labels, Config, ToAdd),
261
:-(
HandlerFuns = HandlerFuns -- FunsToAdd, % sanity check to prevent duplicates
262
:-(
{(HandlerFuns -- FunsToRemove) ++ FunsToAdd, Config}.
263
264 -spec set_up_handlers(event_name(), labels(), config(), [module()]) -> [handler_fun()].
265 set_up_handlers(EventName, Labels, Config, Modules) ->
266 1446 UsedModules = lists:filter(fun(Mod) -> Mod:set_up(EventName, Labels, Config) end, Modules),
267 1446 modules_to_funs(UsedModules).
268
269 -spec deregister(event_name(), labels(), state()) -> state().
270 deregister(EventName, Labels, State) ->
271 1446 case State of
272 #{EventName := HandlerMap} ->
273 1446 case maps:remove(Labels, HandlerMap) of
274 Empty when Empty =:= #{} ->
275 1446 maps:remove(EventName, State);
276 NewHandlerMap ->
277
:-(
State#{EventName := NewHandlerMap}
278 end;
279 #{} ->
280
:-(
State
281 end.
282
283 -spec lookup(event_name(), labels()) -> {ok, handlers()} | {error, map()}.
284 lookup(EventName, Labels) ->
285 14821 try persistent_term:get(?MODULE) of
286 State ->
287 14821 lookup(EventName, Labels, State)
288 catch
289 %% Although persist/0 should be called before handling traffic,
290 %% some instrumented events might happen before that, and they shouldn't fail.
291 error:badarg ->
292
:-(
?LOG_INFO(#{what => mongoose_instrument_lookup_without_persistent_term,
293
:-(
event_name => EventName, labels => Labels}),
294
:-(
gen_server:call(?MODULE, {lookup, EventName, Labels})
295 end.
296
297 -spec lookup(event_name(), labels(), state()) -> {ok, handlers()} | {error, map()}.
298 lookup(EventName, Labels, State) ->
299 14821 case State of
300 #{EventName := #{Labels := Handlers}} ->
301 14821 {ok, Handlers};
302 #{} ->
303
:-(
{error, #{what => event_not_registered, event_name => EventName, labels => Labels}}
304 end.
305
306 -spec label_keys(labels()) -> [atom()].
307 label_keys(Labels) ->
308 1446 lists:sort(maps:keys(Labels)).
309
310 -spec get_handlers(event_name(), labels()) -> handlers().
311 get_handlers(EventName, Labels) ->
312 14821 case lookup(EventName, Labels) of
313 14821 {ok, Handlers} -> Handlers;
314
:-(
{error, ErrorMap} -> error(ErrorMap)
315 end.
316
317 -spec handle_event(event_name(), labels(), measurements(), handlers()) -> ok.
318 handle_event(Event, Labels, Measurements, {EventHandlers, Config}) ->
319 14821 lists:foreach(fun(Handler) -> Handler(Event, Labels, Config, Measurements) end, EventHandlers).
320
321 -spec modules_to_funs([module()]) -> [handler_fun()].
322 modules_to_funs(Modules) ->
323 1446 [fun Module:handle_event/4 || Module <- Modules].
324
325 -spec handler_modules() -> [module()].
326 handler_modules() ->
327 1446 [handler_module(Key) || Key <- maps:keys(mongoose_config:get_opt(instrumentation))].
328
329 -spec handler_module(handler_key()) -> module().
330 handler_module(Key) ->
331 5717 list_to_existing_atom("mongoose_instrument_" ++ atom_to_list(Key)).
332
333 -spec config_spec(handler_key()) -> mongoose_config_spec:config_section().
334 config_spec(Key) ->
335 279 Module = handler_module(Key),
336 279 case mongoose_lib:is_exported(Module, config_spec, 0) of
337 93 true -> Module:config_spec();
338 186 false -> #section{}
339 end.
340
341 -spec all_handler_keys() -> [handler_key()].
342 all_handler_keys() ->
343 93 [prometheus, exometer, log].
Line Hits Source