@@ -56,13 +56,17 @@ namespace NWilson {
5656 int UncaughtExceptions = std::uncaught_exceptions();
5757 bool Sent = false ;
5858 bool Ignored = false ;
59+ NActors::TActorSystem* ActorSystem;
5960
60- TData (TInstant startTime, ui64 startCycles, TTraceId traceId, TFlags flags)
61+ TData (TInstant startTime, ui64 startCycles, TTraceId traceId, TFlags flags, NActors::TActorSystem* actorSystem )
6162 : StartTime(startTime)
6263 , StartCycles(startCycles)
6364 , TraceId(std::move(traceId))
6465 , Flags(flags)
65- {}
66+ , ActorSystem(actorSystem != nullptr ? actorSystem : (NActors::TlsActivationContext ? NActors::TActivationContext::ActorSystem() : nullptr))
67+ {
68+ Y_ABORT_UNLESS (ActorSystem != nullptr , " Attempting to create NWilson::TSpan outside of actor system without providing actorSystem pointer" );
69+ }
6670
6771 ~TData () {
6872 Y_DEBUG_ABORT_UNLESS (Sent || Ignored);
@@ -76,9 +80,9 @@ namespace NWilson {
7680 TSpan (const TSpan&) = delete ;
7781 TSpan (TSpan&&) = default ;
7882
79- TSpan (ui8 verbosity, TTraceId parentId, std::optional<TString> name, TFlags flags = EFlags::NONE)
83+ TSpan (ui8 verbosity, TTraceId parentId, std::optional<TString> name, TFlags flags = EFlags::NONE, NActors::TActorSystem* actorSystem = nullptr )
8084 : Data(parentId
81- ? std::make_unique<TData>(TInstant::Now(), GetCycleCount(), parentId.Span(verbosity), flags)
85+ ? std::make_unique<TData>(TInstant::Now(), GetCycleCount(), parentId.Span(verbosity), flags, actorSystem )
8286 : nullptr )
8387 {
8488 if (Y_UNLIKELY (*this )) {
@@ -93,7 +97,7 @@ namespace NWilson {
9397 Name (std::move (*name));
9498 }
9599
96- Attribute (" node_id" , NActors::TActivationContext:: ActorSystem() ->NodeId );
100+ Attribute (" node_id" , Data-> ActorSystem ->NodeId );
97101 } else {
98102 Data->Ignored = true ; // ignore this span due to verbosity mismatch, still allowing child spans to be created
99103 }
@@ -228,6 +232,14 @@ namespace NWilson {
228232 return Data ? TTraceId (Data->TraceId ) : TTraceId ();
229233 }
230234
235+ NActors::TActorSystem* GetActorSystem () const {
236+ return Data ? Data->ActorSystem : nullptr ;
237+ }
238+
239+ TSpan CreateChild (ui8 verbosity, std::optional<TString> name, TFlags flags = EFlags::NONE) const {
240+ return TSpan (verbosity, GetTraceId (), std::move (name), flags, GetActorSystem ());
241+ }
242+
231243 private:
232244 void Send ();
233245
0 commit comments