feat: implement beam_stats_msg_graphite:to_bin/1
authorSiraaj Khandkar <siraaj@khandkar.net>
Wed, 23 Sep 2015 23:55:21 +0000 (19:55 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Thu, 24 Sep 2015 01:50:31 +0000 (21:50 -0400)
src/beam_stats_msg_graphite.erl

index b8d88a1..5b4e6e2 100644 (file)
@@ -13,7 +13,7 @@
 
 -export(
     [ of_beam_stats/1
 
 -export(
     [ of_beam_stats/1
-    %, to_bin/1
+    , to_bin/1
     ]).
 
 -define(T, #?MODULE).
     ]).
 
 -define(T, #?MODULE).
 -type t() ::
     ?T{}.
 
 -type t() ::
     ?T{}.
 
+%% ============================================================================
+%% API
+%% ============================================================================
+
 -spec of_beam_stats(beam_stats:t()) ->
     [t()].
 of_beam_stats(#beam_stats{node_id=NodeID}=BeamStats) ->
 -spec of_beam_stats(beam_stats:t()) ->
     [t()].
 of_beam_stats(#beam_stats{node_id=NodeID}=BeamStats) ->
@@ -56,6 +60,38 @@ of_beam_stats(#beam_stats
     ++ of_ets(ETS, NodeID, Ts)
     ++ of_processes(Processes, NodeID, Ts).
 
     ++ 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 = bin_join(Path, <<".">>),
+    ValueBin = integer_to_binary(Value),
+    TimestampInt = timestamp_to_integer(Timestamp),
+    TimestampBin = integer_to_binary(TimestampInt),
+    <<PathBin/binary, " ", ValueBin/binary, " ", TimestampBin/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) ->
 -spec of_memory([{atom(), non_neg_integer()}], binary(), erlang:timestamp()) ->
     [t()].
 of_memory(Memory, <<NodeID/binary>>, Timestamp) ->
This page took 0.026264 seconds and 4 git commands to generate.