Skip to content

Commit

Permalink
prometheus: Add a metrics family for Khepri's Ra counters
Browse files Browse the repository at this point in the history
This exposes all of the metrics that Ra collects in counters for the
node's Khepri server under a new metric family in the "detailed"
registry.
  • Loading branch information
the-mikedavis committed Aug 30, 2024
1 parent 86ceac4 commit 443be4e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
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

0 comments on commit 443be4e

Please sign in to comment.