summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWill Holley <willholley@apache.org>2023-03-14 16:30:46 +0000
committerGitHub <noreply@github.com>2023-03-14 16:30:46 +0000
commit0aedd9384d245176f26dc98ddfb8e04022ca19e8 (patch)
treef94e74d856d74ee7b93fee3ea5e9e5ff11def460
parent5cdc0aa1b086f861ec849881ada6fcd97393c9d5 (diff)
downloadcouchdb-0aedd9384d245176f26dc98ddfb8e04022ca19e8.tar.gz
fix: prometheus counter metric naming (#4474)
`couch_prometheus_util:counter_metric/1` is intended to add a `_total` suffix to the name of metrics with type `counter` if the name does not already end with `_total`. The implementation was previously incorrect, resulting in metrics with names like `document_purges_total_total`. This adds basic eunit tests for the failure / success scenarios and fixes the implementation. It is a breaking change for consumers of the `_node/_local/_prometheus` endpoint, however, since the following metric is renamed: * `couchdb_document_purges_total_total` -> `couchdb_document_purges_total`
-rw-r--r--src/couch_prometheus/src/couch_prometheus_util.erl80
-rw-r--r--src/couch_prometheus/test/eunit/couch_prometheus_util_tests.erl70
2 files changed, 77 insertions, 73 deletions
diff --git a/src/couch_prometheus/src/couch_prometheus_util.erl b/src/couch_prometheus/src/couch_prometheus_util.erl
index 255df6876..7147a6539 100644
--- a/src/couch_prometheus/src/couch_prometheus_util.erl
+++ b/src/couch_prometheus/src/couch_prometheus_util.erl
@@ -146,9 +146,9 @@ path_to_name(Path) ->
counter_metric(Path) ->
Name = path_to_name(Path),
- case string:find(Name, <<"_total">>, trailing) == <<"_total">> of
- true -> Name;
- false -> to_bin(io_lib:format("~s_total", [Name]))
+ case lists:suffix("_total", Name) of
+ true -> to_bin(Name);
+ _ -> to_bin(io_lib:format("~s_total", [Name]))
end.
to_bin(Data) when is_list(Data) ->
@@ -167,3 +167,77 @@ val(Data) ->
val(Key, Stats) ->
{Key, Data} = lists:keyfind(Key, 1, Stats),
val(Data).
+
+-ifdef(TEST).
+-include_lib("couch/include/couch_eunit.hrl").
+
+to_prom_counter_test() ->
+ ?assertEqual(
+ <<"couchdb_ddoc_cache 10">>,
+ test_to_prom_output(ddoc_cache, counter, 10)
+ ),
+ ?assertEqual(
+ <<"couchdb_httpd_status_codes{code=\"200\"} 3">>,
+ test_to_prom_output(httpd_status_codes, counter, {[{code, 200}], 3})
+ ).
+
+to_prom_gauge_test() ->
+ ?assertEqual(
+ <<"couchdb_temperature_celsius 36">>,
+ test_to_prom_output(temperature_celsius, gauge, 36)
+ ).
+
+to_prom_summary_test() ->
+ ?assertEqual(
+ <<"couchdb_mango_query_time_seconds{quantile=\"0.75\"} 4.5">>,
+ test_to_prom_summary_output([mango_query_time], [
+ {value, [
+ {min, 0.0},
+ {max, 0.0},
+ {arithmetic_mean, 0.0},
+ {geometric_mean, 0.0},
+ {harmonic_mean, 0.0},
+ {median, 0.0},
+ {variance, 0.0},
+ {standard_deviation, 0.0},
+ {skewness, 0.0},
+ {kurtosis, 0.0},
+ {percentile, [
+ {50, 0.0},
+ {75, 4500},
+ {90, 0.0},
+ {95, 0.0},
+ {99, 0.0},
+ {999, 0.0}
+ ]},
+ {histogram, [
+ {0, 0}
+ ]},
+ {n, 0}
+ ]},
+ {type, histogram},
+ {desc, <<"length of time processing a mango query">>}
+ ])
+ ).
+
+counter_metric_test_() ->
+ [
+ ?_assertEqual(
+ <<"document_purges_total">>,
+ counter_metric([document_purges, total])
+ ),
+ ?_assertEqual(
+ <<"document_purges_total">>,
+ counter_metric([document_purges])
+ )
+ ].
+
+test_to_prom_output(Metric, Type, Val) ->
+ Out = to_prom(Metric, Type, Val),
+ lists:nth(2, Out).
+
+test_to_prom_summary_output(Metric, Info) ->
+ Out = to_prom_summary(Metric, Info),
+ lists:nth(3, Out).
+
+-endif.
diff --git a/src/couch_prometheus/test/eunit/couch_prometheus_util_tests.erl b/src/couch_prometheus/test/eunit/couch_prometheus_util_tests.erl
deleted file mode 100644
index 547741c5f..000000000
--- a/src/couch_prometheus/test/eunit/couch_prometheus_util_tests.erl
+++ /dev/null
@@ -1,70 +0,0 @@
-% Licensed under the Apache License, Version 2.0 (the "License"); you may not
-% use this file except in compliance with the License. You may obtain a copy of
-% the License at
-%
-% http://www.apache.org/licenses/LICENSE-2.0
-%
-% Unless required by applicable law or agreed to in writing, software
-% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-% License for the specific language governing permissions and limitations under
-% the License.
-
--module(couch_prometheus_util_tests).
-
--include_lib("couch/include/couch_eunit.hrl").
-
-couch_prometheus_util_test_() ->
- [
- ?_assertEqual(
- <<"couchdb_ddoc_cache 10">>,
- test_to_prom_output(ddoc_cache, counter, 10)
- ),
- ?_assertEqual(
- <<"couchdb_httpd_status_codes{code=\"200\"} 3">>,
- test_to_prom_output(httpd_status_codes, counter, {[{code, 200}], 3})
- ),
- ?_assertEqual(
- <<"couchdb_temperature_celsius 36">>,
- test_to_prom_output(temperature_celsius, gauge, 36)
- ),
- ?_assertEqual(
- <<"couchdb_mango_query_time_seconds{quantile=\"0.75\"} 4.5">>,
- test_to_prom_sum_output([mango_query_time], [
- {value, [
- {min, 0.0},
- {max, 0.0},
- {arithmetic_mean, 0.0},
- {geometric_mean, 0.0},
- {harmonic_mean, 0.0},
- {median, 0.0},
- {variance, 0.0},
- {standard_deviation, 0.0},
- {skewness, 0.0},
- {kurtosis, 0.0},
- {percentile, [
- {50, 0.0},
- {75, 4500},
- {90, 0.0},
- {95, 0.0},
- {99, 0.0},
- {999, 0.0}
- ]},
- {histogram, [
- {0, 0}
- ]},
- {n, 0}
- ]},
- {type, histogram},
- {desc, <<"length of time processing a mango query">>}
- ])
- )
- ].
-
-test_to_prom_output(Metric, Type, Val) ->
- Out = couch_prometheus_util:to_prom(Metric, Type, Val),
- lists:nth(2, Out).
-
-test_to_prom_sum_output(Metric, Info) ->
- Out = couch_prometheus_util:to_prom_summary(Metric, Info),
- lists:nth(3, Out).