metric.pyx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. from libcpp cimport bool
  2. from util.system.types cimport ui32, ui64, i64
  3. from library.python.monlib.metric cimport (
  4. TGauge, TCounter, TRate, TIntGauge, THistogram,
  5. IHistogramCollectorPtr)
  6. cdef class Gauge:
  7. """
  8. Represents a floating point absolute value
  9. """
  10. @staticmethod
  11. cdef Gauge from_ptr(TGauge* native):
  12. cdef Gauge wrapper = Gauge.__new__(Gauge)
  13. wrapper.__wrapped = native
  14. return wrapper
  15. def set(self, double value):
  16. """
  17. Set metric to the specified value
  18. :param value: metric value
  19. """
  20. self.__wrapped.Set(value)
  21. def get(self):
  22. """
  23. Get metric value.
  24. :param value: metric value
  25. """
  26. return self.__wrapped.Get()
  27. def add(self, double value):
  28. """
  29. Add value to metric.
  30. :param value: metric value
  31. """
  32. return self.__wrapped.Add(value)
  33. cdef class IntGauge:
  34. """
  35. Represents an integer absolute value
  36. """
  37. @staticmethod
  38. cdef IntGauge from_ptr(TIntGauge* native):
  39. cdef IntGauge wrapper = IntGauge.__new__(IntGauge)
  40. wrapper.__wrapped = native
  41. return wrapper
  42. def set(self, i64 value):
  43. """
  44. Set metric to the specified value
  45. :param value: metric value
  46. """
  47. self.__wrapped.Set(value)
  48. def get(self):
  49. """
  50. Get metric value
  51. :param value: metric value
  52. """
  53. return self.__wrapped.Get()
  54. def add(self, i64 value):
  55. """
  56. Add value to metric.
  57. :param value: metric value
  58. """
  59. return self.__wrapped.Add(value)
  60. def inc(self):
  61. """
  62. Add 1 to metric.
  63. """
  64. return self.__wrapped.Inc()
  65. def dec(self):
  66. """
  67. Add -1 to metric.
  68. """
  69. return self.__wrapped.Dec()
  70. cdef class Counter:
  71. """
  72. Represents a counter value
  73. """
  74. @staticmethod
  75. cdef Counter from_ptr(TCounter* native):
  76. cdef Counter wrapper = Counter.__new__(Counter)
  77. wrapper.__wrapped = native
  78. return wrapper
  79. def get(self):
  80. return self.__wrapped.Get()
  81. def inc(self):
  82. """
  83. Increment metric value
  84. """
  85. return self.__wrapped.Inc()
  86. def reset(self):
  87. """
  88. Reset metric value to zero
  89. """
  90. return self.__wrapped.Reset()
  91. cdef class Rate:
  92. """
  93. Represents a time derivative
  94. """
  95. @staticmethod
  96. cdef Rate from_ptr(TRate* native):
  97. cdef Rate wrapper = Rate.__new__(Rate)
  98. wrapper.__wrapped = native
  99. return wrapper
  100. def get(self):
  101. return self.__wrapped.Get()
  102. def inc(self):
  103. """
  104. Increment metric value
  105. """
  106. return self.__wrapped.Inc()
  107. def add(self, ui64 value):
  108. """
  109. Add the value to metric
  110. :param value: value to add to metric
  111. """
  112. return self.__wrapped.Add(value)
  113. cdef class Histogram:
  114. """
  115. Represents some value distribution
  116. """
  117. @staticmethod
  118. cdef Histogram from_ptr(THistogram* native):
  119. cdef Histogram wrapper = Histogram.__new__(Histogram, 0)
  120. wrapper.__is_owner = False
  121. wrapper.__wrapped = native
  122. return wrapper
  123. def __dealloc__(self):
  124. if self.__is_owner:
  125. del self.__wrapped
  126. def collect(self, double value, ui32 count=1):
  127. """
  128. Add a few points with same value to the distribution
  129. :param value: points' value
  130. :param value: point count
  131. """
  132. return self.__wrapped.Record(value, count)