1 -module(beam_stats_msg_graphite).
3 -include("include/beam_stats.hrl").
4 -include("include/beam_stats_ets_table.hrl").
5 -include("include/beam_stats_msg_graphite.hrl").
6 -include("include/beam_stats_process.hrl").
7 -include("include/beam_stats_process_ancestry.hrl").
8 -include("include/beam_stats_processes.hrl").
25 %% ============================================================================
27 %% ============================================================================
29 -spec of_beam_stats(beam_stats:t()) ->
31 of_beam_stats(#beam_stats{node_id=NodeID}=BeamStats) ->
32 NodeIDBin = node_id_to_bin(NodeID),
33 of_beam_stats(BeamStats, NodeIDBin).
35 -spec of_beam_stats(beam_stats:t(), binary()) ->
37 of_beam_stats(#beam_stats
38 { timestamp = Timestamp
41 % TODO: Handle the rest of data points
42 , io_bytes_in = IOBytesIn
43 , io_bytes_out = IOBytesOut
44 , context_switches = ContextSwitches
45 , reductions = Reductions
46 , run_queue = RunQueue
48 , processes = Processes
54 [ cons([N, <<"io">> , <<"bytes_in">> ], IOBytesIn , Ts)
55 , cons([N, <<"io">> , <<"bytes_out">>], IOBytesOut , Ts)
56 , cons([N, <<"context_switches">> ], ContextSwitches, Ts)
57 , cons([N, <<"reductions">> ], Reductions , Ts)
58 , cons([N, <<"run_queue">> ], RunQueue , Ts)
59 | of_memory(Memory, NodeID, Ts)
61 ++ of_ets(ETS, NodeID, Ts)
62 ++ of_processes(Processes, NodeID, Ts).
70 , timestamp = Timestamp
73 PathBin = path_to_bin(Path),
74 ValueBin = integer_to_binary(Value),
75 TimestampInt = timestamp_to_integer(Timestamp),
76 TimestampBin = integer_to_binary(TimestampInt),
77 <<PathBin/binary, " ", ValueBin/binary, " ", TimestampBin/binary>>.
79 -spec path_to_bin([binary()]) ->
82 bin_join(Path, <<".">>).
84 %% ============================================================================
86 %% ============================================================================
88 -spec bin_join([binary()], binary()) ->
90 bin_join([] , <<_/binary>> ) -> <<>>;
91 bin_join([<<B/binary>> | []] , <<_/binary>> ) -> B;
92 bin_join([<<B/binary>> | [_|_]=Bins], <<Sep/binary>>) ->
93 BinsBin = bin_join(Bins, Sep),
94 <<B/binary, Sep/binary, BinsBin/binary>>.
96 -spec timestamp_to_integer(erlang:timestamp()) ->
98 timestamp_to_integer({Megaseconds, Seconds, _}) ->
99 Megaseconds * 1000000 + Seconds.
101 -spec of_memory([{atom(), non_neg_integer()}], binary(), erlang:timestamp()) ->
103 of_memory(Memory, <<NodeID/binary>>, Timestamp) ->
105 fun ({Key, Value}) ->
106 KeyBin = atom_to_binary(Key, latin1),
107 cons([NodeID, <<"memory">>, KeyBin], Value, Timestamp)
109 lists:map(ComponentToMessage, Memory).
111 -spec of_ets(beam_stats_ets_table:t(), binary(), erlang:timestamp()) ->
113 of_ets(PerTableStats, <<NodeID/binary>>, Timestamp) ->
114 OfEtsTable = fun (Table) -> of_ets_table(Table, NodeID, Timestamp) end,
115 NestedMsgs = lists:map(OfEtsTable, PerTableStats),
116 lists:append(NestedMsgs).
118 -spec of_ets_table(beam_stats_ets_table:t(), binary(), erlang:timestamp()) ->
120 of_ets_table(#beam_stats_ets_table
129 IDBin = beam_stats_ets_table:id_to_bin(ID),
130 NameBin = atom_to_binary(Name, latin1),
131 NameAndID = [NameBin, IDBin],
132 [ cons([NodeID, <<"ets_table">>, <<"size">> | NameAndID], Size , Timestamp)
133 , cons([NodeID, <<"ets_table">>, <<"memory">> | NameAndID], Memory, Timestamp)
136 -spec of_processes(beam_stats_processes:t(), binary(), erlang:timestamp()) ->
139 #beam_stats_processes
140 { individual_stats = Processes
141 , count_all = CountAll
142 , count_exiting = CountExiting
143 , count_garbage_collecting = CountGarbageCollecting
144 , count_registered = CountRegistered
145 , count_runnable = CountRunnable
146 , count_running = CountRunning
147 , count_suspended = CountSuspended
148 , count_waiting = CountWaiting
153 OfProcess = fun (P) -> of_process(P, NodeID, Timestamp) end,
154 PerProcessMsgsNested = lists:map(OfProcess, Processes),
155 PerProcessMsgsFlattened = lists:append(PerProcessMsgsNested),
158 [ cons([N, <<"processes_count_all">> ], CountAll , Ts)
159 , cons([N, <<"processes_count_exiting">> ], CountExiting , Ts)
160 , cons([N, <<"processes_count_garbage_collecting">>], CountGarbageCollecting, Ts)
161 , cons([N, <<"processes_count_registered">> ], CountRegistered , Ts)
162 , cons([N, <<"processes_count_runnable">> ], CountRunnable , Ts)
163 , cons([N, <<"processes_count_running">> ], CountRunning , Ts)
164 , cons([N, <<"processes_count_suspended">> ], CountSuspended , Ts)
165 , cons([N, <<"processes_count_waiting">> ], CountWaiting , Ts)
166 | PerProcessMsgsFlattened
169 -spec of_process(beam_stats_process:t(), binary(), erlang:timestamp()) ->
175 , total_heap_size = TotalHeapSize
176 , stack_size = StackSize
177 , message_queue_len = MsgQueueLen
182 Origin = beam_stats_process:get_best_known_origin(Process),
183 OriginBin = proc_origin_to_bin(Origin),
184 PidBin = pid_to_bin(Pid),
185 OriginAndPid = [OriginBin, PidBin],
188 [ cons([N, <<"process_memory">> , OriginAndPid], Memory , Ts)
189 , cons([N, <<"process_total_heap_size">> , OriginAndPid], TotalHeapSize , Ts)
190 , cons([N, <<"process_stack_size">> , OriginAndPid], StackSize , Ts)
191 , cons([N, <<"process_message_queue_len">> , OriginAndPid], MsgQueueLen , Ts)
194 -spec proc_origin_to_bin(beam_stats_process:best_known_origin()) ->
196 proc_origin_to_bin({registered_name, Name}) ->
197 atom_to_binary(Name, utf8);
198 proc_origin_to_bin({ancestry, Ancestry}) ->
199 #beam_stats_process_ancestry
200 { raw_initial_call = InitCallRaw
201 , otp_initial_call = InitCallOTPOpt
202 , otp_ancestors = AncestorsOpt
205 InitCallOTPBinOpt = hope_option:map(InitCallOTPOpt , fun mfa_to_bin/1),
206 InitCallOTPBin = hope_option:get(InitCallOTPBinOpt, Blank),
207 AncestorsBinOpt = hope_option:map(AncestorsOpt , fun ancestors_to_bin/1),
208 AncestorsBin = hope_option:get(AncestorsBinOpt , Blank),
209 InitCallRawBin = mfa_to_bin(InitCallRaw),
210 << InitCallRawBin/binary
212 , InitCallOTPBin/binary
214 , AncestorsBin/binary
217 ancestors_to_bin([]) ->
219 ancestors_to_bin([A | Ancestors]) ->
220 ABin = ancestor_to_bin(A),
221 case ancestors_to_bin(Ancestors)
224 ; <<AncestorsBin/binary>> ->
225 <<ABin/binary, "-", AncestorsBin/binary>>
228 ancestor_to_bin(A) when is_atom(A) ->
229 atom_to_binary(A, utf8);
230 ancestor_to_bin(A) when is_pid(A) ->
234 PidList = erlang:pid_to_list(Pid),
235 PidBin = re:replace(PidList, "[\.]", "_", [global, {return, binary}]),
236 re:replace(PidBin , "[><]", "" , [global, {return, binary}]).
238 -spec mfa_to_bin(mfa()) ->
240 mfa_to_bin({Module, Function, Arity}) ->
241 ModuleBin = atom_to_binary(Module , utf8),
242 FunctionBin = atom_to_binary(Function, utf8),
243 ArityBin = erlang:integer_to_binary(Arity),
244 <<ModuleBin/binary, "-", FunctionBin/binary, "-", ArityBin/binary>>.
246 -spec cons([binary()], integer(), erlang:timestamp()) ->
248 cons(Path, Value, Timestamp) ->
252 , timestamp = Timestamp
255 -spec node_id_to_bin(node()) ->
257 node_id_to_bin(NodeID) ->
258 NodeIDBin = atom_to_binary(NodeID, utf8),
259 re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]).