Skip to content

Commit

Permalink
Support for Erlang VM metrics remorting
Browse files Browse the repository at this point in the history
  • Loading branch information
blinkov committed May 24, 2012
1 parent c9e1b99 commit ea6b9f3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
61 changes: 51 additions & 10 deletions src/estatsd_server.erl
Expand Up @@ -11,7 +11,7 @@
-module(estatsd_server). -module(estatsd_server).
-behaviour(gen_server). -behaviour(gen_server).


-export([start_link/3]). -export([start_link/4]).


%-export([key2str/1,flush/0]). %% export for debugging %-export([key2str/1,flush/0]). %% export for debugging


Expand All @@ -22,18 +22,19 @@
flush_interval, % ms interval between stats flushing flush_interval, % ms interval between stats flushing
flush_timer, % TRef of interval timer flush_timer, % TRef of interval timer
graphite_host, % graphite server host graphite_host, % graphite server host
graphite_port % graphite server port graphite_port, % graphite server port
vm_metrics % flag to enable sending VM metrics on flush
}). }).


start_link(FlushIntervalMs, GraphiteHost, GraphitePort) -> start_link(FlushIntervalMs, GraphiteHost, GraphitePort, VmMetrics) ->
gen_server:start_link({local, ?MODULE}, gen_server:start_link({local, ?MODULE},
?MODULE, ?MODULE,
[FlushIntervalMs, GraphiteHost, GraphitePort], [FlushIntervalMs, GraphiteHost, GraphitePort, VmMetrics],
[]). []).


%% %%


init([FlushIntervalMs, GraphiteHost, GraphitePort]) -> init([FlushIntervalMs, GraphiteHost, GraphitePort, VmMetrics]) ->
error_logger:info_msg("estatsd will flush stats to ~p:~w every ~wms\n", error_logger:info_msg("estatsd will flush stats to ~p:~w every ~wms\n",
[ GraphiteHost, GraphitePort, FlushIntervalMs ]), [ GraphiteHost, GraphitePort, FlushIntervalMs ]),
ets:new(statsd, [named_table, set]), ets:new(statsd, [named_table, set]),
Expand All @@ -45,7 +46,8 @@ init([FlushIntervalMs, GraphiteHost, GraphitePort]) ->
flush_interval = FlushIntervalMs, flush_interval = FlushIntervalMs,
flush_timer = Tref, flush_timer = Tref,
graphite_host = GraphiteHost, graphite_host = GraphiteHost,
graphite_port = GraphitePort graphite_port = GraphitePort,
vm_metrics = VmMetrics
}, },
{ok, State}. {ok, State}.


Expand Down Expand Up @@ -135,16 +137,18 @@ unixtime() -> {Meg,S,_Mic} = erlang:now(), Meg*1000000 + S.
do_report(All, Gauges, State) -> do_report(All, Gauges, State) ->
% One time stamp string used in all stats lines: % One time stamp string used in all stats lines:
TsStr = num2str(unixtime()), TsStr = num2str(unixtime()),
{MsgCounters, NumCounters} = do_report_counters(All, TsStr, State), {MsgCounters, NumCounters} = do_report_counters(All, TsStr, State),
{MsgTimers, NumTimers} = do_report_timers(TsStr, State), {MsgTimers, NumTimers} = do_report_timers(TsStr, State),
{MsgGauges, NumGauges} = do_report_gauges(Gauges), {MsgGauges, NumGauges} = do_report_gauges(Gauges),
{MsgVmMetrics, NumVmMetrics} = do_report_vm_metrics(TsStr, State),
%% REPORT TO GRAPHITE %% REPORT TO GRAPHITE
case NumTimers + NumCounters + NumGauges of case NumTimers + NumCounters + NumGauges + NumVmMetrics of
0 -> nothing_to_report; 0 -> nothing_to_report;
NumStats -> NumStats ->
FinalMsg = [ MsgCounters, FinalMsg = [ MsgCounters,
MsgTimers, MsgTimers,
MsgGauges, MsgGauges,
MsgVmMetrics,
%% Also graph the number of graphs we're graphing: %% Also graph the number of graphs we're graphing:
"statsd.numStats ", num2str(NumStats), " ", TsStr, "\n" "statsd.numStats ", num2str(NumStats), " ", TsStr, "\n"
], ],
Expand Down Expand Up @@ -217,3 +221,40 @@ do_report_gauges(Gauges) ->
end, [], Gauges end, [], Gauges
), ),
{Msg, length(Gauges)}. {Msg, length(Gauges)}.

