feat: implement beam_stats_msg_graphite:of_memory/3
authorSiraaj Khandkar <siraaj@khandkar.net>
Wed, 23 Sep 2015 13:35:26 +0000 (09:35 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Thu, 24 Sep 2015 01:50:31 +0000 (21:50 -0400)
include/beam_stats_msg_graphite.hrl [new file with mode: 0644]
src/beam_stats_msg_graphite.erl [new file with mode: 0644]

diff --git a/include/beam_stats_msg_graphite.hrl b/include/beam_stats_msg_graphite.hrl
new file mode 100644 (file)
index 0000000..56df5cc
--- /dev/null
@@ -0,0 +1,5 @@
+-record(beam_stats_msg_graphite,
+    { path      :: [binary()]
+    , value     :: integer()
+    , timestamp :: erlang:timestamp()
+    }).
diff --git a/src/beam_stats_msg_graphite.erl b/src/beam_stats_msg_graphite.erl
new file mode 100644 (file)
index 0000000..1592549
--- /dev/null
@@ -0,0 +1,56 @@
+-module(beam_stats_msg_graphite).
+
+-include("include/beam_stats.hrl").
+-include("include/beam_stats_msg_graphite.hrl").
+
+-export_type(
+    [ t/0
+    ]).
+
+-export(
+    [ of_beam_stats/1
+    %, to_bin/1
+    ]).
+
+-define(T, #?MODULE).
+
+-type t() ::
+    ?T{}.
+
+-spec of_beam_stats(beam_stats:t()) ->
+    [t()].
+of_beam_stats(#beam_stats{node_id=NodeID}=BeamStats) ->
+    NodeIDBin = node_id_to_bin(NodeID),
+    of_beam_stats(BeamStats, NodeIDBin).
+
+-spec of_beam_stats(beam_stats:t(), binary()) ->
+    [t()].
+of_beam_stats(#beam_stats
+    { timestamp = Timestamp
+    , node_id   = _
+    , memory    = Memory
+    % TODO: Handle the rest of data point
+    },
+    <<NodeID/binary>>
+) ->
+    of_memory(Memory, NodeID, Timestamp).
+
+-spec of_memory([{atom(), non_neg_integer()}], binary(), erlang:timestamp()) ->
+    [t()].
+of_memory(Memory, <<NodeID/binary>>, Timestamp) ->
+    ComponentToMessage =
+        fun ({Key, Value}) ->
+            KeyBin = atom_to_binary(Key, latin1),
+            ?T
+            { path      = [NodeID, <<"memory">>, KeyBin]
+            , value     = Value
+            , timestamp = Timestamp
+            }
+        end,
+    lists:map(ComponentToMessage, Memory).
+
+-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.027499 seconds and 4 git commands to generate.