Skip to content

Commit 443be4e

Browse files
committed
prometheus: Add a metrics family for Khepri's Ra counters
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.
1 parent 86ceac4 commit 443be4e

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

deps/rabbit/src/rabbit_khepri.erl

+9
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
nodes/0,
106106
locally_known_nodes/0,
107107
get_ra_cluster_name/0,
108+
get_server_id/1,
108109
get_store_id/0,
109110
transfer_leadership/1,
110111

@@ -669,6 +670,14 @@ locally_known_nodes() ->
669670
get_ra_cluster_name() ->
670671
?RA_CLUSTER_NAME.
671672

673+
-spec get_server_id(Node) -> RaServerId when
674+
Node :: node(),
675+
RaServerId :: ra:server_id().
676+
%% @doc Returns the Ra server id of the Khepri member on the given node.
677+
678+
get_server_id(Node) ->
679+
{?RA_CLUSTER_NAME, Node}.
680+
672681
-spec get_store_id() -> StoreId when
673682
StoreId :: khepri:store_id().
674683
%% @doc Returns the Khepri store identifier.

deps/rabbitmq_prometheus/src/collectors/prometheus_rabbitmq_core_metrics_collector.erl

+31-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

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

21+
-include_lib("stdlib/include/assert.hrl").
22+
-include_lib("kernel/include/logger.hrl").
2123
-include_lib("rabbit_common/include/rabbit.hrl").
2224

2325
-behaviour(prometheus_collector).
@@ -298,6 +300,12 @@ deregister_cleanup(_) -> ok.
298300
collect_mf('detailed', Callback) ->
299301
collect(true, ?DETAILED_METRIC_NAME_PREFIX, vhosts_filter_from_pdict(), enabled_mfs_from_pdict(?METRICS_RAW), Callback),
300302
collect(true, ?CLUSTER_METRIC_NAME_PREFIX, vhosts_filter_from_pdict(), enabled_mfs_from_pdict(?METRICS_CLUSTER), Callback),
303+
case is_mf_enabled(khepri) of
304+
true ->
305+
collect_khepri_info(Callback);
306+
false ->
307+
ok
308+
end,
301309
%% identity is here to enable filtering on a cluster name (as already happens in existing dashboards)
302310
emit_identity_info(Callback),
303311
ok;
@@ -331,6 +339,20 @@ totals(Callback) ->
331339
end || {Table, Name, Type, Help} <- ?TOTALS],
332340
ok.
333341

342+
collect_khepri_info(Callback) ->
343+
ServerId = rabbit_khepri:get_server_id(node()),
344+
maps:foreach(
345+
fun (Name, #{type := Type, help := Help, values := #{ServerId := Value}}) ->
346+
Callback(
347+
create_mf(
348+
<<?DETAILED_METRIC_NAME_PREFIX/binary,
349+
"khepri_",
350+
(prometheus_model_helpers:metric_name(Name))/binary>>,
351+
Help, Type, [Value]));
352+
(_Name, _Format) ->
353+
ok
354+
end, seshat:format(ra)).
355+
334356
emit_identity_info(Callback) ->
335357
add_metric_family(build_info(), Callback),
336358
add_metric_family(identity_info(), Callback),
@@ -814,12 +836,19 @@ sum('', B) ->
814836
sum(A, B) ->
815837
A + B.
816838

839+
is_mf_enabled(MF) ->
840+
case get(prometheus_mf_filter) of
841+
undefined ->
842+
false;
843+
MFNameSet ->
844+
sets:is_element(MF, MFNameSet)
845+
end.
846+
817847
enabled_mfs_from_pdict(AllMFs) ->
818848
case get(prometheus_mf_filter) of
819849
undefined ->
820850
[];
821-
MFNames ->
822-
MFNameSet = sets:from_list(MFNames),
851+
MFNameSet ->
823852
[ MF || MF = {Table, _} <- AllMFs, sets:is_element(Table, MFNameSet) ]
824853
end.
825854

deps/rabbitmq_prometheus/src/rabbit_prometheus_handler.erl

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ put_filtering_options_into_process_dictionary(Request) ->
170170
end,
171171
case parse_metric_families(Families) of
172172
Fs when is_list(Fs) ->
173-
put(prometheus_mf_filter, Fs);
173+
put(prometheus_mf_filter, sets:from_list(Fs, [{version, 2}]));
174174
_ -> ok
175175
end,
176176
ok.

0 commit comments

Comments
 (0)