Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow name to deal with floats/ints #5

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .rebar3/erlcinfo
Binary file not shown.
3 changes: 1 addition & 2 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
%% -*- erlang -*-
{deps,
[
{exometer_core, ".*", {git, "git://github.com/Feuerlabs/exometer_core.git", "09c2a93"}},
{afunix, ".*", {git, "git://github.com/tonyrog/afunix.git", "180eed9"}}
{exometer_core, ".*", {git, "git://github.com/Feuerlabs/exometer_core.git", "09c2a93"}}
]}.

{erl_opts,
Expand Down
2 changes: 1 addition & 1 deletion src/exometer_collectd.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{vsn, git},
{registered, []},
{applications, [kernel, stdlib,
lager, afunix, exometer_core]},
lager, exometer_core]},
{included_applications, []},
{env, []}
]}.
29 changes: 15 additions & 14 deletions src/exometer_report_collectd.erl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
-define(UNIX_EPOCH, 62167219200).

exometer_init(Opts) ->
?log(info, "exometer_report_collectd(~p): Starting~n", [Opts]),
?log(info, "Exometer(~p): Starting~n", [Opts]),
SockPath = get_opt(path, Opts, ?DEFAULT_PATH),
ConnectTimeout = get_opt(connect_timeout, Opts, ?CONNECT_TIMEOUT),
ReconnectInterval =
Expand Down Expand Up @@ -119,6 +119,7 @@ exometer_init(Opts) ->
},
case connect_collectd(SockPath, ConnectTimeout) of
{ok, Sock} ->
?log(info, "Exometer collectd connection succeeded"),
{ok, St0#st{socket = Sock}};
{error, _} = Error ->
?log(warning, "Exometer collectd connection failed; ~p. Retry in ~p~n",
Expand Down Expand Up @@ -160,7 +161,6 @@ exometer_unsubscribe(Metric, DataPoint, _Extra, St) ->
%% Exometer report when no collectd socket connection exists.
exometer_report(_Metric, _DataPoint, _Extra, _Value, St)
when St#st.socket =:= undefined ->
?log(warning, "Report metric: No connection. Value lost~n"),
{ok, St};

%% Invoked through the remote_exometer() function to
Expand Down Expand Up @@ -208,21 +208,16 @@ exometer_info({exometer_callback, refresh_metric,
%% the entry, and we should do nothing.
case ets:lookup(exometer_collectd, ets_key(Metric, DataPoint)) of
[] ->
?log(debug, "refresh_metric(~p, ~p): No longer subscribed~n",
[Metric, DataPoint]),
{ok, St};
[{_, _TRef}] ->
?log(info, "Refreshing metric ~p_~p = ~p~n",
[Metric, DataPoint, Value]),
{ok, report_exometer_(Metric, DataPoint, Extra, Value, St)}
end;
exometer_info({exometer_callback, reconnect}, St) ->
?log(info, "Reconnecting: ~p~n", [St]),
case connect_collectd(St) of
{ok, NSt} ->
{ok, NSt};
Err ->
?log(warning, "Could not reconnect: ~p~n", [Err]),
Err,
prepare_reconnect(),
{ok, St}
end;
Expand All @@ -247,7 +242,7 @@ report_exometer_(Metric, DataPoint, Extra, Value,
type_map = TypeMap} = St) ->
case get_type(TypeMap, Extra, ets_key(Metric, DataPoint)) of
error ->
?log(warning,
?log(warning,
"Could not resolve ~p to a collectd type."
"Update exometer_report_collectd -> type_map in app.config. "
"Value lost~n", [ets_key(Metric, DataPoint)]),
Expand All @@ -262,9 +257,9 @@ report_exometer_(Metric, DataPoint, Extra, Value,

send_request(Sock, Request, Metric, DataPoint, Extra, Value,
#st{read_timeout = TOut} = St) ->
try afunix:send(Sock, Request) of
try gen_tcp:send(Sock, Request) of
ok ->
case afunix:recv(Sock, 0, TOut) of
case gen_tcp:recv(Sock, 0, TOut) of
{ok, Bin} ->
%% Parse the reply
case parse_reply(Request, Bin, St) of
Expand Down Expand Up @@ -310,8 +305,14 @@ ets_key(Metric, DataPoint) ->
Metric ++ [ DataPoint ].

%% Add metric and datapoint within metric
name(Metric, DataPoint) when is_integer(DataPoint) or is_float(DataPoint) ->
name(Metric, value(DataPoint));

name(Metric, DataPoint) when is_atom(DataPoint) ->
name(Metric, atom_to_list(DataPoint));

name(Metric, DataPoint) ->
metric_to_string(Metric) ++ "_" ++ atom_to_list(DataPoint).
metric_to_string(Metric) ++ "_" ++ DataPoint.

metric_to_string([Final]) ->
metric_elem_to_list(Final);
Expand Down Expand Up @@ -345,7 +346,7 @@ connect_collectd(St) ->
end.

connect_collectd(SocketPath, ConnectTimeout) ->
afunix:connect(SocketPath, [{active, false}, {mode, binary}], ConnectTimeout).
gen_tcp:connect({local, SocketPath}, 0, [{active, false}, {mode, binary}, local], ConnectTimeout).

unix_time() ->
datetime_to_unix_time(erlang:universaltime()).
Expand Down Expand Up @@ -416,7 +417,7 @@ get_type(TypeMap, Extra, Name) ->

maybe_reconnect_after(Socket) ->
%% Close socket if open
if Socket =/= undefined -> afunix:close(Socket);
if Socket =/= undefined -> gen_tcp:close(Socket);
true -> true
end,
prepare_reconnect().
Expand Down