origin_attributes.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "origin_attributes.h"
  2. #include <library/cpp/yt/assert/assert.h>
  3. #include <library/cpp/yt/misc/thread_name.h>
  4. #include <library/cpp/yt/misc/tls.h>
  5. #include <library/cpp/yt/string/format.h>
  6. #include <util/system/thread.h>
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. YT_DEFINE_THREAD_LOCAL(bool, ErrorSanitizerEnabled, false);
  10. YT_DEFINE_THREAD_LOCAL(TInstant, ErrorSanitizerDatetimeOverride);
  11. YT_DEFINE_THREAD_LOCAL(TSharedRef, ErrorSanitizerLocalHostNameOverride);
  12. TErrorSanitizerGuard::TErrorSanitizerGuard(TInstant datetimeOverride, TSharedRef localHostNameOverride)
  13. : SavedEnabled_(ErrorSanitizerEnabled())
  14. , SavedDatetimeOverride_(ErrorSanitizerDatetimeOverride())
  15. , SavedLocalHostNameOverride_(ErrorSanitizerLocalHostNameOverride())
  16. {
  17. ErrorSanitizerEnabled() = true;
  18. ErrorSanitizerDatetimeOverride() = datetimeOverride;
  19. ErrorSanitizerLocalHostNameOverride() = std::move(localHostNameOverride);
  20. }
  21. TErrorSanitizerGuard::~TErrorSanitizerGuard()
  22. {
  23. YT_ASSERT(ErrorSanitizerEnabled());
  24. ErrorSanitizerEnabled() = SavedEnabled_;
  25. ErrorSanitizerDatetimeOverride() = SavedDatetimeOverride_;
  26. ErrorSanitizerLocalHostNameOverride() = std::move(SavedLocalHostNameOverride_);
  27. }
  28. bool IsErrorSanitizerEnabled() noexcept
  29. {
  30. return ErrorSanitizerEnabled();
  31. }
  32. ////////////////////////////////////////////////////////////////////////////////
  33. bool TOriginAttributes::operator==(const TOriginAttributes& other) const noexcept
  34. {
  35. return
  36. Host == other.Host &&
  37. Datetime == other.Datetime &&
  38. Pid == other.Pid &&
  39. Tid == other.Tid &&
  40. ExtensionData == other.ExtensionData;
  41. }
  42. void TOriginAttributes::Capture()
  43. {
  44. if (ErrorSanitizerEnabled()) {
  45. Datetime = ErrorSanitizerDatetimeOverride();
  46. HostHolder = ErrorSanitizerLocalHostNameOverride();
  47. Host = HostHolder.empty() ? TStringBuf() : TStringBuf(HostHolder.Begin(), HostHolder.End());
  48. return;
  49. }
  50. Datetime = TInstant::Now();
  51. Pid = GetPID();
  52. Tid = TThread::CurrentThreadId();
  53. ThreadName = GetCurrentThreadName();
  54. ExtensionData = NDetail::GetExtensionData();
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////
  57. namespace NDetail {
  58. std::optional<TOriginAttributes::TErasedExtensionData> GetExtensionData()
  59. {
  60. using TFunctor = TOriginAttributes::TErasedExtensionData(*)();
  61. if (auto strong = NGlobal::GetErasedVariable(GetExtensionDataTag)) {
  62. return strong->AsConcrete<TFunctor>()();
  63. }
  64. return std::nullopt;
  65. }
  66. TString FormatOrigin(const TOriginAttributes& attributes)
  67. {
  68. using TFunctor = TString(*)(const TOriginAttributes&);
  69. if (auto strong = NGlobal::GetErasedVariable(FormatOriginTag)) {
  70. return strong->AsConcrete<TFunctor>()(attributes);
  71. }
  72. return Format(
  73. "%v (pid %v, thread %v)",
  74. attributes.Host,
  75. attributes.Pid,
  76. MakeFormatterWrapper([&] (auto* builder) {
  77. auto threadName = attributes.ThreadName.ToStringBuf();
  78. if (threadName.empty()) {
  79. FormatValue(builder, attributes.Tid, "v");
  80. return;
  81. }
  82. FormatValue(builder, threadName, "v");
  83. }));
  84. }
  85. } // namespace NDetail
  86. ////////////////////////////////////////////////////////////////////////////////
  87. } // namespace NYT