Skip to content

Commit

Permalink
Expose virtual host metadata to Prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Mar 5, 2024
1 parent e590c9f commit e008870
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
4 changes: 2 additions & 2 deletions deps/rabbit/src/vhost.erl
Expand Up @@ -142,15 +142,15 @@ get_limits(#vhost{limits = Value}) -> Value.
-spec get_metadata(vhost()) -> metadata().
get_metadata(#vhost{metadata = Value}) -> Value.

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

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

-spec get_default_queue_type(vhost()) -> binary() | undefined.
-spec get_default_queue_type(vhost()) -> rabbit_types:option(binary()).
get_default_queue_type(#vhost{} = VHost) ->
maps:get(default_queue_type, get_metadata(VHost), undefined);
get_default_queue_type(_VHost) ->
Expand Down
11 changes: 11 additions & 0 deletions deps/rabbitmq_ct_helpers/src/rabbit_ct_broker_helpers.erl
Expand Up @@ -116,6 +116,8 @@
add_vhost/2,
add_vhost/3,
add_vhost/4,
add_vhost_with_metadata/4,
add_vhost_with_metadata/5,
delete_vhost/2,
delete_vhost/3,
delete_vhost/4,
Expand Down Expand Up @@ -1510,6 +1512,15 @@ add_vhost(Config, Node, VHost) ->
add_vhost(Config, Node, VHost, Username) ->
catch rpc(Config, Node, rabbit_vhost, add, [VHost, Username]).

add_vhost_with_metadata(Config, Node, VHost, Metadata) ->
add_vhost_with_metadata(Config, Node, VHost, Metadata, <<"acting-user">>).

add_vhost_with_metadata(Config, Node, VHost, Description, Tags) ->
add_vhost_with_metadata(Config, Node, VHost, Description, Tags, <<"acting-user">>).

add_vhost_with_metadata(Config, Node, VHost, Description, Tags, Username) ->
catch rpc(Config, Node, rabbit_vhost, add, [VHost, Description, Tags, Username]).

delete_vhost(Config, VHost) ->
delete_vhost(Config, 0, VHost).

Expand Down
Expand Up @@ -623,12 +623,13 @@ get_data(MF, true, VHostsFilter, _) when is_map(VHostsFilter), MF == queue_metri
get_data(queue_consumer_count, true, _, _) ->
ets:tab2list(queue_metrics);
get_data(vhost_status, _, _, _) ->
[ { #{<<"vhost">> => VHost},
case rabbit_vhost_sup_sup:is_vhost_alive(VHost) of
[ {
vhost_labels(VHost),
case rabbit_vhost_sup_sup:is_vhost_alive(vhost:get_name(VHost)) of
true -> 1;
false -> 0
end}
|| VHost <- rabbit_vhost:list() ];
|| VHost <- rabbit_vhost:all() ];
get_data(exchange_bindings, _, _, _) ->
Exchanges = lists:foldl(fun
(#exchange{internal = true}, Acc) ->
Expand Down Expand Up @@ -743,3 +744,27 @@ queues_filter_from_pdict() ->
Pattern ->
Pattern
end.

-spec vhost_labels(vhost:vhost()) -> #{binary() => any()}.
vhost_labels(VHost) ->
M0 = #{<<"vhost">> => vhost:get_name(VHost)},
M1 = case vhost:get_description(VHost) of
undefined -> M0;
V1 -> maps:put(<<"description">>, V1, M0)
end,
M2 = case vhost:get_tags(VHost) of
[] -> M1;
V2 ->
Bin = join_bins(<<",">>, [rabbit_data_coercion:to_binary(T) || T <- V2]),
maps:put(<<"tags">>, Bin, M1)
end,
M3 = case vhost:get_default_queue_type(VHost) of
undefined -> M2;
V3 -> maps:put(<<"default_queue_type">>, V3, M2)
end,
M3.

-spec join_bins(Separator :: binary(), List :: [binary()]) -> binary().
join_bins(_, []) -> <<>>;
join_bins(Separator, [H | Tail]) ->
lists:foldl(fun(V, Acc) -> <<Acc/binary, Separator/binary, V/binary>> end, H, Tail).
Expand Up @@ -110,7 +110,7 @@ init_per_group(detailed_metrics, Config0) ->
VHost1Conn = rabbit_ct_client_helpers:open_unmanaged_connection(Config1, 0, <<"vhost-1">>),
{ok, VHost1Ch} = amqp_connection:open_channel(VHost1Conn),

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

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

exchange_bindings_metric(Config) ->
Expand Down

0 comments on commit e008870

Please sign in to comment.