Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global_labels environment configuration #91

Merged
merged 1 commit into from Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/prometheus_collector.erl
Expand Up @@ -89,6 +89,7 @@
prometheus_vm_system_info_collector]).

-include("prometheus.hrl").
-include("prometheus_model.hrl").

%%====================================================================
%% Types
Expand Down Expand Up @@ -137,7 +138,18 @@ enabled_collectors() ->
Registry :: prometheus_registry:registry(),
Collector :: collector(),
Callback :: collect_mf_callback().
collect_mf(Registry, Collector, Callback) ->
collect_mf(Registry, Collector, Callback0) ->
Callback = case application:get_env(prometheus, global_labels) of
undefined ->
Callback0;
{ok, Labels0} ->
Labels = prometheus_model_helpers:label_pairs(Labels0),
fun (MF=#'MetricFamily'{metric=Metrics0}) ->
Metrics = [M#'Metric'{label=Labels ++ ML}
|| M=#'Metric'{label=ML} <- Metrics0],
Callback0(MF#'MetricFamily'{metric=Metrics})
end
end,
ok = Collector:collect_mf(Registry, Callback).

%%====================================================================
Expand Down
Expand Up @@ -21,7 +21,8 @@ prometheus_mnesia_on_test_() ->
prometheus_eunit_common:stop(X) end,
[fun test_mnesia_on_collector/0,
fun test_mnesia_on_collector_env_on/0,
fun test_mnesia_on_collector_env_off/0]}.
fun test_mnesia_on_collector_env_off/0,
fun test_mnesia_on_collector_global_labels/0]}.

test_mnesia_on_collector_env_on() ->
prometheus_registry:register_collector(prometheus_mnesia_collector),
Expand Down Expand Up @@ -63,3 +64,13 @@ test_mnesia_on_collector() ->
?assertMatch({match,_}, re:run(Metrics, "erlang_mnesia_committed_transactions")),
?assertMatch({match,_}, re:run(Metrics, "erlang_mnesia_logged_transactions")),
?assertMatch({match,_}, re:run(Metrics, "erlang_mnesia_restarted_transactions")).

test_mnesia_on_collector_global_labels() ->
Metrics = try
application:set_env(prometheus, global_labels, [{node, node()}]),
prometheus_registry:register_collector(prometheus_mnesia_collector),
prometheus_text_format:format()
after
application:unset_env(prometheus, global_labels)
end,
?assertMatch({match,_}, re:run(Metrics, "erlang_mnesia_held_locks{node=")).
Expand Up @@ -8,7 +8,8 @@ prometheus_format_test_() ->
fun prometheus_eunit_common:stop/1,
[fun test_default_metrics/1,
fun test_all_metrics/1,
fun test_custom_metrics/1]}.
fun test_custom_metrics/1,
fun test_global_labels/1]}.

test_default_metrics(_) ->
prometheus_registry:register_collector(prometheus_vm_memory_collector),
Expand Down Expand Up @@ -67,3 +68,16 @@ test_custom_metrics(_) ->
after
application:unset_env(prometheus, vm_memory_collector_metrics)
end.

test_global_labels(_) ->
Metrics = try
prometheus:start(),
application:set_env(prometheus, global_labels, [{node, node()}]),
prometheus_registry:register_collector(prometheus_vm_memory_collector),
prometheus_text_format:format()
after
application:unset_env(prometheus, global_labels)
end,
[
?_assertMatch({match, _}, re:run(Metrics, "erlang_vm_memory_atom_bytes_total{node="))
].
Expand Up @@ -8,7 +8,8 @@ prometheus_format_test_() ->
fun prometheus_eunit_common:stop/1,
[fun test_default_metrics/1,
fun test_all_metrics/1,
fun test_custom_metrics/1]}.
fun test_custom_metrics/1,
fun test_global_labels/1]}.

test_default_metrics(_) ->
prometheus_registry:register_collector(prometheus_vm_statistics_collector),
Expand Down Expand Up @@ -133,3 +134,17 @@ test_custom_metrics(_) ->
after
application:unset_env(prometheus, vm_statistics_collector_metrics)
end.

