Skip to content

Commit 689ea65

Browse files
authored
Merge pull request emqx#7018 from k32/mria-prom-metrics
feat(mria): Add prometheus metrics
2 parents 0834b77 + c00f4ef commit 689ea65

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

apps/emqx_prometheus/rebar.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{deps,
44
[ {emqx, {path, "../emqx"}},
55
%% FIXME: tag this as v3.1.3
6-
{prometheus, {git, "https://github.com/emqx/prometheus.erl", {ref, "9994c76adca40d91a2545102230ccce2423fd8a7"}}},
6+
{prometheus, {git, "https://github.com/deadtrickster/prometheus.erl", {tag, "v4.8.1"}}},
77
{hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.24.0"}}},
88
{minirest, {git, "https://github.com/emqx/minirest", {tag, "1.2.11"}}}
99
]}.

apps/emqx_prometheus/src/emqx_prometheus.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ add_collect_family(Name, Data, Callback, Type) ->
205205
Callback(create_schema(Name, <<"">>, Data, Type)).
206206

207207
create_schema(Name, Help, Data, Type) ->
208-
create_mf(Name, Help, Type, ?MODULE, Data).
208+
create_mf(Name, Help, Type, ?MODULE, Data).
209209

210210
%%--------------------------------------------------------------------
211211
%% Collector
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
%%--------------------------------------------------------------------
2+
%% Copyright (c) 2022 EMQ Technologies Co., Ltd. All Rights Reserved.
3+
%%
4+
%% Licensed under the Apache License, Version 2.0 (the "License");
5+
%% you may not use this file except in compliance with the License.
6+
%% You may obtain a copy of the License at
7+
%%
8+
%% http://www.apache.org/licenses/LICENSE-2.0
9+
%%
10+
%% Unless required by applicable law or agreed to in writing, software
11+
%% distributed under the License is distributed on an "AS IS" BASIS,
12+
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
%% See the License for the specific language governing permissions and
14+
%% limitations under the License.
15+
%%--------------------------------------------------------------------
16+
-module(emqx_prometheus_mria).
17+
18+
-export([deregister_cleanup/1,
19+
collect_mf/2
20+
]).
21+
22+
-include_lib("prometheus/include/prometheus.hrl").
23+
24+
%% Please don't remove this attribute, prometheus uses it to
25+
%% automatically register collectors.
26+
-behaviour(prometheus_collector).
27+
28+
%%====================================================================
29+
%% Macros
30+
%%====================================================================
31+
32+
-define(METRIC_NAME_PREFIX, "emqx_mria_").
33+
34+
%%====================================================================
35+
%% Collector API
36+
%%====================================================================
37+
38+
%% @private
39+
deregister_cleanup(_) -> ok.
40+
41+
%% @private
42+
-spec collect_mf(_Registry, Callback) -> ok when
43+
_Registry :: prometheus_registry:registry(),
44+
Callback :: prometheus_collector:callback().
45+
collect_mf(_Registry, Callback) ->
46+
case mria_rlog:backend() of
47+
rlog ->
48+
Metrics = metrics(),
49+
_ = [add_metric_family(Metric, Callback) || Metric <- Metrics],
50+
ok;
51+
mnesia ->
52+
ok
53+
end.
54+
55+
add_metric_family({Name, Metrics}, Callback) ->
56+
Callback(prometheus_model_helpers:create_mf( ?METRIC_NAME(Name)
57+
, <<"">>
58+
, gauge
59+
, catch_all(Metrics)
60+
)).
61+
62+
%%====================================================================
63+
%% Internal functions
64+
%%====================================================================
65+
66+
metrics() ->
67+
Metrics = case mria_rlog:role() of
68+
replicant ->
69+
[lag, bootstrap_time, bootstrap_num_keys, message_queue_len, replayq_len];
70+
core ->
71+
[last_intercepted_trans, weight, replicants, server_mql]
72+
end,
73+
[{MetricId, fun() -> get_shard_metric(MetricId) end} || MetricId <- Metrics].
74+
75+
get_shard_metric(Metric) ->
76+
%% TODO: only report shards that are up
77+
[{[{shard, Shard}], get_shard_metric(Metric, Shard)} ||
78+
Shard <- mria_schema:shards(), Shard =/= undefined].
79+
80+
get_shard_metric(replicants, Shard) ->
81+
length(mria_status:agents(Shard));
82+
get_shard_metric(Metric, Shard) ->
83+
maps:get(Metric, mria_status:get_shard_stats(Shard), undefined).
84+
85+
catch_all(DataFun) ->
86+
try DataFun()
87+
catch _:_ -> undefined
88+
end.

0 commit comments

Comments
 (0)