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").
27 %% ============================================================================
29 %% ============================================================================
31 -spec of_beam_stats(beam_stats:t()) ->
33 of_beam_stats(#beam_stats{node_id=NodeID}=BeamStats) ->
34 NodeIDBin = node_id_to_bin(NodeID),
35 of_beam_stats(BeamStats, NodeIDBin).
37 -spec of_beam_stats(beam_stats:t(), binary()) ->
39 of_beam_stats(#beam_stats
40 { timestamp = Timestamp
43 , io_bytes_in = IOBytesIn
44 , io_bytes_out = IOBytesOut
45 , context_switches = ContextSwitches
46 , reductions = Reductions
47 , run_queue = RunQueue
49 , processes = Processes
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)
63 ++ of_ets(ETS, NodeID, Ts)
64 ++ of_processes(Processes, NodeID, Ts),
65 lists:map(fun path_prefix_schema_version/1, Msgs).
73 , timestamp = Timestamp
76 PathBin = path_to_bin(Path),
77 ValueBin = integer_to_binary(Value),
78 TimestampInt = timestamp_to_integer(Timestamp),
79 TimestampBin = integer_to_binary(TimestampInt),
80 <<PathBin/binary, " ", ValueBin/binary, " ", TimestampBin/binary>>.
82 -spec path_to_bin([binary()]) ->
85 bin_join(Path, <<".">>).
87 -spec node_id_to_bin(node()) ->
89 node_id_to_bin(NodeID) ->
90 NodeIDBin = atom_to_binary(NodeID, utf8),
91 re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]).
93 %% ============================================================================
95 %% ============================================================================
97 -spec path_prefix_schema_version(t()) ->
99 path_prefix_schema_version(?T{}=T) ->
100 path_prefix(T, schema_version()).
102 -spec path_prefix(t(), binary()) ->
104 path_prefix(?T{path=Path}=T, <<Prefix/binary>>) ->
105 T?T{path = [Prefix | Path]}.
107 -spec schema_version() ->
112 -spec bin_join([binary()], binary()) ->
114 bin_join([] , <<_/binary>> ) -> <<>>;
115 bin_join([<<B/binary>> | []] , <<_/binary>> ) -> B;
116 bin_join([<<B/binary>> | [_|_]=Bins], <<Sep/binary>>) ->
117 BinsBin = bin_join(Bins, Sep),
118 <<B/binary, Sep/binary, BinsBin/binary>>.
120 -spec timestamp_to_integer(erlang:timestamp()) ->
122 timestamp_to_integer({Megaseconds, Seconds, _}) ->
123 Megaseconds * 1000000 + Seconds.
125 -spec of_memory([{atom(), non_neg_integer()}], binary(), erlang:timestamp()) ->
127 of_memory(Memory, <<NodeID/binary>>, Timestamp) ->
129 fun ({Key, Value}) ->
130 KeyBin = atom_to_binary(Key, latin1),
131 cons([NodeID, <<"memory">>, KeyBin], Value, Timestamp)
133 lists:map(ComponentToMessage, Memory).
135 -spec of_ets(beam_stats_ets:t(), binary(), erlang:timestamp()) ->
137 of_ets(PerTableStats, <<NodeID/binary>>, Timestamp) ->
138 OfEtsTable = fun (Table) -> of_ets_table(Table, NodeID, Timestamp) end,
139 MsgsNested = lists:map(OfEtsTable, PerTableStats),
140 MsgsFlattened = lists:append(MsgsNested),
141 aggregate_by_path(MsgsFlattened, Timestamp).
143 -spec of_ets_table(beam_stats_ets_table:t(), binary(), erlang:timestamp()) ->
145 of_ets_table(#beam_stats_ets_table
156 of true -> <<"NAMED">>
159 NameBin = atom_to_binary(Name, latin1),
160 NameAndID = [NameBin, IDType],
161 [ cons([NodeID, <<"ets_table">>, <<"size">> | NameAndID], Size , Timestamp)
162 , cons([NodeID, <<"ets_table">>, <<"memory">> | NameAndID], Memory, Timestamp)
165 -spec of_processes(beam_stats_processes:t(), binary(), erlang:timestamp()) ->
168 #beam_stats_processes
169 { individual_stats = Processes
170 , count_all = CountAll
171 , count_exiting = CountExiting
172 , count_garbage_collecting = CountGarbageCollecting
173 , count_registered = CountRegistered
174 , count_runnable = CountRunnable
175 , count_running = CountRunning
176 , count_suspended = CountSuspended
177 , count_waiting = CountWaiting
182 OfProcess = fun (P) -> of_process(P, NodeID, Timestamp) end,
183 PerProcessMsgsNested = lists:map(OfProcess, Processes),
184 PerProcessMsgsFlattened = lists:append(PerProcessMsgsNested),
185 PerProcessMsgsAggregates = aggregate_by_path(PerProcessMsgsFlattened, Timestamp),
188 [ cons([N, <<"processes_count_all">> ], CountAll , Ts)
189 , cons([N, <<"processes_count_exiting">> ], CountExiting , Ts)
190 , cons([N, <<"processes_count_garbage_collecting">>], CountGarbageCollecting, Ts)
191 , cons([N, <<"processes_count_registered">> ], CountRegistered , Ts)
192 , cons([N, <<"processes_count_runnable">> ], CountRunnable , Ts)
193 , cons([N, <<"processes_count_running">> ], CountRunning , Ts)
194 , cons([N, <<"processes_count_suspended">> ], CountSuspended , Ts)
195 , cons([N, <<"processes_count_waiting">> ], CountWaiting , Ts)
196 | PerProcessMsgsAggregates
199 -spec of_process(beam_stats_process:t(), binary(), erlang:timestamp()) ->
205 , total_heap_size = TotalHeapSize
206 , stack_size = StackSize
207 , message_queue_len = MsgQueueLen
208 , reductions = Reductions
213 Origin = beam_stats_process:get_best_known_origin(Process),
214 OriginBin = proc_origin_to_bin(Origin),
217 [ cons([N, <<"process_memory">> , OriginBin], Memory , Ts)
218 , cons([N, <<"process_total_heap_size">> , OriginBin], TotalHeapSize , Ts)
219 , cons([N, <<"process_stack_size">> , OriginBin], StackSize , Ts)
220 , cons([N, <<"process_message_queue_len">> , OriginBin], MsgQueueLen , Ts)
221 , cons([N, <<"process_reductions">> , OriginBin], Reductions , Ts)
224 -spec aggregate_by_path([t()], erlang:timestamp()) ->
226 aggregate_by_path(Msgs, Timestamp) ->
228 fun (?T{path=K, value=V}, ValsByPath) ->
229 dict:update_counter(K, V, ValsByPath)
231 ValsByPathDict = lists:foldl(Aggregate, dict:new(), Msgs),
232 ValsByPathList = dict:to_list(ValsByPathDict),
233 [cons(Path, Value, Timestamp) || {Path, Value} <- ValsByPathList].
235 -spec proc_origin_to_bin(beam_stats_process:best_known_origin()) ->
237 proc_origin_to_bin({registered_name, Name}) ->
238 NameBin = atom_to_binary(Name, utf8),
239 <<"named--", NameBin/binary>>;
240 proc_origin_to_bin({ancestry, Ancestry}) ->
241 #beam_stats_process_ancestry
242 { raw_initial_call = InitCallRaw
243 , otp_initial_call = InitCallOTPOpt
244 , otp_ancestors = AncestorsOpt
247 InitCallOTPBinOpt = hope_option:map(InitCallOTPOpt , fun mfa_to_bin/1),
248 InitCallOTPBin = hope_option:get(InitCallOTPBinOpt, Blank),
249 AncestorsBinOpt = hope_option:map(AncestorsOpt , fun ancestors_to_bin/1),
250 AncestorsBin = hope_option:get(AncestorsBinOpt , Blank),
251 InitCallRawBin = mfa_to_bin(InitCallRaw),
253 , InitCallRawBin/binary
255 , InitCallOTPBin/binary
257 , AncestorsBin/binary
260 ancestors_to_bin([]) ->
262 ancestors_to_bin([A | Ancestors]) ->
263 ABin = ancestor_to_bin(A),
264 case ancestors_to_bin(Ancestors)
267 ; <<AncestorsBin/binary>> ->
268 <<ABin/binary, "-", AncestorsBin/binary>>
271 ancestor_to_bin(A) when is_atom(A) ->
272 atom_to_binary(A, utf8);
273 ancestor_to_bin(A) when is_pid(A) ->
276 -spec mfa_to_bin(mfa()) ->
278 mfa_to_bin({Module, Function, Arity}) ->
279 ModuleBin = atom_to_binary(Module , utf8),
280 FunctionBin = atom_to_binary(Function, utf8),
281 ArityBin = erlang:integer_to_binary(Arity),
282 <<ModuleBin/binary, "-", FunctionBin/binary, "-", ArityBin/binary>>.
284 -spec cons([binary()], integer(), erlang:timestamp()) ->
286 cons(Path, Value, Timestamp) ->
290 , timestamp = Timestamp