| 1 | -module(beam_stats_msg_graphite). |
| 2 | |
| 3 | -include("include/beam_stats.hrl"). |
| 4 | -include("include/beam_stats_msg_graphite.hrl"). |
| 5 | |
| 6 | -export_type( |
| 7 | [ t/0 |
| 8 | ]). |
| 9 | |
| 10 | -export( |
| 11 | [ of_beam_stats/1 |
| 12 | %, to_bin/1 |
| 13 | ]). |
| 14 | |
| 15 | -define(T, #?MODULE). |
| 16 | |
| 17 | -type t() :: |
| 18 | ?T{}. |
| 19 | |
| 20 | -spec of_beam_stats(beam_stats:t()) -> |
| 21 | [t()]. |
| 22 | of_beam_stats(#beam_stats{node_id=NodeID}=BeamStats) -> |
| 23 | NodeIDBin = node_id_to_bin(NodeID), |
| 24 | of_beam_stats(BeamStats, NodeIDBin). |
| 25 | |
| 26 | -spec of_beam_stats(beam_stats:t(), binary()) -> |
| 27 | [t()]. |
| 28 | of_beam_stats(#beam_stats |
| 29 | { timestamp = Timestamp |
| 30 | , node_id = _ |
| 31 | , memory = Memory |
| 32 | % TODO: Handle the rest of data points |
| 33 | , io_bytes_in = IOBytesIn |
| 34 | , io_bytes_out = IOBytesOut |
| 35 | , context_switches = ContextSwitches |
| 36 | , reductions = Reductions |
| 37 | , run_queue = RunQueue |
| 38 | , ets = _ETS |
| 39 | , processes = _Processes |
| 40 | }, |
| 41 | <<NodeID/binary>> |
| 42 | ) -> |
| 43 | Ts = Timestamp, |
| 44 | N = NodeID, |
| 45 | [ cons([N, <<"io">> , <<"bytes_in">> ], IOBytesIn , Ts) |
| 46 | , cons([N, <<"io">> , <<"bytes_out">>], IOBytesOut , Ts) |
| 47 | , cons([N, <<"context_switches">> ], ContextSwitches, Ts) |
| 48 | , cons([N, <<"reductions">> ], Reductions , Ts) |
| 49 | , cons([N, <<"run_queue">> ], RunQueue , Ts) |
| 50 | | of_memory(Memory, NodeID, Ts) |
| 51 | ]. |
| 52 | |
| 53 | -spec of_memory([{atom(), non_neg_integer()}], binary(), erlang:timestamp()) -> |
| 54 | [t()]. |
| 55 | of_memory(Memory, <<NodeID/binary>>, Timestamp) -> |
| 56 | ComponentToMessage = |
| 57 | fun ({Key, Value}) -> |
| 58 | KeyBin = atom_to_binary(Key, latin1), |
| 59 | ?T |
| 60 | { path = [NodeID, <<"memory">>, KeyBin] |
| 61 | , value = Value |
| 62 | , timestamp = Timestamp |
| 63 | } |
| 64 | end, |
| 65 | lists:map(ComponentToMessage, Memory). |
| 66 | |
| 67 | -spec cons([binary()], integer(), erlang:timestamp()) -> |
| 68 | t(). |
| 69 | cons(Path, Value, Timestamp) -> |
| 70 | ?T |
| 71 | { path = Path |
| 72 | , value = Value |
| 73 | , timestamp = Timestamp |
| 74 | }. |
| 75 | |
| 76 | -spec node_id_to_bin(node()) -> |
| 77 | binary(). |
| 78 | node_id_to_bin(NodeID) -> |
| 79 | NodeIDBin = atom_to_binary(NodeID, utf8), |
| 80 | re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]). |