Skip to content

Commit ee05675

Browse files
author
ttt161
committed
bump progressor
1 parent b5ce5ee commit ee05675

3 files changed

Lines changed: 177 additions & 40 deletions

File tree

apps/hellgate/test/hg_invoice_lite_tests_SUITE.erl

Lines changed: 147 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
-export([payment_success_empty_cvv/1]).
2828
-export([payment_has_optional_fields/1]).
2929
-export([payment_last_trx_correct/1]).
30+
-export([payment_success_trace/1]).
3031

3132
-type config() :: hg_ct_helper:config().
3233
-type test_case_name() :: hg_ct_helper:test_case_name().
@@ -60,10 +61,10 @@ groups() ->
6061
{group, pool_payments}
6162
]},
6263
{pool_payments, [parallel], lists:foldl(fun(_, Acc) -> [payment_ok_test | Acc] end, [], lists:seq(1, 100))},
63-
{payments, [], [payment_success]},
64-
{paymentsx, [parallel], [
64+
{payments, [parallel], [
6565
payment_start_idempotency,
6666
payment_success,
67+
payment_success_trace,
6768
payment_w_first_blacklisted_success,
6869
payment_w_all_blacklisted,
6970
register_payment_success,
@@ -243,15 +244,58 @@ payment_success(C) ->
243244
Payment
244245
),
245246

247+
?assertMatch(
248+
#domain_TransactionInfo{
249+
extra = #{
250+
<<"payment.payer_session_info.redirect_url">> := RedirectURL
251+
}
252+
},
253+
Trx
254+
).
255+
256+
-spec payment_success_trace(config()) -> test_return().
257+
payment_success_trace(C) ->
258+
Client = cfg(client, C),
259+
InvoiceID = start_invoice(<<"rubberduck">>, make_due_date(10), 42000, C),
260+
Context = #base_Content{
261+
type = <<"application/x-erlang-binary">>,
262+
data = erlang:term_to_binary({you, 643, "not", [<<"welcome">>, here]})
263+
},
264+
PayerSessionInfo = #domain_PayerSessionInfo{
265+
redirect_url = <<"https://redirectly.io/merchant">>
266+
},
267+
PaymentParams = (make_payment_params(?pmt_sys(<<"visa-ref">>)))#payproc_InvoicePaymentParams{
268+
payer_session_info = PayerSessionInfo,
269+
context = Context
270+
},
271+
PaymentID = process_payment(InvoiceID, PaymentParams, Client),
272+
PaymentID = await_payment_capture(InvoiceID, PaymentID, Client),
273+
246274
RootUrl = unicode:characters_to_binary(cfg(root_url, C)),
247-
FullUrl = <<RootUrl/binary, "/traces/internal/invoice/", InvoiceID/binary>>,
248-
{ok, _Status, _Headers, Ref} = hackney:get(FullUrl),
249-
{ok, Body} = hackney:body(Ref),
275+
UrlInternal = <<RootUrl/binary, "/traces/internal/invoice/", InvoiceID/binary>>,
276+
UrlJaeger = <<RootUrl/binary, "/traces/jaeger/invoice/", InvoiceID/binary>>,
277+
{ok, _Status, _Headers, RefInternal} = hackney:get(UrlInternal),
278+
{ok, BodyInternal} = hackney:body(RefInternal),
250279
[
251280
#{
252-
<<"args">> := _,
281+
<<"args">> := #{
282+
<<"content_type">> := <<"thrift_call">>,
283+
<<"content">> := #{
284+
<<"call">> := #{
285+
<<"function">> := <<"Create">>,
286+
<<"service">> := <<"Invoicing">>
287+
},
288+
<<"params">> := _
289+
}
290+
},
253291
<<"error">> := null,
254-
<<"events">> := _,
292+
<<"events">> := [
293+
#{
294+
<<"event_id">> := 1,
295+
<<"event_payload">> := _,
296+
<<"event_timestamp">> := _
297+
}
298+
],
255299
<<"finished">> := _,
256300
<<"otel_trace_id">> := _,
257301
<<"retry_attempts">> := 0,
@@ -263,30 +307,103 @@ payment_success(C) ->
263307
<<"task_status">> := <<"finished">>,
264308
<<"task_type">> := <<"init">>
265309
},
266-
_,
267-
_,
268-
_,
269-
_,
270-
_,
271-
_,
272-
_,
273-
_,
274-
_,
275-
_,
276-
_,
277-
_,
278-
_,
279-
_
280-
] = json:decode(Body),
281-
282-
?assertMatch(
283-
#domain_TransactionInfo{
284-
extra = #{
285-
<<"payment.payer_session_info.redirect_url">> := RedirectURL
310+
#{<<"task_type">> := <<"call">>, <<"task_status">> := <<"finished">>},
311+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"finished">>},
312+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"finished">>},
313+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"finished">>},
314+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"finished">>},
315+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"finished">>},
316+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"finished">>},
317+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"finished">>},
318+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"finished">>},
319+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"finished">>},
320+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"finished">>},
321+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"finished">>},
322+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"finished">>},
323+
#{<<"task_type">> := <<"timeout">>, <<"task_status">> := <<"cancelled">>}
324+
] = json:decode(BodyInternal),
325+
{ok, _Status2, _Headers2, RefJaeger} = hackney:get(UrlJaeger),
326+
{ok, BodyJaeger} = hackney:body(RefJaeger),
327+
#{
328+
<<"data">> := [
329+
#{
330+
<<"traceId">> := _,
331+
<<"processes">> := #{
332+
InvoiceID := #{
333+
<<"service_name">> := <<"hellgate_invoice">>,
334+
<<"tags">> := []
335+
}
336+
},
337+
<<"spans">> := [
338+
#{
339+
<<"operationName">> := <<"init">>,
340+
<<"process">> := #{
341+
<<"service_name">> := <<"hellgate_invoice">>,
342+
<<"tags">> := []
343+
},
344+
<<"processID">> := InvoiceID,
345+
<<"spanId">> := _,
346+
<<"traceId">> := _,
347+
<<"startTime">> := _,
348+
<<"duration">> := _,
349+
<<"tags">> := [
350+
#{
351+
<<"key">> := <<"task.status">>,
352+
<<"type">> := <<"string">>,
353+
<<"value">> := <<"finished">>
354+
},
355+
#{
356+
<<"key">> := <<"task.retries">>,
357+
<<"type">> := <<"int64">>,
358+
<<"value">> := 0
359+
},
360+
#{
361+
<<"key">> := <<"task.input">>,
362+
<<"type">> := <<"string">>,
363+
<<"value">> := _NestedJsonArgs
364+
}
365+
],
366+
<<"logs">> := [
367+
#{
368+
<<"timestamp">> := _,
369+
<<"fields">> := [
370+
#{
371+
<<"key">> := <<"event.id">>,
372+
<<"type">> := <<"int64">>,
373+
<<"value">> := 1
374+
},
375+
#{
376+
<<"key">> := <<"event.payload">>,
377+
<<"type">> := <<"string">>,
378+
<<"value">> := _NestedJsonEvent
379+
}
380+
]
381+
}
382+
]
383+
},
384+
#{<<"operationName">> := <<"call">>},
385+
#{<<"operationName">> := <<"timeout">>},
386+
#{<<"operationName">> := <<"timeout">>},
387+
#{<<"operationName">> := <<"timeout">>},
388+
#{<<"operationName">> := <<"timeout">>},
389+
#{<<"operationName">> := <<"timeout">>},
390+
#{<<"operationName">> := <<"timeout">>},
391+
#{<<"operationName">> := <<"timeout">>},
392+
#{<<"operationName">> := <<"timeout">>},
393+
#{<<"operationName">> := <<"timeout">>},
394+
#{<<"operationName">> := <<"timeout">>},
395+
#{<<"operationName">> := <<"timeout">>},
396+
#{<<"operationName">> := <<"timeout">>},
397+
#{<<"operationName">> := <<"timeout">>}
398+
]
286399
}
287-
},
288-
Trx
289-
).
400+
]
401+
} = json:decode(BodyJaeger),
402+
BadInvoiceUrl = <<RootUrl/binary, "/traces/internal/invoice/UnknownInvoice">>,
403+
{ok, 404, _, _} = hackney:get(BadInvoiceUrl),
404+
BadFormatUrl = <<RootUrl/binary, "/traces/external/invoice/", InvoiceID/binary>>,
405+
{ok, 400, _, _} = hackney:get(BadFormatUrl),
406+
ok.
290407

