fix: set statsd flush_time_interval = max(flush_time_interval, sample_time_interval)

flush_time_interval is used to calculate statsd sampling rate:
    rate = sample_time_interval / flush_time_interval
This means that flush_time_interval must always be greater than (or equal to)
sample_time_interval, otherwise, the sampling rate will be invalid (> 1).

Relates to EMQX-9055
This commit is contained in:
Serge Tupchii 2023-03-03 18:58:45 +02:00
parent bff087f40a
commit b3907128e8
2 changed files with 16 additions and 2 deletions

View File

@ -1,7 +1,7 @@
%% -*- mode: erlang -*- %% -*- mode: erlang -*-
{application, emqx_statsd, [ {application, emqx_statsd, [
{description, "EMQX Statsd"}, {description, "EMQX Statsd"},
{vsn, "5.0.5"}, {vsn, "5.0.6"},
{registered, []}, {registered, []},
{mod, {emqx_statsd_app, []}}, {mod, {emqx_statsd_app, []}},
{applications, [ {applications, [

View File

@ -79,6 +79,7 @@ init(Conf) ->
sample_time_interval := SampleTimeInterval, sample_time_interval := SampleTimeInterval,
flush_time_interval := FlushTimeInterval flush_time_interval := FlushTimeInterval
} = Conf, } = Conf,
FlushTimeInterval1 = flush_interval(FlushTimeInterval, SampleTimeInterval),
{Host, Port} = emqx_schema:parse_server(Server, ?SERVER_PARSE_OPTS), {Host, Port} = emqx_schema:parse_server(Server, ?SERVER_PARSE_OPTS),
Tags = maps:fold(fun(K, V, Acc) -> [{to_bin(K), to_bin(V)} | Acc] end, [], TagsRaw), Tags = maps:fold(fun(K, V, Acc) -> [{to_bin(K), to_bin(V)} | Acc] end, [], TagsRaw),
Opts = [{tags, Tags}, {host, Host}, {port, Port}, {prefix, <<"emqx">>}], Opts = [{tags, Tags}, {host, Host}, {port, Port}, {prefix, <<"emqx">>}],
@ -86,7 +87,7 @@ init(Conf) ->
{ok, {ok,
ensure_timer(#{ ensure_timer(#{
sample_time_interval => SampleTimeInterval, sample_time_interval => SampleTimeInterval,
flush_time_interval => FlushTimeInterval, flush_time_interval => FlushTimeInterval1,
estatsd_pid => Pid estatsd_pid => Pid
})}. })}.
@ -129,6 +130,19 @@ terminate(_Reason, #{estatsd_pid := Pid}) ->
%% Internal function %% Internal function
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
flush_interval(FlushInterval, SampleInterval) when FlushInterval >= SampleInterval ->
FlushInterval;
flush_interval(_FlushInterval, SampleInterval) ->
?SLOG(
warning,
#{
msg =>
"Configured flush_time_interval is lower than sample_time_interval, "
"setting: flush_time_interval = sample_time_interval."
}
),
SampleInterval.
ensure_timer(State = #{sample_time_interval := SampleTimeInterval}) -> ensure_timer(State = #{sample_time_interval := SampleTimeInterval}) ->
State#{timer => emqx_misc:start_timer(SampleTimeInterval, ?SAMPLE_TIMEOUT)}. State#{timer => emqx_misc:start_timer(SampleTimeInterval, ?SAMPLE_TIMEOUT)}.