Skip to content

Commit 4f62761

Browse files
the-mikedavismkuratczyk
authored andcommitted
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 fa44b76 commit 4f62761

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

deps/rabbit/src/rabbit_khepri.erl

+9
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
nodes/0,
111111
locally_known_nodes/0,
112112
get_ra_cluster_name/0,
113+
get_server_id/1,
113114
get_store_id/0,
114115
transfer_leadership/1,
115116

@@ -677,6 +678,14 @@ locally_known_nodes() ->
677678
get_ra_cluster_name() ->
678679
?RA_CLUSTER_NAME.
679680

681+
-spec get_server_id(Node) -> RaServerId when
682+
Node :: node(),
683+
RaServerId :: ra:server_id().
684+
%% @doc Returns the Ra server id of the Khepri member on the given node.
685+
686+
get_server_id(Node) ->
687+
{?RA_CLUSTER_NAME, Node}.
688+
680689
-spec get_store_id() -> StoreId when
681690
StoreId :: khepri:store_id().
682691
%% @doc Returns the Khepri store identifier.

deps/rabbitmq_prometheus/src/collectors/prometheus_rabbitmq_core_metrics_collector.erl

+31-3
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).
@@ -303,6 +305,12 @@ deregister_cleanup(_) -> ok.
303305
collect_mf('detailed', Callback) ->
304306
collect(true, ?DETAILED_METRIC_NAME_PREFIX, vhosts_filter_from_pdict(), enabled_mfs_from_pdict(?METRICS_RAW), Callback),
305307
collect(true, ?CLUSTER_METRIC_NAME_PREFIX, vhosts_filter_from_pdict(), enabled_mfs_from_pdict(?METRICS_CLUSTER), Callback),
308+
case is_mf_enabled(khepri) of
309+
true ->
310+
collect_khepri_info(Callback);
311+
false ->
312+
ok
313+
end,
306314
%% identity is here to enable filtering on a cluster name (as already happens in existing dashboards)
307315
emit_identity_info(<<"detailed">>, Callback),
308316
ok;
@@ -339,6 +347,20 @@ totals(Callback) ->
339347
end || {Table, Name, Type, Help} <- ?TOTALS],
340348
ok.
341349

350+
collect_khepri_info(Callback) ->
351+
ServerId = rabbit_khepri:get_server_id(node()),
352+
maps:foreach(
353+
fun (Name, #{type := Type, help := Help, values := #{ServerId := Value}}) ->
354+
Callback(
355+
create_mf(
356+
<<?DETAILED_METRIC_NAME_PREFIX/binary,
357+
"khepri_",
358+
(prometheus_model_helpers:metric_name(Name))/binary>>,
359+
Help, Type, [Value]));
360+
(_Name, _Format) ->
361+
ok
362+
end, seshat:format(ra)).
363+
342364
emit_identity_info(Endpoint, Callback) ->
343365
add_metric_family(build_info(), Callback),
344366
add_metric_family(identity_info(Endpoint), Callback),
@@ -871,12 +893,19 @@ sum('', B) ->
871893
sum(A, B) ->
872894
A + B.
873895

896+
is_mf_enabled(MF) ->
897+
case get(prometheus_mf_filter) of
898+
undefined ->
899+
false;
900+
MFNameSet ->
901+
sets:is_element(MF, MFNameSet)
902+
end.
903+
874904
enabled_mfs_from_pdict(AllMFs) ->
875905
case get(prometheus_mf_filter) of
876906
undefined ->
877907
[];
878-
MFNames ->
879-
MFNameSet = sets:from_list(MFNames),
908+
MFNameSet ->
880909
[ MF || MF = {Table, _} <- AllMFs, sets:is_element(Table, MFNameSet) ]
881910
end.
882911

@@ -890,4 +919,3 @@ vhosts_filter_from_pdict() ->
890919
Enabled = maps:from_list([ {VHost, true} || VHost <- L ]),
891920
maps:merge(All, Enabled)
892921
end.
893-

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)