291408
-spec payment_w_first_blacklisted_success(config()) -> test_return().
292409
payment_w_first_blacklisted_success(C) ->

apps/hg_progressor/src/hg_progressor_handler.erl

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@ init(Request, Opts) ->
1717
NS = maps:get(namespace, Opts),
1818
Format = cowboy_req:binding(format, Request),
1919
ProcessID = cowboy_req:binding(process_id, Request),
20-
Req = handle(Method, NS, ProcessID, Format, Request),
21-
{ok, Req, undefined}.
20+
maybe
21+
{format_is_valid, true} ?= {format_is_valid, Format =:= <<"internal">> orelse Format =:= <<"jaeger">>},
22+
{process_id_is_valid, true} ?= {process_id_is_valid, is_binary(ProcessID)},
23+
Req = handle(Method, NS, ProcessID, Format, Request),
24+
{ok, Req, undefined}
25+
else
26+
{format_is_valid, false} ->
27+
Req1 = cowboy_req:reply(400, #{}, <<"Invalid Format">>, Request),
28+
{ok, Req1, undefined};
29+
{process_id_is_valid, false} ->
30+
Req2 = cowboy_req:reply(400, #{}, <<"Invalid ProcessID">>, Request),
31+
{ok, Req2, undefined}
32+
end.
2233

2334
-spec terminate(term(), cowboy_req:req(), undefined) ->
2435
ok.
@@ -32,7 +43,7 @@ handle(<<"GET">>, NS, ProcessID, Format, Request) ->
3243
Trace = unmarshal_trace(NS, ProcessID, RawTrace, Format),
3344
Body = unicode:characters_to_binary(json:encode(Trace)),
3445
cowboy_req:reply(200, #{}, Body, Request);
35-
{error, _} = _Error ->
46+
{error, <<"process not found">>} = _Error ->
3647
cowboy_req:reply(404, #{}, <<"Unknown process">>, Request)
3748
end;
3849
handle(_, _NS, _ProcessID, _Format, Request) ->
@@ -45,7 +56,7 @@ unmarshal_trace(NS, ProcessID, RawTrace, <<"jaeger">> = Format) ->
4556
#{
4657
data => [
4758
#{
48-
traceID => trace_id(NS, ProcessID),
59+
traceId => trace_id(NS, ProcessID),
4960
spans => Spans,
5061
processes => #{
5162
ProcessID => #{
@@ -79,7 +90,7 @@ unmarshal_trace_unit(NS, ProcessID, #{task_type := TaskType, task_id := TaskID}
7990
},
8091
warnings => [],
8192
traceId => trace_id(NS, ProcessID),
82-
span_id => integer_to_binary(TaskID),
93+
spanId => integer_to_binary(TaskID),
8394
operationName => TaskType,
8495
startTime => start_time(TraceUnit),
8596
duration => duration(TraceUnit),
@@ -166,11 +177,11 @@ unmarshal_events(BinEvents, Format) ->
166177
BinEvents
167178
).
168179

169-
unmarshal_event(Event, Payload, <<"internal">>) ->
170-
Event#{event_payload => Payload};
180+
unmarshal_event(#{event_timestamp := Ts} = Event, Payload, <<"internal">>) ->
181+
Event#{event_payload => Payload, event_timestamp => to_microseconds(Ts)};
171182
unmarshal_event(#{event_id := EventID, event_timestamp := Ts}, Payload, <<"jaeger">>) ->
172183
#{
173-
timestamp => Ts,
184+
timestamp => to_microseconds(Ts),
174185
fields => [
175186
#{
176187
key => <<"event.id">>,
@@ -232,7 +243,6 @@ trace_id(NS, ProcessID) ->
232243
NsBin = erlang:atom_to_binary(NS),
233244
HexList = [io_lib:format("~2.16.0b", [B]) || <<B>> <= <<NsBin/binary, ProcessID/binary>>],
234245
Hex = lists:flatten(HexList),
235-
io:format(user, "HEX: ~p~n", [Hex]),
236246
case length(Hex) of
237247
Len when Len < 32 -> unicode:characters_to_binary(lists:duplicate(32 - Len, $0) ++ Hex);
238248
Len when Len > 32 -> unicode:characters_to_binary(string:slice(Hex, 0, 32));
@@ -261,6 +271,16 @@ error_tag(#{task_status := <<"error">>, response := {error, ReasonTerm}}) ->
261271
error_tag(_) ->
262272
[].
263273

274+
to_microseconds(Timestamp) when Timestamp < 100000000000 ->
275+
%% seconds
276+
Timestamp * 1000000;
277+
to_microseconds(Timestamp) when Timestamp < 100000000000000 ->
278+
%% milliseconds
279+
Timestamp * 1000;
280+
to_microseconds(Timestamp) when Timestamp < 100000000000000000 ->
281+
%% microseconds
282+
Timestamp.
283+
264284
-define(is_integer(T), (T == byte orelse T == i8 orelse T == i16 orelse T == i32 orelse T == i64)).
265285
-define(is_number(T), (?is_integer(T) orelse T == double)).
266286
-define(is_scalar(T), (?is_number(T) orelse T == string orelse element(1, T) == enum)).

rebar.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
0},
101101
{<<"progressor">>,
102102
{git,"https://github.com/valitydev/progressor.git",
103-
{ref,"99c356d6aa55943d3f402d0c0551f7a3a0ee3927"}},
103+
{ref,"614b2ddea250e24f4e17f285e9468e9cf913674e"}},
104104
0},
105105
{<<"prometheus">>,{pkg,<<"prometheus">>,<<"4.11.0">>},0},
106106
{<<"prometheus_cowboy">>,{pkg,<<"prometheus_cowboy">>,<<"0.1.9">>},0},

0 commit comments

Comments
 (0)