1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2014 Erlang Solutions Ltd. |
3 |
|
%% |
4 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
%% you may not use this file except in compliance with the License. |
6 |
|
%% You may obtain a copy of the License at |
7 |
|
%% |
8 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
%% |
10 |
|
%% Unless required by applicable law or agreed to in writing, software |
11 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
%% See the License for the specific language governing permissions and |
14 |
|
%% limitations under the License. |
15 |
|
%%============================================================================== |
16 |
|
-module(mongoose_metrics). |
17 |
|
|
18 |
|
-include("mongoose.hrl"). |
19 |
|
-include("mongoose_metrics_definitions.hrl"). |
20 |
|
|
21 |
|
%% API |
22 |
|
-export([init/0, |
23 |
|
init_mongooseim_metrics/0, |
24 |
|
create_generic_hook_metric/2, |
25 |
|
create_probe_metric/3, |
26 |
|
ensure_db_pool_metric/1, |
27 |
|
update/3, |
28 |
|
ensure_metric/3, |
29 |
|
ensure_subscribed_metric/3, |
30 |
|
get_metric_value/1, |
31 |
|
get_metric_values/1, |
32 |
|
get_metric_value/2, |
33 |
|
sample_metric/1, |
34 |
|
get_host_type_metric_names/1, |
35 |
|
get_global_metric_names/0, |
36 |
|
get_aggregated_values/1, |
37 |
|
increment_generic_hook_metric/2, |
38 |
|
get_rdbms_data_stats/0, |
39 |
|
get_rdbms_data_stats/1, |
40 |
|
get_dist_data_stats/0, |
41 |
|
get_up_time/0, |
42 |
|
get_mnesia_running_db_nodes_count/0, |
43 |
|
remove_host_type_metrics/1, |
44 |
|
remove_all_metrics/0, |
45 |
|
get_report_interval/0, |
46 |
|
get_host_type_prefix/1 |
47 |
|
]). |
48 |
|
|
49 |
|
-ignore_xref([get_dist_data_stats/0, get_mnesia_running_db_nodes_count/0, |
50 |
|
get_rdbms_data_stats/0, get_rdbms_data_stats/1, get_up_time/0, |
51 |
|
remove_host_type_metrics/1, get_report_interval/0, |
52 |
|
sample_metric/1, get_metric_value/1]). |
53 |
|
|
54 |
|
-define(PREFIXES, mongoose_metrics_prefixes). |
55 |
|
-define(DEFAULT_REPORT_INTERVAL, 60000). %%60s |
56 |
|
|
57 |
|
-type use_or_skip() :: use | skip. |
58 |
|
-type hook_name() :: atom(). |
59 |
|
-type metric_name() :: atom() | list(atom() | binary()). |
60 |
|
-type short_metric_type() :: spiral | histogram | counter | gauge. |
61 |
|
-type metric_type() :: tuple() | short_metric_type(). |
62 |
|
|
63 |
|
%% --------------------------------------------------------------------- |
64 |
|
%% API |
65 |
|
%% --------------------------------------------------------------------- |
66 |
|
|
67 |
|
-spec init() -> ok. |
68 |
|
init() -> |
69 |
53 |
prepare_prefixes(), |
70 |
53 |
create_vm_metrics(), |
71 |
53 |
create_global_metrics(?GLOBAL_COUNTERS), |
72 |
53 |
create_data_metrics(), |
73 |
53 |
create_host_type_metrics(). |
74 |
|
|
75 |
|
-spec init_mongooseim_metrics() -> ok. |
76 |
|
init_mongooseim_metrics() -> |
77 |
53 |
create_host_type_hook_metrics(), |
78 |
53 |
create_global_metrics(?MNESIA_COUNTERS), |
79 |
53 |
init_subscriptions(). |
80 |
|
|
81 |
|
init_subscriptions() -> |
82 |
53 |
Reporters = exometer_report:list_reporters(), |
83 |
53 |
lists:foreach( |
84 |
|
fun({Name, _ReporterPid}) -> |
85 |
:-( |
Interval = get_report_interval(), |
86 |
:-( |
subscribe_to_all(Name, Interval) |
87 |
|
end, Reporters). |
88 |
|
|
89 |
|
-spec create_generic_hook_metric(mongooseim:host_type(), atom()) -> |
90 |
|
ok | {ok, already_present} | {error, any()}. |
91 |
|
create_generic_hook_metric(HostType, Hook) -> |
92 |
18328 |
UseOrSkip = filter_hook(Hook), |
93 |
18328 |
do_create_generic_hook_metric(HostType, Hook, UseOrSkip). |
94 |
|
|
95 |
|
-spec create_probe_metric(mongooseim:host_type_or_global(), atom(), module()) -> |
96 |
|
ok | {ok, already_present} | {error, any()}. |
97 |
|
create_probe_metric(HostType, Name, Module) -> |
98 |
159 |
{Metric, Spec} = ?PROBE(Name, Module), |
99 |
159 |
ensure_metric(HostType, Metric, Spec). |
100 |
|
|
101 |
|
% TODO: change to HostType after mongoose_wpool_rdbms |
102 |
|
ensure_db_pool_metric({rdbms, Host, Tag} = Name) -> |
103 |
55 |
ensure_metric(Host, |
104 |
|
[data, rdbms, Tag], |
105 |
|
{function, mongoose_metrics, get_rdbms_data_stats, [[Name]], proplist, |
106 |
|
[workers | ?INET_STATS]}). |
107 |
|
|
108 |
|
-spec update(HostType :: mongooseim:host_type_or_global(), Name :: term() | list(), |
109 |
|
Change :: term()) -> any(). |
110 |
|
update(HostType, Name, Change) when is_list(Name) -> |
111 |
1348223 |
exometer:update(name_by_all_metrics_are_global(HostType, Name), Change); |
112 |
|
update(HostType, Name, Change) -> |
113 |
475663 |
update(HostType, [Name], Change). |
114 |
|
|
115 |
|
-spec ensure_metric(mongooseim:host_type_or_global(), metric_name(), metric_type()) -> |
116 |
|
ok | {ok, already_present} | {error, any()}. |
117 |
|
ensure_metric(HostType, Metric, Type) when is_tuple(Type) -> |
118 |
690 |
ensure_metric(HostType, Metric, Type, element(1, Type)); |
119 |
|
ensure_metric(HostType, Metric, Type) -> |
120 |
50870 |
ensure_metric(HostType, Metric, Type, Type). |
121 |
|
|
122 |
|
get_metric_value(HostType, Name) when is_list(Name) -> |
123 |
262 |
get_metric_value(name_by_all_metrics_are_global(HostType, Name)); |
124 |
|
get_metric_value(HostType, Name) -> |
125 |
256 |
get_metric_value(HostType, [Name]). |
126 |
|
|
127 |
|
get_metric_value(Metric) -> |
128 |
360 |
exometer:get_value(Metric). |
129 |
|
|
130 |
|
get_metric_values(Metric) when is_list(Metric) -> |
131 |
232 |
exometer:get_values(Metric); |
132 |
|
get_metric_values(HostType) -> |
133 |
184 |
exometer:get_values([HostType]). |
134 |
|
|
135 |
|
%% Force update a probe metric |
136 |
|
sample_metric(Metric) -> |
137 |
6 |
exometer:sample(Metric). |
138 |
|
|
139 |
|
get_host_type_metric_names(HostType) -> |
140 |
91 |
HostTypeName = get_host_type_prefix(HostType), |
141 |
91 |
[MetricName || {[_HostTypeName | MetricName], _, _} <- exometer:find_entries([HostTypeName])]. |
142 |
|
|
143 |
|
get_global_metric_names() -> |
144 |
2 |
get_host_type_metric_names(global). |
145 |
|
|
146 |
|
get_aggregated_values(Metric) -> |
147 |
11050 |
exometer:aggregate([{{['_', Metric], '_', '_'}, [], [true]}], [one, count, value]). |
148 |
|
|
149 |
|
-spec increment_generic_hook_metric(mongooseim:host_type_or_global(), atom()) -> ok | {error, any()}. |
150 |
|
increment_generic_hook_metric(HostType, Hook) -> |
151 |
428613 |
UseOrSkip = filter_hook(Hook), |
152 |
428613 |
do_increment_generic_hook_metric(HostType, Hook, UseOrSkip). |
153 |
|
|
154 |
|
get_rdbms_data_stats() -> |
155 |
:-( |
Pools = lists:filter(fun({Type, _Host, _Tag}) -> Type == rdbms end, mongoose_wpool:get_pools()), |
156 |
:-( |
get_rdbms_data_stats(Pools). |
157 |
|
|
158 |
|
get_rdbms_data_stats(Pools) -> |
159 |
130 |
RDBMSWorkers = |
160 |
|
lists:flatmap( |
161 |
|
fun({Type, Host, Tag}) -> |
162 |
130 |
PoolName = mongoose_wpool:make_pool_name(Type, Host, Tag), |
163 |
130 |
Wpool = wpool_pool:find_wpool(PoolName), |
164 |
130 |
PoolSize = wpool_pool:wpool_get(size, Wpool), |
165 |
130 |
[whereis(wpool_pool:worker_name(PoolName, I)) || I <- lists:seq(1, PoolSize)] |
166 |
|
end, |
167 |
|
Pools), |
168 |
|
|
169 |
130 |
get_rdbms_stats(RDBMSWorkers). |
170 |
|
|
171 |
|
get_dist_data_stats() -> |
172 |
130 |
DistStats = [dist_inet_stats(PortOrPid) || {_, PortOrPid} <- erlang:system_info(dist_ctrl)], |
173 |
130 |
[{connections, length(DistStats)} | merge_stats(DistStats)]. |
174 |
|
|
175 |
|
-spec get_up_time() -> {value, integer()}. |
176 |
|
get_up_time() -> |
177 |
130 |
{value, erlang:round(element(1, erlang:statistics(wall_clock))/1000)}. |
178 |
|
|
179 |
|
-spec get_mnesia_running_db_nodes_count() -> {value, non_neg_integer()}. |
180 |
|
get_mnesia_running_db_nodes_count() -> |
181 |
132 |
{value, length(mnesia:system_info(running_db_nodes))}. |
182 |
|
|
183 |
|
remove_host_type_metrics(HostType) -> |
184 |
:-( |
HostTypeName = get_host_type_prefix(HostType), |
185 |
:-( |
lists:foreach(fun remove_metric/1, exometer:find_entries([HostTypeName])). |
186 |
|
|
187 |
|
remove_all_metrics() -> |
188 |
53 |
persistent_term:erase(?PREFIXES), |
189 |
53 |
lists:foreach(fun remove_metric/1, exometer:find_entries([])). |
190 |
|
|
191 |
|
%% --------------------------------------------------------------------- |
192 |
|
%% Internal functions |
193 |
|
%% --------------------------------------------------------------------- |
194 |
|
|
195 |
|
prepare_prefixes() -> |
196 |
53 |
Prebuilt = maps:from_list([begin |
197 |
283 |
Prefix = make_host_type_prefix(HT), |
198 |
283 |
{Prefix, Prefix} |
199 |
53 |
end || HT <- ?ALL_HOST_TYPES ]), |
200 |
53 |
Prefixes = maps:from_list([ {HT, make_host_type_prefix(HT)} |
201 |
53 |
|| HT <- ?ALL_HOST_TYPES ]), |
202 |
53 |
persistent_term:put(?PREFIXES, maps:merge(Prebuilt, Prefixes)). |
203 |
|
|
204 |
|
-spec all_metrics_are_global() -> boolean(). |
205 |
|
all_metrics_are_global() -> |
206 |
1400093 |
mongoose_config:get_opt(all_metrics_are_global). |
207 |
|
|
208 |
|
get_host_type_prefix(global) -> |
209 |
802339 |
global; |
210 |
|
get_host_type_prefix(HostType) when is_binary(HostType) -> |
211 |
576730 |
case persistent_term:get(?PREFIXES, #{}) of |
212 |
576721 |
#{HostType := HostTypePrefix} -> HostTypePrefix; |
213 |
9 |
#{} -> make_host_type_prefix(HostType) |
214 |
|
end. |
215 |
|
|
216 |
|
make_host_type_prefix(HT) when is_binary(HT) -> |
217 |
575 |
binary:replace(HT, <<" ">>, <<"_">>, [global]). |
218 |
|
|
219 |
|
pick_prefix_by_all_metrics_are_global(HostType) -> |
220 |
1400093 |
case all_metrics_are_global() of |
221 |
21115 |
true -> global; |
222 |
1378978 |
false -> get_host_type_prefix(HostType) |
223 |
|
end. |
224 |
|
|
225 |
|
pick_by_all_metrics_are_global(WhenGlobal, WhenNot) -> |
226 |
:-( |
case all_metrics_are_global() of |
227 |
:-( |
true -> WhenGlobal; |
228 |
:-( |
false -> WhenNot |
229 |
|
end. |
230 |
|
|
231 |
|
-spec name_by_all_metrics_are_global(HostType :: mongooseim:host_type_or_global(), |
232 |
|
Name :: list()) -> FinalName :: list(). |
233 |
|
name_by_all_metrics_are_global(HostType, Name) -> |
234 |
1400093 |
[pick_prefix_by_all_metrics_are_global(HostType) | Name]. |
235 |
|
|
236 |
|
get_report_interval() -> |
237 |
358 |
application:get_env(exometer_core, mongooseim_report_interval, |
238 |
|
?DEFAULT_REPORT_INTERVAL). |
239 |
|
|
240 |
|
-spec do_create_generic_hook_metric(HostType :: mongooseim:host_type_or_global(), |
241 |
|
Hook :: hook_name(), |
242 |
|
UseOrSkip :: use_or_skip()) -> |
243 |
|
ok | {ok, already_present} | {error, any()}. |
244 |
|
do_create_generic_hook_metric(_, _, skip) -> |
245 |
6142 |
ok; |
246 |
|
do_create_generic_hook_metric(HostType, Hook, use) -> |
247 |
12186 |
ensure_metric(HostType, Hook, spiral). |
248 |
|
|
249 |
|
-spec do_increment_generic_hook_metric(HostType :: mongooseim:host_type_or_global(), |
250 |
|
Hook :: hook_name(), |
251 |
|
UseOrSkip :: use_or_skip()) -> |
252 |
|
ok | {error, any()}. |
253 |
|
do_increment_generic_hook_metric(_, _, skip) -> |
254 |
230489 |
ok; |
255 |
|
do_increment_generic_hook_metric(HostType, Hook, use) -> |
256 |
198124 |
update(HostType, Hook, 1). |
257 |
|
|
258 |
|
get_rdbms_stats(RDBMSWorkers) -> |
259 |
130 |
RDBMSConnections = [{catch mongoose_rdbms:get_db_info(Pid), Pid} || Pid <- RDBMSWorkers], |
260 |
130 |
Ports = [get_port_from_rdbms_connection(Conn) || Conn <- RDBMSConnections], |
261 |
130 |
PortStats = [inet_stats(Port) || Port <- lists:flatten(Ports)], |
262 |
130 |
[{workers, length(RDBMSConnections)} | merge_stats(PortStats)]. |
263 |
|
|
264 |
|
get_port_from_rdbms_connection({{ok, DB, Pid}, _WorkerPid}) when DB =:= mysql; |
265 |
|
DB =:= pgsql -> |
266 |
650 |
ProcState = sys:get_state(Pid), |
267 |
650 |
get_port_from_proc_state(DB, ProcState); |
268 |
|
get_port_from_rdbms_connection({{ok, odbc, Pid}, WorkerPid}) -> |
269 |
:-( |
Links = element(2, erlang:process_info(Pid, links)) -- [WorkerPid], |
270 |
:-( |
[Port || Port <- Links, is_port(Port), {name, "tcp_inet"} == erlang:port_info(Port, name)]; |
271 |
|
get_port_from_rdbms_connection(_) -> |
272 |
:-( |
undefined. |
273 |
|
|
274 |
|
%% @doc Gets a socket from mysql/epgsql library Gen_server state |
275 |
|
get_port_from_proc_state(mysql, State) -> |
276 |
|
%% -record(state, {server_version, connection_id, socket, sockmod, ssl_opts, |
277 |
|
%% host, port, user, password, log_warnings, |
278 |
|
%% ping_timeout, |
279 |
|
%% query_timeout, query_cache_time, |
280 |
|
%% affected_rows = 0, status = 0, warning_count = 0, insert_id = 0, |
281 |
|
%% transaction_level = 0, ping_ref = undefined, |
282 |
|
%% stmts = dict:new(), query_cache = empty, cap_found_rows = false}). |
283 |
650 |
SockInfo = element(4, State), |
284 |
650 |
get_port_from_sock(SockInfo); |
285 |
|
get_port_from_proc_state(pgsql, State) -> |
286 |
|
%% -record(state, {mod, |
287 |
|
%% sock, |
288 |
|
%% data = <<>>, |
289 |
|
%% backend, |
290 |
|
%% handler, |
291 |
|
%% codec, |
292 |
|
%% queue = queue:new(), |
293 |
|
%% async, |
294 |
|
%% parameters = [], |
295 |
|
%% types = [], |
296 |
|
%% columns = [], |
297 |
|
%% rows = [], |
298 |
|
%% results = [], |
299 |
|
%% batch = [], |
300 |
|
%% sync_required, |
301 |
|
%% txstatus, |
302 |
|
%% complete_status :: undefined | atom() | {atom(), integer()}, |
303 |
|
%% repl_last_received_lsn, |
304 |
|
%% repl_last_flushed_lsn, |
305 |
|
%% repl_last_applied_lsn, |
306 |
|
%% repl_feedback_required, |
307 |
|
%% repl_cbmodule, |
308 |
|
%% repl_cbstate, |
309 |
|
%% repl_receiver}). |
310 |
:-( |
SockInfo = element(3, State), |
311 |
:-( |
get_port_from_sock(SockInfo). |
312 |
|
|
313 |
|
get_port_from_sock({sslsocket, {_, Port, _, _}, _}) -> |
314 |
650 |
Port; |
315 |
|
get_port_from_sock(Port) -> |
316 |
:-( |
Port. |
317 |
|
|
318 |
|
merge_stats(Stats) -> |
319 |
260 |
OrdDict = lists:foldl(fun(Stat, Acc) -> |
320 |
921 |
StatDict = orddict:from_list(Stat), |
321 |
921 |
orddict:merge(fun merge_stats_fun/3, Acc, StatDict) |
322 |
|
end, orddict:from_list(?EMPTY_INET_STATS), Stats), |
323 |
|
|
324 |
260 |
orddict:to_list(OrdDict). |
325 |
|
|
326 |
|
merge_stats_fun(recv_max, V1, V2) -> |
327 |
921 |
erlang:max(V1, V2); |
328 |
|
merge_stats_fun(send_max, V1, V2) -> |
329 |
921 |
erlang:max(V1, V2); |
330 |
|
merge_stats_fun(_, V1, V2) -> |
331 |
4605 |
V1 + V2. |
332 |
|
|
333 |
|
dist_inet_stats(Pid) when is_pid(Pid) -> |
334 |
:-( |
try |
335 |
:-( |
{ok, {sslsocket, FD, _Pids}} = tls_sender:dist_tls_socket(Pid), |
336 |
:-( |
gen_tcp = element(1, FD), |
337 |
:-( |
inet_stats(element(2, FD)) |
338 |
|
catch C:R:S -> |
339 |
:-( |
?LOG_INFO(#{what => dist_inet_stats_failed, class => C, reason => R, stacktrace => S}), |
340 |
:-( |
?EMPTY_INET_STATS |
341 |
|
end; |
342 |
|
dist_inet_stats(Port) -> |
343 |
271 |
inet_stats(Port). |
344 |
|
|
345 |
|
inet_stats(Port) -> |
346 |
921 |
try |
347 |
921 |
{ok, Stats} = inet:getstat(Port, ?INET_STATS), |
348 |
921 |
Stats |
349 |
|
catch C:R:S -> |
350 |
:-( |
?LOG_INFO(#{what => inet_stats_failed, class => C, reason => R, stacktrace => S}), |
351 |
:-( |
?EMPTY_INET_STATS |
352 |
|
end. |
353 |
|
|
354 |
|
remove_metric({Name, _, _}) -> |
355 |
17622 |
exometer_admin:delete_entry(Name). |
356 |
|
|
357 |
|
%% decided whether to use a metric for given hook or not |
358 |
|
-spec filter_hook(hook_name()) -> use_or_skip(). |
359 |
6923 |
filter_hook(sm_register_connection_hook) -> skip; |
360 |
6921 |
filter_hook(sm_remove_connection_hook) -> skip; |
361 |
352 |
filter_hook(auth_failed) -> skip; |
362 |
24133 |
filter_hook(user_send_packet) -> skip; |
363 |
4467 |
filter_hook(user_send_message) -> skip; |
364 |
7333 |
filter_hook(user_send_presence) -> skip; |
365 |
8300 |
filter_hook(user_send_iq) -> skip; |
366 |
41636 |
filter_hook(user_receive_packet) -> skip; |
367 |
12603 |
filter_hook(user_receive_message) -> skip; |
368 |
15562 |
filter_hook(user_receive_presence) -> skip; |
369 |
11879 |
filter_hook(user_receive_iq) -> skip; |
370 |
359 |
filter_hook(xmpp_bounce_message) -> skip; |
371 |
357 |
filter_hook(xmpp_stanza_dropped) -> skip; |
372 |
63265 |
filter_hook(xmpp_send_element) -> skip; |
373 |
474 |
filter_hook(roster_get) -> skip; |
374 |
314 |
filter_hook(roster_set) -> skip; |
375 |
1120 |
filter_hook(roster_push) -> skip; |
376 |
6407 |
filter_hook(register_user) -> skip; |
377 |
6376 |
filter_hook(remove_user) -> skip; |
378 |
345 |
filter_hook(privacy_iq_get) -> skip; |
379 |
422 |
filter_hook(privacy_iq_set) -> skip; |
380 |
3866 |
filter_hook(privacy_check_packet) -> skip; |
381 |
243 |
filter_hook(mam_get_prefs) -> skip; |
382 |
390 |
filter_hook(mam_set_prefs) -> skip; |
383 |
581 |
filter_hook(mam_remove_archive) -> skip; |
384 |
7661 |
filter_hook(mam_archive_message) -> skip; |
385 |
42 |
filter_hook(mam_muc_get_prefs) -> skip; |
386 |
42 |
filter_hook(mam_muc_set_prefs) -> skip; |
387 |
743 |
filter_hook(mam_muc_remove_archive) -> skip; |
388 |
540 |
filter_hook(mam_muc_lookup_messages) -> skip; |
389 |
2567 |
filter_hook(mam_muc_archive_message) -> skip; |
390 |
408 |
filter_hook(mam_muc_flush_messages) -> skip; |
391 |
|
|
392 |
210310 |
filter_hook(_) -> use. |
393 |
|
|
394 |
|
create_global_metrics(Metrics) -> |
395 |
106 |
lists:foreach(fun({Metric, Spec}) -> ensure_metric(global, Metric, Spec) end, Metrics). |
396 |
|
|
397 |
|
create_vm_metrics() -> |
398 |
53 |
lists:foreach(fun({Metric, FunSpec, DataPoints}) -> |
399 |
106 |
FunSpecTuple = list_to_tuple(FunSpec ++ [DataPoints]), |
400 |
106 |
catch ensure_metric(global, Metric, FunSpecTuple) |
401 |
|
end, ?VM_STATS). |
402 |
|
|
403 |
|
-spec create_host_type_metrics() -> ok. |
404 |
|
create_host_type_metrics() -> |
405 |
53 |
lists:foreach(fun create_host_type_metrics/1, ?ALL_HOST_TYPES). |
406 |
|
|
407 |
|
-spec create_host_type_metrics(mongooseim:host_type()) -> 'ok'. |
408 |
|
create_host_type_metrics(HostType) -> |
409 |
283 |
lists:foreach(fun(Name) -> ensure_metric(HostType, Name, spiral) end, ?GENERAL_SPIRALS), |
410 |
283 |
lists:foreach(fun(Name) -> ensure_metric(HostType, Name, histogram) end, ?GENERAL_HISTOGRAMS), |
411 |
283 |
lists:foreach(fun(Name) -> ensure_metric(HostType, Name, counter) end, ?TOTAL_COUNTERS). |
412 |
|
|
413 |
|
-spec create_host_type_hook_metrics() -> ok. |
414 |
|
create_host_type_hook_metrics() -> |
415 |
53 |
lists:foreach(fun create_host_type_hook_metrics/1, ?ALL_HOST_TYPES). |
416 |
|
|
417 |
|
-spec create_host_type_hook_metrics(mongooseim:host_type()) -> 'ok'. |
418 |
|
create_host_type_hook_metrics(HostType) -> |
419 |
283 |
Hooks = mongoose_metrics_hooks:get_hooks(HostType), |
420 |
283 |
gen_hook:add_handlers(Hooks). |
421 |
|
|
422 |
|
ensure_metric(HostType, Metric, Type, ShortType) when is_atom(Metric) -> |
423 |
24157 |
ensure_metric(HostType, [Metric], Type, ShortType); |
424 |
|
|
425 |
|
ensure_metric(HostType, Metric, Type, probe = ShortType) -> |
426 |
349 |
PrefixedMetric = name_by_all_metrics_are_global(HostType, Metric), |
427 |
349 |
{ShortType, Opts} = Type, |
428 |
349 |
case exometer:info(PrefixedMetric, type) of |
429 |
|
undefined -> |
430 |
275 |
ExometerOpts = [{module, mongoose_metrics_probe}, {type, ShortType}] ++ Opts, |
431 |
275 |
do_create_metric(PrefixedMetric, ad_hoc, ExometerOpts); |
432 |
|
_ -> |
433 |
74 |
{ok, already_present} |
434 |
|
end; |
435 |
|
ensure_metric(HostType, Metric, Type, ShortType) when is_list(Metric) -> |
436 |
|
%% the split into ShortType and Type is needed because function metrics are |
437 |
|
%% defined as tuples (that is Type), while exometer:info returns only 'function' |
438 |
51211 |
PrefixedMetric = name_by_all_metrics_are_global(HostType, Metric), |
439 |
51211 |
case exometer:info(PrefixedMetric, type) of |
440 |
|
undefined -> |
441 |
17756 |
do_create_metric(PrefixedMetric, Type, []); |
442 |
33455 |
ShortType -> {ok, already_present} |
443 |
|
end. |
444 |
|
|
445 |
|
%% @doc Creates a metric and subcribes it to the reporters |
446 |
|
-spec ensure_subscribed_metric(HostType :: mongooseim:host_type_or_global(), |
447 |
|
Metric :: metric_name(), |
448 |
|
Type :: metric_type()) -> ok | term(). |
449 |
|
ensure_subscribed_metric(HostType, Metric, Type) -> |
450 |
9321 |
case ensure_metric(HostType, Metric, Type) of |
451 |
|
ok -> |
452 |
48 |
PrefixedMetric = name_by_all_metrics_are_global(HostType, Metric), |
453 |
48 |
Reporters = exometer_report:list_reporters(), |
454 |
48 |
Interval = get_report_interval(), |
455 |
48 |
lists:foreach( |
456 |
|
fun({Reporter, _Pid}) -> |
457 |
:-( |
FullMetric = {PrefixedMetric, Type, []}, |
458 |
:-( |
subscribe_metric(Reporter, FullMetric, Interval) |
459 |
|
end, |
460 |
|
Reporters); |
461 |
|
{ok, already_present} -> |
462 |
9273 |
?LOG_DEBUG(#{what => metric_already_present, |
463 |
9273 |
host_type => HostType, metric => Metric, type => Type}), |
464 |
9273 |
ok; |
465 |
|
Other -> |
466 |
:-( |
?LOG_WARNING(#{what => cannot_create_metric, reason => Other, |
467 |
:-( |
host_type => HostType, metric => Metric,type => Type}), |
468 |
:-( |
Other |
469 |
|
end. |
470 |
|
|
471 |
|
do_create_metric(PrefixedMetric, ExometerType, ExometerOpts) -> |
472 |
18031 |
case catch exometer:new(PrefixedMetric, ExometerType, ExometerOpts) of |
473 |
:-( |
{'EXIT', {exists, _}} -> {ok, already_present}; |
474 |
18031 |
ok -> ok; |
475 |
:-( |
{'EXIT', Error} -> {error, Error} |
476 |
|
end. |
477 |
|
|
478 |
|
create_data_metrics() -> |
479 |
53 |
lists:foreach(fun(Metric) -> ensure_metric(global, Metric, histogram) end, |
480 |
|
?GLOBAL_HISTOGRAMS), |
481 |
53 |
lists:foreach(fun(Metric) -> ensure_metric(global, Metric, spiral) end, |
482 |
|
?GLOBAL_SPIRALS), |
483 |
53 |
lists:foreach(fun({Metric, Spec}) -> ensure_metric(global, Metric, Spec) end, |
484 |
|
?DATA_FUN_METRICS). |
485 |
|
|
486 |
|
start_metrics_subscriptions(Reporter, MetricPrefix, Interval) -> |
487 |
:-( |
[subscribe_metric(Reporter, Metric, Interval) |
488 |
:-( |
|| Metric <- exometer:find_entries(MetricPrefix)]. |
489 |
|
|
490 |
|
subscribe_metric(Reporter, {Name, counter, _}, Interval) -> |
491 |
:-( |
subscribe_verbose(Reporter, Name, [value], Interval); |
492 |
|
subscribe_metric(Reporter, {Name, histogram, _}, Interval) -> |
493 |
:-( |
subscribe_verbose(Reporter, Name, [min, mean, max, median, 95, 99, 999], Interval); |
494 |
|
subscribe_metric(Reporter, {Name, _, _}, Interval) -> |
495 |
:-( |
subscribe_verbose(Reporter, Name, default, Interval). |
496 |
|
|
497 |
|
subscribe_verbose(Reporter, Name, Types, Interval) -> |
498 |
:-( |
case exometer_report:subscribe(Reporter, Name, Types, Interval) of |
499 |
:-( |
ok -> ok; |
500 |
|
Other -> |
501 |
:-( |
?LOG_ERROR(#{what => metrics_subscribe_failed, |
502 |
|
reporter => Reporter, metric_name => Name, |
503 |
:-( |
reason => Other}), |
504 |
:-( |
Other |
505 |
|
end. |
506 |
|
|
507 |
|
subscribe_to_all(Reporter, Interval) -> |
508 |
:-( |
HostTypePrefixes = pick_by_all_metrics_are_global([], ?ALL_HOST_TYPES), |
509 |
:-( |
lists:foreach( |
510 |
|
fun(Prefix) -> |
511 |
:-( |
UnspacedPrefix = get_host_type_prefix(Prefix), |
512 |
:-( |
start_metrics_subscriptions(Reporter, [UnspacedPrefix], Interval) |
513 |
|
end, [global | HostTypePrefixes]). |