Skip to content

Commit

Permalink
Migrates back to scoper' woody event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
nanodirijabl committed Oct 25, 2023
1 parent bc7c30d commit c71f868
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 32 deletions.
21 changes: 12 additions & 9 deletions apps/capi/src/capi_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@

-spec authorize_api_key(operation_id(), swag_server:api_key(), request_context(), handler_opts()) ->
Result :: false | {true, capi_auth:preauth_context()}.
authorize_api_key(OperationID, ApiKey, _Context, _HandlerOpts) ->
authorize_api_key(OperationID, ApiKey, Context, _HandlerOpts) ->
ok = set_otel_context(Context),
%% No actual authorization here: see capi_v2.capi_handler for details
case capi_auth:preauthorize_api_key(ApiKey) of
{ok, Context} ->
{true, Context};
{ok, Context1} ->
{true, Context1};
{error, Error} ->
_ = logger:info("API Key preauthorization failed for ~p due to ~p", [OperationID, Error]),
false
Expand Down Expand Up @@ -117,7 +118,6 @@ handle_function_(OperationID, Req, SwagContext0, _HandlerOpts) ->

Context = create_processing_context(OperationID, SwagContext, WoodyContext),
ok = set_context_meta(Context),
ok = sync_scoper_otel_meta(),
{ok, RequestState} = prepare(OperationID, Req, Context, get_handlers()),
#{authorize := Authorize, process := Process} = RequestState,
{ok, Resolution} = Authorize(),
Expand Down Expand Up @@ -244,6 +244,14 @@ process_general_error(Class, Reason, Stacktrace, OperationID, Req, SwagContext)
),
{error, server_error(500)}.

set_otel_context(#{cowboy_req := Req}) ->
Headers = cowboy_req:headers(Req),
%% Implicitly puts OTEL context into process dictionary.
%% Since cowboy does not reuse process for other requests, we don't care
%% about cleaning it up.
_OtelCtx = otel_propagator_text_map:extract(maps:to_list(Headers)),
ok.

-spec set_context_meta(processing_context()) -> ok.
set_context_meta(Context) ->
AuthContext = capi_handler_utils:get_auth_context(Context),
Expand All @@ -257,11 +265,6 @@ set_context_meta(Context) ->
},
scoper:add_meta(Meta).

-spec sync_scoper_otel_meta() -> ok.
sync_scoper_otel_meta() ->
_ = otel_span:set_attributes(otel_tracer:current_span_ctx(), genlib_map:flatten_join($., scoper:collect())),
ok.

-spec set_request_meta(operation_id(), request_data()) -> ok.
set_request_meta(OperationID, Req) ->
InterestParams = [
Expand Down
2 changes: 1 addition & 1 deletion apps/capi/test/capi_ct_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ mock_services_(Services, SupPid) when is_pid(SupPid) ->
WoodyOpts = #{
ip => IP,
port => 0,
event_handler => {capi_woody_event_handler, #{}},
event_handler => {scoper_woody_event_handler, #{}},
handlers => lists:map(fun mock_service_handler/1, Services)
},
ChildSpec = woody_server:child_spec(ServerID, WoodyOpts),
Expand Down
2 changes: 1 addition & 1 deletion apps/capi_woody_client/src/capi_woody_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ call_service(ServiceName, Function, Args, Context0) ->
Context1 = set_deadline(Deadline, Context0),
Retry = get_service_retry(ServiceName, Function),
EventHandlerOpts = genlib_app:env(capi_pcidss, scoper_event_handler_options, #{}),
EventHandler = {capi_woody_event_handler, EventHandlerOpts},
EventHandler = {scoper_woody_event_handler, EventHandlerOpts},
call_service(ServiceName, Function, Args, Context1, EventHandler, Retry).

call_service(ServiceName, Function, Args, Context, EventHandler, Retry) ->
Expand Down
14 changes: 0 additions & 14 deletions apps/capi_woody_client/src/capi_woody_event_handler.erl

This file was deleted.

14 changes: 7 additions & 7 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
0},
{<<"bouncer_client">>,
{git,"https://github.com/valitydev/bouncer-client-erlang.git",
{ref,"2ce8d4aeeef490bf2a013e7c5a1aee498d5b6178"}},
{ref,"4f89634d9a009cac807cf86a0eff9e80d33318ea"}},
0},
{<<"bouncer_proto">>,
{git,"https://github.com/valitydev/bouncer-proto.git",
Expand All @@ -23,7 +23,7 @@
0},
{<<"certifi">>,{pkg,<<"certifi">>,<<"2.6.1">>},1},
{<<"cg_mon">>,
{git,"https://github.com/valitydev/cg_mon.git",
{git,"https://github.com/rbkmoney/cg_mon.git",
{ref,"5a87a37694e42b6592d3b4164ae54e0e87e24e18"}},
1},
{<<"chatterbox">>,{pkg,<<"ts_chatterbox">>,<<"0.13.0">>},2},
Expand All @@ -44,11 +44,11 @@
{<<"ctx">>,{pkg,<<"ctx">>,<<"0.6.0">>},2},
{<<"damsel">>,
{git,"https://github.com/valitydev/damsel.git",
{ref,"bfedcb9dbb0bfdbd7a06a86417b49be6e807b98d"}},
{ref,"f718741970470474efcd32800daf885cb8d75584"}},
0},
{<<"dmt_client">>,
{git,"https://github.com/valitydev/dmt_client.git",
{ref,"ce6678af1499230fe13f8b34258aabe8b92ac722"}},
{ref,"b8bc0281dbf1e55a1a67ef6da861e0353ff14913"}},
0},
{<<"dmt_core">>,
{git,"https://github.com/valitydev/dmt-core.git",
Expand All @@ -57,7 +57,7 @@
{<<"email_validator">>,{pkg,<<"email_validator">>,<<"1.1.0">>},0},
{<<"erl_health">>,
{git,"https://github.com/valitydev/erlang-health.git",
{ref,"982af88738ca062eea451436d830eef8c1fbe3f9"}},
{ref,"7ffbc855bdbe79e23efad1803b0b185c9ea8d2f1"}},
0},
{<<"genlib">>,
{git,"https://github.com/valitydev/genlib.git",
Expand Down Expand Up @@ -115,7 +115,7 @@
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.8.0">>},1},
{<<"scoper">>,
{git,"https://github.com/valitydev/scoper.git",
{ref,"7f3183df279bc8181efe58dafd9cae164f495e6f"}},
{ref,"41a14a558667316998af9f49149ee087ffa8bef2"}},
0},
{<<"snowflake">>,
{git,"https://github.com/valitydev/snowflake.git",
Expand Down Expand Up @@ -143,7 +143,7 @@
1},
{<<"token_keeper_client">>,
{git,"https://github.com/valitydev/token-keeper-client.git",
{ref,"de4f666c93a71c8fcab2cfb3b77f2e91225b650c"}},
{ref,"417916aab09baf0cd7f1a2a174945446203ea0bf"}},
0},
{<<"token_keeper_proto">>,
{git,"https://github.com/valitydev/token-keeper-proto.git",
Expand Down

0 comments on commit c71f868

Please sign in to comment.