Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prometheus: Add a metrics family for Khepri's Ra counters #12190

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions deps/rabbit/src/rabbit_khepri.erl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
nodes/0,
locally_known_nodes/0,
get_ra_cluster_name/0,
get_server_id/1,
get_store_id/0,
transfer_leadership/1,

Expand Down Expand Up @@ -669,6 +670,14 @@ locally_known_nodes() ->
get_ra_cluster_name() ->
?RA_CLUSTER_NAME.

-spec get_server_id(Node) -> RaServerId when
Node :: node(),
RaServerId :: ra:server_id().
%% @doc Returns the Ra server id of the Khepri member on the given node.

get_server_id(Node) ->
{?RA_CLUSTER_NAME, Node}.

-spec get_store_id() -> StoreId when
StoreId :: khepri:store_id().
%% @doc Returns the Khepri store identifier.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

-import(prometheus_text_format, [escape_label_value/1]).

-include_lib("stdlib/include/assert.hrl").
-include_lib("kernel/include/logger.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").

-behaviour(prometheus_collector).
Expand Down Expand Up @@ -298,6 +300,12 @@ deregister_cleanup(_) -> ok.
collect_mf('detailed', Callback) ->
collect(true, ?DETAILED_METRIC_NAME_PREFIX, vhosts_filter_from_pdict(), enabled_mfs_from_pdict(?METRICS_RAW), Callback),
collect(true, ?CLUSTER_METRIC_NAME_PREFIX, vhosts_filter_from_pdict(), enabled_mfs_from_pdict(?METRICS_CLUSTER), Callback),
case is_mf_enabled(khepri) of
true ->
collect_khepri_info(Callback);
false ->
ok
end,
%% identity is here to enable filtering on a cluster name (as already happens in existing dashboards)
emit_identity_info(Callback),
ok;
Expand Down Expand Up @@ -331,6 +339,20 @@ totals(Callback) ->
end || {Table, Name, Type, Help} <- ?TOTALS],
ok.

collect_khepri_info(Callback) ->
ServerId = rabbit_khepri:get_server_id(node()),
maps:foreach(
fun (Name, #{type := Type, help := Help, values := #{ServerId := Value}}) ->
Callback(
create_mf(
<<?DETAILED_METRIC_NAME_PREFIX/binary,
"khepri_",
(prometheus_model_helpers:metric_name(Name))/binary>>,
Help, Type, [Value]));
(_Name, _Format) ->
ok
end, seshat:format(ra)).

emit_identity_info(Callback) ->
add_metric_family(build_info(), Callback),
add_metric_family(identity_info(), Callback),
Expand Down Expand Up @@ -814,12 +836,19 @@ sum('', B) ->
sum(A, B) ->
A + B.

is_mf_enabled(MF) ->
case get(prometheus_mf_filter) of
undefined ->
false;
MFNameSet ->
sets:is_element(MF, MFNameSet)
end.

enabled_mfs_from_pdict(AllMFs) ->
case get(prometheus_mf_filter) of
undefined ->
[];
MFNames ->
MFNameSet = sets:from_list(MFNames),
MFNameSet ->
[ MF || MF = {Table, _} <- AllMFs, sets:is_element(Table, MFNameSet) ]
end.

Expand Down
2 changes: 1 addition & 1 deletion deps/rabbitmq_prometheus/src/rabbit_prometheus_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ put_filtering_options_into_process_dictionary(Request) ->
end,
case parse_metric_families(Families) of
Fs when is_list(Fs) ->
put(prometheus_mf_filter, Fs);
put(prometheus_mf_filter, sets:from_list(Fs, [{version, 2}]));
_ -> ok
end,
ok.
Expand Down
Loading