| 1 | -module(beam_stats_msg_graphite). |
| 2 | |
| 3 | -include("include/beam_stats.hrl"). |
| 4 | -include("include/beam_stats_ets_table.hrl"). |
| 5 | -include("include/beam_stats_msg_graphite.hrl"). |
| 6 | |
| 7 | -export_type( |
| 8 | [ t/0 |
| 9 | ]). |
| 10 | |
| 11 | -export( |
| 12 | [ of_beam_stats/1 |
| 13 | %, to_bin/1 |
| 14 | ]). |
| 15 | |
| 16 | -define(T, #?MODULE). |
| 17 | |
| 18 | -type t() :: |
| 19 | ?T{}. |
| 20 | |
| 21 | -spec of_beam_stats(beam_stats:t()) -> |
| 22 | [t()]. |
| 23 | of_beam_stats(#beam_stats{node_id=NodeID}=BeamStats) -> |
| 24 | NodeIDBin = node_id_to_bin(NodeID), |
| 25 | of_beam_stats(BeamStats, NodeIDBin). |
| 26 | |
| 27 | -spec of_beam_stats(beam_stats:t(), binary()) -> |
| 28 | [t()]. |
| 29 | of_beam_stats(#beam_stats |
| 30 | { timestamp = Timestamp |
| 31 | , node_id = _ |
| 32 | , memory = Memory |
| 33 | % TODO: Handle the rest of data points |
| 34 | , io_bytes_in = IOBytesIn |
| 35 | , io_bytes_out = IOBytesOut |
| 36 | , context_switches = ContextSwitches |
| 37 | , reductions = Reductions |
| 38 | , run_queue = RunQueue |
| 39 | , ets = ETS |
| 40 | , processes = _Processes |
| 41 | }, |
| 42 | <<NodeID/binary>> |
| 43 | ) -> |
| 44 | Ts = Timestamp, |
| 45 | N = NodeID, |
| 46 | [ cons([N, <<"io">> , <<"bytes_in">> ], IOBytesIn , Ts) |
| 47 | , cons([N, <<"io">> , <<"bytes_out">>], IOBytesOut , Ts) |
| 48 | , cons([N, <<"context_switches">> ], ContextSwitches, Ts) |
| 49 | , cons([N, <<"reductions">> ], Reductions , Ts) |
| 50 | , cons([N, <<"run_queue">> ], RunQueue , Ts) |
| 51 | | of_memory(Memory, NodeID, Ts) |
| 52 | ] |
| 53 | ++ of_ets(ETS, NodeID, Ts) |
| 54 | . |
| 55 | |
| 56 | -spec of_memory([{atom(), non_neg_integer()}], binary(), erlang:timestamp()) -> |
| 57 | [t()]. |
| 58 | of_memory(Memory, <<NodeID/binary>>, Timestamp) -> |
| 59 | ComponentToMessage = |
| 60 | fun ({Key, Value}) -> |
| 61 | KeyBin = atom_to_binary(Key, latin1), |
| 62 | cons([NodeID, <<"memory">>, KeyBin], Value, Timestamp) |
| 63 | end, |
| 64 | lists:map(ComponentToMessage, Memory). |
| 65 | |
| 66 | -spec of_ets(beam_stats_ets_table:t(), binary(), erlang:timestamp()) -> |
| 67 | [t()]. |
| 68 | of_ets(PerTableStats, <<NodeID/binary>>, Timestamp) -> |
| 69 | OfEtsTable = fun (Table) -> of_ets_table(Table, NodeID, Timestamp) end, |
| 70 | NestedMsgs = lists:map(OfEtsTable, PerTableStats), |
| 71 | lists:append(NestedMsgs). |
| 72 | |
| 73 | -spec of_ets_table(beam_stats_ets_table:t(), binary(), erlang:timestamp()) -> |
| 74 | [t()]. |
| 75 | of_ets_table(#beam_stats_ets_table |
| 76 | { id = ID |
| 77 | , name = Name |
| 78 | , size = Size |
| 79 | , memory = Memory |
| 80 | }, |
| 81 | <<NodeID/binary>>, |
| 82 | Timestamp |
| 83 | ) -> |
| 84 | IDBin = beam_stats_ets_table:id_to_bin(ID), |
| 85 | NameBin = atom_to_binary(Name, latin1), |
| 86 | NameAndID = [NameBin, IDBin], |
| 87 | [ cons([NodeID, <<"ets_table">>, <<"size">> | NameAndID], Size , Timestamp) |
| 88 | , cons([NodeID, <<"ets_table">>, <<"memory">> | NameAndID], Memory, Timestamp) |
| 89 | ]. |
| 90 | |
| 91 | -spec cons([binary()], integer(), erlang:timestamp()) -> |
| 92 | t(). |
| 93 | cons(Path, Value, Timestamp) -> |
| 94 | ?T |
| 95 | { path = Path |
| 96 | , value = Value |
| 97 | , timestamp = Timestamp |
| 98 | }. |
| 99 | |
| 100 | -spec node_id_to_bin(node()) -> |
| 101 | binary(). |
| 102 | node_id_to_bin(NodeID) -> |
| 103 | NodeIDBin = atom_to_binary(NodeID, utf8), |
| 104 | re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]). |