Skip to content

Commit 9d6e984

Browse files
committed
Add seshat:format/2 to format only for a given name
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.
1 parent 9daf519 commit 9d6e984

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/seshat.erl

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
counters/2,
1818
counters/3,
1919
delete/2,
20-
format/1
20+
format/1,
21+
format/2
2122
]).
2223

2324
-type group() :: term().
@@ -142,21 +143,20 @@ counters(Group, Name, FieldNames) ->
142143
format(Group) ->
143144
ets:foldl(fun({_Name, Ref, Fields0, Label}, Acc) ->
144145
Fields = resolve_fields(Fields0),
145-
lists:foldl(
146-
fun ({MetricName, Index, Type, Help}, Acc0) ->
147-
InitialMetric = #{type => Type,
148-
help => Help,
149-
values => #{}},
150-
Metric = maps:get(MetricName, Acc0,
151-
InitialMetric),
152-
Values = maps:get(values, Metric),
153-
Counter = counters:get(Ref, Index),
154-
Values1 = Values#{Label => Counter},
155-
Metric1 = Metric#{values => Values1},
156-
Acc0#{MetricName => Metric1}
157-
end, Acc, Fields)
146+
format_fields(Fields, Ref, Label, Acc)
158147
end, #{}, seshat_counters_server:get_table(Group)).
159148

149+
-spec format(group(), name()) -> format_result().
150+
151+
format(Group, Name) ->
152+
case ets:lookup(seshat_counters_server:get_table(Group), Name) of
153+
[{Name, Ref, Fields0, Label}] ->
154+
Fields = resolve_fields(Fields0),
155+
format_fields(Fields, Ref, Label, #{});
156+
_ ->
157+
#{}
158+
end.
159+
160160
%% internal
161161

162162
resolve_fields(Fields) when is_list(Fields) ->
@@ -183,3 +183,16 @@ new_counter(Group, Name, Fields, FieldsSpec, Label) ->
183183
error(invalid_field_specification)
184184
end.
185185

186+
format_fields(Fields, Ref, Label, Acc) ->
187+
lists:foldl(
188+
fun ({MetricName, Index, Type, Help}, Acc0) ->
189+
InitialMetric = #{type => Type,
190+
help => Help,
191+
values => #{}},
192+
Metric = maps:get(MetricName, Acc0, InitialMetric),
193+
Values = maps:get(values, Metric),
194+
Counter = counters:get(Ref, Index),
195+
Values1 = Values#{Label => Counter},
196+
Metric1 = Metric#{values => Values1},
197+
Acc0#{MetricName => Metric1}
198+
end, Acc, Fields).

test/seshat_test.erl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ test_suite_test_() ->
2121
[ fun overview/0,
2222
fun counters_with_persistent_term_field_spec/0,
2323
fun prometheus_format_multiple_names/0,
24+
fun prometheus_format_single_name/0,
2425
fun prometheus_format_with_labels/0,
2526
fun invalid_fields/0 ]}.
2627

@@ -103,6 +104,19 @@ prometheus_format_multiple_names() ->
103104
?assertEqual(ExpectedPrometheusFormat, PrometheusFormat),
104105
ok.
105106

107+
prometheus_format_single_name() ->
108+
Group = people,
109+
Counters = [{foo, 1, counter, "Total foos given"}],
110+
seshat:new_group(Group),
111+
seshat:new(Group, {name, you}, Counters),
112+
seshat:new(Group, {name, me}, Counters),
113+
PrometheusFormat = seshat:format(Group, {name, me}),
114+
ExpectedPrometheusFormat = #{foo => #{type => counter,
115+
help => "Total foos given",
116+
values => #{{name, me} => 0}}},
117+
?assertEqual(ExpectedPrometheusFormat, PrometheusFormat),
118+
ok.
119+
106120
prometheus_format_with_labels() ->
107121
Group = people,
108122
Counters = [{foo, 1, counter, "Total foos given"}],

0 commit comments

Comments
 (0)