origin_attributes.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "public.h"
  3. #include <library/cpp/yt/global/access.h>
  4. #include <library/cpp/yt/memory/ref.h>
  5. #include <library/cpp/yt/misc/guid.h>
  6. #include <library/cpp/yt/misc/thread_name.h>
  7. #include <library/cpp/yt/threading/public.h>
  8. #include <util/datetime/base.h>
  9. #include <util/system/getpid.h>
  10. namespace NYT {
  11. ////////////////////////////////////////////////////////////////////////////////
  12. //! When this guard is set, newly created errors do not have non-deterministic
  13. //! system attributes and have "datetime" and "host" attributes overridden with a given values.
  14. class TErrorSanitizerGuard
  15. : public TNonCopyable
  16. {
  17. public:
  18. TErrorSanitizerGuard(TInstant datetimeOverride, TSharedRef localHostNameOverride);
  19. ~TErrorSanitizerGuard();
  20. private:
  21. const bool SavedEnabled_;
  22. const TInstant SavedDatetimeOverride_;
  23. const TSharedRef SavedLocalHostNameOverride_;
  24. };
  25. bool IsErrorSanitizerEnabled() noexcept;
  26. ////////////////////////////////////////////////////////////////////////////////
  27. struct TOriginAttributes
  28. {
  29. static constexpr size_t ExtensionDataByteSizeCap = 64;
  30. using TErasedExtensionData = TErasedStorage<ExtensionDataByteSizeCap>;
  31. TProcessId Pid = 0;
  32. NThreading::TThreadId Tid;
  33. TThreadName ThreadName;
  34. TInstant Datetime;
  35. TSharedRef HostHolder;
  36. mutable TStringBuf Host;
  37. // Opaque storage for data from yt/yt/core.
  38. // Currently may contain FiberId, TraceId, SpandId.
  39. std::optional<TErasedExtensionData> ExtensionData;
  40. bool operator==(const TOriginAttributes& other) const noexcept;
  41. void Capture();
  42. };
  43. ////////////////////////////////////////////////////////////////////////////////
  44. namespace NDetail {
  45. inline constexpr NGlobal::TVariableTag GetExtensionDataTag = {};
  46. inline constexpr NGlobal::TVariableTag FormatOriginTag = {};
  47. inline constexpr NGlobal::TVariableTag ExtractFromDictionaryTag = {};
  48. ////////////////////////////////////////////////////////////////////////////////
  49. // These are "weak" symbols.
  50. // NB(arkady-e1ppa): ExtractFromDictionary symbol is left in yt/yt/core/misc/origin_attributes
  51. // because it depends on ytree for now.
  52. std::optional<TOriginAttributes::TErasedExtensionData> GetExtensionData();
  53. TString FormatOrigin(const TOriginAttributes& attributes);
  54. ////////////////////////////////////////////////////////////////////////////////
  55. // Weak symbol.
  56. TOriginAttributes ExtractFromDictionary(TErrorAttributes* attributes);
  57. // Default impl of weak symbol.
  58. TOriginAttributes ExtractFromDictionaryDefault(TErrorAttributes* attributes);
  59. } // namespace NDetail
  60. ////////////////////////////////////////////////////////////////////////////////
  61. } // namespace NYT