fake_metrics.erl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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([start_link/0,
  13. notify/3,
  14. get_metrics/0,
  15. reset_metrics/0,
  16. stop/0
  17. ]).
  18. %% ------------------------------------------------------------------
  19. %% gen_server Function Exports
  20. %% ------------------------------------------------------------------
  21. -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  22. terminate/2, code_change/3]).
  23. %% ------------------------------------------------------------------
  24. %% API Function Definitions
  25. %% ------------------------------------------------------------------
  26. start_link() ->
  27. gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
  28. notify(Name, Value, Type) ->
  29. gen_server:cast(?SERVER, {Name, Value, Type}).
  30. reset_metrics() ->
  31. gen_server:call(?SERVER, reset).
  32. stop() ->
  33. gen_server:call(?SERVER, stop).
  34. get_metrics() ->
  35. gen_server:call(?SERVER, get_metrics).
  36. %% ------------------------------------------------------------------
  37. %% gen_server Function Definitions
  38. %% ------------------------------------------------------------------
  39. -record(state, {
  40. metrics = [] :: list()
  41. }).
  42. init(_) ->
  43. {ok, #state{}}.
  44. handle_call(reset, _From, State) ->
  45. {reply, ok, State#state{metrics = []}};
  46. handle_call(get_metrics, _From, #state{metrics = Metrics}=State) ->
  47. {reply, Metrics, State};
  48. handle_call(stop, _From, State) ->
  49. {stop, normal, stop_ok, State};
  50. handle_call(_Request, _From, State) ->
  51. erlang:error({what, _Request}),
  52. {noreply, ok, State}.
  53. handle_cast({_N, _V, _T}=M, #state{metrics = Metrics} = State) ->
  54. {noreply, State#state{metrics = [M|Metrics]}};
  55. handle_cast(_Msg, State) ->
  56. {noreply, State}.
  57. handle_info(_Info, State) ->
  58. {noreply, State}.
  59. terminate(_Reason, _State) ->
  60. ok.
  61. code_change(_OldVsn, State, _Extra) ->
  62. {ok, State}.