summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlya Khaprov <i.khaprov@gmail.com>2021-08-16 20:32:29 +0200
committerMichael Klishin <michael@clojurewerkz.org>2021-11-11 15:57:51 +0300
commit6e64f8732aad98f386f4ca45248dfc19b1a3ce2f (patch)
treeab433897c17fb78e587b2ad46ae1182ecfcb2912
parentc792a63dfda2e53b91269546e1e26f62ce7837b9 (diff)
downloadrabbitmq-server-git-6e64f8732aad98f386f4ca45248dfc19b1a3ce2f.tar.gz
Add alarms prometheus collector.
close #2653 (cherry picked from commit 9fed915192c1bd72aaec12bddb120b8b0cc0dff4) Conflicts: deps/rabbitmq_prometheus/src/rabbit_prometheus_dispatcher.erl release-notes/3.9.4.md
-rw-r--r--deps/rabbitmq_prometheus/src/collectors/prometheus_rabbitmq_alarm_metrics_collector.erl80
-rw-r--r--deps/rabbitmq_prometheus/src/rabbit_prometheus_dispatcher.erl4
2 files changed, 83 insertions, 1 deletions
diff --git a/deps/rabbitmq_prometheus/src/collectors/prometheus_rabbitmq_alarm_metrics_collector.erl b/deps/rabbitmq_prometheus/src/collectors/prometheus_rabbitmq_alarm_metrics_collector.erl
new file mode 100644
index 0000000000..c210ee0965
--- /dev/null
+++ b/deps/rabbitmq_prometheus/src/collectors/prometheus_rabbitmq_alarm_metrics_collector.erl
@@ -0,0 +1,80 @@
+%% This Source Code Form is subject to the terms of the Mozilla Public
+%% License, v. 2.0. If a copy of the MPL was not distributed with this
+%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
+%%
+%% Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved.
+%%
+-module(prometheus_rabbitmq_alarm_metrics_collector).
+
+-export([register/0, deregister_cleanup/1, collect_mf/2]).
+
+-import(prometheus_model_helpers, [create_mf/4, untyped_metric/1]).
+
+-include_lib("prometheus/include/prometheus.hrl").
+
+-behaviour(prometheus_collector).
+
+-define(METRIC_NAME_PREFIX, "rabbitmq_alarms_").
+
+%%====================================================================
+%% Collector API
+%%====================================================================
+
+register() ->
+ ok = prometheus_registry:register_collector(?MODULE).
+
+deregister_cleanup(_) ->
+ ok.
+
+-spec collect_mf(_Registry, Callback) -> ok
+ when _Registry :: prometheus_registry:registry(),
+ Callback :: prometheus_collector:callback().
+collect_mf(_Registry, Callback) ->
+ try
+ case rabbit_alarm:get_local_alarms(500) %% TODO: figure out timeout
+ of
+ Alarms when is_list(Alarms) ->
+ ActiveAlarms =
+ lists:foldl(fun ({{resource_limit, disk, _}, _}, Acc) ->
+ maps:put(disk_limit, 1, Acc);
+ ({{resource_limit, memory, _}, _}, Acc) ->
+ maps:put(memory_limit, 1, Acc);
+ ({file_descriptor_limit, _}, Acc) ->
+ maps:put(file_descriptor_limit, 1, Acc)
+ end,
+ #{},
+ Alarms),
+
+ Callback(create_mf(?METRIC_NAME(<<"file_descriptor_limit">>),
+ <<"is 1 if file descriptor limit alarm is in effect">>,
+ untyped,
+ [untyped_metric(maps:get(file_descriptor_limit,
+ ActiveAlarms,
+ 0))])),
+ Callback(create_mf(?METRIC_NAME(<<"disk_limit">>),
+ <<"is 1 if disk alarm is in effect">>,
+ untyped,
+ [untyped_metric(maps:get(disk_limit, ActiveAlarms, 0))])),
+ Callback(create_mf(?METRIC_NAME(<<"memory_limit">>),
+ <<"is 1 if memory alarm is in effect">>,
+ untyped,
+ [untyped_metric(maps:get(memory_limit, ActiveAlarms, 0))])),
+ ok;
+ Error ->
+ rabbit_log:error("alarm_metrics_collector failed to emit metrics: "
+ "rabbitm_alarm:get_local_alarms returned ~p",
+ [Error]),
+ %% We are not going to render any alarm metrics here.
+ %% Breaks continuity but at least doesn't crash the
+ %% whole scraping endpoint
+ ok
+ end
+ catch
+ exit:{timeout, _} ->
+ rabbit_log:error("alarm_metrics_collector failed to emit metrics: "
+ "rabbitm_alarm:get_local_alarms timed out"),
+ %% We are not going to render any alarm metrics here.
+ %% Breaks continuity but at least doesn't crash the
+ %% whole scraping endpoint
+ ok
+ end.
diff --git a/deps/rabbitmq_prometheus/src/rabbit_prometheus_dispatcher.erl b/deps/rabbitmq_prometheus/src/rabbit_prometheus_dispatcher.erl
index 918a50f942..242a798089 100644
--- a/deps/rabbitmq_prometheus/src/rabbit_prometheus_dispatcher.erl
+++ b/deps/rabbitmq_prometheus/src/rabbit_prometheus_dispatcher.erl
@@ -13,7 +13,9 @@
build_dispatcher() ->
{ok, _} = application:ensure_all_started(prometheus),
- prometheus_registry:register_collectors([prometheus_rabbitmq_core_metrics_collector]),
+ prometheus_registry:register_collectors([
+ prometheus_rabbitmq_core_metrics_collector,
+ prometheus_rabbitmq_alarm_metrics_collector]),
prometheus_registry:register_collectors('per-object', [
prometheus_vm_system_info_collector,
prometheus_vm_dist_collector,