Gauge.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #pragma once
  2. #include <memory>
  3. #include <string_view>
  4. #include <c10/macros/Macros.h>
  5. #include <c10/util/SmallVector.h>
  6. namespace c10::monitor {
  7. namespace detail {
  8. class GaugeImpl;
  9. class GaugeBackendIf {
  10. public:
  11. virtual ~GaugeBackendIf() = default;
  12. virtual void record(int64_t value) noexcept = 0;
  13. };
  14. class GaugeBackendFactoryIf {
  15. public:
  16. virtual ~GaugeBackendFactoryIf() = default;
  17. // May return nullptr if the gauge will be ignored by the given backend.
  18. virtual std::unique_ptr<GaugeBackendIf> create(
  19. std::string_view key) noexcept = 0;
  20. };
  21. void C10_API registerGaugeBackend(std::unique_ptr<GaugeBackendFactoryIf>);
  22. } // namespace detail
  23. // A handle to a Gauge.
  24. class C10_API GaugeHandle {
  25. public:
  26. explicit GaugeHandle(std::string_view key);
  27. void record(int64_t value);
  28. private:
  29. // NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
  30. detail::GaugeImpl& impl_;
  31. };
  32. } // namespace c10::monitor
  33. #define STATIC_GAUGE(_key) \
  34. []() -> ::c10::monitor::GaugeHandle& { \
  35. static ::c10::monitor::GaugeHandle handle(#_key); \
  36. return handle; \
  37. }()