refactor(config): support hocon
This commit is contained in:
parent
8356102450
commit
9bc3b4684a
|
@ -2,41 +2,12 @@
|
||||||
## Statsd for EMQ X
|
## Statsd for EMQ X
|
||||||
##--------------------------------------------------------------------
|
##--------------------------------------------------------------------
|
||||||
|
|
||||||
## The statsd server host
|
emqx_statsd:{
|
||||||
##
|
# statsd server
|
||||||
## Default: "127.0.0.1"
|
server: "127.0.0.1:8125"
|
||||||
statsd.host = "127.0.0.1"
|
batch_size: 10
|
||||||
|
prefix: "emqx"
|
||||||
## The statsd server port
|
tags: {"from": "emqx"}
|
||||||
##
|
sample_time_interval: "10s"
|
||||||
## Default: "127.0.0.1"
|
flush_time_interval: "10s"
|
||||||
statsd.port = 8125
|
}
|
||||||
|
|
||||||
## statsd prefix
|
|
||||||
##
|
|
||||||
## Default: emqx
|
|
||||||
# statsd.prefix = emqx
|
|
||||||
|
|
||||||
## statsd tag key
|
|
||||||
##
|
|
||||||
# statsd.tag.1.key = from
|
|
||||||
|
|
||||||
## statsd tag value
|
|
||||||
##
|
|
||||||
# statsd.tag.1.value = emqx
|
|
||||||
|
|
||||||
## statsd batch_size
|
|
||||||
##
|
|
||||||
## Default: 10
|
|
||||||
statsd.batch_size = 10
|
|
||||||
|
|
||||||
## statsd sample time interval
|
|
||||||
##
|
|
||||||
## Default: 10s
|
|
||||||
statsd.sample_time_interval = 10s
|
|
||||||
|
|
||||||
## statsd flush time interval
|
|
||||||
##
|
|
||||||
## Default: 10s
|
|
||||||
statsd.flush_time_interval = 10s
|
|
||||||
|
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
%% emqx_statsd config
|
|
||||||
|
|
||||||
{mapping, "statsd.host", "emqx_statsd.host", [
|
|
||||||
{default, "127.0.0.1"},
|
|
||||||
{datatype, string}
|
|
||||||
]}.
|
|
||||||
|
|
||||||
{mapping, "statsd.port", "emqx_statsd.port", [
|
|
||||||
{default, 8125},
|
|
||||||
{datatype, integer}
|
|
||||||
]}.
|
|
||||||
|
|
||||||
{mapping, "statsd.prefix", "emqx_statsd.prefix", [
|
|
||||||
{datatype, string}
|
|
||||||
]}.
|
|
||||||
|
|
||||||
{mapping, "statsd.tag.$id.key", "emqx_statsd.tag", [
|
|
||||||
{datatype, string}
|
|
||||||
]}.
|
|
||||||
|
|
||||||
{mapping, "statsd.tag.$id.value", "emqx_statsd.tag", [
|
|
||||||
{datatype, string}
|
|
||||||
]}.
|
|
||||||
|
|
||||||
{mapping, "statsd.batch_size", "emqx_statsd.batch_size", [
|
|
||||||
{default, 10},
|
|
||||||
{datatype, integer}
|
|
||||||
]}.
|
|
||||||
|
|
||||||
{mapping, "statsd.sample_time_interval", "emqx_statsd.sample_time_interval", [
|
|
||||||
{default, "2s"},
|
|
||||||
{datatype, {duration, ms}}
|
|
||||||
]}.
|
|
||||||
|
|
||||||
{mapping, "statsd.flush_time_interval", "emqx_statsd.flush_time_interval", [
|
|
||||||
{default, "10s"},
|
|
||||||
{datatype, {duration, ms}}
|
|
||||||
]}.
|
|
||||||
|
|
||||||
{translation, "emqx_stasd.host", fun(Conf) ->
|
|
||||||
{ok, IPAddress} = inet:parse_address(cuttlefish:conf_get("statsd.host", Conf, "127.0.0.1")),
|
|
||||||
IPAddress
|
|
||||||
end}.
|
|
||||||
|
|
||||||
{translation, "emqx_statsd.tag", fun(Conf) ->
|
|
||||||
Tags = cuttlefish_variable:filter_by_prefix("statsd.tag", Conf),
|
|
||||||
lists:foldl(
|
|
||||||
fun({["statsd", "tag", Id, "key"], Key}, AccIn) ->
|
|
||||||
[{Key, cuttlefish:conf_get("statsd." ++ Id ++ ".value", Conf)} | AccIn];
|
|
||||||
(_, AccIn) ->
|
|
||||||
AccIn
|
|
||||||
end, [], Tags)
|
|
||||||
end}.
|
|
|
@ -28,11 +28,11 @@
|
||||||
|
|
||||||
start(_StartType, _StartArgs) ->
|
start(_StartType, _StartArgs) ->
|
||||||
{ok, Sup} = emqx_statsd_sup:start_link(),
|
{ok, Sup} = emqx_statsd_sup:start_link(),
|
||||||
emqx_statsd_sup:start_statsd(),
|
{ok, _} = emqx_statsd_sup:start_statsd(),
|
||||||
?LOG(info, "emqx statsd start: successfully"),
|
?LOG(info, "emqx statsd start: successfully"),
|
||||||
{ok, Sup}.
|
{ok, Sup}.
|
||||||
|
|
||||||
stop(_) ->
|
stop(_) ->
|
||||||
emqx_statsd_sup:stop_statsd(),
|
ok = emqx_statsd_sup:stop_statsd(),
|
||||||
?LOG(info, "emqx statsd stop: successfully"),
|
?LOG(info, "emqx statsd stop: successfully"),
|
||||||
ok.
|
ok.
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
-module(emqx_statsd_schema).
|
||||||
|
|
||||||
|
-include_lib("typerefl/include/types.hrl").
|
||||||
|
|
||||||
|
-behaviour(hocon_schema).
|
||||||
|
|
||||||
|
-export([ structs/0
|
||||||
|
, fields/1]).
|
||||||
|
|
||||||
|
structs() -> ["emqx_statsd"].
|
||||||
|
|
||||||
|
fields("emqx_statsd") ->
|
||||||
|
[ {server, fun server/1}
|
||||||
|
, {prefix, fun prefix/1}
|
||||||
|
, {tags, map()}
|
||||||
|
, {batch_size, fun batch_size/1}
|
||||||
|
, {sample_time_interval, fun duration_s/1}
|
||||||
|
, {flush_time_interval, fun duration_s/1}].
|
||||||
|
|
||||||
|
server(type) -> string();
|
||||||
|
server(default) -> "192.168.1.1:8125";
|
||||||
|
server(not_nullable) -> true;
|
||||||
|
server(_) -> undefined.
|
||||||
|
|
||||||
|
prefix(type) -> string();
|
||||||
|
prefix(default) -> "emqx";
|
||||||
|
prefix(not_nullable) -> false;
|
||||||
|
prefix(_) -> undefined.
|
||||||
|
|
||||||
|
batch_size(type) -> integer();
|
||||||
|
batch_size(not_nullable) -> true;
|
||||||
|
batch_size(default) -> 10;
|
||||||
|
batch_size(_) -> undefined.
|
||||||
|
|
||||||
|
duration_s(type) -> emqx_schema:duration_s();
|
||||||
|
duration_s(not_nullable) -> true;
|
||||||
|
duration_s(default) -> "10s";
|
||||||
|
duration_s(_) -> undefined.
|
|
@ -26,8 +26,8 @@ start_statsd() ->
|
||||||
{ok, _Pid1} = supervisor:start_child(?MODULE, emqx_statsd_child_spec(Pid)).
|
{ok, _Pid1} = supervisor:start_child(?MODULE, emqx_statsd_child_spec(Pid)).
|
||||||
|
|
||||||
stop_statsd() ->
|
stop_statsd() ->
|
||||||
supervisor:terminate_child(emqx_statsd_sup, emqx_statsd),
|
ok = supervisor:terminate_child(?MODULE, emqx_statsd),
|
||||||
supervisor:terminate_child(emqx_statsd_sup, estatsd).
|
ok = supervisor:terminate_child(?MODULE, estatsd).
|
||||||
%%==============================================================================================
|
%%==============================================================================================
|
||||||
%% internal
|
%% internal
|
||||||
estatsd_child_spec() ->
|
estatsd_child_spec() ->
|
||||||
|
@ -39,22 +39,36 @@ estatsd_child_spec() ->
|
||||||
, modules => [estatsd]}.
|
, modules => [estatsd]}.
|
||||||
|
|
||||||
estatsd_options() ->
|
estatsd_options() ->
|
||||||
Host = application:get_env(?APP, host, ?DEFAULT_HOST),
|
Server = get_conf(server, {?DEFAULT_HOST, ?DEFAULT_PORT}),
|
||||||
Port = application:get_env(?APP, port, ?DEFAULT_PORT),
|
{Host, Port} = host_port(Server),
|
||||||
Prefix = application:get_env(?APP, prefix, ?DEFAULT_PREFIX),
|
Prefix = get_conf(prefix, ?DEFAULT_PREFIX),
|
||||||
Tags = application:get_env(?APP, tags, ?DEFAULT_TAGS),
|
Tags = tags(get_conf(tags, ?DEFAULT_TAGS)),
|
||||||
BatchSize = application:get_env(?APP, batch_size, ?DEFAULT_BATCH_SIZE),
|
BatchSize = get_conf(batch_size, ?DEFAULT_BATCH_SIZE),
|
||||||
[{host, Host}, {port, Port}, {prefix, Prefix}, {tags, Tags}, {batch_size, BatchSize}].
|
[{host, Host}, {port, Port}, {prefix, Prefix}, {tags, Tags}, {batch_size, BatchSize}].
|
||||||
|
|
||||||
|
host_port({Host, Port}) -> {Host, Port};
|
||||||
|
host_port(Server) ->
|
||||||
|
case string:tokens(Server, ":") of
|
||||||
|
[Domain] -> {Domain, ?DEFAULT_PORT};
|
||||||
|
[Domain, Port] -> {Domain, list_to_integer(Port)}
|
||||||
|
end.
|
||||||
|
|
||||||
|
tags(Map) ->
|
||||||
|
Tags = maps:to_list(Map),
|
||||||
|
[{atom_to_binary(Key, utf8), Value} || {Key, Value} <- Tags].
|
||||||
|
|
||||||
emqx_statsd_child_spec(Pid) ->
|
emqx_statsd_child_spec(Pid) ->
|
||||||
#{id => emqx_statsd
|
#{id => emqx_statsd
|
||||||
, start => {emqx_statsd, start_link, [emqx_statsd_options(Pid)]}
|
, start => {emqx_statsd, start_link, [[{estatsd_pid, Pid} | emqx_statsd_options()]]}
|
||||||
, restart => permanent
|
, restart => permanent
|
||||||
, shutdown => 5000
|
, shutdown => 5000
|
||||||
, type => worker
|
, type => worker
|
||||||
, modules => [emqx_statsd]}.
|
, modules => [emqx_statsd]}.
|
||||||
|
|
||||||
emqx_statsd_options(Pid) ->
|
emqx_statsd_options() ->
|
||||||
SampleTimeInterval = application:get_env(?APP, sample_time_interval, ?DEFAULT_SAMPLE_TIME_INTERVAL),
|
SampleTimeInterval = get_conf(sample_time_interval, ?DEFAULT_SAMPLE_TIME_INTERVAL),
|
||||||
FlushTimeInterval = application:get_env(?APP, flush_time_interval, ?DEFAULT_FLUSH_TIME_INTERVAL),
|
FlushTimeInterval = get_conf(flush_time_interval, ?DEFAULT_FLUSH_TIME_INTERVAL),
|
||||||
[{estatsd_pid, Pid}, {sample_time_interval, SampleTimeInterval}, {flush_time_interval, FlushTimeInterval}].
|
[{sample_time_interval, SampleTimeInterval}, {flush_time_interval, FlushTimeInterval}].
|
||||||
|
|
||||||
|
get_conf(Key, Default) ->
|
||||||
|
emqx_config:get([?APP, Key], Default).
|
||||||
|
|
Loading…
Reference in New Issue