chore: remove finished TODO item
[beam_stats.git] / src / beam_stats_msg_graphite.erl
CommitLineData
a9ed8751
SK
1-module(beam_stats_msg_graphite).
2
3-include("include/beam_stats.hrl").
6e1a5b00 4-include("include/beam_stats_ets_table.hrl").
a9ed8751 5-include("include/beam_stats_msg_graphite.hrl").
4bb8ddfe
SK
6-include("include/beam_stats_process.hrl").
7-include("include/beam_stats_process_ancestry.hrl").
8-include("include/beam_stats_processes.hrl").
a9ed8751
SK
9
10-export_type(
11 [ t/0
12 ]).
13
14-export(
15 [ of_beam_stats/1
ece99ea3 16 , of_beam_stats/2
67948b18 17 , to_bin/1
9b9299d9 18 , path_to_bin/1
ece99ea3
SK
19 , add_path_prefix/2
20 , node_id_to_bin/1
a9ed8751
SK
21 ]).
22
23-define(T, #?MODULE).
24
25-type t() ::
26 ?T{}.
27
67948b18
SK
28%% ============================================================================
29%% API
30%% ============================================================================
31
a9ed8751
SK
32-spec of_beam_stats(beam_stats:t()) ->
33 [t()].
34of_beam_stats(#beam_stats{node_id=NodeID}=BeamStats) ->
35 NodeIDBin = node_id_to_bin(NodeID),
36 of_beam_stats(BeamStats, NodeIDBin).
37
38-spec of_beam_stats(beam_stats:t(), binary()) ->
39 [t()].
40of_beam_stats(#beam_stats
41 { timestamp = Timestamp
42 , node_id = _
43 , memory = Memory
a37cd038
SK
44 , io_bytes_in = IOBytesIn
45 , io_bytes_out = IOBytesOut
46 , context_switches = ContextSwitches
47 , reductions = Reductions
48 , run_queue = RunQueue
6e1a5b00 49 , ets = ETS
4bb8ddfe 50 , processes = Processes
a9ed8751
SK
51 },
52 <<NodeID/binary>>
53) ->
a37cd038
SK
54 Ts = Timestamp,
55 N = NodeID,
56 [ cons([N, <<"io">> , <<"bytes_in">> ], IOBytesIn , Ts)
57 , cons([N, <<"io">> , <<"bytes_out">>], IOBytesOut , Ts)
58 , cons([N, <<"context_switches">> ], ContextSwitches, Ts)
59 , cons([N, <<"reductions">> ], Reductions , Ts)
60 , cons([N, <<"run_queue">> ], RunQueue , Ts)
61 | of_memory(Memory, NodeID, Ts)
6e1a5b00
SK
62 ]
63 ++ of_ets(ETS, NodeID, Ts)
4bb8ddfe 64 ++ of_processes(Processes, NodeID, Ts).
a9ed8751 65
67948b18
SK
66-spec to_bin(t()) ->
67 binary().
68to_bin(
69 ?T
70 { path = Path
71 , value = Value
72 , timestamp = Timestamp
73 }
74) ->
9b9299d9 75 PathBin = path_to_bin(Path),
67948b18
SK
76 ValueBin = integer_to_binary(Value),
77 TimestampInt = timestamp_to_integer(Timestamp),
78 TimestampBin = integer_to_binary(TimestampInt),
79 <<PathBin/binary, " ", ValueBin/binary, " ", TimestampBin/binary>>.
80
ece99ea3
SK
81-spec add_path_prefix(t(), binary()) ->
82 t().
83add_path_prefix(?T{path=Path}=T, <<Prefix/binary>>) ->
84 T?T{path = [Prefix | Path]}.
85
9b9299d9
SK
86-spec path_to_bin([binary()]) ->
87 binary().
88path_to_bin(Path) ->
89 bin_join(Path, <<".">>).
90
ece99ea3
SK
91-spec node_id_to_bin(node()) ->
92 binary().
93node_id_to_bin(NodeID) ->
94 NodeIDBin = atom_to_binary(NodeID, utf8),
95 re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]).
96
67948b18
SK
97%% ============================================================================
98%% Helpers
99%% ============================================================================
100
101-spec bin_join([binary()], binary()) ->
102 binary().
103bin_join([] , <<_/binary>> ) -> <<>>;
104bin_join([<<B/binary>> | []] , <<_/binary>> ) -> B;
105bin_join([<<B/binary>> | [_|_]=Bins], <<Sep/binary>>) ->
106 BinsBin = bin_join(Bins, Sep),
107 <<B/binary, Sep/binary, BinsBin/binary>>.
108
109-spec timestamp_to_integer(erlang:timestamp()) ->
110 non_neg_integer().
111timestamp_to_integer({Megaseconds, Seconds, _}) ->
112 Megaseconds * 1000000 + Seconds.
113
a9ed8751
SK
114-spec of_memory([{atom(), non_neg_integer()}], binary(), erlang:timestamp()) ->
115 [t()].
116of_memory(Memory, <<NodeID/binary>>, Timestamp) ->
117 ComponentToMessage =
118 fun ({Key, Value}) ->
119 KeyBin = atom_to_binary(Key, latin1),
d07a494b 120 cons([NodeID, <<"memory">>, KeyBin], Value, Timestamp)
a9ed8751
SK
121 end,
122 lists:map(ComponentToMessage, Memory).
123
7581255c 124-spec of_ets(beam_stats_ets:t(), binary(), erlang:timestamp()) ->
6e1a5b00
SK
125 [t()].
126of_ets(PerTableStats, <<NodeID/binary>>, Timestamp) ->
127 OfEtsTable = fun (Table) -> of_ets_table(Table, NodeID, Timestamp) end,
128 NestedMsgs = lists:map(OfEtsTable, PerTableStats),
129 lists:append(NestedMsgs).
130
131-spec of_ets_table(beam_stats_ets_table:t(), binary(), erlang:timestamp()) ->
132 [t()].
133of_ets_table(#beam_stats_ets_table
134 { id = ID
135 , name = Name
136 , size = Size
137 , memory = Memory
138 },
139 <<NodeID/binary>>,
140 Timestamp
141) ->
142 IDBin = beam_stats_ets_table:id_to_bin(ID),
143 NameBin = atom_to_binary(Name, latin1),
144 NameAndID = [NameBin, IDBin],
145 [ cons([NodeID, <<"ets_table">>, <<"size">> | NameAndID], Size , Timestamp)
146 , cons([NodeID, <<"ets_table">>, <<"memory">> | NameAndID], Memory, Timestamp)
147 ].
148
4bb8ddfe
SK
149-spec of_processes(beam_stats_processes:t(), binary(), erlang:timestamp()) ->
150 [t()].
151of_processes(
152 #beam_stats_processes
153 { individual_stats = Processes
154 , count_all = CountAll
155 , count_exiting = CountExiting
156 , count_garbage_collecting = CountGarbageCollecting
157 , count_registered = CountRegistered
158 , count_runnable = CountRunnable
159 , count_running = CountRunning
160 , count_suspended = CountSuspended
161 , count_waiting = CountWaiting
162 },
163 <<NodeID/binary>>,
164 Timestamp
165) ->
166 OfProcess = fun (P) -> of_process(P, NodeID, Timestamp) end,
167 PerProcessMsgsNested = lists:map(OfProcess, Processes),
168 PerProcessMsgsFlattened = lists:append(PerProcessMsgsNested),
169 Ts = Timestamp,
170 N = NodeID,
171 [ cons([N, <<"processes_count_all">> ], CountAll , Ts)
172 , cons([N, <<"processes_count_exiting">> ], CountExiting , Ts)
173 , cons([N, <<"processes_count_garbage_collecting">>], CountGarbageCollecting, Ts)
174 , cons([N, <<"processes_count_registered">> ], CountRegistered , Ts)
175 , cons([N, <<"processes_count_runnable">> ], CountRunnable , Ts)
176 , cons([N, <<"processes_count_running">> ], CountRunning , Ts)
177 , cons([N, <<"processes_count_suspended">> ], CountSuspended , Ts)
178 , cons([N, <<"processes_count_waiting">> ], CountWaiting , Ts)
179 | PerProcessMsgsFlattened
180 ].
181
182-spec of_process(beam_stats_process:t(), binary(), erlang:timestamp()) ->
183 [t()].
184of_process(
185 #beam_stats_process
186 { pid = Pid
187 , memory = Memory
188 , total_heap_size = TotalHeapSize
189 , stack_size = StackSize
190 , message_queue_len = MsgQueueLen
191 }=Process,
192 <<NodeID/binary>>,
193 Timestamp
194) ->
195 Origin = beam_stats_process:get_best_known_origin(Process),
196 OriginBin = proc_origin_to_bin(Origin),
197 PidBin = pid_to_bin(Pid),
198 OriginAndPid = [OriginBin, PidBin],
199 Ts = Timestamp,
200 N = NodeID,
8dd2cb56
SK
201 [ cons([N, <<"process_memory">> | OriginAndPid], Memory , Ts)
202 , cons([N, <<"process_total_heap_size">> | OriginAndPid], TotalHeapSize , Ts)
203 , cons([N, <<"process_stack_size">> | OriginAndPid], StackSize , Ts)
204 , cons([N, <<"process_message_queue_len">> | OriginAndPid], MsgQueueLen , Ts)
4bb8ddfe
SK
205 ].
206
207-spec proc_origin_to_bin(beam_stats_process:best_known_origin()) ->
208 binary().
209proc_origin_to_bin({registered_name, Name}) ->
210 atom_to_binary(Name, utf8);
211proc_origin_to_bin({ancestry, Ancestry}) ->
212 #beam_stats_process_ancestry
213 { raw_initial_call = InitCallRaw
214 , otp_initial_call = InitCallOTPOpt
215 , otp_ancestors = AncestorsOpt
216 } = Ancestry,
217 Blank = <<"NONE">>,
218 InitCallOTPBinOpt = hope_option:map(InitCallOTPOpt , fun mfa_to_bin/1),
219 InitCallOTPBin = hope_option:get(InitCallOTPBinOpt, Blank),
220 AncestorsBinOpt = hope_option:map(AncestorsOpt , fun ancestors_to_bin/1),
221 AncestorsBin = hope_option:get(AncestorsBinOpt , Blank),
222 InitCallRawBin = mfa_to_bin(InitCallRaw),
223 << InitCallRawBin/binary
224 , "--"
225 , InitCallOTPBin/binary
226 , "--"
227 , AncestorsBin/binary
228 >>.
229
230ancestors_to_bin([]) ->
231 <<>>;
232ancestors_to_bin([A | Ancestors]) ->
233 ABin = ancestor_to_bin(A),
234 case ancestors_to_bin(Ancestors)
235 of <<>> ->
236 ABin
237 ; <<AncestorsBin/binary>> ->
238 <<ABin/binary, "-", AncestorsBin/binary>>
239 end.
240
241ancestor_to_bin(A) when is_atom(A) ->
242 atom_to_binary(A, utf8);
243ancestor_to_bin(A) when is_pid(A) ->
244 pid_to_bin(A).
245
246pid_to_bin(Pid) ->
247 PidList = erlang:pid_to_list(Pid),
248 PidBin = re:replace(PidList, "[\.]", "_", [global, {return, binary}]),
249 re:replace(PidBin , "[><]", "" , [global, {return, binary}]).
250
251-spec mfa_to_bin(mfa()) ->
252 binary().
253mfa_to_bin({Module, Function, Arity}) ->
254 ModuleBin = atom_to_binary(Module , utf8),
255 FunctionBin = atom_to_binary(Function, utf8),
256 ArityBin = erlang:integer_to_binary(Arity),
257 <<ModuleBin/binary, "-", FunctionBin/binary, "-", ArityBin/binary>>.
258
a37cd038
SK
259-spec cons([binary()], integer(), erlang:timestamp()) ->
260 t().
261cons(Path, Value, Timestamp) ->
262 ?T
263 { path = Path
264 , value = Value
265 , timestamp = Timestamp
266 }.
This page took 0.062109 seconds and 4 git commands to generate.