Commit | Line | Data |
---|---|---|
a9ed8751 SK |
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 | |
a37cd038 SK |
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 | |
a9ed8751 SK |
40 | }, |
41 | <<NodeID/binary>> | |
42 | ) -> | |
a37cd038 SK |
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 | ]. | |
a9ed8751 SK |
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), | |
d07a494b | 59 | cons([NodeID, <<"memory">>, KeyBin], Value, Timestamp) |
a9ed8751 SK |
60 | end, |
61 | lists:map(ComponentToMessage, Memory). | |
62 | ||
a37cd038 SK |
63 | -spec cons([binary()], integer(), erlang:timestamp()) -> |
64 | t(). | |
65 | cons(Path, Value, Timestamp) -> | |
66 | ?T | |
67 | { path = Path | |
68 | , value = Value | |
69 | , timestamp = Timestamp | |
70 | }. | |
71 | ||
a9ed8751 SK |
72 | -spec node_id_to_bin(node()) -> |
73 | binary(). | |
74 | node_id_to_bin(NodeID) -> | |
75 | NodeIDBin = atom_to_binary(NodeID, utf8), | |
76 | re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]). |