Преглед изворни кода

feat(ds): Add metrics worker to the builtin db supervision tree

ieQu1 пре 1 година
родитељ
комит
c9de336234

+ 75 - 0
apps/emqx_durable_storage/src/emqx_ds_builtin_metrics.erl

@@ -0,0 +1,75 @@
+%%--------------------------------------------------------------------
+%% Copyright (c) 2023-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
+%%
+%% Licensed under the Apache License, Version 2.0 (the "License");
+%% you may not use this file except in compliance with the License.
+%% You may obtain a copy of the License at
+%%
+%%     http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing, software
+%% distributed under the License is distributed on an "AS IS" BASIS,
+%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+%% See the License for the specific language governing permissions and
+%% limitations under the License.
+%%--------------------------------------------------------------------
+-module(emqx_ds_builtin_metrics).
+
+%% API:
+-export([child_spec/0, init_for_db/1, init_for_shard/2]).
+
+%% behavior callbacks:
+-export([]).
+
+%% internal exports:
+-export([]).
+
+-export_type([]).
+
+%%================================================================================
+%% Type declarations
+%%================================================================================
+
+-define(WORKER, ?MODULE).
+
+-define(DB_METRICS,
+        [
+
+        ]).
+
+-define(SHARD_METRICS,
+        [
+         'egress.bytes',
+         'egress.batches',
+         'egress.messages',
+         {slide, 'egress.flush_time'}
+        ]).
+
+%%================================================================================
+%% API functions
+%%================================================================================
+
+-spec child_spec() -> supervisor:child_spec().
+child_spec() ->
+    emqx_metrics_worker:child_spec(?WORKER).
+
+-spec init_for_db(emqx_ds:db()) -> ok.
+init_for_db(DB) ->
+    emqx_metrics_worker:create_metrics(?WORKER, DB, ?DB_METRICS, []).
+
+-spec init_for_shard(emqx_ds:db(), emqx_ds_replication_layer:shard_id()) -> ok.
+init_for_shard(DB, ShardId) ->
+    Id = iolist_to_binary([atom_to_list(DB), $/, ShardId]),
+    emqx_metrics_worker:create_metrics(?WORKER, Id, ?SHARD_METRICS, []).
+
+%%================================================================================
+%% behavior callbacks
+%%================================================================================
+
+%%================================================================================
+%% Internal exports
+%%================================================================================
+
+%%================================================================================
+%% Internal functions
+%%================================================================================

+ 2 - 1
apps/emqx_durable_storage/src/emqx_ds_builtin_sup.erl

@@ -81,6 +81,7 @@ stop_db(DB) ->
 %% Chidren are attached dynamically to this one.
 init(?top) ->
     %% Children:
+    MetricsWorker = emqx_ds_builtin_metrics:child_spec(),
     MetadataServer = #{
         id => metadata_server,
         start => {emqx_ds_replication_layer_meta, start_link, []},
@@ -102,7 +103,7 @@ init(?top) ->
         period => 1,
         auto_shutdown => never
     },
-    {ok, {SupFlags, [MetadataServer, DBsSup]}};
+    {ok, {SupFlags, [MetricsWorker, MetadataServer, DBsSup]}};
 init(?databases) ->
     %% Children are added dynamically:
     SupFlags = #{