|
| 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