From 9f2b0ca46bc813476ac821792b0bc53e4cd74c50 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Fri, 5 Jun 2026 20:54:43 +0200 Subject: [PATCH] Spawned processes inherit the parent's flow identity --- src/gm_ctflow.erl | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/gm_ctflow.erl b/src/gm_ctflow.erl index a010875..d1fdf0d 100644 --- a/src/gm_ctflow.erl +++ b/src/gm_ctflow.erl @@ -7,6 +7,7 @@ ]). -export([ note_flow/1 + , get_flow/0 , ct_log/2 %% ct_log(Fmt, Args) , ct_log/3 %% ct_log(Flow, Fmt, Args) , timestamp/0 ]). @@ -44,9 +45,14 @@ get(Key, Default) -> erase(Key) -> gm_ctflow_state:erase(Key). - +-spec note_flow(flow()) -> 'ok'. note_flow(Flow) -> - erlang:put({?MODULE, flow}, Flow). + erlang:put({?MODULE, flow}, Flow), + ok. + +-spec get_flow() -> flow() | 'undefined'. +get_flow() -> + erlang:get({?MODULE, flow}). ct_log(Fmt, Args) -> ct_log(erlang:get({?MODULE, flow}), Fmt, Args). @@ -136,13 +142,20 @@ timestamp() -> [Year,Month,Day,Hour,Min,Sec,MilliSec])). spawn(F) -> + Flow = get_flow(), proc_lib:spawn(fun() -> + maybe_inherit_flow(Flow), ct_util:mark_process(), F() end). spawn_opt(F, Opts) -> + Flow = get_flow(), proc_lib:spawn_opt(fun() -> - ct_util:mark_process(), - F() - end, Opts). + maybe_inherit_flow(Flow), + ct_util:mark_process(), + F() + end, Opts). + +maybe_inherit_flow(undefined) -> ok; +maybe_inherit_flow(Flow) -> note_flow(Flow).