emqx_modules_conf.erl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-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. %% @doc The emqx-modules configuration interoperable interfaces
  17. -module(emqx_modules_conf).
  18. -behaviour(emqx_config_handler).
  19. %% Load/Unload
  20. -export([
  21. load/0,
  22. unload/0
  23. ]).
  24. -export([
  25. topic_metrics/0,
  26. add_topic_metrics/1,
  27. remove_topic_metrics/1,
  28. is_telemetry_enabled/0,
  29. set_telemetry_status/1
  30. ]).
  31. %% config handlers
  32. -export([
  33. pre_config_update/3,
  34. post_config_update/5
  35. ]).
  36. %%--------------------------------------------------------------------
  37. %% Load/Unload
  38. %%--------------------------------------------------------------------
  39. -spec load() -> ok.
  40. load() ->
  41. emqx_conf:add_handler([topic_metrics], ?MODULE),
  42. emqx_conf:add_handler([telemetry], ?MODULE).
  43. -spec unload() -> ok.
  44. unload() ->
  45. emqx_conf:remove_handler([telemetry]),
  46. emqx_conf:remove_handler([topic_metrics]).
  47. %%--------------------------------------------------------------------
  48. %% Topic-Metrics
  49. %%--------------------------------------------------------------------
  50. -spec topic_metrics() -> [emqx_types:topic()].
  51. topic_metrics() ->
  52. lists:map(
  53. fun(#{topic := Topic}) -> Topic end,
  54. emqx:get_config([topic_metrics])
  55. ).
  56. -spec add_topic_metrics(emqx_types:topic()) ->
  57. {ok, emqx_types:topic()}
  58. | {error, term()}.
  59. add_topic_metrics(Topic) ->
  60. case cfg_update([topic_metrics], ?FUNCTION_NAME, Topic) of
  61. {ok, _} -> {ok, Topic};
  62. {error, Reason} -> {error, Reason}
  63. end.
  64. -spec remove_topic_metrics(emqx_types:topic()) ->
  65. ok
  66. | {error, term()}.
  67. remove_topic_metrics(Topic) ->
  68. case cfg_update([topic_metrics], ?FUNCTION_NAME, Topic) of
  69. {ok, _} -> ok;
  70. {error, Reason} -> {error, Reason}
  71. end.
  72. -spec is_telemetry_enabled() -> boolean().
  73. is_telemetry_enabled() ->
  74. IsOfficial = emqx_telemetry:official_version(emqx_release:version()),
  75. emqx_conf:get([telemetry, enable], IsOfficial).
  76. -spec set_telemetry_status(boolean()) -> ok | {error, term()}.
  77. set_telemetry_status(Status) ->
  78. case cfg_update([telemetry], set_telemetry_status, Status) of
  79. {ok, _} -> ok;
  80. {error, _} = Error -> Error
  81. end.
  82. %%--------------------------------------------------------------------
  83. %% Config Handler
  84. %%--------------------------------------------------------------------
  85. -spec pre_config_update(
  86. list(atom()),
  87. emqx_config:update_request(),
  88. emqx_config:raw_config()
  89. ) ->
  90. {ok, emqx_config:update_request()} | {error, term()}.
  91. pre_config_update(_, {add_topic_metrics, Topic0}, RawConf) ->
  92. Topic = #{<<"topic">> => Topic0},
  93. case lists:member(Topic, RawConf) of
  94. true ->
  95. {error, already_existed};
  96. _ ->
  97. {ok, RawConf ++ [Topic]}
  98. end;
  99. pre_config_update(_, {remove_topic_metrics, Topic0}, RawConf) ->
  100. Topic = #{<<"topic">> => Topic0},
  101. case lists:member(Topic, RawConf) of
  102. true ->
  103. {ok, RawConf -- [Topic]};
  104. _ ->
  105. {error, not_found}
  106. end;
  107. pre_config_update(_, {set_telemetry_status, Status}, RawConf) ->
  108. {ok, RawConf#{<<"enable">> => Status}}.
  109. -spec post_config_update(
  110. list(atom()),
  111. emqx_config:update_request(),
  112. emqx_config:config(),
  113. emqx_config:config(),
  114. emqx_config:app_envs()
  115. ) ->
  116. ok | {ok, Result :: any()} | {error, Reason :: term()}.
  117. post_config_update(
  118. _,
  119. {add_topic_metrics, Topic},
  120. _NewConfig,
  121. _OldConfig,
  122. _AppEnvs
  123. ) ->
  124. case emqx_topic_metrics:register(Topic) of
  125. ok -> ok;
  126. {error, Reason} -> {error, Reason}
  127. end;
  128. post_config_update(
  129. _,
  130. {remove_topic_metrics, Topic},
  131. _NewConfig,
  132. _OldConfig,
  133. _AppEnvs
  134. ) ->
  135. case emqx_topic_metrics:deregister(Topic) of
  136. ok -> ok;
  137. {error, Reason} -> {error, Reason}
  138. end;
  139. post_config_update(
  140. _,
  141. {set_telemetry_status, Status},
  142. _NewConfig,
  143. _OldConfig,
  144. _AppEnvs
  145. ) ->
  146. case Status of
  147. true -> emqx_telemetry:enable();
  148. false -> emqx_telemetry:disable()
  149. end.
  150. %%--------------------------------------------------------------------
  151. %% Private
  152. %%--------------------------------------------------------------------
  153. res({ok, Result}) -> {ok, Result};
  154. res({error, {pre_config_update, ?MODULE, Reason}}) -> {error, Reason};
  155. res({error, {post_config_update, ?MODULE, Reason}}) -> {error, Reason};
  156. res({error, Reason}) -> {error, Reason}.
  157. cfg_update(Path, Action, Params) ->
  158. res(
  159. emqx_conf:update(
  160. Path,
  161. {Action, Params},
  162. #{override_to => cluster}
  163. )
  164. ).