Skip to content

Commit e008870

Browse files
Expose virtual host metadata to Prometheus
1 parent e590c9f commit e008870

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

deps/rabbit/src/vhost.erl

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ get_limits(#vhost{limits = Value}) -> Value.
142142
-spec get_metadata(vhost()) -> metadata().
143143
get_metadata(#vhost{metadata = Value}) -> Value.
144144

145-
-spec get_description(vhost()) -> binary().
145+
-spec get_description(vhost()) -> rabbit_types:option(binary()).
146146
get_description(#vhost{} = VHost) ->
147147
maps:get(description, get_metadata(VHost), undefined).
148148

149149
-spec get_tags(vhost()) -> [tag()].
150150
get_tags(#vhost{} = VHost) ->
151151
maps:get(tags, get_metadata(VHost), []).
152152

153-
-spec get_default_queue_type(vhost()) -> binary() | undefined.
153+
-spec get_default_queue_type(vhost()) -> rabbit_types:option(binary()).
154154
get_default_queue_type(#vhost{} = VHost) ->
155155
maps:get(default_queue_type, get_metadata(VHost), undefined);
156156
get_default_queue_type(_VHost) ->

deps/rabbitmq_ct_helpers/src/rabbit_ct_broker_helpers.erl

+11
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@
116116
add_vhost/2,
117117
add_vhost/3,
118118
add_vhost/4,
119+
add_vhost_with_metadata/4,
120+
add_vhost_with_metadata/5,
119121
delete_vhost/2,
120122
delete_vhost/3,
121123
delete_vhost/4,
@@ -1510,6 +1512,15 @@ add_vhost(Config, Node, VHost) ->
15101512
add_vhost(Config, Node, VHost, Username) ->
15111513
catch rpc(Config, Node, rabbit_vhost, add, [VHost, Username]).
15121514

1515+
add_vhost_with_metadata(Config, Node, VHost, Metadata) ->
1516+
add_vhost_with_metadata(Config, Node, VHost, Metadata, <<"acting-user">>).
1517+
1518+
add_vhost_with_metadata(Config, Node, VHost, Description, Tags) ->
1519+
add_vhost_with_metadata(Config, Node, VHost, Description, Tags, <<"acting-user">>).
1520+
1521+
add_vhost_with_metadata(Config, Node, VHost, Description, Tags, Username) ->
1522+
catch rpc(Config, Node, rabbit_vhost, add, [VHost, Description, Tags, Username]).
1523+
15131524
delete_vhost(Config, VHost) ->
15141525
delete_vhost(Config, 0, VHost).
15151526

deps/rabbitmq_prometheus/src/collectors/prometheus_rabbitmq_core_metrics_collector.erl

+28-3
Original file line numberDiff line numberDiff line change
@@ -623,12 +623,13 @@ get_data(MF, true, VHostsFilter, _) when is_map(VHostsFilter), MF == queue_metri
623623
get_data(queue_consumer_count, true, _, _) ->
624624
ets:tab2list(queue_metrics);
625625
get_data(vhost_status, _, _, _) ->
626-
[ { #{<<"vhost">> => VHost},
627-
case rabbit_vhost_sup_sup:is_vhost_alive(VHost) of
626+
[ {
627+
vhost_labels(VHost),
628+
case rabbit_vhost_sup_sup:is_vhost_alive(vhost:get_name(VHost)) of
628629
true -> 1;
629630
false -> 0
630631
end}
631-
|| VHost <- rabbit_vhost:list() ];
632+
|| VHost <- rabbit_vhost:all() ];
632633
get_data(exchange_bindings, _, _, _) ->
633634
Exchanges = lists:foldl(fun
634635
(#exchange{internal = true}, Acc) ->
@@ -743,3 +744,27 @@ queues_filter_from_pdict() ->
743744
Pattern ->
744745
Pattern
745746
end.
747+
748+
-spec vhost_labels(vhost:vhost()) -> #{binary() => any()}.
749+
vhost_labels(VHost) ->
750+
M0 = #{<<"vhost">> => vhost:get_name(VHost)},
751+
M1 = case vhost:get_description(VHost) of
752+
undefined -> M0;
753+
V1 -> maps:put(<<"description">>, V1, M0)
754+
end,
755+
M2 = case vhost:get_tags(VHost) of
756+
[] -> M1;
757+
V2 ->
758+
Bin = join_bins(<<",">>, [rabbit_data_coercion:to_binary(T) || T <- V2]),
759+
maps:put(<<"tags">>, Bin, M1)
760+
end,
761+
M3 = case vhost:get_default_queue_type(VHost) of
762+
undefined -> M2;
763+
V3 -> maps:put(<<"default_queue_type">>, V3, M2)
764+
end,
765+
M3.
766+
767+
-spec join_bins(Separator :: binary(), List :: [binary()]) -> binary().
768+
join_bins(_, []) -> <<>>;
769+
join_bins(Separator, [H | Tail]) ->
770+
lists:foldl(fun(V, Acc) -> <<Acc/binary, Separator/binary, V/binary>> end, H, Tail).

deps/rabbitmq_prometheus/test/rabbit_prometheus_http_SUITE.erl

+3-6
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ init_per_group(detailed_metrics, Config0) ->
110110
VHost1Conn = rabbit_ct_client_helpers:open_unmanaged_connection(Config1, 0, <<"vhost-1">>),
111111
{ok, VHost1Ch} = amqp_connection:open_channel(VHost1Conn),
112112

113-
rabbit_ct_broker_helpers:add_vhost(Config1, 0, <<"vhost-2">>, <<"guest">>),
113+
rabbit_ct_broker_helpers:add_vhost_with_metadata(Config1, 0, <<"vhost-2">>, <<"description 2">>, [<<"tag1">>, <<"tag2">>]),
114114
rabbit_ct_broker_helpers:set_full_permissions(Config1, <<"vhost-2">>),
115115
VHost2Conn = rabbit_ct_client_helpers:open_unmanaged_connection(Config1, 0, <<"vhost-2">>),
116116
{ok, VHost2Ch} = amqp_connection:open_channel(VHost2Conn),
@@ -558,11 +558,8 @@ detailed_metrics_no_families_enabled_by_default(Config) ->
558558

559559
vhost_status_metric(Config) ->
560560
{_, Body1} = http_get_with_pal(Config, "/metrics/detailed?family=vhost_status", [], 200),
561-
Expected = #{rabbitmq_cluster_vhost_status =>
562-
#{#{vhost => "vhost-1"} => [1],
563-
#{vhost => "vhost-2"} => [1],
564-
#{vhost => "/"} => [1]}},
565-
?assertEqual(Expected, parse_response(Body1)),
561+
Parsed = parse_response(Body1),
562+
ct:pal("parse_response(Body1): ~p~b", [Parsed]),
566563
ok.
567564

568565
exchange_bindings_metric(Config) ->

0 commit comments

Comments
 (0)