emqx_telemetry_api.erl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-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_telemetry_api).
  17. -behaviour(minirest_api).
  18. -include_lib("hocon/include/hoconsc.hrl").
  19. -include_lib("typerefl/include/types.hrl").
  20. -import(hoconsc, [mk/2, ref/1, ref/2, array/1]).
  21. -export([
  22. status/2,
  23. data/2
  24. ]).
  25. -export([
  26. api_spec/0,
  27. paths/0,
  28. schema/1,
  29. fields/1
  30. ]).
  31. -define(BAD_REQUEST, 'BAD_REQUEST').
  32. api_spec() ->
  33. emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
  34. paths() ->
  35. [
  36. "/telemetry/status",
  37. "/telemetry/data"
  38. ].
  39. schema("/telemetry/status") ->
  40. #{
  41. 'operationId' => status,
  42. get =>
  43. #{
  44. description => ?DESC(get_telemetry_status_api),
  45. tags => [<<"Telemetry">>],
  46. responses =>
  47. #{200 => status_schema(?DESC(get_telemetry_status_api))}
  48. },
  49. put =>
  50. #{
  51. description => ?DESC(update_telemetry_status_api),
  52. tags => [<<"Telemetry">>],
  53. 'requestBody' => status_schema(?DESC(update_telemetry_status_api)),
  54. responses =>
  55. #{
  56. 200 => status_schema(?DESC(update_telemetry_status_api))
  57. }
  58. }
  59. };
  60. schema("/telemetry/data") ->
  61. #{
  62. 'operationId' => data,
  63. get =>
  64. #{
  65. description => ?DESC(get_telemetry_data_api),
  66. tags => [<<"Telemetry">>],
  67. responses =>
  68. #{200 => mk(ref(?MODULE, telemetry), #{desc => ?DESC(get_telemetry_data_api)})}
  69. }
  70. }.
  71. status_schema(Desc) ->
  72. mk(ref(?MODULE, status), #{in => body, desc => Desc}).
  73. fields(status) ->
  74. [
  75. {enable,
  76. mk(
  77. boolean(),
  78. #{
  79. desc => ?DESC(enable),
  80. default => true,
  81. example => false
  82. }
  83. )}
  84. ];
  85. fields(telemetry) ->
  86. [
  87. {emqx_version,
  88. mk(
  89. string(),
  90. #{
  91. desc => ?DESC(emqx_version),
  92. example => <<"5.0.0-beta.3-32d1547c">>
  93. }
  94. )},
  95. {license,
  96. mk(
  97. map(),
  98. #{
  99. desc => ?DESC(license),
  100. example => #{edition => <<"opensource">>}
  101. }
  102. )},
  103. {os_name,
  104. mk(
  105. string(),
  106. #{
  107. desc => ?DESC(os_name),
  108. example => <<"Linux">>
  109. }
  110. )},
  111. {os_version,
  112. mk(
  113. string(),
  114. #{
  115. desc => ?DESC(os_version),
  116. example => <<"20.04">>
  117. }
  118. )},
  119. {otp_version,
  120. mk(
  121. string(),
  122. #{
  123. desc => ?DESC(otp_version),
  124. example => <<"24">>
  125. }
  126. )},
  127. {up_time,
  128. mk(
  129. integer(),
  130. #{
  131. desc => ?DESC(up_time),
  132. example => 20220113
  133. }
  134. )},
  135. {uuid,
  136. mk(
  137. string(),
  138. #{
  139. desc => ?DESC(uuid),
  140. example => <<"AAAAAAAA-BBBB-CCCC-2022-DDDDEEEEFFF">>
  141. }
  142. )},
  143. {nodes_uuid,
  144. mk(
  145. array(binary()),
  146. #{
  147. desc => ?DESC(nodes_uuid),
  148. example => [
  149. <<"AAAAAAAA-BBBB-CCCC-2022-DDDDEEEEFFF">>,
  150. <<"ZZZZZZZZ-CCCC-BBBB-2022-DDDDEEEEFFF">>
  151. ]
  152. }
  153. )},
  154. {active_plugins,
  155. mk(
  156. array(binary()),
  157. #{
  158. desc => ?DESC(active_plugins),
  159. example => [<<"Plugin A">>, <<"Plugin B">>]
  160. }
  161. )},
  162. {active_modules,
  163. mk(
  164. array(binary()),
  165. #{
  166. desc => ?DESC(active_modules),
  167. example => [<<"Module A">>, <<"Module B">>]
  168. }
  169. )},
  170. {num_clients,
  171. mk(
  172. integer(),
  173. #{
  174. desc => ?DESC(num_clients),
  175. example => 20220113
  176. }
  177. )},
  178. {messages_received,
  179. mk(
  180. integer(),
  181. #{
  182. desc => ?DESC(messages_received),
  183. example => 2022
  184. }
  185. )},
  186. {messages_sent,
  187. mk(
  188. integer(),
  189. #{
  190. desc => ?DESC(messages_sent),
  191. example => 2022
  192. }
  193. )}
  194. ].
  195. %%--------------------------------------------------------------------
  196. %% HTTP API
  197. %%--------------------------------------------------------------------
  198. status(get, _Params) ->
  199. {200, get_telemetry_status()};
  200. status(put, #{body := Body}) ->
  201. Enable = maps:get(<<"enable">>, Body),
  202. case Enable =:= emqx_modules_conf:is_telemetry_enabled() of
  203. true ->
  204. Reason =
  205. case Enable of
  206. true -> <<"Telemetry status is already enabled">>;
  207. false -> <<"Telemetry status is already disabled">>
  208. end,
  209. {400, #{code => 'BAD_REQUEST', message => Reason}};
  210. false ->
  211. case enable_telemetry(Enable) of
  212. ok ->
  213. {200, get_telemetry_status()};
  214. {error, Reason} ->
  215. {400, #{
  216. code => 'BAD_REQUEST',
  217. message => Reason
  218. }}
  219. end
  220. end.
  221. data(get, _Request) ->
  222. {200, emqx_json:encode(get_telemetry_data())}.
  223. %%--------------------------------------------------------------------
  224. %% Internal functions
  225. %%--------------------------------------------------------------------
  226. enable_telemetry(Enable) ->
  227. emqx_modules_conf:set_telemetry_status(Enable).
  228. get_telemetry_status() ->
  229. #{enable => emqx_modules_conf:is_telemetry_enabled()}.
  230. get_telemetry_data() ->
  231. {ok, TelemetryData} = emqx_telemetry:get_telemetry(),
  232. TelemetryData.