Skip to content

Commit b6daa5c

Browse files
committed
Introduce compatibility layer for metric promtool names
1 parent 22d0c41 commit b6daa5c

File tree

7 files changed

+145
-6
lines changed

7 files changed

+145
-6
lines changed

rebar.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
undefined_function_calls,
3131
undefined_functions,
3232
locals_not_used,
33-
deprecated_function_calls,
34-
deprecated_functions
33+
{deprecated_function_calls, next_major_release},
34+
{deprecated_functions, next_major_release}
3535
]}.
3636

3737
{dialyzer, [

src/collectors/mnesia/prometheus_mnesia_collector.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ collect_mf(_Registry, Callback) ->
8888
case mnesia_running() of
8989
true ->
9090
EnabledMetrics = enabled_metrics(),
91-
Metrics = metrics(EnabledMetrics),
91+
Metrics = prometheus_collectors_compat:pre_promtool_compat(metrics(EnabledMetrics)),
9292
[
9393
add_metric_family(Metric, Callback)
9494
|| {Name, _, _, _} = Metric <- Metrics, metric_enabled(Name, EnabledMetrics)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
-module(prometheus_collectors_compat).
2+
-if(?OTP_RELEASE >= 27).
3+
-define(MODULEDOC(Str), -moduledoc(Str)).
4+
-define(DOC(Str), -doc(Str)).
5+
-else.
6+
-define(MODULEDOC(Str), -compile([])).
7+
-define(DOC(Str), -compile([])).
8+
-endif.
9+
10+
?MODULEDOC("""
11+
Compatibility with previous collector metric names.
12+
13+
Up to version 6.0, some default collector metrics did not have `promtool` compliant names.
14+
The issue was fixed in version 6.0. This module is a compatibility layer for the old names.
15+
16+
It is disabled by default, to configure you need to set
17+
```erlang
18+
{prometheus, [
19+
{collectors_compat, true}
20+
]}
21+
```
22+
23+
This will be supported only for one major version.
24+
""").
25+
?MODULEDOC(#{deprecated => ~"Kept for compatibility and scheduled to be removed on the next major"}).
26+
27+
-export([pre_promtool_compat/1]).
28+
-deprecated([{'_', '_', next_major_release}]).
29+
30+
?DOC(false).
31+
-spec pre_promtool_compat([dynamic()]) -> [dynamic()].
32+
pre_promtool_compat(Metrics) ->
33+
case is_enabled() of
34+
true ->
35+
lists:map(fun pre_promtool_compat_fun/1, Metrics);
36+
_ ->
37+
Metrics
38+
end.
39+
40+
pre_promtool_compat_fun({failed_transactions_total, counter, Help, Fun}) ->
41+
{failed_transactions, counter, Help, Fun};
42+
pre_promtool_compat_fun({committed_transactions_total, counter, Help, Fun}) ->
43+
{committed_transactions, counter, Help, Fun};
44+
pre_promtool_compat_fun({logged_transactions_total, counter, Help, Fun}) ->
45+
{logged_transactions, counter, Help, Fun};
46+
pre_promtool_compat_fun({restarted_transactions_total, counter, Help, Fun}) ->
47+
{restarted_transactions, counter, Help, Fun};
48+
pre_promtool_compat_fun({atom_bytes, gauge, Help, Value}) ->
49+
{atom_bytes_total, gauge, Help, Value};
50+
pre_promtool_compat_fun({bytes, gauge, Help, Value}) ->
51+
{bytes_total, gauge, Help, Value};
52+
pre_promtool_compat_fun({processes_bytes, gauge, Help, Value}) ->
53+
{processes_bytes_total, gauge, Help, Value};
54+
pre_promtool_compat_fun({system_bytes, gauge, Help, Value}) ->
55+
{system_bytes_total, gauge, Help, Value};
56+
pre_promtool_compat_fun({context_switches_total, counter, Help, Value}) ->
57+
{context_switches, counter, Help, Value};
58+
pre_promtool_compat_fun({garbage_collection_number_of_gcs_total, counter, Help, Value}) ->
59+
{garbage_collection_number_of_gcs, counter, Help, Value};
60+
pre_promtool_compat_fun({garbage_collection_bytes_reclaimed_total, counter, Help, Value}) ->
61+
{garbage_collection_bytes_reclaimed, counter, Help, Value};
62+
pre_promtool_compat_fun({garbage_collection_words_reclaimed_total, counter, Help, Value}) ->
63+
{garbage_collection_words_reclaimed, counter, Help, Value};
64+
pre_promtool_compat_fun({runtime_seconds_total, counter, Help, Value}) ->
65+
{runtime_milliseconds, counter, Help, Value};
66+
pre_promtool_compat_fun({wallclock_time_seconds_total, counter, Help, Value}) ->
67+
{wallclock_time_milliseconds, counter, Help, Value};
68+
pre_promtool_compat_fun({ports, gauge, Help}) ->
69+
{port_count, gauge, Help};
70+
pre_promtool_compat_fun({processes, gauge, Help}) ->
71+
{process_count, gauge, Help};
72+
pre_promtool_compat_fun({atoms, gauge, Help}) ->
73+
{atom_count, gauge, Help};
74+
pre_promtool_compat_fun(Metric) ->
75+
Metric.
76+
77+
-spec is_enabled() -> dynamic().
78+
is_enabled() ->
79+
application:get_env(prometheus, collectors_compat, false).

src/collectors/vm/prometheus_vm_memory_collector.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ deregister_cleanup(_) ->
7070
_Registry :: prometheus_registry:registry(),
7171
Callback :: prometheus_collector:collect_mf_callback().
7272
collect_mf(_Registry, Callback) ->
73-
Metrics = metrics(),
73+
Metrics = prometheus_collectors_compat:pre_promtool_compat(metrics()),
7474
EnabledMetrics = enabled_metrics(),
7575
[
7676
add_metric_family(Metric, Callback)

src/collectors/vm/prometheus_vm_statistics_collector.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ deregister_cleanup(_) ->
8989
_Registry :: prometheus_registry:registry(),
9090
Callback :: prometheus_collector:collect_mf_callback().
9191
collect_mf(_Registry, Callback) ->
92-
Metrics = metrics(),
92+
Metrics = prometheus_collectors_compat:pre_promtool_compat(metrics()),
9393
EnabledMetrics = enabled_metrics(),
9494
[
9595
add_metric_family(Metric, Callback)

src/collectors/vm/prometheus_vm_system_info_collector.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ deregister_cleanup(_) ->
127127
_Registry :: prometheus_registry:registry(),
128128
Callback :: prometheus_collector:collect_mf_callback().
129129
collect_mf(_Registry, Callback) ->
130-
Metrics = metrics(),
130+
Metrics = prometheus_collectors_compat:pre_promtool_compat(metrics()),
131131
EnabledMetrics = enabled_metrics(),
132132
[
133133
add_metric_family(Metric, Callback)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
-module(prometheus_collectors_compat_tests).
2+
3+
-include_lib("eunit/include/eunit.hrl").
4+
5+
prometheus_format_test_() ->
6+
{setup,
7+
fun() ->
8+
mnesia:start(),
9+
prometheus_eunit_common:start()
10+
end,
11+
fun(X) ->
12+
mnesia:stop(),
13+
prometheus_eunit_common:stop(X)
14+
end,
15+
fun test_all_metrics/1}.
16+
17+
test_all_metrics(_) ->
18+
try
19+
application:set_env(prometheus, collectors_compat, true),
20+
Collectors = [
21+
prometheus_mnesia_collector,
22+
prometheus_vm_memory_collector,
23+
prometheus_vm_msacc_collector,
24+
prometheus_vm_statistics_collector,
25+
prometheus_vm_system_info_collector
26+
],
27+
[prometheus_registry:register_collector(Collector) || Collector <- Collectors],
28+
Metrics = prometheus_text_format:format(),
29+
[
30+
?_assertMatch({match, _}, re:run(Metrics, "erlang_mnesia_failed_transactions")),
31+
?_assertMatch({match, _}, re:run(Metrics, "erlang_mnesia_committed_transactions")),
32+
?_assertMatch({match, _}, re:run(Metrics, "erlang_mnesia_logged_transactions")),
33+
?_assertMatch({match, _}, re:run(Metrics, "erlang_mnesia_restarted_transactions")),
34+
?_assertMatch({match, _}, re:run(Metrics, "erlang_vm_memory_atom_bytes_total")),
35+
?_assertMatch({match, _}, re:run(Metrics, "erlang_vm_memory_bytes_total")),
36+
?_assertMatch({match, _}, re:run(Metrics, "erlang_vm_memory_processes_bytes_total")),
37+
?_assertMatch({match, _}, re:run(Metrics, "erlang_vm_memory_system_bytes_total")),
38+
?_assertMatch({match, _}, re:run(Metrics, "erlang_vm_statistics_context_switches")),
39+
?_assertMatch(
40+
{match, _}, re:run(Metrics, "erlang_vm_statistics_garbage_collection_number_of_gcs")
41+
),
42+
?_assertMatch(
43+
{match, _},
44+
re:run(Metrics, "erlang_vm_statistics_garbage_collection_words_reclaimed")
45+
),
46+
?_assertMatch(
47+
{match, _},
48+
re:run(Metrics, "erlang_vm_statistics_garbage_collection_bytes_reclaimed")
49+
),
50+
?_assertMatch({match, _}, re:run(Metrics, "erlang_vm_statistics_runtime_milliseconds")),
51+
?_assertMatch(
52+
{match, _}, re:run(Metrics, "erlang_vm_statistics_wallclock_time_milliseconds")
53+
),
54+
?_assertMatch({match, _}, re:run(Metrics, "erlang_vm_port_count")),
55+
?_assertMatch({match, _}, re:run(Metrics, "erlang_vm_process_count")),
56+
?_assertMatch({match, _}, re:run(Metrics, "erlang_vm_atom_count"))
57+
]
58+
after
59+
application:unset_env(prometheus, collectors_compat)
60+
end.

0 commit comments

Comments
 (0)