Kaynağa Gözat

fix(opents): change the schema of tags to object style

firest 2 yıl önce
ebeveyn
işleme
5291adf2c0

+ 19 - 32
apps/emqx_bridge_opents/src/emqx_bridge_opents.erl

@@ -135,6 +135,14 @@ fields(action_parameters) ->
             )}
     ];
 fields(action_parameters_data) ->
+    TagsError = fun(Data) ->
+        ?SLOG(warning, #{
+            msg => "invalid_tags_template",
+            path => "opents.parameters.data.tags",
+            data => Data
+        }),
+        false
+    end,
     [
         {timestamp,
             mk(
@@ -154,7 +162,7 @@ fields(action_parameters_data) ->
             )},
         {tags,
             mk(
-                hoconsc:union([array(ref(?MODULE, action_parameters_data_tags)), binary()]),
+                hoconsc:union([map(), binary()]),
                 #{
                     required => true,
                     desc => ?DESC("config_parameters_tags"),
@@ -164,17 +172,17 @@ fields(action_parameters_data) ->
                                 [{var, _}] ->
                                     true;
                                 _ ->
-                                    ?SLOG(warning, #{
-                                        msg => "invalid_tags_template",
-                                        path => "opents.parameters.data.tags",
-                                        data => Tmpl
-                                    }),
-                                    false
+                                    TagsError(Tmpl)
                             end;
-                        ([_ | _] = Tags) when is_list(Tags) ->
-                            true;
-                        (_) ->
-                            false
+                        (Map) when is_map(Map) ->
+                            case maps:size(Map) >= 1 of
+                                true ->
+                                    true;
+                                _ ->
+                                    TagsError(Map)
+                            end;
+                        (Any) ->
+                            TagsError(Any)
                     end
                 }
             )},
@@ -187,25 +195,6 @@ fields(action_parameters_data) ->
                 }
             )}
     ];
-fields(action_parameters_data_tags) ->
-    [
-        {tag,
-            mk(
-                binary(),
-                #{
-                    required => true,
-                    desc => ?DESC("tags_tag")
-                }
-            )},
-        {value,
-            mk(
-                binary(),
-                #{
-                    required => true,
-                    desc => ?DESC("tags_value")
-                }
-            )}
-    ];
 fields("post_bridge_v2") ->
     emqx_bridge_schema:type_and_name_fields(enum([opents])) ++ fields(action_config);
 fields("put_bridge_v2") ->
@@ -221,8 +210,6 @@ desc(action_parameters) ->
     ?DESC("action_parameters");
 desc(action_parameters_data) ->
     ?DESC("action_parameters_data");
-desc(action_parameters_data_tags) ->
-    ?DESC("action_parameters_data_tags");
 desc(Method) when Method =:= "get"; Method =:= "put"; Method =:= "post" ->
     ["Configuration for OpenTSDB using `", string:to_upper(Method), "` method."];
 desc(_) ->

+ 3 - 3
apps/emqx_bridge_opents/src/emqx_bridge_opents_connector.erl

@@ -358,15 +358,15 @@ preproc_data_template(DataList) ->
                 case Tags of
                     Tmpl when is_binary(Tmpl) ->
                         emqx_placeholder:preproc_tmpl(Tmpl);
-                    List ->
+                    Map when is_map(Map) ->
                         [
                             tags
                             | [
                                 {
-                                    emqx_placeholder:preproc_tmpl(TagName),
+                                    emqx_placeholder:preproc_tmpl(emqx_utils_conv:bin(TagName)),
                                     emqx_placeholder:preproc_tmpl(TagValue)
                                 }
-                             || #{tag := TagName, value := TagValue} <- List
+                             || {TagName, TagValue} <- maps:to_list(Map)
                             ]
                         ]
                 end,

+ 2 - 2
apps/emqx_bridge_opents/test/emqx_bridge_opents_SUITE.erl

@@ -311,7 +311,7 @@ t_list_tags(Config) ->
                 <<"data">> => [
                     #{
                         <<"metric">> => <<"${metric}">>,
-                        <<"tags">> => [#{<<"tag">> => <<"host">>, <<"value">> => <<"valueA">>}],
+                        <<"tags">> => #{<<"host">> => <<"valueA">>},
                         value => <<"${value}">>
                     }
                 ]
@@ -356,7 +356,7 @@ t_list_tags_with_var(Config) ->
                 <<"data">> => [
                     #{
                         <<"metric">> => <<"${metric}">>,
-                        <<"tags">> => [#{<<"tag">> => <<"host">>, <<"value">> => <<"${value}">>}],
+                        <<"tags">> => #{<<"host">> => <<"${value}">>},
                         value => <<"${value}">>
                     }
                 ]

+ 4 - 22
rel/i18n/emqx_bridge_opents.hocon

@@ -37,45 +37,27 @@ action_parameters_data.label:
 """Parameter Data"""
 
 config_parameters_timestamp.desc:
-"""Timestamp. Placeholders in format of ${var} is supported"""
+"""Timestamp. Placeholders in the format of ${var} are supported"""
 
 config_parameters_timestamp.label:
 """Timestamp"""
 
 config_parameters_metric.desc:
-"""Metric. Placeholders in format of ${var} is supported"""
+"""Metric. Placeholders in the format of ${var} are supported"""
 
 config_parameters_metric.label:
 """Metric"""
 
 config_parameters_tags.desc:
-"""Tags. Only supports with placeholder to extract tags from a variable or a list of tags"""
+"""Tags. Only supports with placeholder to extract tags from a variable or a tags map"""
 
 config_parameters_tags.label:
 """Tags"""
 
 config_parameters_value.desc:
-"""Value. Placeholders in format of ${var} is supported"""
+"""Value. Placeholders in the format of ${var} are supported"""
 
 config_parameters_value.label:
 """Value"""
 
-action_parameters_data_tags.desc:
-"""OpenTSDB data tags"""
-
-action_parameters_data_tags.label:
-"""Tags"""
-
-tags_tag.desc:
-"""The name of this tag. Placeholders in format of ${var} is supported"""
-
-tags_tag.label:
-"""Tag"""
-
-tags_value.desc:
-"""The value of this tag. Placeholders in format of ${var} is supported"""
-
-tags_value.label:
-"""Value"""
-
 }