Skip to content

Commit

Permalink
add tokens consumed and remaining stats to crossbar response (2600hz#…
Browse files Browse the repository at this point in the history
…5788)

* add tokens consumed and remaining stats to crossbar response
  • Loading branch information
mk1s authored and lazedo committed May 30, 2019
1 parent be57dbf commit b23ba4a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions applications/crossbar/src/api_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,11 @@ maybe_set_pull_response_stream({FileLength, TransportFun}, Req, Context)
maybe_set_pull_response_stream(Other, Req, Context) ->
{Other, Req, Context}.

-spec get_token_obj(cb_context:context()) -> kz_json:object().
get_token_obj(Context) ->
kz_json:from_list([{<<"consumed">>, cb_modules_util:token_cost(Context)}
,{<<"remaining">>, cb_modules_util:tokens_remaining(Context)}
]).
%%------------------------------------------------------------------------------
%% @doc This function extracts the response fields and puts them in a proplist.
%% @end
Expand All @@ -1582,6 +1587,7 @@ do_create_resp_envelope(Context) ->
Resp = case cb_context:response(Context) of
{'ok', RespData} ->
[{<<"auth_token">>, cb_context:auth_token(Context)}
,{<<"tokens">>, get_token_obj(Context)}
,{<<"status">>, <<"success">>}
,{<<"request_id">>, cb_context:req_id(Context)}
,{<<"node">>, kz_nodes:node_encoded()}
Expand All @@ -1593,6 +1599,7 @@ do_create_resp_envelope(Context) ->
{'error', {ErrorCode, ErrorMsg, RespData}} ->
lager:debug("generating error ~b ~s response", [ErrorCode, ErrorMsg]),
[{<<"auth_token">>, kz_term:to_binary(cb_context:auth_token(Context))}
,{<<"tokens">>, get_token_obj(Context)}
,{<<"request_id">>, cb_context:req_id(Context)}
,{<<"node">>, kz_nodes:node_encoded()}
,{<<"version">>, kz_util:kazoo_version()}
Expand Down
5 changes: 5 additions & 0 deletions applications/crossbar/src/modules/cb_modules_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

,bucket_name/1
,token_cost/1, token_cost/2, token_cost/3
,tokens_remaining/1
,bind/2

,take_sync_field/1
Expand Down Expand Up @@ -126,6 +127,10 @@ bucket_name('undefined', AccountId) ->
bucket_name(IP, AccountId) ->
<<IP/binary, "/", AccountId/binary>>.

-spec tokens_remaining(cb_context:context()) -> non_neg_integer().
tokens_remaining(Context) ->
kz_buckets:tokens_remaining(<<"crossbar">>, bucket_name(Context)).

-spec token_cost(cb_context:context()) -> non_neg_integer().
token_cost(Context) ->
token_cost(Context, 1).
Expand Down
10 changes: 10 additions & 0 deletions core/kazoo_token_buckets/src/kz_buckets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
,start_bucket/1, start_bucket/2, start_bucket/3, start_bucket/4, start_bucket/5
,exists/1, exists/2
,tokens/0
,tokens_remaining/2

,get_bucket/2, get_bucket/3
]).
Expand Down Expand Up @@ -255,6 +256,15 @@ print_bucket_info(#bucket{key={App, _}}=Bucket, _OldApp) ->
),
print_bucket_info(Bucket, App).

-spec tokens_remaining(kz_term:ne_binary(), kz_term:ne_binary()) -> non_neg_integer().
tokens_remaining(App, Key) ->
case ets:lookup(table_id(), {App, Key}) of
[] ->
?MAX_TOKENS(App);
[#bucket{srv=P}] ->
kz_token_bucket:tokens(P)
end.

%%%=============================================================================
%%% ETS
%%%=============================================================================
Expand Down

0 comments on commit b23ba4a

Please sign in to comment.