Jelajahi Sumber

Merge pull request #14091 from terry-xiaoyu/improve-log-readability-influxdb-ts

fix(influxdb): unsupported write syntax for timestamp field
Xinyu Liu 1 tahun lalu
induk
melakukan
9d74b1a5da

+ 4 - 2
apps/emqx_bridge_datalayers/test/emqx_bridge_datalayers_SUITE.erl

@@ -874,7 +874,9 @@ t_bad_timestamp(Config) ->
                         [
                             #{
                                 error := [
-                                    {error, {bad_timestamp, <<"bad_timestamp">>}}
+                                    {error,
+                                        {bad_timestamp,
+                                            {non_integer_timestamp, <<"bad_timestamp">>}}}
                                 ]
                             }
                         ],
@@ -883,7 +885,7 @@ t_bad_timestamp(Config) ->
                 {sync, false} ->
                     ?assertEqual(
                         {error, [
-                            {error, {bad_timestamp, <<"bad_timestamp">>}}
+                            {error, {bad_timestamp, {non_integer_timestamp, <<"bad_timestamp">>}}}
                         ]},
                         Return
                     );

+ 7 - 2
apps/emqx_bridge_influxdb/src/emqx_bridge_influxdb_connector.erl

@@ -714,8 +714,13 @@ parse_timestamp([TsBin]) ->
         {ok, binary_to_integer(TsBin)}
     catch
         _:_ ->
-            {error, TsBin}
-    end.
+            {error, {non_integer_timestamp, TsBin}}
+    end;
+parse_timestamp(InvalidTs) ->
+    %% The timestamp field must be a single integer or a single placeholder. i.e. the
+    %%   following is not allowed:
+    %%   - weather,location=us-midwest,season=summer temperature=82 ${timestamp}00
+    {error, {unsupported_placeholder_usage_for_timestamp, InvalidTs}}.
 
 continue_lines_to_points(Data, Item, Rest, ResultPointsAcc, ErrorPointsAcc) ->
     case line_to_point(Data, Item) of

+ 8 - 4
apps/emqx_bridge_influxdb/test/emqx_bridge_influxdb_SUITE.erl

@@ -915,6 +915,10 @@ t_tag_set_use_literal_value(Config) ->
     ?assertEqual(TsStr, TimeReturned).
 
 t_bad_timestamp(Config) ->
+    test_bad_timestamp(Config, <<"bad_timestamp">>, non_integer_timestamp),
+    test_bad_timestamp(Config, <<"${timestamp}000">>, unsupported_placeholder_usage_for_timestamp).
+
+test_bad_timestamp(Config, Timestamp, ErrTag) ->
     InfluxDBType = ?config(influxdb_type, Config),
     InfluxDBName = ?config(influxdb_name, Config),
     QueryMode = ?config(query_mode, Config),
@@ -929,7 +933,7 @@ t_bad_timestamp(Config) ->
         %% N.B.: this single space characters are relevant
         <<"${topic}", " ", "payload=${payload},", "${clientid}_int_value=${payload.int_key}i,",
             "uint_value=${payload.uint_key}u,"
-            "bool=${payload.bool}", " ", "bad_timestamp">>,
+            "bool=${payload.bool}", " ", Timestamp/binary>>,
     %% append this to override the config
     InfluxDBConfigString1 =
         io_lib:format(
@@ -983,16 +987,16 @@ t_bad_timestamp(Config) ->
                         [
                             #{
                                 error := [
-                                    {error, {bad_timestamp, <<"bad_timestamp">>}}
+                                    {error, {bad_timestamp, {ErrTag, _}}}
                                 ]
                             }
                         ],
                         ?of_kind(influxdb_connector_send_query_error, Trace)
                     );
                 {sync, false} ->
-                    ?assertEqual(
+                    ?assertMatch(
                         {error, [
-                            {error, {bad_timestamp, <<"bad_timestamp">>}}
+                            {error, {bad_timestamp, {ErrTag, _}}}
                         ]},
                         Return
                     );

+ 11 - 0
changes/ee/fix-14091.en.md

@@ -0,0 +1,11 @@
+A simple fix that removes `function_clause` from the log message if the user has provided an unsupported write syntax:
+
+```
+weather,location=us-midwest,season=summer temperature=82 ${timestamp}u
+```
+
+The error log before this fix:
+
+```
+pid: <0.558392.0>, info: {"stacktrace":["{emqx_bridge_influxdb_connector,parse_timestamp,[[1719350482910000000,<<\"u\">>]],[{file,\"emqx_bridge_influxdb_connector.erl\"},{line,692}]}", ...], ..., "error":"{error,function_clause}"}, tag: ERROR, msg: resource_exception
+```