Skip to content

Commit b517947

Browse files
gomoripetimichaelklishin
authored andcommitted
Add test for stream consumer max offset lag prometheus metric
(cherry picked from commit 8d4de89)
1 parent b4cd0a1 commit b517947

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

deps/rabbitmq_prometheus/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ PROJECT_DESCRIPTION = Prometheus metrics for RabbitMQ
1111
PROJECT_MOD := rabbit_prometheus_app
1212
DEPS = accept cowboy rabbit rabbitmq_management_agent prometheus rabbitmq_web_dispatch
1313
BUILD_DEPS = amqp_client rabbit_common rabbitmq_management
14-
TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers eunit_formatters
14+
TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers eunit_formatters rabbitmq_stream
1515

1616
EUNIT_OPTS = no_tty, {report, {eunit_progress, [colored, profile]}}
1717

deps/rabbitmq_prometheus/test/rabbit_prometheus_http_SUITE.erl

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
-include_lib("common_test/include/ct.hrl").
1212
-include_lib("eunit/include/eunit.hrl").
1313
-include_lib("rabbitmq_ct_helpers/include/rabbit_mgmt_test.hrl").
14+
-include_lib("rabbitmq_ct_helpers/include/rabbit_assert.hrl").
1415

15-
-compile(export_all).
16+
-compile([export_all, nowarn_export_all]).
1617

1718
all() ->
1819
[
@@ -68,7 +69,8 @@ groups() ->
6869
queue_consumer_count_and_queue_metrics_mutually_exclusive_test,
6970
vhost_status_metric,
7071
exchange_bindings_metric,
71-
exchange_names_metric
72+
exchange_names_metric,
73+
stream_pub_sub_metrics
7274
]},
7375
{special_chars, [], [core_metrics_special_chars]},
7476
{authentication, [], [basic_auth]}
@@ -708,6 +710,37 @@ exchange_names_metric(Config) ->
708710
}, Names),
709711
ok.
710712

713+
stream_pub_sub_metrics(Config) ->
714+
Stream1 = atom_to_list(?FUNCTION_NAME) ++ "1",
715+
MsgPerBatch1 = 2,
716+
publish_via_stream_protocol(list_to_binary(Stream1), MsgPerBatch1, Config),
717+
Stream2 = atom_to_list(?FUNCTION_NAME) ++ "2",
718+
MsgPerBatch2 = 3,
719+
publish_via_stream_protocol(list_to_binary(Stream2), MsgPerBatch2, Config),
720+
721+
%% aggregated metrics
722+
723+
%% wait for the stream to emit stats
724+
%% (collect_statistics_interval set to 100ms in this test group)
725+
?awaitMatch(V when V == #{rabbitmq_stream_consumer_max_offset_lag => #{undefined => [3]}},
726+
begin
727+
{_, Body1} = http_get_with_pal(Config, "/metrics", [], 200),
728+
maps:with([rabbitmq_stream_consumer_max_offset_lag],
729+
parse_response(Body1))
730+
end,
731+
100),
732+
733+
%% per-object metrics
734+
{_, Body2} = http_get_with_pal(Config, "/metrics/detailed?family=stream_consumer_metrics",
735+
[], 200),
736+
ParsedBody2 = parse_response(Body2),
737+
#{rabbitmq_detailed_stream_consumer_max_offset_lag := MaxOffsetLag} = ParsedBody2,
738+
739+
?assertEqual([{#{vhost => "/", queue => Stream1}, [2]},
740+
{#{vhost => "/", queue => Stream2}, [3]}],
741+
lists:sort(maps:to_list(MaxOffsetLag))),
742+
ok.
743+
711744
core_metrics_special_chars(Config) ->
712745
{_, Body1} = http_get_with_pal(Config, "/metrics/detailed?family=queue_coarse_metrics", [], 200),
713746
?assertMatch(#{rabbitmq_detailed_queue_messages :=
@@ -753,6 +786,30 @@ basic_auth(Config) ->
753786
rabbit_ct_broker_helpers:delete_user(Config, <<"monitor">>),
754787
rabbit_ct_broker_helpers:delete_user(Config, <<"management">>).
755788

789+
%% -------------------------------------------------------------------
790+
%% Helpers
791+
%% -------------------------------------------------------------------
792+
793+
publish_via_stream_protocol(Stream, MsgPerBatch, Config) ->
794+
{ok, S, C0} = stream_test_utils:connect(Config, 0),
795+
{ok, C1} = stream_test_utils:create_stream(S, C0, Stream),
796+
PublisherId = 98,
797+
{ok, C2} = stream_test_utils:declare_publisher(S, C1, Stream, PublisherId),
798+
Payloads = lists:duplicate(MsgPerBatch, <<"m1">>),
799+
SequenceFrom1 = 1,
800+
{ok, C3} = stream_test_utils:publish(S, C2, PublisherId, SequenceFrom1, Payloads),
801+
802+
PublisherId2 = 99,
803+
{ok, C4} = stream_test_utils:declare_publisher(S, C3, Stream, PublisherId2),
804+
Payloads2 = lists:duplicate(MsgPerBatch, <<"m2">>),
805+
SequenceFrom2 = SequenceFrom1 + MsgPerBatch,
806+
{ok, C5} = stream_test_utils:publish(S, C4, PublisherId2, SequenceFrom2, Payloads2),
807+
808+
SubscriptionId = 97,
809+
{ok, C6} = stream_test_utils:subscribe(S, C5, Stream, SubscriptionId, _InitialCredit = 1),
810+
%% delivery of first batch of messages
811+
{{deliver, SubscriptionId, _Bin1}, _C7} = stream_test_utils:receive_stream_commands(S, C6),
812+
ok.
756813

757814
http_get(Config, ReqHeaders, CodeExp) ->
758815
Path = proplists:get_value(prometheus_path, Config, "/metrics"),

0 commit comments

Comments
 (0)