Skip to content

Commit da27d18

Browse files
Merge pull request #14972 from rabbitmq/mergify/bp/v4.1.x/pr-14970
rabbitmq_tracing: explicitly specify UTF-8 encoding for served trace files (backport #14966) (backport #14970)
2 parents 4a9806c + f2c2c0b commit da27d18

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

deps/rabbitmq_tracing/src/rabbit_tracing_wm_file.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
-module(rabbit_tracing_wm_file).
88

99
-export([init/2, resource_exists/2, serve/2, content_types_provided/2,
10-
is_authorized/2, allowed_methods/2, delete_resource/2]).
10+
charsets_provided/2, is_authorized/2, allowed_methods/2, delete_resource/2]).
1111
-export([serve/1]).
1212

1313
-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
@@ -20,6 +20,9 @@ init(Req, _State) ->
2020
content_types_provided(ReqData, Context) ->
2121
{[{<<"text/plain">>, serve}], ReqData, Context}.
2222

23+
charsets_provided(ReqData, Context) ->
24+
{[<<"utf-8">>], ReqData, Context}.
25+
2326
allowed_methods(ReqData, Context) ->
2427
{[<<"HEAD">>, <<"GET">>, <<"DELETE">>], ReqData, Context}.
2528

deps/rabbitmq_tracing/test/rabbit_tracing_SUITE.erl

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ groups() ->
2424
[
2525
{non_parallel_tests, [], [
2626
tracing_test,
27-
tracing_validation_test
27+
tracing_validation_test,
28+
trace_file_content_type_test
2829
]}
2930
].
3031

@@ -92,9 +93,10 @@ tracing_test(Config) ->
9293
#amqp_msg{props = #'P_basic'{},
9394
payload = <<"Hello world">>}),
9495

95-
rabbit_ct_client_helpers:close_channel(Ch),
96-
97-
timer:sleep(100),
96+
rabbit_ct_helpers:await_condition(fun() ->
97+
TraceFiles = http_get(Config, "/trace-files/"),
98+
lists:any(fun(#{name := Name}) -> Name =:= <<"test.log">> end, TraceFiles)
99+
end),
98100

99101
http_delete(Config, "/traces/%2f/test", ?NO_CONTENT),
100102
[] = http_get(Config, "/traces/%2f/"),
@@ -128,6 +130,36 @@ tracing_validation_test(Config) ->
128130
http_delete(Config, Path, ?NO_CONTENT),
129131
ok.
130132

133+
trace_file_content_type_test(Config) ->
134+
case filelib:is_dir(?LOG_DIR) of
135+
true -> {ok, Files} = file:list_dir(?LOG_DIR),
136+
[ok = file:delete(?LOG_DIR ++ F) || F <- Files];
137+
_ -> ok
138+
end,
139+
140+
Args = #{format => <<"text">>,
141+
pattern => <<"#">>},
142+
http_put(Config, "/traces/%2f/test-charset", Args, ?CREATED),
143+
144+
Ch = rabbit_ct_client_helpers:open_channel(Config),
145+
amqp_channel:cast(Ch, #'basic.publish'{ exchange = <<"amq.topic">>,
146+
routing_key = <<"key">> },
147+
#amqp_msg{props = #'P_basic'{},
148+
payload = <<"Test message">>}),
149+
150+
rabbit_ct_helpers:await_condition(fun() ->
151+
TraceFiles = http_get(Config, "/trace-files/"),
152+
lists:any(fun(#{name := Name}) -> Name =:= <<"test-charset.log">> end, TraceFiles)
153+
end),
154+
155+
http_delete(Config, "/traces/%2f/test-charset", ?NO_CONTENT),
156+
Headers = http_get_headers(Config, "/trace-files/test-charset.log"),
157+
ContentType = proplists:get_value("content-type", Headers),
158+
?assertEqual(match, re:run(ContentType, "text/plain", [{capture, none}])),
159+
?assertEqual(match, re:run(ContentType, "charset=utf-8", [{capture, none}])),
160+
http_delete(Config, "/trace-files/test-charset.log", ?NO_CONTENT),
161+
ok.
162+
131163
%%---------------------------------------------------------------------------
132164
%% TODO: Below is copied from rabbit_mgmt_test_http,
133165
%% should be moved to use rabbit_mgmt_test_util once rabbitmq_management
@@ -154,6 +186,15 @@ http_get_raw(Config, Path, User, Pass, CodeExp) ->
154186
assert_code(CodeExp, CodeAct, "GET", Path, ResBody),
155187
ResBody.
156188

189+
http_get_headers(Config, Path) ->
190+
http_get_headers(Config, Path, "guest", "guest", ?OK).
191+
192+
http_get_headers(Config, Path, User, Pass, CodeExp) ->
193+
{ok, {{_HTTP, CodeAct, _}, Headers, ResBody}} =
194+
req(Config, get, Path, [auth_header(User, Pass)]),
195+
assert_code(CodeExp, CodeAct, "GET", Path, ResBody),
196+
Headers.
197+
157198
http_put(Config, Path, List, CodeExp) ->
158199
http_put_raw(Config, Path, format_for_upload(List), CodeExp).
159200

0 commit comments

Comments
 (0)