emqx_external_trace.hrl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2019-2024 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. -ifndef(EMQX_EXT_TRACE_HRL).
  17. -define(EMQX_EXT_TRACE_HRL, true).
  18. %% --------------------------------------------------------------------
  19. %% Macros
  20. -define(EXT_TRACE_START, '$ext_trace_start').
  21. -define(EXT_TRACE_STOP, '$ext_trace_stop').
  22. -define(EMQX_EXTERNAL_MODULE, emqx_external_trace).
  23. -define(PROVIDER, {?EMQX_EXTERNAL_MODULE, trace_provider}).
  24. -ifndef(EMQX_RELEASE_EDITION).
  25. -define(EMQX_RELEASE_EDITION, ce).
  26. -endif.
  27. -if(?EMQX_RELEASE_EDITION == ee).
  28. -define(with_provider(IfRegistered, IfNotRegistered),
  29. fun() ->
  30. case persistent_term:get(?PROVIDER, undefined) of
  31. undefined ->
  32. IfNotRegistered;
  33. Provider ->
  34. Provider:IfRegistered
  35. end
  36. end()
  37. ).
  38. -define(EXT_TRACE_ANY(FuncName, Any, Attrs),
  39. ?with_provider(
  40. FuncName(Any, Attrs),
  41. Any
  42. )
  43. ).
  44. -define(EXT_TRACE_ADD_ATTRS(Attrs),
  45. ?with_provider(add_span_attrs(Attrs), ok)
  46. ).
  47. -define(EXT_TRACE_ADD_ATTRS(Attrs, Ctx),
  48. ?with_provider(add_span_attrs(Attrs, Ctx), ok)
  49. ).
  50. -define(EXT_TRACE_SET_STATUS_OK(),
  51. ?with_provider(
  52. set_status_ok(),
  53. ok
  54. )
  55. ).
  56. -define(EXT_TRACE_SET_STATUS_ERROR(),
  57. ?with_provider(
  58. set_status_error(),
  59. ok
  60. )
  61. ).
  62. -define(EXT_TRACE_SET_STATUS_ERROR(Msg),
  63. ?with_provider(
  64. set_status_error(Msg),
  65. ok
  66. )
  67. ).
  68. -define(EXT_TRACE_WITH_ACTION_START(FuncName, Any, Attrs),
  69. ?with_provider(
  70. FuncName(?EXT_TRACE_START, Any, Attrs),
  71. Any
  72. )
  73. ).
  74. -define(EXT_TRACE_WITH_ACTION_STOP(FuncName, Any, Attrs),
  75. ?with_provider(
  76. FuncName(?EXT_TRACE_STOP, Any, Attrs),
  77. ok
  78. )
  79. ).
  80. -define(EXT_TRACE_WITH_PROCESS_FUN(FuncName, Any, Attrs, ProcessFun),
  81. ?with_provider(
  82. FuncName(Any, Attrs, ProcessFun),
  83. ProcessFun(Any)
  84. )
  85. ).
  86. -type event_name() :: opentelemetry:event_name().
  87. -else.
  88. -define(EXT_TRACE_ANY(_FuncName, Any, _Attrs), Any).
  89. -define(EXT_TRACE_ADD_ATTRS(_Attrs), ok).
  90. -define(EXT_TRACE_ADD_ATTRS(_Attrs, _Ctx), ok).
  91. -define(EXT_TRACE_SET_STATUS_OK(), ok).
  92. -define(EXT_TRACE_SET_STATUS_ERROR(), ok).
  93. -define(EXT_TRACE_SET_STATUS_ERROR(_), ok).
  94. -define(EXT_TRACE_WITH_ACTION_START(_FuncName, Any, _Attrs), Any).
  95. -define(EXT_TRACE_WITH_ACTION_STOP(_FuncName, Any, _Attrs), ok).
  96. -define(EXT_TRACE_WITH_PROCESS_FUN(_FuncName, Any, _Attrs, ProcessFun), ProcessFun(Any)).
  97. -endif.
  98. %% --------------------------------------------------------------------
  99. %% types
  100. -type attrs() :: #{atom() => _}.
  101. -endif.