feat: use new msg abstractions in StatsD consumer.
[beam_stats.git] / src / beam_stats_msg_graphite.erl
index b8d88a1..c88e327 100644 (file)
 
 -export(
     [ of_beam_stats/1
-    %, to_bin/1
+    , of_beam_stats/2
+    , to_bin/1
+    , path_to_bin/1
+    , add_path_prefix/2
+    , node_id_to_bin/1
     ]).
 
 -define(T, #?MODULE).
 -type t() ::
     ?T{}.
 
+%% ============================================================================
+%% API
+%% ============================================================================
+
 -spec of_beam_stats(beam_stats:t()) ->
     [t()].
 of_beam_stats(#beam_stats{node_id=NodeID}=BeamStats) ->
@@ -56,6 +64,54 @@ of_beam_stats(#beam_stats
     ++ of_ets(ETS, NodeID, Ts)
     ++ of_processes(Processes, NodeID, Ts).
 
+-spec to_bin(t()) ->
+    binary().
+to_bin(
+    ?T
+    { path      = Path
+    , value     = Value
+    , timestamp = Timestamp
+    }
+) ->
+    PathBin = path_to_bin(Path),
+    ValueBin = integer_to_binary(Value),
+    TimestampInt = timestamp_to_integer(Timestamp),
+    TimestampBin = integer_to_binary(TimestampInt),
+    <<PathBin/binary, " ", ValueBin/binary, " ", TimestampBin/binary>>.
+
+-spec add_path_prefix(t(), binary()) ->
+    t().
+add_path_prefix(?T{path=Path}=T, <<Prefix/binary>>) ->
+    T?T{path = [Prefix | Path]}.
+
+-spec path_to_bin([binary()]) ->
+    binary().
+path_to_bin(Path) ->
+    bin_join(Path, <<".">>).
+
+-spec node_id_to_bin(node()) ->
+    binary().
+node_id_to_bin(NodeID) ->
+    NodeIDBin = atom_to_binary(NodeID, utf8),
+    re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]).
+
+%% ============================================================================
+%% Helpers
+%% ============================================================================
+
+-spec bin_join([binary()], binary()) ->
+    binary().
+bin_join([]                         , <<_/binary>>  ) -> <<>>;
+bin_join([<<B/binary>> | []]        , <<_/binary>>  ) -> B;
+bin_join([<<B/binary>> | [_|_]=Bins], <<Sep/binary>>) ->
+    BinsBin = bin_join(Bins, Sep),
+    <<B/binary, Sep/binary, BinsBin/binary>>.
+
+-spec timestamp_to_integer(erlang:timestamp()) ->
+    non_neg_integer().
+timestamp_to_integer({Megaseconds, Seconds, _}) ->
+    Megaseconds * 1000000 + Seconds.
+
 -spec of_memory([{atom(), non_neg_integer()}], binary(), erlang:timestamp()) ->
     [t()].
 of_memory(Memory, <<NodeID/binary>>, Timestamp) ->
@@ -143,10 +199,10 @@ of_process(
     OriginAndPid = [OriginBin, PidBin],
     Ts = Timestamp,
     N  = NodeID,
-    [ cons([N, <<"process_memory">>            , OriginAndPid], Memory        , Ts)
-    , cons([N, <<"process_total_heap_size">>   , OriginAndPid], TotalHeapSize , Ts)
-    , cons([N, <<"process_stack_size">>        , OriginAndPid], StackSize     , Ts)
-    , cons([N, <<"process_message_queue_len">> , OriginAndPid], MsgQueueLen   , Ts)
+    [ cons([N, <<"process_memory">>            | OriginAndPid], Memory        , Ts)
+    , cons([N, <<"process_total_heap_size">>   | OriginAndPid], TotalHeapSize , Ts)
+    , cons([N, <<"process_stack_size">>        | OriginAndPid], StackSize     , Ts)
+    , cons([N, <<"process_message_queue_len">> | OriginAndPid], MsgQueueLen   , Ts)
     ].
 
 -spec proc_origin_to_bin(beam_stats_process:best_known_origin()) ->
@@ -209,9 +265,3 @@ cons(Path, Value, Timestamp) ->
     , value     = Value
     , timestamp = Timestamp
     }.
-
--spec node_id_to_bin(node()) ->
-    binary().
-node_id_to_bin(NodeID) ->
-    NodeIDBin = atom_to_binary(NodeID, utf8),
-    re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]).
This page took 0.025517 seconds and 4 git commands to generate.