refactor: update emqx_prometheus's headers from map() to list()

This commit is contained in:
Zhongwen Deng 2023-01-12 12:48:26 +08:00
parent 9f6df27759
commit 1194d07a07
3 changed files with 21 additions and 12 deletions

View File

@ -708,6 +708,8 @@ typename_to_spec("qos()", _Mod) ->
#{type => integer, minimum => 0, maximum => 2, example => 0}; #{type => integer, minimum => 0, maximum => 2, example => 0};
typename_to_spec("{binary(), binary()}", _Mod) -> typename_to_spec("{binary(), binary()}", _Mod) ->
#{type => object, example => #{}}; #{type => object, example => #{}};
typename_to_spec("{string(), string()}", _Mod) ->
#{type => object, example => #{}};
typename_to_spec("comma_separated_list()", _Mod) -> typename_to_spec("comma_separated_list()", _Mod) ->
#{type => string, example => <<"item1,item2">>}; #{type => string, example => <<"item1,item2">>};
typename_to_spec("comma_separated_binary()", _Mod) -> typename_to_spec("comma_separated_binary()", _Mod) ->

View File

@ -112,7 +112,7 @@ handle_info({timeout, Timer, ?TIMER_MSG}, State = #{timer := Timer}) ->
handle_info(_Msg, State) -> handle_info(_Msg, State) ->
{noreply, State}. {noreply, State}.
push_to_push_gateway(Uri, Headers0, JobName) when is_map(Headers0) -> push_to_push_gateway(Uri, Headers, JobName) when is_list(Headers) ->
[Name, Ip] = string:tokens(atom_to_list(node()), "@"), [Name, Ip] = string:tokens(atom_to_list(node()), "@"),
JobName1 = emqx_placeholder:preproc_tmpl(JobName), JobName1 = emqx_placeholder:preproc_tmpl(JobName),
JobName2 = binary_to_list( JobName2 = binary_to_list(
@ -124,21 +124,15 @@ push_to_push_gateway(Uri, Headers0, JobName) when is_map(Headers0) ->
Url = lists:concat([Uri, "/metrics/job/", JobName2]), Url = lists:concat([Uri, "/metrics/job/", JobName2]),
Data = prometheus_text_format:format(), Data = prometheus_text_format:format(),
Headers = maps:fold(
fun(K, V, Acc) ->
[{atom_to_list(K), binary_to_list(V)} | Acc]
end,
[],
Headers0
),
case httpc:request(post, {Url, Headers, "text/plain", Data}, ?HTTP_OPTIONS, []) of case httpc:request(post, {Url, Headers, "text/plain", Data}, ?HTTP_OPTIONS, []) of
{ok, {{"HTTP/1.1", 200, _}, _Headers, _Body}} -> {ok, {{"HTTP/1.1", 200, _}, _RespHeaders, _RespBody}} ->
ok; ok;
Error -> Error ->
?SLOG(error, #{ ?SLOG(error, #{
msg => "post_to_push_gateway_failed", msg => "post_to_push_gateway_failed",
error => Error, error => Error,
url => Url url => Url,
headers => Headers
}), }),
failed failed
end. end.

View File

@ -25,7 +25,8 @@
roots/0, roots/0,
fields/1, fields/1,
desc/1, desc/1,
translation/1 translation/1,
convert_headers/1
]). ]).
namespace() -> "prometheus". namespace() -> "prometheus".
@ -54,10 +55,11 @@ fields("prometheus") ->
)}, )},
{headers, {headers,
?HOCON( ?HOCON(
map(), list({string(), string()}),
#{ #{
default => #{}, default => #{},
required => false, required => false,
converter => fun ?MODULE:convert_headers/1,
desc => ?DESC(headers) desc => ?DESC(headers)
} }
)}, )},
@ -145,6 +147,17 @@ fields("prometheus") ->
desc("prometheus") -> ?DESC(prometheus); desc("prometheus") -> ?DESC(prometheus);
desc(_) -> undefined. desc(_) -> undefined.
convert_headers(Headers) when is_map(Headers) ->
maps:fold(
fun(K, V, Acc) ->
[{binary_to_list(K), binary_to_list(V)} | Acc]
end,
[],
Headers
);
convert_headers(Headers) when is_list(Headers) ->
Headers.
%% for CI test, CI don't load the whole emqx_conf_schema. %% for CI test, CI don't load the whole emqx_conf_schema.
translation(Name) -> translation(Name) ->
emqx_conf_schema:translation(Name). emqx_conf_schema:translation(Name).