From: Siraaj Khandkar Date: Thu, 24 Sep 2015 00:10:35 +0000 (-0400) Subject: feat: implement beam_stats_msg_statsd_gauge abstraction X-Git-Tag: 0.10.0^2~5 X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=9b9299d9a59c8e5236c49c048846c9c4d5ed4a8b;p=beam_stats.git feat: implement beam_stats_msg_statsd_gauge abstraction --- diff --git a/include/beam_stats_msg_statsd_gauge.hrl b/include/beam_stats_msg_statsd_gauge.hrl new file mode 100644 index 0000000..445c8a7 --- /dev/null +++ b/include/beam_stats_msg_statsd_gauge.hrl @@ -0,0 +1,4 @@ +-record(beam_stats_msg_statsd_gauge, + { name :: binary() + , value :: non_neg_integer() + }). diff --git a/src/beam_stats_msg_graphite.erl b/src/beam_stats_msg_graphite.erl index 5b4e6e2..62028b1 100644 --- a/src/beam_stats_msg_graphite.erl +++ b/src/beam_stats_msg_graphite.erl @@ -14,6 +14,7 @@ -export( [ of_beam_stats/1 , to_bin/1 + , path_to_bin/1 ]). -define(T, #?MODULE). @@ -69,12 +70,17 @@ to_bin( , timestamp = Timestamp } ) -> - PathBin = bin_join(Path, <<".">>), + PathBin = path_to_bin(Path), ValueBin = integer_to_binary(Value), TimestampInt = timestamp_to_integer(Timestamp), TimestampBin = integer_to_binary(TimestampInt), <>. +-spec path_to_bin([binary()]) -> + binary(). +path_to_bin(Path) -> + bin_join(Path, <<".">>). + %% ============================================================================ %% Helpers %% ============================================================================ diff --git a/src/beam_stats_msg_statsd_gauge.erl b/src/beam_stats_msg_statsd_gauge.erl new file mode 100644 index 0000000..07e5d02 --- /dev/null +++ b/src/beam_stats_msg_statsd_gauge.erl @@ -0,0 +1,47 @@ +-module(beam_stats_msg_statsd_gauge). + +-include("include/beam_stats_msg_graphite.hrl"). +-include("include/beam_stats_msg_statsd_gauge.hrl"). + +-export_type( + [ t/0 + ]). + +-export( + [ of_msg_graphite/1 + , to_bin/1 + ]). + +-define(T, #?MODULE). + +-type t() :: + ?T{}. + +of_msg_graphite( + #beam_stats_msg_graphite + { path = Path + , value = Value + , timestamp = _Timestamp + } +) -> + PathBin = beam_stats_msg_graphite:path_to_bin(Path), + cons(PathBin, Value). + +-spec cons(binary(), non_neg_integer()) -> + t(). +cons(<>, Value) -> + ?T + { name = Name + , value = Value + }. + +-spec to_bin(t()) -> + binary(). +to_bin( + ?T + { name = <> + , value = Value + } +) when Value >= 0 -> + ValueBin = integer_to_binary(Value), + << Name/binary, ":", ValueBin/binary, "|g\n">>.