Преглед изворни кода

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
Serge Tupchii пре 3 година
родитељ
комит
b3907128e8
2 измењених фајлова са 16 додато и 2 уклоњено
  1. 1 1
      apps/emqx_statsd/src/emqx_statsd.app.src
  2. 15 1
      apps/emqx_statsd/src/emqx_statsd.erl

+ 1 - 1
apps/emqx_statsd/src/emqx_statsd.app.src

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

+ 15 - 1
apps/emqx_statsd/src/emqx_statsd.erl

@@ -79,6 +79,7 @@ init(Conf) ->
         sample_time_interval := SampleTimeInterval,
         flush_time_interval := FlushTimeInterval
     } = Conf,
+    FlushTimeInterval1 = flush_interval(FlushTimeInterval, SampleTimeInterval),
     {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),
     Opts = [{tags, Tags}, {host, Host}, {port, Port}, {prefix, <<"emqx">>}],
@@ -86,7 +87,7 @@ init(Conf) ->
     {ok,
         ensure_timer(#{
             sample_time_interval => SampleTimeInterval,
-            flush_time_interval => FlushTimeInterval,
+            flush_time_interval => FlushTimeInterval1,
             estatsd_pid => Pid
         })}.
 
@@ -129,6 +130,19 @@ terminate(_Reason, #{estatsd_pid := Pid}) ->
 %% 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}) ->
     State#{timer => emqx_misc:start_timer(SampleTimeInterval, ?SAMPLE_TIMEOUT)}.