forked from raimdev/ejabberd-prometheus-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_prometheus.erl
122 lines (98 loc) · 3.59 KB
/
mod_prometheus.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
-module(mod_prometheus).
%%%-------------------------------------------------------------------
%%% @author Skythet
%%% @copyright (C) 2016, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 22. Sep 2017 3:37 PM
%%%-------------------------------------------------------------------
-author("Skythet").
%% API
-export([start/2,
stop/1,
process/2
]).
-define(_ENABLED_STATS, [
total_run_queue_lengths,
total_active_tasks,
context_switches,
garbage_collection,
io,
reductions,
runtime,
allocated_areas,
port_count,
port_limit,
process_count,
process_limit,
connected_users_number
]).
start(_Host, _Opts) ->
ok.
stop(_Host) ->
ok.
process(_, _) ->
Node = erlang:node(),
Metrics = memory_metrics(erlang:memory(), Node) ++
statistics(?_ENABLED_STATS, Node),
{200, [{<<"Content-Type">>, <<"text/plain">>}], list_to_bitstring(Metrics)}.
memory_metrics([], _) ->
[];
memory_metrics([{Type, Value} | MemoryInfoTail], Node) ->
Metric = io_lib:format("ejabberd_memory_~s_bytes{node=\"~w\"} ~w~n", [Type, Node, Value]),
[Metric, memory_metrics(MemoryInfoTail, Node)].
metric_format(MetricName, Node, Value) ->
io_lib:format("ejabberd_~s{node=\"~w\"} ~w~n", [MetricName, Node, Value]).
statistics([], _) ->
[];
statistics([StatName | StatsTail], Node) ->
[statistic(StatName, Node), statistics(StatsTail, Node)].
statistic(reductions, Node) ->
{TotalReductions, ReductionsSinceLastCall} = erlang:statistics(reductions),
[
metric_format("reductions_total", Node, TotalReductions),
metric_format("reductions_since_last_call", Node, ReductionsSinceLastCall)
];
statistic(schedulers_count, Node) ->
SchedulersCount = erlang:system_info(schedulers_count),
metric_format("schedulers_count", Node, SchedulersCount);
statistic(runtime, Node) ->
{TotalRuntime, _} = erlang:statistics(runtime),
metric_format("run_time_total_seconds", Node, TotalRuntime / 1000);
statistic(io, Node) ->
{{input, InputBytes}, {output, OutputBytes}} = erlang:statistics(io),
[metric_format("io_input_bytes", Node, InputBytes), metric_format("io_output_bytes", Node, OutputBytes)];
statistic(garbage_collection, Node) ->
{GcsCount, WordsReclaimed, _} = erlang:statistics(garbage_collection),
[metric_format("gc_number", Node, GcsCount), metric_format("gc_words_reclaimed_total", Node, WordsReclaimed)];
statistic(connected_users_number, Node) ->
ConnectedUsersNumber = ejabberd_sm:connected_users_number(),
metric_format(connected_users_number, Node, ConnectedUsersNumber);
statistic(context_switches, Node) ->
{ContextSwitches, 0} = erlang:statistics(context_switches),
metric_format(context_switches, Node, ContextSwitches);
statistic(Metric, Node) when
Metric == port_count;
Metric == port_limit;
Metric == process_count;
Metric == process_limit
->
metric_format(Metric, Node, erlang:system_info(Metric));
statistic(allocated_areas, Node) ->
allocated_areas_metrics(erlang:system_info(allocated_areas), Node);
statistic(StatName, Node) ->
metric_format(StatName, Node, erlang:statistics(StatName)).
allocated_areas_metrics([], _) ->
[];
allocated_areas_metrics([{Area, AllocatedBytes, UsedBytes} | MetricTail], Node) ->
[
metric_format(["allocated_areas_", atom_to_list(Area), "_bytes"], Node, AllocatedBytes),
metric_format(["allocated_areas_", atom_to_list(Area), "_used_bytes"], Node, UsedBytes),
allocated_areas_metrics(MetricTail, Node)
];
allocated_areas_metrics([{Area, AllocatedBytes} | MetricTail], Node) ->
[
metric_format(["allocated_areas_", atom_to_list(Area), "_bytes"], Node, AllocatedBytes),
allocated_areas_metrics(MetricTail, Node)
].