diff --git a/ydb/library/actors/wilson/wilson_span.cpp b/ydb/library/actors/wilson/wilson_span.cpp index 11beb0f486a3..017e17f23abd 100644 --- a/ydb/library/actors/wilson/wilson_span.cpp +++ b/ydb/library/actors/wilson/wilson_span.cpp @@ -53,9 +53,7 @@ namespace NWilson { } void TSpan::Send() { - if (TlsActivationContext) { - TActivationContext::Send(new IEventHandle(MakeWilsonUploaderId(), {}, new TEvWilson(&Data->Span))); - } + Data->ActorSystem->Send(new IEventHandle(MakeWilsonUploaderId(), {}, new TEvWilson(&Data->Span))); Data->Sent = true; } diff --git a/ydb/library/actors/wilson/wilson_span.h b/ydb/library/actors/wilson/wilson_span.h index 93458ce1300e..9aa7900f1908 100644 --- a/ydb/library/actors/wilson/wilson_span.h +++ b/ydb/library/actors/wilson/wilson_span.h @@ -56,13 +56,17 @@ namespace NWilson { int UncaughtExceptions = std::uncaught_exceptions(); bool Sent = false; bool Ignored = false; + NActors::TActorSystem* ActorSystem; - TData(TInstant startTime, ui64 startCycles, TTraceId traceId, TFlags flags) + TData(TInstant startTime, ui64 startCycles, TTraceId traceId, TFlags flags, NActors::TActorSystem* actorSystem) : StartTime(startTime) , StartCycles(startCycles) , TraceId(std::move(traceId)) , Flags(flags) - {} + , ActorSystem(actorSystem != nullptr ? actorSystem : (NActors::TlsActivationContext ? NActors::TActivationContext::ActorSystem() : nullptr)) + { + Y_ABORT_UNLESS(ActorSystem != nullptr, "Attempting to create NWilson::TSpan outside of actor system without providing actorSystem pointer"); + } ~TData() { Y_DEBUG_ABORT_UNLESS(Sent || Ignored); @@ -76,9 +80,9 @@ namespace NWilson { TSpan(const TSpan&) = delete; TSpan(TSpan&&) = default; - TSpan(ui8 verbosity, TTraceId parentId, std::optional name, TFlags flags = EFlags::NONE) + TSpan(ui8 verbosity, TTraceId parentId, std::optional name, TFlags flags = EFlags::NONE, NActors::TActorSystem* actorSystem = nullptr) : Data(parentId - ? std::make_unique(TInstant::Now(), GetCycleCount(), parentId.Span(verbosity), flags) + ? std::make_unique(TInstant::Now(), GetCycleCount(), parentId.Span(verbosity), flags, actorSystem) : nullptr) { if (Y_UNLIKELY(*this)) { @@ -93,7 +97,7 @@ namespace NWilson { Name(std::move(*name)); } - Attribute("node_id", NActors::TActivationContext::ActorSystem()->NodeId); + Attribute("node_id", Data->ActorSystem->NodeId); } else { Data->Ignored = true; // ignore this span due to verbosity mismatch, still allowing child spans to be created } @@ -228,6 +232,14 @@ namespace NWilson { return Data ? TTraceId(Data->TraceId) : TTraceId(); } + NActors::TActorSystem* GetActorSystem() const { + return Data ? Data->ActorSystem : nullptr; + } + + TSpan CreateChild(ui8 verbosity, std::optional name, TFlags flags = EFlags::NONE) const { + return TSpan(verbosity, GetTraceId(), std::move(name), flags, GetActorSystem()); + } + private: void Send(); diff --git a/ydb/library/actors/wilson/wilson_trace.h b/ydb/library/actors/wilson/wilson_trace.h index 331218b9602d..2cba7e4bdabe 100644 --- a/ydb/library/actors/wilson/wilson_trace.h +++ b/ydb/library/actors/wilson/wilson_trace.h @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -222,6 +223,10 @@ namespace NWilson { const void *GetSpanIdPtr() const { return &SpanId; } static constexpr size_t GetSpanIdSize() { return sizeof(ui64); } + TString GetHexTraceId() const { + return HexEncode(GetTraceIdPtr(), GetTraceIdSize()); + } + void Validate() const { Y_DEBUG_ABORT_UNLESS(*this || !SpanId); }