Skip to content

Commit e7be0ad

Browse files
committed
fix: prometheus metrics annotations
## Why Previously, the `_node/_local/_prometheus` endpoints would return invalid annotations for certain metrics. For example: ``` # TYPE couchdb_database_reads_total counterof times a document was read from a database couchdb_database_reads_total 2208 ``` In general this doesn't matter too much - prometheus will infer the type from the metric name - but some clients will respect it and generate errors/warnings if the annotations are incorrect. ## What This removes the carriage returns from the prometheus output. These were added as an incorrect fix to spurious newlines in the output but caused this bug with the annotations. The actual bug causing the additional newlines is that the result of `type_def/3` was additionally wrapped in a list. This commit removes the re-wrapping. removes the carriage return and adds a test for the output. This changes the output of `to_prom` slightly because the first element of the result is no longer
1 parent f06c3ec commit e7be0ad

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

src/couch_prometheus/src/couch_prometheus_util.erl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ couch_to_prom(Path, Info, _All) ->
116116
type_def(Metric, Type, Desc) ->
117117
Name = to_prom_name(Metric),
118118
[
119-
to_bin(io_lib:format("\n# HELP ~s ~s\r", [Name, Desc])),
119+
to_bin(io_lib:format("\n# HELP ~s ~s", [Name, Desc])),
120120
to_bin(io_lib:format("# TYPE ~s ~s", [Name, Type]))
121121
].
122122

@@ -126,7 +126,7 @@ to_prom(_Metric, _Type, _Desc, []) ->
126126
[];
127127
to_prom(Metric, Type, Desc, Instances) when is_list(Instances) ->
128128
TypeStr = type_def(Metric, Type, Desc),
129-
[TypeStr] ++ lists:flatmap(fun(Inst) -> to_prom(Metric, Inst) end, Instances);
129+
TypeStr ++ lists:flatmap(fun(Inst) -> to_prom(Metric, Inst) end, Instances);
130130
to_prom(Metric, Type, Desc, Data) ->
131131
to_prom(Metric, Type, Desc, [Data]).
132132

@@ -221,6 +221,23 @@ desc(Info) ->
221221
-ifdef(TEST).
222222
-include_lib("couch/include/couch_eunit.hrl").
223223

224+
to_prom_annotations_test() ->
225+
Out = to_prom(couchdb_bt_engine_cache_size, gauge, "number of entries in the btree cache", 9),
226+
[
227+
?assertEqual(
228+
<<"\n# HELP couchdb_couchdb_bt_engine_cache_size number of entries in the btree cache">>,
229+
lists:nth(1, Out)
230+
),
231+
?assertEqual(
232+
<<"# TYPE couchdb_couchdb_bt_engine_cache_size gauge">>,
233+
lists:nth(2, Out)
234+
),
235+
?assertEqual(
236+
<<"couchdb_couchdb_bt_engine_cache_size 9">>,
237+
lists:nth(3, Out)
238+
)
239+
].
240+
224241
to_prom_counter_test() ->
225242
[
226243
?assertEqual(
@@ -288,10 +305,10 @@ counter_metric_test_() ->
288305

289306
test_to_prom_output(Metric, Type, Desc, Val) ->
290307
Out = to_prom(Metric, Type, Desc, Val),
291-
lists:nth(2, Out).
308+
lists:nth(3, Out).
292309

293310
test_to_prom_summary_output(Metric, Info) ->
294311
Out = to_prom_summary(Metric, Info),
295-
lists:nth(3, Out).
312+
lists:nth(4, Out).
296313

297314
-endif.

src/couch_prometheus/test/eunit/couch_prometheus_e2e_tests.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,12 @@ t_survives_mem3_sync_termination(_) ->
177177
ok = supervisor:terminate_child(mem3_sup, mem3_sync),
178178
?assertEqual(undefined, whereis(mem3_sync)),
179179
?assertMatch(
180-
[[_, _], <<"couchdb_internal_replication_jobs 0">>],
180+
[_, _, <<"couchdb_internal_replication_jobs 0">>],
181181
couch_prometheus:get_internal_replication_jobs_stat()
182182
),
183183
{ok, _} = supervisor:restart_child(mem3_sup, mem3_sync),
184184
?assertMatch(
185-
[[_, _], <<"couchdb_internal_replication_jobs", _/binary>>],
185+
[_, _, <<"couchdb_internal_replication_jobs", _/binary>>],
186186
couch_prometheus:get_internal_replication_jobs_stat()
187187
).
188188

0 commit comments

Comments
 (0)