You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by wi...@apache.org on 2023/03/13 13:18:04 UTC

[couchdb] 01/01: fix: remove duplicate couchdb_erlang* from _prometheus

This is an automated email from the ASF dual-hosted git repository.

willholley pushed a commit to branch remove-prometheus-duplicates
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 41e6108c3d9dc627e0936620cf04151762393023
Author: Will Holley <wi...@apache.org>
AuthorDate: Fri Mar 10 09:10:40 2023 +0000

    fix: remove duplicate couchdb_erlang* from _prometheus
    
    The `_node/_local/_prometheus` was returning duplicate rows for the
    following metrics:
    
    ```
    couchdb_erlang_memory_bytes
    couchdb_erlang_gc_collections_total
    couchdb_erlang_gc_words_reclaimed_total
    couchdb_erlang_context_switches_total
    couchdb_erlang_reductions_total
    couchdb_erlang_processes
    couchdb_erlang_process_limit
    ```
    
    Prometheus will gracefully handle the duplication, picking the first
    entry only, but it bloats the response and can potentially cause
    unexpected results if there's a signficant delay capturing the
    samples.
    
    The duplication is caused by a duplicate function call to
    `get_vm_stats()` in the prometheus endpoint handler. Removing the
    duplicate call fixes the problem.
---
 src/couch_prometheus/src/couch_prometheus_server.erl  |  1 -
 .../test/eunit/couch_prometheus_e2e_tests.erl         | 19 ++++++++++++++++++-
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/src/couch_prometheus/src/couch_prometheus_server.erl b/src/couch_prometheus/src/couch_prometheus_server.erl
index 7a0eb4bf9..7597c7e28 100644
--- a/src/couch_prometheus/src/couch_prometheus_server.erl
+++ b/src/couch_prometheus/src/couch_prometheus_server.erl
@@ -108,7 +108,6 @@ get_couchdb_stats() ->
 get_system_stats() ->
     lists:flatten([
         get_uptime_stat(),
-        get_vm_stats(),
         get_io_stats(),
         get_message_queue_stats(),
         get_run_queue_stats(),
diff --git a/src/couch_prometheus/test/eunit/couch_prometheus_e2e_tests.erl b/src/couch_prometheus/test/eunit/couch_prometheus_e2e_tests.erl
index 5f458ba8c..09c48a5cb 100644
--- a/src/couch_prometheus/test/eunit/couch_prometheus_e2e_tests.erl
+++ b/src/couch_prometheus/test/eunit/couch_prometheus_e2e_tests.erl
@@ -39,7 +39,8 @@ e2e_test_() ->
                 [
                     ?TDEF_FE(t_chttpd_port),
                     ?TDEF_FE(t_prometheus_port),
-                    ?TDEF_FE(t_metric_updated)
+                    ?TDEF_FE(t_metric_updated),
+                    ?TDEF_FE(t_no_duplicate_metrics)
                 ]
             }
         }
@@ -105,6 +106,22 @@ t_reject_prometheus_port(Port) ->
     Response = test_request:get(node_local_url(Port), [?CONTENT_JSON, ?AUTH]),
     ?assertEqual({error, {conn_failed, {error, econnrefused}}}, Response).
 
+t_no_duplicate_metrics(Port) ->
+    Url = node_local_url(Port),
+    Stats = get_stats(Url),
+    Lines = re:split(Stats, "\n"),
+    % Filter the result to only the lines containing the metric
+    % definition, not the values. These lines always start with
+    % a # character.
+    MetricDefs = lists:filter(fun(S) -> string:find(S, "#") =:= S end, Lines),
+    ?assertNotEqual(erlang:length(MetricDefs), 0),
+    Diff = get_duplicates(MetricDefs),
+    ?debugVal(Diff),
+    ?assertEqual(erlang:length(Diff), 0).
+
+get_duplicates(List) ->
+    List -- sets:to_list(sets:from_list(List)).
+
 t_metric_updated(Port) ->
     % The passage of time should increment this metric
     Metric = "couchdb_uptime_seconds",