diff --git a/propagators/opentelemetry_process_propagator/src/opentelemetry_process_propagator.erl b/propagators/opentelemetry_process_propagator/src/opentelemetry_process_propagator.erl index 4c50d1fd..ce579a1f 100644 --- a/propagators/opentelemetry_process_propagator/src/opentelemetry_process_propagator.erl +++ b/propagators/opentelemetry_process_propagator/src/opentelemetry_process_propagator.erl @@ -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} -> diff --git a/propagators/opentelemetry_process_propagator/test/opentelemetry_process_propagator_test.exs b/propagators/opentelemetry_process_propagator/test/opentelemetry_process_propagator_test.exs index 9d74e774..d2b927cc 100644 --- a/propagators/opentelemetry_process_propagator/test/opentelemetry_process_propagator_test.exs +++ b/propagators/opentelemetry_process_propagator/test/opentelemetry_process_propagator_test.exs @@ -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 diff --git a/propagators/opentelemetry_process_propagator/test/task_test.exs b/propagators/opentelemetry_process_propagator/test/task_test.exs index 0d847b3b..6cd4ecfe 100644 --- a/propagators/opentelemetry_process_propagator/test/task_test.exs +++ b/propagators/opentelemetry_process_propagator/test/task_test.exs @@ -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 @@ -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