protobuf_encoder.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "protobuf.h"
  2. #include <util/datetime/base.h>
  3. namespace NMonitoring {
  4. namespace {
  5. NProto::EMetricType ConvertMetricType(EMetricType type) {
  6. switch (type) {
  7. case EMetricType::GAUGE:
  8. return NProto::GAUGE;
  9. case EMetricType::COUNTER:
  10. return NProto::COUNTER;
  11. case EMetricType::RATE:
  12. return NProto::RATE;
  13. case EMetricType::IGAUGE:
  14. return NProto::IGAUGE;
  15. case EMetricType::HIST:
  16. return NProto::HISTOGRAM;
  17. case EMetricType::HIST_RATE:
  18. return NProto::HIST_RATE;
  19. case EMetricType::DSUMMARY:
  20. return NProto::DSUMMARY;
  21. case EMetricType::LOGHIST:
  22. return NProto::LOGHISTOGRAM;
  23. case EMetricType::UNKNOWN:
  24. return NProto::UNKNOWN;
  25. }
  26. }
  27. void FillHistogram(
  28. const IHistogramSnapshot& snapshot,
  29. NProto::THistogram* histogram)
  30. {
  31. for (ui32 i = 0; i < snapshot.Count(); i++) {
  32. histogram->AddBounds(snapshot.UpperBound(i));
  33. histogram->AddValues(snapshot.Value(i));
  34. }
  35. }
  36. void FillSummaryDouble(const ISummaryDoubleSnapshot& snapshot, NProto::TSummaryDouble* summary) {
  37. summary->SetSum(snapshot.GetSum());
  38. summary->SetMin(snapshot.GetMin());
  39. summary->SetMax(snapshot.GetMax());
  40. summary->SetLast(snapshot.GetLast());
  41. summary->SetCount(snapshot.GetCount());
  42. }
  43. void FillLogHistogram(const TLogHistogramSnapshot& snapshot, NProto::TLogHistogram* logHist) {
  44. logHist->SetBase(snapshot.Base());
  45. logHist->SetZerosCount(snapshot.ZerosCount());
  46. logHist->SetStartPower(snapshot.StartPower());
  47. for (ui32 i = 0; i < snapshot.Count(); ++i) {
  48. logHist->AddBuckets(snapshot.Bucket(i));
  49. }
  50. }
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // TSingleamplesEncoder
  53. ///////////////////////////////////////////////////////////////////////////////
  54. class TSingleSamplesEncoder final: public IMetricEncoder {
  55. public:
  56. TSingleSamplesEncoder(NProto::TSingleSamplesList* samples)
  57. : Samples_(samples)
  58. , Sample_(nullptr)
  59. {
  60. }
  61. private:
  62. void OnStreamBegin() override {
  63. }
  64. void OnStreamEnd() override {
  65. }
  66. void OnCommonTime(TInstant time) override {
  67. Samples_->SetCommonTime(time.MilliSeconds());
  68. }
  69. void OnMetricBegin(EMetricType type) override {
  70. Sample_ = Samples_->AddSamples();
  71. Sample_->SetMetricType(ConvertMetricType(type));
  72. }
  73. void OnMetricEnd() override {
  74. Sample_ = nullptr;
  75. }
  76. void OnLabelsBegin() override {
  77. }
  78. void OnLabelsEnd() override {
  79. }
  80. void OnLabel(TStringBuf name, TStringBuf value) override {
  81. NProto::TLabel* label = (Sample_ == nullptr)
  82. ? Samples_->AddCommonLabels()
  83. : Sample_->AddLabels();
  84. label->SetName(TString{name});
  85. label->SetValue(TString{value});
  86. }
  87. void OnDouble(TInstant time, double value) override {
  88. Y_ENSURE(Sample_, "metric not started");
  89. Sample_->SetTime(time.MilliSeconds());
  90. Sample_->SetFloat64(value);
  91. }
  92. void OnInt64(TInstant time, i64 value) override {
  93. Y_ENSURE(Sample_, "metric not started");
  94. Sample_->SetTime(time.MilliSeconds());
  95. Sample_->SetInt64(value);
  96. }
  97. void OnUint64(TInstant time, ui64 value) override {
  98. Y_ENSURE(Sample_, "metric not started");
  99. Sample_->SetTime(time.MilliSeconds());
  100. Sample_->SetUint64(value);
  101. }
  102. void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) override {
  103. Y_ENSURE(Sample_, "metric not started");
  104. Sample_->SetTime(time.MilliSeconds());
  105. FillHistogram(*snapshot, Sample_->MutableHistogram());
  106. }
  107. void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override {
  108. Y_ENSURE(Sample_, "metric not started");
  109. Sample_->SetTime(time.MilliSeconds());
  110. FillSummaryDouble(*snapshot, Sample_->MutableSummaryDouble());
  111. }
  112. void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override {
  113. Y_ENSURE(Sample_, "metric not started");
  114. Sample_->SetTime(time.MilliSeconds());
  115. FillLogHistogram(*snapshot, Sample_->MutableLogHistogram());
  116. }
  117. void Close() override {
  118. }
  119. private:
  120. NProto::TSingleSamplesList* Samples_;
  121. NProto::TSingleSample* Sample_;
  122. };
  123. ///////////////////////////////////////////////////////////////////////////////
  124. // TMultiSamplesEncoder
  125. ///////////////////////////////////////////////////////////////////////////////
  126. class TMultiSamplesEncoder final: public IMetricEncoder {
  127. public:
  128. TMultiSamplesEncoder(NProto::TMultiSamplesList* samples)
  129. : Samples_(samples)
  130. , Sample_(nullptr)
  131. {
  132. }
  133. private:
  134. void OnStreamBegin() override {
  135. }
  136. void OnStreamEnd() override {
  137. }
  138. void OnCommonTime(TInstant time) override {
  139. Samples_->SetCommonTime(time.MilliSeconds());
  140. }
  141. void OnMetricBegin(EMetricType type) override {
  142. Sample_ = Samples_->AddSamples();
  143. Sample_->SetMetricType(ConvertMetricType(type));
  144. }
  145. void OnMetricEnd() override {
  146. Sample_ = nullptr;
  147. }
  148. void OnLabelsBegin() override {
  149. }
  150. void OnLabelsEnd() override {
  151. }
  152. void OnLabel(TStringBuf name, TStringBuf value) override {
  153. NProto::TLabel* label = (Sample_ == nullptr)
  154. ? Samples_->AddCommonLabels()
  155. : Sample_->AddLabels();
  156. label->SetName(TString{name});
  157. label->SetValue(TString{value});
  158. }
  159. void OnDouble(TInstant time, double value) override {
  160. Y_ENSURE(Sample_, "metric not started");
  161. NProto::TPoint* point = Sample_->AddPoints();
  162. point->SetTime(time.MilliSeconds());
  163. point->SetFloat64(value);
  164. }
  165. void OnInt64(TInstant time, i64 value) override {
  166. Y_ENSURE(Sample_, "metric not started");
  167. NProto::TPoint* point = Sample_->AddPoints();
  168. point->SetTime(time.MilliSeconds());
  169. point->SetInt64(value);
  170. }
  171. void OnUint64(TInstant time, ui64 value) override {
  172. Y_ENSURE(Sample_, "metric not started");
  173. NProto::TPoint* point = Sample_->AddPoints();
  174. point->SetTime(time.MilliSeconds());
  175. point->SetUint64(value);
  176. }
  177. void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) override {
  178. Y_ENSURE(Sample_, "metric not started");
  179. NProto::TPoint* point = Sample_->AddPoints();
  180. point->SetTime(time.MilliSeconds());
  181. FillHistogram(*snapshot, point->MutableHistogram());
  182. }
  183. void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override {
  184. Y_ENSURE(Sample_, "metric not started");
  185. NProto::TPoint* point = Sample_->AddPoints();
  186. point->SetTime(time.MilliSeconds());
  187. FillSummaryDouble(*snapshot, point->MutableSummaryDouble());
  188. }
  189. void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override {
  190. Y_ENSURE(Sample_, "metric not started");
  191. NProto::TPoint* point = Sample_->AddPoints();
  192. point->SetTime(time.MilliSeconds());
  193. FillLogHistogram(*snapshot, point->MutableLogHistogram());
  194. }
  195. void Close() override {
  196. }
  197. private:
  198. NProto::TMultiSamplesList* Samples_;
  199. NProto::TMultiSample* Sample_;
  200. };
  201. }
  202. IMetricEncoderPtr EncoderProtobuf(NProto::TSingleSamplesList* samples) {
  203. return MakeHolder<TSingleSamplesEncoder>(samples);
  204. }
  205. IMetricEncoderPtr EncoderProtobuf(NProto::TMultiSamplesList* samples) {
  206. return MakeHolder<TMultiSamplesEncoder>(samples);
  207. }
  208. }