buffered_encoder_base.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include "string_pool.h"
  3. #include <library/cpp/monlib/encode/encoder.h>
  4. #include <library/cpp/monlib/encode/encoder_state.h>
  5. #include <library/cpp/monlib/encode/format.h>
  6. #include <library/cpp/monlib/metrics/metric_value.h>
  7. #include <util/datetime/base.h>
  8. #include <util/digest/numeric.h>
  9. namespace NMonitoring {
  10. class TBufferedEncoderBase : public IMetricEncoder {
  11. public:
  12. void OnStreamBegin() override;
  13. void OnStreamEnd() override;
  14. void OnCommonTime(TInstant time) override;
  15. void OnMetricBegin(EMetricType type) override;
  16. void OnMetricEnd() override;
  17. void OnLabelsBegin() override;
  18. void OnLabelsEnd() override;
  19. void OnLabel(TStringBuf name, TStringBuf value) override;
  20. void OnLabel(ui32 name, ui32 value) override;
  21. std::pair<ui32, ui32> PrepareLabel(TStringBuf name, TStringBuf value) override;
  22. void OnDouble(TInstant time, double value) override;
  23. void OnInt64(TInstant time, i64 value) override;
  24. void OnUint64(TInstant time, ui64 value) override;
  25. void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) override;
  26. void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override;
  27. void OnLogHistogram(TInstant, TLogHistogramSnapshotPtr) override;
  28. protected:
  29. using TPooledStr = TStringPoolBuilder::TValue;
  30. struct TPooledLabel {
  31. TPooledLabel(const TPooledStr* key, const TPooledStr* value)
  32. : Key{key}
  33. , Value{value}
  34. {
  35. }
  36. bool operator==(const TPooledLabel& other) const {
  37. return std::tie(Key, Value) == std::tie(other.Key, other.Value);
  38. }
  39. bool operator!=(const TPooledLabel& other) const {
  40. return !(*this == other);
  41. }
  42. const TPooledStr* Key;
  43. const TPooledStr* Value;
  44. };
  45. using TPooledLabels = TVector<TPooledLabel>;
  46. struct TPooledLabelsHash {
  47. size_t operator()(const TPooledLabels& val) const {
  48. size_t hash{0};
  49. for (auto v : val) {
  50. hash = CombineHashes<size_t>(hash, reinterpret_cast<size_t>(v.Key));
  51. hash = CombineHashes<size_t>(hash, reinterpret_cast<size_t>(v.Value));
  52. }
  53. return hash;
  54. }
  55. };
  56. using TMetricMap = THashMap<TPooledLabels, size_t, TPooledLabelsHash>;
  57. struct TMetric {
  58. EMetricType MetricType = EMetricType::UNKNOWN;
  59. TPooledLabels Labels;
  60. TMetricTimeSeries TimeSeries;
  61. };
  62. protected:
  63. TString FormatLabels(const TPooledLabels& labels) const;
  64. protected:
  65. TEncoderState State_;
  66. TStringPoolBuilder LabelNamesPool_;
  67. TStringPoolBuilder LabelValuesPool_;
  68. TInstant CommonTime_ = TInstant::Zero();
  69. TPooledLabels CommonLabels_;
  70. TVector<TMetric> Metrics_;
  71. TMetricMap MetricMap_;
  72. EMetricsMergingMode MetricsMergingMode_ = EMetricsMergingMode::DEFAULT;
  73. };
  74. }