Skip to content

Commit

Permalink
Allow looking up parent when parent is a named process
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffutter committed Jan 28, 2024
1 parent 3a9ed42 commit e7a5a38
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ fetch_ctx(Pid) ->
otel_ctx(Dictionary)
end.

-spec pdict(pid()) -> [{term(), term()}] | undefined.
-spec pdict(pid() | atom()) -> [{term(), term()}] | undefined.
pdict(Name) when is_atom(Name) ->
case whereis(Name) of
undefined -> undefined;
Pid -> pdict(Pid)
end;
pdict(Pid) ->
case process_info(Pid, dictionary) of
{dictionary, Dict} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ defmodule OpentelemetryProcessPropagatorTest do

assert_receive ^ctx
end

test "fetches the parent ctx when parent is named" do
Process.register(self(), TestParent)

span_ctx = Tracer.start_span("test")
Tracer.set_current_span(span_ctx)

ctx = Ctx.get_current()

pid = self()

:proc_lib.spawn(fn ->
p_ctx = fetch_parent_ctx()
send(pid, p_ctx)
end)

assert_receive ^ctx
end
end

describe "fetch_parent_ctx/1" do
Expand Down
20 changes: 19 additions & 1 deletion propagators/opentelemetry_process_propagator/test/task_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,26 @@ defmodule OpentelemetryProcessPropagator.TaskTest do
end
end

test "start_link" do
test "start_link with named parent" do
Tracer.with_span "parent span", %{attributes: %{a: 1}} do
Process.register(self(), TestParent)

# Elixir.Task.Supervised.start(TestParent, [], {:erlang, :apply, [fn ->
# Process.get() |> IO.inspect(label: "A")
#
# ctx = OpentelemetryProcessPropagator.fetch_parent_ctx()
# OpenTelemetry.Ctx.attach(ctx)
# # Process.get() |> IO.inspect(label: "Supervised")
# ctx_test_function(:start_link)
#
# :ok
# end, []]})

Task.start_link(fn ->
Process.get() |> IO.inspect(label: "B")

ctx = OpentelemetryProcessPropagator.fetch_parent_ctx()
OpenTelemetry.Ctx.attach(ctx)
ctx_test_function(:start_link)

:ok
Expand All @@ -374,6 +391,7 @@ defmodule OpentelemetryProcessPropagator.TaskTest do

assert_receive {:span, span(name: "already traced fun", parent_span_id: ^root_span_id, attributes: attrs)}
assert %{value: :start_link} == attributes(attrs)
assert false
end

test "start_link_mfa" do
Expand Down

0 comments on commit e7a5a38

Please sign in to comment.