Skip to content

Commit

Permalink
Add seshat:format/2 to format only for a given name
Browse files Browse the repository at this point in the history
This will be useful in RabbitMQ to expose Khepri metrics for example:
we want to select just Khepri's metrics out of the `ra` group.
  • Loading branch information
the-mikedavis committed Sep 3, 2024
1 parent 9daf519 commit 9d6e984
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/seshat.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
counters/2,
counters/3,
delete/2,
format/1
format/1,
format/2
]).

-type group() :: term().
Expand Down Expand Up @@ -142,21 +143,20 @@ counters(Group, Name, FieldNames) ->
format(Group) ->
ets:foldl(fun({_Name, Ref, Fields0, Label}, Acc) ->
Fields = resolve_fields(Fields0),
lists:foldl(
fun ({MetricName, Index, Type, Help}, Acc0) ->
InitialMetric = #{type => Type,
help => Help,
values => #{}},
Metric = maps:get(MetricName, Acc0,
InitialMetric),
Values = maps:get(values, Metric),
Counter = counters:get(Ref, Index),
Values1 = Values#{Label => Counter},
Metric1 = Metric#{values => Values1},
Acc0#{MetricName => Metric1}
end, Acc, Fields)
format_fields(Fields, Ref, Label, Acc)
end, #{}, seshat_counters_server:get_table(Group)).

-spec format(group(), name()) -> format_result().

format(Group, Name) ->
case ets:lookup(seshat_counters_server:get_table(Group), Name) of
[{Name, Ref, Fields0, Label}] ->
Fields = resolve_fields(Fields0),
format_fields(Fields, Ref, Label, #{});
_ ->
#{}
end.

%% internal

resolve_fields(Fields) when is_list(Fields) ->
Expand All @@ -183,3 +183,16 @@ new_counter(Group, Name, Fields, FieldsSpec, Label) ->
error(invalid_field_specification)
end.

format_fields(Fields, Ref, Label, Acc) ->
lists:foldl(
fun ({MetricName, Index, Type, Help}, Acc0) ->
InitialMetric = #{type => Type,
help => Help,
values => #{}},
Metric = maps:get(MetricName, Acc0, InitialMetric),
Values = maps:get(values, Metric),
Counter = counters:get(Ref, Index),
Values1 = Values#{Label => Counter},
Metric1 = Metric#{values => Values1},
Acc0#{MetricName => Metric1}
end, Acc, Fields).
14 changes: 14 additions & 0 deletions test/seshat_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test_suite_test_() ->
[ fun overview/0,
fun counters_with_persistent_term_field_spec/0,
fun prometheus_format_multiple_names/0,
fun prometheus_format_single_name/0,
fun prometheus_format_with_labels/0,
fun invalid_fields/0 ]}.

Expand Down Expand Up @@ -103,6 +104,19 @@ prometheus_format_multiple_names() ->
?assertEqual(ExpectedPrometheusFormat, PrometheusFormat),
ok.

prometheus_format_single_name() ->
Group = people,
Counters = [{foo, 1, counter, "Total foos given"}],
seshat:new_group(Group),
seshat:new(Group, {name, you}, Counters),
seshat:new(Group, {name, me}, Counters),
PrometheusFormat = seshat:format(Group, {name, me}),
ExpectedPrometheusFormat = #{foo => #{type => counter,
help => "Total foos given",
values => #{{name, me} => 0}}},
?assertEqual(ExpectedPrometheusFormat, PrometheusFormat),
ok.

prometheus_format_with_labels() ->
Group = people,
Counters = [{foo, 1, counter, "Total foos given"}],
Expand Down

0 comments on commit 9d6e984

Please sign in to comment.