Skip to content

Commit 0e87dc1

Browse files
committed
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 0e87dc1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ prometheus_format_test_() ->
4848
fun test_untyped/1,
4949
fun test_nan_gauge/1,
5050
fun test_counter/1,
51+
fun test_gauge_and_counter/1,
5152
fun test_dcounter/1,
5253
fun test_summary/1,
5354
fun test_dsummary/1,
@@ -112,6 +113,24 @@ test_counter(_) ->
112113
prometheus_text_format:format()
113114
).
114115

116+
test_gauge_and_counter(_) ->
117+
prometheus_gauge:new([{name, pool_size}, {help, "MongoDB Connections pool size"}]),
118+
prometheus_gauge:set(pool_size, 365),
119+
prometheus_counter:new([{name, http_requests_total}, {help, "Http request count"}]),
120+
prometheus_counter:inc(http_requests_total),
121+
?_assertEqual(
122+
<<
123+
"# TYPE pool_size gauge\n"
124+
"# HELP pool_size MongoDB Connections pool size\n"
125+
"pool_size 365\n"
126+
"# TYPE http_requests_total counter\n"
127+
"# HELP http_requests_total Http request count\n"
128+
"http_requests_total 1\n"
129+
"\n"
130+
>>,
131+
prometheus_text_format:format()
132+
).
133+
115134
test_dcounter(_) ->
116135
prometheus_counter:new([{name, dtest}, {help, "qw\"\\e"}]),
117136
prometheus_counter:inc(dtest, 1.5),

0 commit comments

Comments
 (0)