fake_metrics.erl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. %% @doc This stub module mimics part of folsom's API. It allows us to
  2. %% test the metrics instrumentation of pooler without introducing a
  3. %% dependency on folsom or another metrics application.
  4. %%
  5. -module(fake_metrics).
  6. -behaviour(gen_server).
  7. -define(SERVER, ?MODULE).
  8. -include_lib("eunit/include/eunit.hrl").
  9. %% ------------------------------------------------------------------
  10. %% API Function Exports
  11. %% ------------------------------------------------------------------
  12. -export([
  13. start_link/0,
  14. notify/3,
  15. get_metrics/0,
  16. reset_metrics/0,
  17. stop/0
  18. ]).
  19. %% ------------------------------------------------------------------
  20. %% gen_server Function Exports
  21. %% ------------------------------------------------------------------
  22. -export([
  23. init/1,
  24. handle_call/3,
  25. handle_cast/2,
  26. handle_info/2,
  27. terminate/2,
  28. code_change/3
  29. ]).
  30. %% ------------------------------------------------------------------
  31. %% API Function Definitions
  32. %% ------------------------------------------------------------------
  33. start_link() ->
  34. gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
  35. notify(Name, Value, Type) ->
  36. gen_server:cast(?SERVER, {Name, Value, Type}).
  37. reset_metrics() ->
  38. gen_server:call(?SERVER, reset).
  39. stop() ->
  40. gen_server:call(?SERVER, stop).
  41. get_metrics() ->
  42. gen_server:call(?SERVER, get_metrics).
  43. %% ------------------------------------------------------------------
  44. %% gen_server Function Definitions
  45. %% ------------------------------------------------------------------
  46. -record(state, {
  47. metrics = [] :: list()
  48. }).
  49. init(_) ->
  50. {ok, #state{}}.
  51. handle_call(reset, _From, State) ->
  52. {reply, ok, State#state{metrics = []}};
  53. handle_call(get_metrics, _From, #state{metrics = Metrics} = State) ->
  54. {reply, Metrics, State};
  55. handle_call(stop, _From, State) ->
  56. {stop, normal, stop_ok, State};
  57. handle_call(_Request, _From, State) ->
  58. erlang:error({what, _Request}),
  59. {noreply, ok, State}.
  60. handle_cast({_N, _V, _T} = M, #state{metrics = Metrics} = State) ->
  61. {noreply, State#state{metrics = [M | Metrics]}};
  62. handle_cast(_Msg, State) ->
  63. {noreply, State}.
  64. handle_info(_Info, State) ->
  65. {noreply, State}.
  66. terminate(_Reason, _State) ->
  67. ok.
  68. code_change(_OldVsn, State, _Extra) ->
  69. {ok, State}.