do_report_vm_metrics(TsStr, State) ->
case State#state.vm_metrics of
true ->
{TotalReductions, Reductions} = erlang:statistics(reductions),
{NumberOfGCs, WordsReclaimed, _} = erlang:statistics(garbage_collection),
{{input, Input}, {output, Output}} = erlang:statistics(io),
RunQueue = erlang:statistics(run_queue),
StatsData = [
{process_count, erlang:system_info(process_count)},
{reductions, Reductions},
{total_reductions, TotalReductions},
{number_of_gcs, NumberOfGCs},
{words_reclaimed, WordsReclaimed},
{input, Input},
{output, Output},
{run_queue, RunQueue}
],
StatsMsg = lists:map(fun({Key, Val}) ->
[
"stats.vm.stats.", key2str(Key), " ",
io_lib:format("~w", [Val]), " ",
TsStr, "\n"
]
end, StatsData),
MemoryMsg = lists:map(fun({Key, Val}) ->
[
"stats.vm.memory.", key2str(Key), " ",
io_lib:format("~w", [Val]), " ",
TsStr, "\n"
]
end, erlang:memory()),
Msg = StatsMsg ++ MemoryMsg;
false ->
Msg = []
end,
{Msg, length(Msg)}.
14 changes: 9 additions & 5 deletions src/estatsd_sup.erl
Expand Up @@ -11,32 +11,36 @@
-define(FLUSH_INTERVAL, appvar(flush_interval, 10000)). -define(FLUSH_INTERVAL, appvar(flush_interval, 10000)).
-define(GRAPHITE_HOST, appvar(graphite_host, "127.0.0.1")). -define(GRAPHITE_HOST, appvar(graphite_host, "127.0.0.1")).
-define(GRAPHITE_PORT, appvar(graphite_port, 2003)). -define(GRAPHITE_PORT, appvar(graphite_port, 2003)).
-define(VM_METRICS, appvar(vm_metrics, true)).


%% =================================================================== %% ===================================================================
%% API functions %% API functions
%% =================================================================== %% ===================================================================




start_link() -> start_link() ->
start_link( ?FLUSH_INTERVAL, ?GRAPHITE_HOST, ?GRAPHITE_PORT). start_link( ?FLUSH_INTERVAL, ?GRAPHITE_HOST, ?GRAPHITE_PORT, ?VM_METRICS).


start_link(FlushIntervalMs) -> start_link(FlushIntervalMs) ->
start_link( FlushIntervalMs, ?GRAPHITE_HOST, ?GRAPHITE_PORT). start_link( FlushIntervalMs, ?GRAPHITE_HOST, ?GRAPHITE_PORT, ?VM_METRICS).


start_link(FlushIntervalMs, GraphiteHost, GraphitePort) -> start_link(FlushIntervalMs, GraphiteHost, GraphitePort) ->
start_link( FlushIntervalMs, GraphiteHost, GraphitePort, ?VM_METRICS).

start_link(FlushIntervalMs, GraphiteHost, GraphitePort, VmMetrics) ->
supervisor:start_link({local, ?MODULE}, supervisor:start_link({local, ?MODULE},
?MODULE, ?MODULE,
[FlushIntervalMs, GraphiteHost, GraphitePort]). [FlushIntervalMs, GraphiteHost, GraphitePort, VmMetrics]).


%% =================================================================== %% ===================================================================
%% Supervisor callbacks %% Supervisor callbacks
%% =================================================================== %% ===================================================================


init([FlushIntervalMs, GraphiteHost, GraphitePort]) -> init([FlushIntervalMs, GraphiteHost, GraphitePort, VmMetrics]) ->
Children = [ Children = [
{estatsd_server, {estatsd_server,
{estatsd_server, start_link, {estatsd_server, start_link,
[FlushIntervalMs, GraphiteHost, GraphitePort]}, [FlushIntervalMs, GraphiteHost, GraphitePort, VmMetrics]},
permanent, 5000, worker, [estatsd_server]} permanent, 5000, worker, [estatsd_server]}
], ],
{ok, { {one_for_one, 10000, 10}, Children} }. {ok, { {one_for_one, 10000, 10}, Children} }.
Expand Down

0 comments on commit ea6b9f3

Please sign in to comment.