test_global_labels(_) ->
Metrics = try
prometheus:start(),
application:set_env(prometheus, global_labels, [{node, node()}]),
prometheus_registry:register_collector(prometheus_vm_statistics_collector),
prometheus_text_format:format()
after
application:unset_env(prometheus, global_labels)
end,
[
?_assertMatch({match, _},
re:run(Metrics, "erlang_vm_statistics_bytes_output_total{node="))
].
Expand Up @@ -8,7 +8,8 @@ prometheus_format_test_() ->
fun prometheus_eunit_common:stop/1,
[fun test_default_metrics/1,
fun test_all_metrics/1,
fun test_custom_metrics/1]}.
fun test_custom_metrics/1,
fun test_global_labels/1]}.

test_default_metrics(_) ->
prometheus_registry:register_collector(prometheus_vm_system_info_collector),
Expand Down Expand Up @@ -116,3 +117,17 @@ test_custom_metrics(_) ->
after
application:unset_env(prometheus, vm_system_info_collector_metrics)
end.


test_global_labels(_) ->
Metrics = try
prometheus:start(),
application:set_env(prometheus, global_labels, [{node, node()}]),
prometheus_registry:register_collector(prometheus_vm_system_info_collector),
prometheus_text_format:format()
after
application:unset_env(prometheus, global_labels)
end,
[
?_assertMatch({match, _}, re:run(Metrics, "erlang_vm_dirty_cpu_schedulers{node="))
].
23 changes: 23 additions & 0 deletions test/eunit/metric/prometheus_boolean_tests.erl
Expand Up @@ -17,6 +17,7 @@ prometheus_format_test_() ->
fun test_default_value/1,
fun test_collector1/1,
fun test_collector2/1,
fun test_collector3/1,
fun test_values/1]}.

test_registration(_)->
Expand Down Expand Up @@ -194,3 +195,25 @@ test_collector2(_) ->
value= <<"label_value">>}],
untyped=#'Untyped'{value=0}}]}],
prometheus_collector:collect_mf_to_list(prometheus_boolean))].


test_collector3(_) ->
MFList = try
prometheus:start(),
application:set_env(prometheus, global_labels, [{node, node()}]),
prometheus_boolean:new([{name, simple_boolean},
{labels, ["label"]},
{help, ""}]),
prometheus_boolean:set(simple_boolean, [label_value], false),
prometheus_collector:collect_mf_to_list(prometheus_boolean)
after
application:unset_env(prometheus, global_labels)
end,
NodeBin = atom_to_binary(node(), utf8),
[?_assertMatch([#'MetricFamily'{metric=
[#'Metric'{label=[#'LabelPair'{name= <<"node">>,
value= NodeBin},
#'LabelPair'{name= "label",
value= <<"label_value">>}],
untyped=#'Untyped'{value=0}}]}],
MFList)].
25 changes: 24 additions & 1 deletion test/eunit/metric/prometheus_counter_tests.erl
Expand Up @@ -16,7 +16,8 @@ prometheus_format_test_() ->
fun test_default_value/1,
fun test_values/1,
fun test_collector1/1,
fun test_collector2/1]}.
fun test_collector2/1,
fun test_collector3/1]}.

test_registration(_)->
Name = http_requests_total,
Expand Down Expand Up @@ -173,3 +174,25 @@ test_collector2(_) ->
value= <<"label_value">>}],
counter=#'Counter'{value=1}}]}],
prometheus_collector:collect_mf_to_list(prometheus_counter))].


test_collector3(_) ->
MFList = try
prometheus:start(),
application:set_env(prometheus, global_labels, [{node, node()}]),
prometheus_counter:new([{name, simple_counter},
{labels, ["label"]},
{help, ""}]),
prometheus_counter:inc(simple_counter, [label_value], 1),
prometheus_collector:collect_mf_to_list(prometheus_counter)
after
application:unset_env(prometheus, global_labels)
end,
NodeBin = atom_to_binary(node(), utf8),
[?_assertMatch([#'MetricFamily'{metric=
[#'Metric'{label=[#'LabelPair'{name= <<"node">>,
value= NodeBin},
#'LabelPair'{name= "label",
value= <<"label_value">>}],
counter=#'Counter'{value=1}}]}],
MFList)].
25 changes: 24 additions & 1 deletion test/eunit/metric/prometheus_gauge_tests.erl
Expand Up @@ -22,7 +22,8 @@ prometheus_format_test_() ->
fun test_default_value/1,
fun test_values/1,
fun test_collector1/1,
fun test_collector2/1]}.
fun test_collector2/1,
fun test_collector3/1]}.

