Skip to content

Commit fbce631

Browse files
the-mikedavisNelsonVides
authored andcommitted
prometheus_text_format: Fix argument order for format_into/3 fun
The formatting function should take the state as the first argument and new data as the second. See `format/1` which passes in the trailing linefeed as the second argument. This doesn't make much difference when calling `format/1` because the swapped arguments only caused the metrics to be concatenated in reverse. But this fix is important for using `format_into/3` correctly, for example with `cowboy_req:stream_body/3`.
1 parent 566e985 commit fbce631

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/formats/prometheus_text_format.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ format_into_create_mf_callback_fn(Fmt) ->
101101
"\n"
102102
>>,
103103
Bin = render_metrics(Prologue, Name, Metrics),
104-
put(?MODULE, Fmt(Bin, erase(?MODULE)))
104+
put(?MODULE, Fmt(erase(?MODULE), Bin))
105105
end.
106106

107107
?DOC("""

test/eunit/format/prometheus_text_format_tests.erl

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ prometheus_format_test_() ->
5555
fun test_quantile_dsummary/1,
5656
fun test_histogram/1,
5757
fun test_histogram_float/1,
58-
fun test_dhistogram/1
58+
fun test_dhistogram/1,
59+
fun test_format_into/1
5960
]}.
6061

6162
content_type_test() ->
@@ -302,3 +303,32 @@ test_dhistogram(_) ->
302303
>>,
303304
prometheus_text_format:format()
304305
).
306+
307+
test_format_into(_) ->
308+
prometheus_gauge:new([{name, pool_size}, {help, "MongoDB Connections pool size"}]),
309+
prometheus_gauge:set(pool_size, 365),
310+
prometheus_counter:new([{name, http_requests_total}, {help, "Http request count"}]),
311+
prometheus_counter:inc(http_requests_total),
312+
313+
{ok, Fd} = ram_file:open("", [write, read, binary]),
314+
Fmt = fun(Size, Data) ->
315+
file:write(Fd, Data),
316+
Size + byte_size(Data)
317+
end,
318+
Size = prometheus_text_format:format_into(default, Fmt, 0),
319+
?assertEqual({ok, Size}, ram_file:get_size(Fd)),
320+
{ok, Buf} = file:pread(Fd, 0, Size),
321+
ok = file:close(Fd),
322+
323+
?_assertEqual(
324+
<<
325+
"# TYPE pool_size gauge\n"
326+
"# HELP pool_size MongoDB Connections pool size\n"
327+
"pool_size 365\n"
328+
"# TYPE http_requests_total counter\n"
329+
"# HELP http_requests_total Http request count\n"
330+
"http_requests_total 1\n"
331+
"\n"
332+
>>,
333+
Buf
334+
).

0 commit comments

Comments
 (0)