feat: prefix Graphite paths with current schema version
[beam_stats.git] / src / beam_stats_msg_graphite.erl
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 -include("include/beam_stats_process.hrl").
7 -include("include/beam_stats_process_ancestry.hrl").
8 -include("include/beam_stats_processes.hrl").
9
10 -export_type(
11 [ t/0
12 ]).
13
14 -export(
15 [ of_beam_stats/1
16 , of_beam_stats/2
17 , to_bin/1
18 , path_to_bin/1
19 , node_id_to_bin/1
20 ]).
21
22 -define(T, #?MODULE).
23
24 -type t() ::
25 ?T{}.
26
27 %% ============================================================================
28 %% API
29 %% ============================================================================
30
31 -spec of_beam_stats(beam_stats:t()) ->
32 [t()].
33 of_beam_stats(#beam_stats{node_id=NodeID}=BeamStats) ->
34 NodeIDBin = node_id_to_bin(NodeID),
35 of_beam_stats(BeamStats, NodeIDBin).
36
37 -spec of_beam_stats(beam_stats:t(), binary()) ->
38 [t()].
39 of_beam_stats(#beam_stats
40 { timestamp = Timestamp
41 , node_id = _
42 , memory = Memory
43 , io_bytes_in = IOBytesIn
44 , io_bytes_out = IOBytesOut
45 , context_switches = ContextSwitches
46 , reductions = Reductions
47 , run_queue = RunQueue
48 , ets = ETS
49 , processes = Processes
50 },
51 <<NodeID/binary>>
52 ) ->
53 Ts = Timestamp,
54 N = NodeID,
55 Msgs =
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)
62 ]
63 ++ of_ets(ETS, NodeID, Ts)
64 ++ of_processes(Processes, NodeID, Ts),
65 lists:map(fun path_prefix_schema_version/1, Msgs).
66
67 -spec to_bin(t()) ->
68 binary().
69 to_bin(
70 ?T
71 { path = Path
72 , value = Value
73 , timestamp = Timestamp
74 }
75 ) ->
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>>.
81
82 -spec path_to_bin([binary()]) ->
83 binary().
84 path_to_bin(Path) ->
85 bin_join(Path, <<".">>).
86
87 -spec node_id_to_bin(node()) ->
88 binary().
89 node_id_to_bin(NodeID) ->
90 NodeIDBin = atom_to_binary(NodeID, utf8),
91 re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]).
92
93 %% ============================================================================
94 %% Helpers
95 %% ============================================================================
96
97 -spec path_prefix_schema_version(t()) ->
98 t().
99 path_prefix_schema_version(?T{}=T) ->
100 path_prefix(T, schema_version()).
101
102 -spec path_prefix(t(), binary()) ->
103 t().
104 path_prefix(?T{path=Path}=T, <<Prefix/binary>>) ->
105 T?T{path = [Prefix | Path]}.
106
107 -spec schema_version() ->
108 binary().
109 schema_version() ->
110 <<"beam_stats_v0">>.
111
112 -spec bin_join([binary()], binary()) ->
113 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>>.
119
120 -spec timestamp_to_integer(erlang:timestamp()) ->
121 non_neg_integer().
122 timestamp_to_integer({Megaseconds, Seconds, _}) ->
123 Megaseconds * 1000000 + Seconds.
124
125 -spec of_memory([{atom(), non_neg_integer()}], binary(), erlang:timestamp()) ->
126 [t()].
127 of_memory(Memory, <<NodeID/binary>>, Timestamp) ->
128 ComponentToMessage =
129 fun ({Key, Value}) ->
130 KeyBin = atom_to_binary(Key, latin1),
131 cons([NodeID, <<"memory">>, KeyBin], Value, Timestamp)
132 end,
133 lists:map(ComponentToMessage, Memory).
134
135 -spec of_ets(beam_stats_ets:t(), binary(), erlang:timestamp()) ->
136 [t()].
137 of_ets(PerTableStats, <<NodeID/binary>>, Timestamp) ->
138 OfEtsTable = fun (Table) -> of_ets_table(Table, NodeID, Timestamp) end,
139 NestedMsgs = lists:map(OfEtsTable, PerTableStats),
140 lists:append(NestedMsgs).
141
142 -spec of_ets_table(beam_stats_ets_table:t(), binary(), erlang:timestamp()) ->
143 [t()].
144 of_ets_table(#beam_stats_ets_table
145 { id = ID
146 , name = Name
147 , size = Size
148 , memory = Memory
149 },
150 <<NodeID/binary>>,
151 Timestamp
152 ) ->
153 IDBin = beam_stats_ets_table:id_to_bin(ID),
154 NameBin = atom_to_binary(Name, latin1),
155 NameAndID = [NameBin, IDBin],
156 [ cons([NodeID, <<"ets_table">>, <<"size">> | NameAndID], Size , Timestamp)
157 , cons([NodeID, <<"ets_table">>, <<"memory">> | NameAndID], Memory, Timestamp)
158 ].
159
160 -spec of_processes(beam_stats_processes:t(), binary(), erlang:timestamp()) ->
161 [t()].
162 of_processes(
163 #beam_stats_processes
164 { individual_stats = Processes
165 , count_all = CountAll
166 , count_exiting = CountExiting
167 , count_garbage_collecting = CountGarbageCollecting
168 , count_registered = CountRegistered
169 , count_runnable = CountRunnable
170 , count_running = CountRunning
171 , count_suspended = CountSuspended
172 , count_waiting = CountWaiting
173 },
174 <<NodeID/binary>>,
175 Timestamp
176 ) ->
177 OfProcess = fun (P) -> of_process(P, NodeID, Timestamp) end,
178 PerProcessMsgsNested = lists:map(OfProcess, Processes),
179 PerProcessMsgsFlattened = lists:append(PerProcessMsgsNested),
180 PerProcessMsgsAggregates = aggregate_by_path(PerProcessMsgsFlattened, Timestamp),
181 Ts = Timestamp,
182 N = NodeID,
183 [ cons([N, <<"processes_count_all">> ], CountAll , Ts)
184 , cons([N, <<"processes_count_exiting">> ], CountExiting , Ts)
185 , cons([N, <<"processes_count_garbage_collecting">>], CountGarbageCollecting, Ts)
186 , cons([N, <<"processes_count_registered">> ], CountRegistered , Ts)
187 , cons([N, <<"processes_count_runnable">> ], CountRunnable , Ts)
188 , cons([N, <<"processes_count_running">> ], CountRunning , Ts)
189 , cons([N, <<"processes_count_suspended">> ], CountSuspended , Ts)
190 , cons([N, <<"processes_count_waiting">> ], CountWaiting , Ts)
191 | PerProcessMsgsAggregates
192 ].
193
194 -spec of_process(beam_stats_process:t(), binary(), erlang:timestamp()) ->
195 [t()].
196 of_process(
197 #beam_stats_process
198 { pid = _
199 , memory = Memory
200 , total_heap_size = TotalHeapSize
201 , stack_size = StackSize
202 , message_queue_len = MsgQueueLen
203 }=Process,
204 <<NodeID/binary>>,
205 Timestamp
206 ) ->
207 Origin = beam_stats_process:get_best_known_origin(Process),
208 OriginBin = proc_origin_to_bin(Origin),
209 Ts = Timestamp,
210 N = NodeID,
211 [ cons([N, <<"process_memory">> , OriginBin], Memory , Ts)
212 , cons([N, <<"process_total_heap_size">> , OriginBin], TotalHeapSize , Ts)
213 , cons([N, <<"process_stack_size">> , OriginBin], StackSize , Ts)
214 , cons([N, <<"process_message_queue_len">> , OriginBin], MsgQueueLen , Ts)
215 ].
216
217 -spec aggregate_by_path([t()], erlang:timestamp()) ->
218 [t()].
219 aggregate_by_path(Msgs, Timestamp) ->
220 Aggregate =
221 fun (?T{path=K, value=V}, ValsByPath) ->
222 dict:update_counter(K, V, ValsByPath)
223 end,
224 ValsByPathDict = lists:foldl(Aggregate, dict:new(), Msgs),
225 ValsByPathList = dict:to_list(ValsByPathDict),
226 [cons(Path, Value, Timestamp) || {Path, Value} <- ValsByPathList].
227
228 -spec proc_origin_to_bin(beam_stats_process:best_known_origin()) ->
229 binary().
230 proc_origin_to_bin({registered_name, Name}) ->
231 NameBin = atom_to_binary(Name, utf8),
232 <<"named--", NameBin/binary>>;
233 proc_origin_to_bin({ancestry, Ancestry}) ->
234 #beam_stats_process_ancestry
235 { raw_initial_call = InitCallRaw
236 , otp_initial_call = InitCallOTPOpt
237 , otp_ancestors = AncestorsOpt
238 } = Ancestry,
239 Blank = <<"NONE">>,
240 InitCallOTPBinOpt = hope_option:map(InitCallOTPOpt , fun mfa_to_bin/1),
241 InitCallOTPBin = hope_option:get(InitCallOTPBinOpt, Blank),
242 AncestorsBinOpt = hope_option:map(AncestorsOpt , fun ancestors_to_bin/1),
243 AncestorsBin = hope_option:get(AncestorsBinOpt , Blank),
244 InitCallRawBin = mfa_to_bin(InitCallRaw),
245 << "spawned-via--"
246 , InitCallRawBin/binary
247 , "--"
248 , InitCallOTPBin/binary
249 , "--"
250 , AncestorsBin/binary
251 >>.
252
253 ancestors_to_bin([]) ->
254 <<>>;
255 ancestors_to_bin([A | Ancestors]) ->
256 ABin = ancestor_to_bin(A),
257 case ancestors_to_bin(Ancestors)
258 of <<>> ->
259 ABin
260 ; <<AncestorsBin/binary>> ->
261 <<ABin/binary, "-", AncestorsBin/binary>>
262 end.
263
264 ancestor_to_bin(A) when is_atom(A) ->
265 atom_to_binary(A, utf8);
266 ancestor_to_bin(A) when is_pid(A) ->
267 <<"PID">>.
268
269 -spec mfa_to_bin(mfa()) ->
270 binary().
271 mfa_to_bin({Module, Function, Arity}) ->
272 ModuleBin = atom_to_binary(Module , utf8),
273 FunctionBin = atom_to_binary(Function, utf8),
274 ArityBin = erlang:integer_to_binary(Arity),
275 <<ModuleBin/binary, "-", FunctionBin/binary, "-", ArityBin/binary>>.
276
277 -spec cons([binary()], integer(), erlang:timestamp()) ->
278 t().
279 cons(Path, Value, Timestamp) ->
280 ?T
281 { path = Path
282 , value = Value
283 , timestamp = Timestamp
284 }.
This page took 0.083862 seconds and 4 git commands to generate.