Skip to content

Commit

Permalink
add rabbitmq_connections_total, close #34
Browse files Browse the repository at this point in the history
  • Loading branch information
deadtrickster committed Jan 20, 2018
1 parent d2ca2a3 commit 1e8afd0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ ERLANG_MK_COMMIT = rabbitmq-tmp
.PHONY: docker_build docker_push docker_latest docker_latest_pure

docker_build:
docker build -t deadtrickster/rabbitmq_prometheus\:3.7.1 .
docker build -t deadtrickster/rabbitmq_prometheus\:3.7.2 .
docker build -t deadtrickster/rabbitmq_prometheus\:latest .
docker build -t deadtrickster/rabbitmq_prometheus\:3.7.1-pure -f Dockerfile.pure .
docker build -t deadtrickster/rabbitmq_prometheus\:3.7.2-pure -f Dockerfile.pure .
docker build -t deadtrickster/rabbitmq_prometheus\:latest-pure -f Dockerfile.pure .

docker_push:
docker push deadtrickster/rabbitmq_prometheus\:3.7.1
docker push deadtrickster/rabbitmq_prometheus\:3.7.2
docker push deadtrickster/rabbitmq_prometheus\:latest
docker push deadtrickster/rabbitmq_prometheus\:3.7.1-pure
docker push deadtrickster/rabbitmq_prometheus\:3.7.2-pure
docker push deadtrickster/rabbitmq_prometheus\:latest-pure

docker_latest:
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ This exporter supports the following options via `rabbitmq_exporter` entry of `p
- `path` - scrape endpoint. Default is `"metrics"`. Note RabbitMQ translates this to `"/api/metrics"`;
- `format` - scrape format. Default is `prometheus_text_format`;
- `exchange_messages_stat` - same as `queue_messages_state` but for the exchanges;
- `queue_messages_stat` - messages state to export. Default is hopefully reasonable. You can read more about possible values [here](https://raw.githack.com/rabbitmq/rabbitmq-management/rabbitmq_v3_6_5/priv/www/doc/stats.html).
- `queue_messages_stat` - messages state to export. Default is hopefully reasonable. You can read more about possible values [here](https://raw.githack.com/rabbitmq/rabbitmq-management/rabbitmq_v3_6_5/priv/www/doc/stats.html);
- `connections_total_enabled` - Default is `false`. If `true`, the exporter will iterate over all connections and export count grouped by connection state (running, flow, etc).

Sample `/etc/rabbitmq/rabbitmq.config` showing how to customize the scrape `path`:
Sample `/etc/rabbitmq/rabbitmq.config` showing how to customize the scrape `path`, and `connections_total_enabled`:

```erlang
[
Expand All @@ -124,7 +125,8 @@ Sample `/etc/rabbitmq/rabbitmq.config` showing how to customize the scrape `path
%% environment before the "rabbitmq_management" one.
{prometheus, [
{rabbitmq_exporter, [
{path, "/mymetrics"}
{path, "/mymetrics"},
{connections_total_enabled, true}
]}
]},
{rabbitmq_management, [
Expand All @@ -148,6 +150,11 @@ For the latest list of supported options look [here](https://github.com/deadtric
Type: gauge.<br />
RabbitMQ Connections count.

* `rabbitmq_connections_total` (disabled by default)<br />
Type: gauge.<br />
Labels: state.<br />
RabbitMQ connections count grouped by connection state.

* `rabbitmq_channels`<br />
Type: gauge.<br />
RabbitMQ Channels count.
Expand Down
32 changes: 32 additions & 0 deletions src/collectors/prometheus_rabbitmq_overview_collector.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ collect_mf(_Registry, Callback) ->
_ ->
ok
end,
case prometheus_rabbitmq_exporter_config:connections_total_enabled() of
true ->
collect_rabbit_connections(Callback);
_ ->
ok
end,
ok.

collect_metrics(_, {messages_stat, MSKey, Stats}) ->
Expand All @@ -133,6 +139,32 @@ collect_rabbit_memory(Callback) ->
Callback(create_mf(FullName, Help, gauge, Value))
end || {Name, Help} <- ?MEMORY_METRICS].

collect_rabbit_connections(Callback) ->
Table = ets:new('$$connectinos_stat$$', [set]),
try
Connections = ets:select(connection_stats, [{{'$1', '$2'}, [], ['$2']}], 10),
connections_loop(Connections, Table),
Callback(create_mf(?METRIC_NAME(connections_total), "RabbitMQ connections count grouped by connection state.",
gauge, ets:tab2list(Table)))
after
ets:delete(Table)
end,
ok.

connections_loop('$end_of_table', _Table) ->
ok;
connections_loop({Connections, Continuation}, Table) ->
[begin
Labels = [{state, proplists:get_value(state, Connection)}],
try
ets:update_counter(Table, Labels, 1)
catch error:badarg ->
ets:insert(Table, {Labels, 1})
end
end
|| Connection <- Connections],
connections_loop(ets:select(Continuation), Table).

mf(Callback, Metric, Proplist) ->
{Name, Type, Help, Fun} = case Metric of
{Key, Type1, Help1} ->
Expand Down
2 changes: 1 addition & 1 deletion src/prometheus_rabbitmq_exporter.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, prometheus_rabbitmq_exporter,
[{description, "RabbitMQ Prometheus.io metrics exporter"},
{vsn, "v3.7.1.1"},
{vsn, "v3.7.2.1"},
{modules, []},
{registered, []},
{env, []},
Expand Down
9 changes: 8 additions & 1 deletion src/prometheus_rabbitmq_exporter_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
-export([path/0,
queue_messages_stat/0,
exchange_messages_stat/0,
memory_stat_enabled/0]).
memory_stat_enabled/0,
connections_total_enabled/0]).

-define(DEFAULT_PATH, "/metrics").
-define(DEFAULT_QUEUE_MESSAGES_STAT, [messages_published_total,
Expand All @@ -27,6 +28,7 @@
messages_redelivered_total,
messages_returned_total]).
-define(DEFAULT_MEMORY_STAT_ENABLED, false).
-define(DEFAULT_CONNECTIONS_TOTAL_ENABLED, false).

config() ->
application:get_env(prometheus, rabbitmq_exporter, []).
Expand All @@ -46,3 +48,8 @@ exchange_messages_stat() ->
memory_stat_enabled() ->
Config = config(),
proplists:get_value(memory_stat_enabled, Config, ?DEFAULT_MEMORY_STAT_ENABLED).

connections_total_enabled() ->
Config = config(),
proplists:get_value(connections_total_enabled, Config, ?DEFAULT_CONNECTIONS_TOTAL_ENABLED).

0 comments on commit 1e8afd0

Please sign in to comment.