Skip to content

Commit f52f57e

Browse files
committed
Merge branch 'master' into epic/update-prod-profile
2 parents fa09552 + 198a884 commit f52f57e

6 files changed

Lines changed: 16 additions & 128 deletions

File tree

apps/mg_core/src/mg_core_machine.erl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,14 +1563,12 @@ try_suicide(#{}, _) ->
15631563

15641564
-spec attach_otel_ctx(mg_core_events_machine:request_context()) -> ok.
15651565
attach_otel_ctx(ReqCtx) ->
1566-
mg_core_otel:maybe_attach_otel_ctx(request_context_to_otel_context(ReqCtx)).
1566+
woody_rpc_helper:attach_otel_context(request_context_to_otel_context(ReqCtx)).
15671567

15681568
-spec request_context_to_otel_context(mg_core_events_machine:request_context()) -> otel_ctx:t().
1569-
request_context_to_otel_context(#{<<"otel">> := OpaqueOtelCtx}) ->
1570-
OtelCtx0 = otel_ctx:get_current(),
1571-
mg_core_otel:restore_otel_stub(OtelCtx0, OpaqueOtelCtx);
1572-
request_context_to_otel_context(_Other) ->
1573-
otel_ctx:get_current().
1569+
request_context_to_otel_context(Ctx) ->
1570+
{_WoodyContext, OtelContext} = woody_rpc_helper:decode_rpc_context(Ctx),
1571+
OtelContext.
15741572

15751573
%%
15761574
%% retrying

apps/mg_core/src/mg_core_otel.erl

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
44

5-
-export([pack_otel_stub/1]).
6-
-export([restore_otel_stub/2]).
7-
-export([maybe_attach_otel_ctx/1]).
8-
95
-export([span_start/3]).
106
-export([span_end/1]).
117
-export([record_current_span_ctx/3]).
@@ -27,59 +23,6 @@
2723

2824
%%
2925

30-
%% @doc Packs OTEL context for storage.
31-
-spec pack_otel_stub(otel_ctx:t()) -> packed_otel_stub().
32-
pack_otel_stub(Ctx) ->
33-
case otel_tracer:current_span_ctx(Ctx) of
34-
undefined ->
35-
[];
36-
#span_ctx{trace_id = TraceID, span_id = SpanID, trace_flags = TraceFlags} ->
37-
[trace_id_to_binary(TraceID), span_id_to_binary(SpanID), TraceFlags]
38-
end.
39-
40-
%% @doc Restores OTEL context with current span. Restored span context
41-
%% status is nor actual nor have according data in OTEL storage
42-
%% backend. Its only purpose is to preserve ability to start new child
43-
%% spans in compliance with OTEL tracer API.
44-
%%
45-
%% Restored otel span can be unfinished if machine is interrupted
46-
%% with node stop, thus span data is lost anyway.
47-
%%
48-
%% We can't get around this issue without implementing our own
49-
%% tracer with distributed storage with write order guarantee.
50-
%%
51-
%% However we can start new span for 'resumption' signal. And set
52-
%% original machine start call as its parent. Same goes for 'timeouts'
53-
%% and 'retries' signals.
54-
-spec restore_otel_stub(otel_ctx:t(), packed_otel_stub()) -> otel_ctx:t().
55-
restore_otel_stub(Ctx, [TraceID, SpanID, TraceFlags]) ->
56-
SpanCtx = otel_tracer:from_remote_span(binary_to_id(TraceID), binary_to_id(SpanID), TraceFlags),
57-
%% NOTE Thus restored span context is considered being remote and not recording.
58-
otel_tracer:set_current_span(Ctx, SpanCtx);
59-
restore_otel_stub(Ctx, _Other) ->
60-
Ctx.
61-
62-
-spec maybe_attach_otel_ctx(otel_ctx:t()) -> ok.
63-
maybe_attach_otel_ctx(NewCtx) when map_size(NewCtx) =:= 0 ->
64-
%% Don't attach empty context
65-
ok;
66-
maybe_attach_otel_ctx(NewCtx) ->
67-
_ = otel_ctx:attach(choose_viable_otel_ctx(NewCtx, otel_ctx:get_current())),
68-
ok.
69-
70-
%% lowest bit is if it is sampled
71-
-define(IS_NOT_SAMPLED(SpanCtx), SpanCtx#span_ctx.trace_flags band 2#1 =/= 1).
72-
73-
-spec choose_viable_otel_ctx(T, T) -> T when T :: otel_ctx:t().
74-
choose_viable_otel_ctx(NewCtx, CurrentCtx) ->
75-
case {otel_tracer:current_span_ctx(NewCtx), otel_tracer:current_span_ctx(CurrentCtx)} of
76-
%% Don't attach if new context is without sampled span and old
77-
%% context has span defined
78-
{SpanCtx = #span_ctx{}, #span_ctx{}} when ?IS_NOT_SAMPLED(SpanCtx) -> CurrentCtx;
79-
{undefined, #span_ctx{}} -> CurrentCtx;
80-
{_, _} -> NewCtx
81-
end.
82-
8326
-spec span_start(term(), opentelemetry:span_name(), otel_span:start_opts()) -> ok.
8427
span_start(Key, SpanName, Opts) ->
8528
Tracer = opentelemetry:get_application_tracer(?MODULE),
@@ -166,53 +109,3 @@ span_id(#span_ctx{span_id = SpanID}) ->
166109
SpanID;
167110
span_id(_) ->
168111
undefined.
169-
170-
-spec trace_id_to_binary(opentelemetry:trace_id()) -> binary().
171-
trace_id_to_binary(TraceID) ->
172-
{ok, EncodedTraceID} = otel_utils:format_binary_string("~32.16.0b", [TraceID]),
173-
EncodedTraceID.
174-
175-
-spec span_id_to_binary(opentelemetry:span_id()) -> binary().
176-
span_id_to_binary(SpanID) ->
177-
{ok, EncodedSpanID} = otel_utils:format_binary_string("~16.16.0b", [SpanID]),
178-
EncodedSpanID.
179-
180-
-spec binary_to_id(binary()) -> non_neg_integer().
181-
binary_to_id(Opaque) when is_binary(Opaque) ->
182-
binary_to_integer(Opaque, 16).
183-
184-
-ifdef(TEST).
185-
-include_lib("eunit/include/eunit.hrl").
186-
187-
-type testgen() :: {_ID, fun(() -> _)}.
188-
-spec test() -> _.
189-
190-
-define(IS_SAMPLED, 1).
191-
-define(NOT_SAMPLED, 0).
192-
-define(OTEL_CTX(IsSampled),
193-
otel_tracer:set_current_span(
194-
otel_ctx:new(),
195-
(otel_tracer_noop:noop_span_ctx())#span_ctx{
196-
trace_id = otel_id_generator:generate_trace_id(),
197-
span_id = otel_id_generator:generate_span_id(),
198-
is_valid = true,
199-
is_remote = true,
200-
is_recording = false,
201-
trace_flags = IsSampled
202-
}
203-
)
204-
).
205-
206-
-spec choose_viable_otel_ctx_test_() -> [testgen()].
207-
choose_viable_otel_ctx_test_() ->
208-
A = ?OTEL_CTX(?IS_SAMPLED),
209-
B = ?OTEL_CTX(?NOT_SAMPLED),
210-
[
211-
?_assertEqual(A, choose_viable_otel_ctx(A, B)),
212-
?_assertEqual(A, choose_viable_otel_ctx(B, A)),
213-
?_assertEqual(A, choose_viable_otel_ctx(A, otel_ctx:new())),
214-
?_assertEqual(B, choose_viable_otel_ctx(otel_ctx:new(), B)),
215-
?_assertEqual(otel_ctx:new(), choose_viable_otel_ctx(otel_ctx:new(), otel_ctx:new()))
216-
].
217-
218-
-endif.

apps/mg_core/test/mg_core_machine_full_test_SUITE.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ do_action(Options, ID, Seq, Action) ->
202202

203203
-spec req_ctx(id(), seq()) -> mg_core:request_context().
204204
req_ctx(ID, Seq) ->
205-
#{
205+
Ctx = woody_rpc_helper:encode_rpc_context(woody_context:new(), otel_ctx:get_current()),
206+
Ctx#{
206207
<<"id">> => ID,
207-
<<"seq">> => Seq,
208-
<<"otel">> => mg_core_otel:pack_otel_stub(otel_ctx:get_current())
208+
<<"seq">> => Seq
209209
}.
210210

211211
-spec id(id()) -> mg_core:id().

apps/mg_woody/rebar.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{deps, [
22
{mg_proto, {git, "https://github.com/valitydev/machinegun-proto", {branch, master}}},
33
{genlib, {git, "https://github.com/valitydev/genlib", {tag, "v1.1.0"}}},
4-
{woody, {git, "https://github.com/valitydev/woody_erlang", {tag, "v1.1.0"}}},
4+
{woody, {git, "https://github.com/valitydev/woody_erlang", {tag, "v1.1.1"}}},
55
{opentelemetry_api, "1.4.0"}
66
]}.

apps/mg_woody/src/mg_woody_automaton.erl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,4 @@ simplify_machine_status(_) ->
321321

322322
-spec to_request_context(otel_ctx:t(), woody_context:ctx()) -> mg_core:request_context().
323323
to_request_context(OtelContext, WoodyContext) ->
324-
#{
325-
<<"otel">> => mg_core_otel:pack_otel_stub(OtelContext),
326-
<<"woody">> => mg_woody_utils:woody_context_to_opaque(WoodyContext)
327-
}.
324+
woody_rpc_helper:encode_rpc_context(WoodyContext, OtelContext).

rebar.lock

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
{git,"https://github.com/valitydev/machinegun-proto",
5151
{ref,"3decc8f8b13c9cd1701deab47781aacddd7dbc92"}},
5252
0},
53-
{<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.3.0">>},2},
53+
{<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.4.0">>},2},
5454
{<<"msgpack">>,
5555
{git,"https://github.com/msgpack/msgpack-erlang",
5656
{ref,"6b544de60723839244866b109d6a79c7caca6bc9"}},
@@ -97,10 +97,10 @@
9797
{<<"tls_certificate_check">>,
9898
{pkg,<<"tls_certificate_check">>,<<"1.26.0">>},
9999
1},
100-
{<<"unicode_util_compat">>,{pkg,<<"unicode_util_compat">>,<<"0.7.0">>},2},
100+
{<<"unicode_util_compat">>,{pkg,<<"unicode_util_compat">>,<<"0.7.1">>},2},
101101
{<<"woody">>,
102102
{git,"https://github.com/valitydev/woody_erlang",
103-
{ref,"cc983a9423325ba1d6a509775eb6ff7ace721539"}},
103+
{ref,"3de9a236b66807d5397b28aed42e2b1c85f36739"}},
104104
0},
105105
{<<"yamerl">>,
106106
{git,"https://github.com/valitydev/yamerl",
@@ -127,7 +127,7 @@
127127
{<<"jsx">>, <<"D12516BAA0BB23A59BB35DCCAF02A1BD08243FCBB9EFE24F2D9D056CCFF71268">>},
128128
{<<"kafka_protocol">>, <<"FC696880C73483C8B032C4BB60F2873046035C7824E1EDCB924CFCE643CF23DD">>},
129129
{<<"metrics">>, <<"25F094DEA2CDA98213CECC3AEFF09E940299D950904393B2A29D191C346A8486">>},
130-
{<<"mimerl">>, <<"D0CD9FC04B9061F82490F6581E0128379830E78535E017F7780F37FEA7545726">>},
130+
{<<"mimerl">>, <<"3882A5CA67FBBE7117BA8947F27643557ADEC38FA2307490C4C4207624CB213B">>},
131131
{<<"opentelemetry">>, <<"7DDA6551EDFC3050EA4B0B40C0D2570423D6372B97E9C60793263EF62C53C3C2">>},
132132
{<<"opentelemetry_api">>, <<"63CA1742F92F00059298F478048DFB826F4B20D49534493D6919A0DB39B6DB04">>},
133133
{<<"opentelemetry_exporter">>, <<"5D546123230771EF4174E37BEDFD77E3374913304CD6EA3CA82A2ADD49CD5D56">>},
@@ -142,7 +142,7 @@
142142
{<<"ssl_verify_fun">>, <<"354C321CF377240C7B8716899E182CE4890C5938111A1296ADD3EC74CF1715DF">>},
143143
{<<"supervisor3">>, <<"D81CDEC31D102FDE407423E1D05B569572850DEEBED86B951D5233C387CBA80B">>},
144144
{<<"tls_certificate_check">>, <<"C0E8FFAB875748F2B122D4D4E465AEAA7249EA539F1004B7922CB3C61FFE261D">>},
145-
{<<"unicode_util_compat">>, <<"BC84380C9AB48177092F43AC89E4DFA2C6D62B40B8BD132B1059ECC7232F9A78">>}]},
145+
{<<"unicode_util_compat">>, <<"A48703A25C170EEDADCA83B11E88985AF08D35F37C6F664D6DCFB106A97782FC">>}]},
146146
{pkg_hash_ext,[
147147
{<<"accept">>, <<"A5167FA1AE90315C3F1DD189446312F8A55D00EFA357E9C569BDA47736B874C3">>},
148148
{<<"acceptor_pool">>, <<"0CBCD83FDC8B9AD2EEE2067EF8B91A14858A5883CB7CD800E6FCD5803E158788">>},
@@ -163,7 +163,7 @@
163163
{<<"jsx">>, <<"0C5CC8FDC11B53CC25CF65AC6705AD39E54ECC56D1C22E4ADB8F5A53FB9427F3">>},
164164
{<<"kafka_protocol">>, <<"687BFD9989998EC8FBBC3ED50D1239A6C07A7DC15B52914AD477413B89ECB621">>},
165165
{<<"metrics">>, <<"69B09ADDDC4F74A40716AE54D140F93BEB0FB8978D8636EADED0C31B6F099F16">>},
166-
{<<"mimerl">>, <<"A1E15A50D1887217DE95F0B9B0793E32853F7C258A5CD227650889B38839FE9D">>},
166+
{<<"mimerl">>, <<"13AF15F9F68C65884ECCA3A3891D50A7B57D82152792F3E19D88650AA126B144">>},
167167
{<<"opentelemetry">>, <<"CDF4F51D17B592FC592B9A75F86A6F808C23044BA7CF7B9534DEBBCC5C23B0EE">>},
168168
{<<"opentelemetry_api">>, <<"3DFBBFAA2C2ED3121C5C483162836C4F9027DEF469C41578AF5EF32589FCFC58">>},
169169
{<<"opentelemetry_exporter">>, <<"A1F9F271F8D3B02B81462A6BFEF7075FD8457FDB06ADFF5D2537DF5E2264D9AF">>},
@@ -178,5 +178,5 @@
178178
{<<"ssl_verify_fun">>, <<"FE4C190E8F37401D30167C8C405EDA19469F34577987C76DDE613E838BBC67F8">>},
179179
{<<"supervisor3">>, <<"E6C2DEDBCABCBA24995A218ACA12DB5E208B80D3252692B22EF0F1A266104B50">>},
180180
{<<"tls_certificate_check">>, <<"1BAD73D88637F788B554A8E939C25DB2BDAAC88B10FFFD5BBA9D1B65F43A6B54">>},
181-
{<<"unicode_util_compat">>, <<"25EEE6D67DF61960CF6A794239566599B09E17E668D3700247BC498638152521">>}]}
181+
{<<"unicode_util_compat">>, <<"B3A917854CE3AE233619744AD1E0102E05673136776FB2FA76234F3E03B23642">>}]}
182182
].

0 commit comments

Comments
 (0)