test_registration(_)->
Name = pool_size,
Expand Down Expand Up @@ -314,3 +315,25 @@ test_collector2(_) ->
value= <<"label_value">>}],
gauge=#'Gauge'{value=1}}]}],
prometheus_collector:collect_mf_to_list(prometheus_gauge))].


test_collector3(_) ->
MFList = try
prometheus:start(),
application:set_env(prometheus, global_labels, [{node, node()}]),
prometheus_gauge:new([{name, simple_gauge},
{labels, ["label"]},
{help, ""}]),
prometheus_gauge:set(simple_gauge, [label_value], 1),
prometheus_collector:collect_mf_to_list(prometheus_gauge)
after
application:unset_env(prometheus, global_labels)
end,
NodeBin = atom_to_binary(node(), utf8),
[?_assertMatch([#'MetricFamily'{metric=
[#'Metric'{label=[#'LabelPair'{name= <<"node">>,
value= NodeBin},
#'LabelPair'{name= "label",
value= <<"label_value">>}],
gauge=#'Gauge'{value=1}}]}],
MFList)].
33 changes: 32 additions & 1 deletion test/eunit/metric/prometheus_histogram_tests.erl
Expand Up @@ -19,7 +19,8 @@ prometheus_format_test_() ->
fun test_default_value/1,
fun test_values/1,
fun test_collector1/1,
fun test_collector2/1]}.
fun test_collector2/1,
fun test_collector3/1]}.

test_registration(_)->
Name = request_duration,
Expand Down Expand Up @@ -379,3 +380,33 @@ test_collector2(_) ->
#'Bucket'{cumulative_count=1,
upper_bound=infinity}]}}]}],
prometheus_collector:collect_mf_to_list(prometheus_histogram))].


test_collector3(_) ->
MFList = try
prometheus:start(),
application:set_env(prometheus, global_labels, [{node, node()}]),
prometheus_histogram:new([{name, simple_histogram},
{labels, ["label"]},
{buckets, [5, 10]},
{help, ""}]),
prometheus_histogram:observe(simple_histogram, [label_value], 7),
prometheus_collector:collect_mf_to_list(prometheus_histogram)
after
application:unset_env(prometheus, global_labels)
end,
NodeBin = atom_to_binary(node(), utf8),
[?_assertMatch([#'MetricFamily'{metric=
[#'Metric'{label=[#'LabelPair'{name= <<"node">>,
value= NodeBin},
#'LabelPair'{name= "label",
value= <<"label_value">>}],
histogram=#'Histogram'{sample_count=1,
sample_sum=7,
bucket=[#'Bucket'{cumulative_count=0,
upper_bound=5},
#'Bucket'{cumulative_count=1,
upper_bound=10},
#'Bucket'{cumulative_count=1,
upper_bound=infinity}]}}]}],
MFList)].
26 changes: 25 additions & 1 deletion test/eunit/metric/prometheus_summary_tests.erl
Expand Up @@ -18,7 +18,8 @@ prometheus_format_test_() ->
fun test_default_value/1,
fun test_values/1,
fun test_collector1/1,
fun test_collector2/1]}.
fun test_collector2/1,
fun test_collector3/1]}.

test_registration(_)->
Name = orders_summary,
Expand Down Expand Up @@ -238,3 +239,26 @@ test_collector2(_) ->
summary=#'Summary'{sample_count=1,
sample_sum=5}}]}],
prometheus_collector:collect_mf_to_list(prometheus_summary))].


test_collector3(_) ->
MFList = try
prometheus:start(),
application:set_env(prometheus, global_labels, [{node, node()}]),
prometheus_summary:new([{name, simple_summary},
{labels, ["label"]},
{help, ""}]),
prometheus_summary:observe(simple_summary, [label_value], 5),
prometheus_collector:collect_mf_to_list(prometheus_summary)
after
application:unset_env(prometheus, global_labels)
end,
NodeBin = atom_to_binary(node(), utf8),
[?_assertMatch([#'MetricFamily'{metric=
[#'Metric'{label=[#'LabelPair'{name= <<"node">>,
value= NodeBin},
#'LabelPair'{name= "label",
value= <<"label_value">>}],
summary=#'Summary'{sample_count=1,
sample_sum=5}}]}],
MFList)].