Skip to content

Commit 2b09eab

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 00274e7 commit 2b09eab

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().
@@ -143,21 +144,20 @@ counters(Group, Name, FieldNames) ->
143144
format(Group) ->
144145
ets:foldl(fun({_Name, Ref, Fields0, Label}, Acc) ->
145146
Fields = resolve_fields(Fields0),
146-
lists:foldl(
147-
fun ({MetricName, Index, Type, Help}, Acc0) ->
148-
InitialMetric = #{type => Type,
149-
help => Help,
150-
values => #{}},
151-
Metric = maps:get(MetricName, Acc0,
152-
InitialMetric),
153-
Values = maps:get(values, Metric),
154-
Counter = counters:get(Ref, Index),
155-
Values1 = Values#{Label => Counter},
156-
Metric1 = Metric#{values => Values1},
157-
Acc0#{MetricName => Metric1}
158-
end, Acc, Fields)
147+
format_fields(Fields, Ref, Label, Acc)
159148
end, #{}, seshat_counters_server:get_table(Group)).
160149

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

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

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