Flatten Graphite consumer options.
authorSiraaj Khandkar <siraaj@khandkar.net>
Wed, 19 Aug 2015 15:48:31 +0000 (11:48 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Wed, 19 Aug 2015 15:48:31 +0000 (11:48 -0400)
src/beam_stats.app.src
src/beam_stats_consumer_graphite.erl

index 24ab5dd..97b0c7d 100644 (file)
     , {consumers,
         [ {beam_stats_consumer_graphite,
             [ {consumption_interval , 60000}
-            , {connect_options,
-                [ {host    , "localhost"}
-                , {port    , 2003}
-                , {timeout , 5000}
-                ]}
+            , {host                 , "localhost"}
+            , {port                 , 2003}
+            , {timeout              , 5000}
             ]}
         , {beam_stats_consumer_csv,
             [ {consumption_interval , 60000}
index 85c002d..be4f1f9 100644 (file)
@@ -5,8 +5,7 @@
 -behaviour(beam_stats_consumer).
 
 -export_type(
-    [         option/0
-    , connect_option/0
+    [ option/0
     ]).
 
 -export(
     , terminate/1
     ]).
 
--type connect_option() ::
-      {host    , inet:ip_address() | inet:hostname()}
-    | {port    , inet:port_number()}
-    | {timeout , timeout()}
-    .
-
 -type option() ::
       {consumption_interval , non_neg_integer()}
-    | {connect_options      , [connect_option()]}
+    | {host                 , inet:ip_address() | inet:hostname()}
+    | {port                 , inet:port_number()}
+    | {timeout              , timeout()}
     .
 
 -record(state,
-    { connect_options = []   :: [connect_option()]
-    , sock            = none :: hope_option:t(Socket :: port())
+    { sock    = none :: hope_option:t(Socket :: port())
+    , host           :: inet:ip_address() | inet:hostname()
+    , port           :: inet:port_number()
+    , timeout        :: timeout()
     }).
 
 -type state() ::
     #state{}.
 
 -define(GRAPHITE_PATH_PREFIX, "beam_stats").
+-define(DEFAULT_HOST        , "localhost").
+-define(DEFAULT_PORT        , 2003).
+-define(DEFAULT_TIMEOUT     , 5000).
 
 -spec init([option()]) ->
     {non_neg_integer(), state()}.
 init(Options) ->
-    ConnectOptions      = hope_kv_list:get(Options, connect_options     , []),
-    ConsumptionInterval = hope_kv_list:get(Options, consumption_interval, 60000),
+    Get = fun (Key, Default) -> hope_kv_list:get(Options, Key, Default) end,
+    ConsumptionInterval = Get(consumption_interval, 60000),
     State = #state
-        { connect_options = ConnectOptions
-        , sock            = none
+        { sock    = none
+        , host    = Get(host    , ?DEFAULT_HOST)
+        , port    = Get(port    , ?DEFAULT_PORT)
+        , timeout = Get(timeout , ?DEFAULT_TIMEOUT)
         },
     {ConsumptionInterval, State}.
 
@@ -83,13 +85,14 @@ try_to_send(#state{sock={some, Sock}}=State, Payload) ->
     state().
 try_to_connect_if_no_socket(#state{sock={some, _}}=State) ->
     State;
-try_to_connect_if_no_socket(#state{sock=none, connect_options=Options}=State) ->
-    DefaultHost    = "localhost",
-    DefaultPort    = 2003,
-    DefaultTimeout = 5000,
-    Host    = hope_kv_list:get(Options, host   , DefaultHost),
-    Port    = hope_kv_list:get(Options, port   , DefaultPort),
-    Timeout = hope_kv_list:get(Options, timeout, DefaultTimeout),
+try_to_connect_if_no_socket(
+    #state
+    { sock    = none
+    , host    = Host
+    , port    = Port
+    , timeout = Timeout
+    }=State
+) ->
     case gen_tcp:connect(Host, Port, [binary, {active, false}], Timeout)
     of  {ok, Sock} ->
             State#state{sock = {some, Sock}}
This page took 0.023233 seconds and 4 git commands to generate.