Skip to content

Commit

Permalink
opentelemetry_process_propagator: Optimize remote pdict lookup (open-…
Browse files Browse the repository at this point in the history
…telemetry#370)

* Get parent pids directly from local pdict

* Fetch only otel_ctx from pdict when possible
  • Loading branch information
GregMefford authored Sep 23, 2024
1 parent 6c3dd45 commit b5fb570
Showing 1 changed file with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ fetch_parent_ctx(MaxDepth) ->

-spec fetch_parent_ctx(non_neg_integer(), atom()) -> otel_ctx:t() | undefined.
fetch_parent_ctx(MaxDepth, Key) ->
Pids = pids(Key, pdict(self())),
Pids = case get(Key) of
List when is_list(List) -> List;
_ -> []
end,
inspect_parent(undefined, lists:sublist(Pids, MaxDepth)).

inspect_parent(Ctx, _Pids) when Ctx =/= undefined ->
Expand All @@ -30,28 +33,30 @@ inspect_parent(_Ctx, [Pid | Rest]) ->
inspect_parent(OtelCtx, [])
end.

-spec fetch_ctx(pid()) -> otel_ctx:t() | undefined.
fetch_ctx(Pid) ->
case pdict(Pid) of
undefined ->
undefined;
Dictionary ->
otel_ctx(Dictionary)
end.

-spec pdict(pid() | atom()) -> [{term(), term()}] | undefined.
pdict(Name) when is_atom(Name) ->
-spec fetch_ctx(pid() | atom()) -> otel_ctx:t() | undefined.
fetch_ctx(Name) when is_atom(Name) ->
case whereis(Name) of
undefined -> undefined;
Pid -> pdict(Pid)
end;
fetch_ctx(Pid) when is_pid(Pid) ->
pdict(Pid).

-if(?OTP_RELEASE >= 27).
%% Fetching a single key from another process's dictionary was introduced in 26.2,
%% so we can't depend on it until 27.
pdict(Pid) ->
case process_info(Pid, {dictionary, '$__current_otel_ctx'}) of
undefined -> undefined;
{{dictionary, '$__current_otel_ctx'}, Ctx} -> Ctx
end.
-else.
pdict(Pid) when is_pid(Pid) ->
case process_info(Pid, dictionary) of
{dictionary, Dict} ->
Dict;
undefined ->
undefined
undefined -> undefined;
{dictionary, Dict} -> otel_ctx(Dict)
end.
-endif.

-spec otel_ctx([{term(), term()}]) -> otel_ctx:t() | undefined.
otel_ctx(Dictionary) ->
Expand All @@ -61,11 +66,3 @@ otel_ctx(Dictionary) ->
{'$__current_otel_ctx', Ctx} ->
Ctx
end.

pids(Key, Dictionary) ->
case lists:keyfind(Key, 1, Dictionary) of
false ->
[];
{Key,Pids} ->
Pids
end.

0 comments on commit b5fb570

Please sign in to comment.