123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- #include "protobuf.h"
- #include <util/datetime/base.h>
- namespace NMonitoring {
- namespace {
- NProto::EMetricType ConvertMetricType(EMetricType type) {
- switch (type) {
- case EMetricType::GAUGE:
- return NProto::GAUGE;
- case EMetricType::COUNTER:
- return NProto::COUNTER;
- case EMetricType::RATE:
- return NProto::RATE;
- case EMetricType::IGAUGE:
- return NProto::IGAUGE;
- case EMetricType::HIST:
- return NProto::HISTOGRAM;
- case EMetricType::HIST_RATE:
- return NProto::HIST_RATE;
- case EMetricType::DSUMMARY:
- return NProto::DSUMMARY;
- case EMetricType::LOGHIST:
- return NProto::LOGHISTOGRAM;
- case EMetricType::UNKNOWN:
- return NProto::UNKNOWN;
- }
- }
- void FillHistogram(
- const IHistogramSnapshot& snapshot,
- NProto::THistogram* histogram)
- {
- for (ui32 i = 0; i < snapshot.Count(); i++) {
- histogram->AddBounds(snapshot.UpperBound(i));
- histogram->AddValues(snapshot.Value(i));
- }
- }
- void FillSummaryDouble(const ISummaryDoubleSnapshot& snapshot, NProto::TSummaryDouble* summary) {
- summary->SetSum(snapshot.GetSum());
- summary->SetMin(snapshot.GetMin());
- summary->SetMax(snapshot.GetMax());
- summary->SetLast(snapshot.GetLast());
- summary->SetCount(snapshot.GetCount());
- }
- void FillLogHistogram(const TLogHistogramSnapshot& snapshot, NProto::TLogHistogram* logHist) {
- logHist->SetBase(snapshot.Base());
- logHist->SetZerosCount(snapshot.ZerosCount());
- logHist->SetStartPower(snapshot.StartPower());
- for (ui32 i = 0; i < snapshot.Count(); ++i) {
- logHist->AddBuckets(snapshot.Bucket(i));
- }
- }
- ///////////////////////////////////////////////////////////////////////////////
- // TSingleamplesEncoder
- ///////////////////////////////////////////////////////////////////////////////
- class TSingleSamplesEncoder final: public IMetricEncoder {
- public:
- TSingleSamplesEncoder(NProto::TSingleSamplesList* samples)
- : Samples_(samples)
- , Sample_(nullptr)
- {
- }
- private:
- void OnStreamBegin() override {
- }
- void OnStreamEnd() override {
- }
- void OnCommonTime(TInstant time) override {
- Samples_->SetCommonTime(time.MilliSeconds());
- }
- void OnMetricBegin(EMetricType type) override {
- Sample_ = Samples_->AddSamples();
- Sample_->SetMetricType(ConvertMetricType(type));
- }
- void OnMetricEnd() override {
- Sample_ = nullptr;
- }
- void OnLabelsBegin() override {
- }
- void OnLabelsEnd() override {
- }
- void OnLabel(TStringBuf name, TStringBuf value) override {
- NProto::TLabel* label = (Sample_ == nullptr)
- ? Samples_->AddCommonLabels()
- : Sample_->AddLabels();
- label->SetName(TString{name});
- label->SetValue(TString{value});
- }
- void OnDouble(TInstant time, double value) override {
- Y_ENSURE(Sample_, "metric not started");
- Sample_->SetTime(time.MilliSeconds());
- Sample_->SetFloat64(value);
- }
- void OnInt64(TInstant time, i64 value) override {
- Y_ENSURE(Sample_, "metric not started");
- Sample_->SetTime(time.MilliSeconds());
- Sample_->SetInt64(value);
- }
- void OnUint64(TInstant time, ui64 value) override {
- Y_ENSURE(Sample_, "metric not started");
- Sample_->SetTime(time.MilliSeconds());
- Sample_->SetUint64(value);
- }
- void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) override {
- Y_ENSURE(Sample_, "metric not started");
- Sample_->SetTime(time.MilliSeconds());
- FillHistogram(*snapshot, Sample_->MutableHistogram());
- }
- void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override {
- Y_ENSURE(Sample_, "metric not started");
- Sample_->SetTime(time.MilliSeconds());
- FillSummaryDouble(*snapshot, Sample_->MutableSummaryDouble());
- }
- void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override {
- Y_ENSURE(Sample_, "metric not started");
- Sample_->SetTime(time.MilliSeconds());
- FillLogHistogram(*snapshot, Sample_->MutableLogHistogram());
- }
- void Close() override {
- }
- private:
- NProto::TSingleSamplesList* Samples_;
- NProto::TSingleSample* Sample_;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // TMultiSamplesEncoder
- ///////////////////////////////////////////////////////////////////////////////
- class TMultiSamplesEncoder final: public IMetricEncoder {
- public:
- TMultiSamplesEncoder(NProto::TMultiSamplesList* samples)
- : Samples_(samples)
- , Sample_(nullptr)
- {
- }
- private:
- void OnStreamBegin() override {
- }
- void OnStreamEnd() override {
- }
- void OnCommonTime(TInstant time) override {
- Samples_->SetCommonTime(time.MilliSeconds());
- }
- void OnMetricBegin(EMetricType type) override {
- Sample_ = Samples_->AddSamples();
- Sample_->SetMetricType(ConvertMetricType(type));
- }
- void OnMetricEnd() override {
- Sample_ = nullptr;
- }
- void OnLabelsBegin() override {
- }
- void OnLabelsEnd() override {
- }
- void OnLabel(TStringBuf name, TStringBuf value) override {
- NProto::TLabel* label = (Sample_ == nullptr)
- ? Samples_->AddCommonLabels()
- : Sample_->AddLabels();
- label->SetName(TString{name});
- label->SetValue(TString{value});
- }
- void OnDouble(TInstant time, double value) override {
- Y_ENSURE(Sample_, "metric not started");
- NProto::TPoint* point = Sample_->AddPoints();
- point->SetTime(time.MilliSeconds());
- point->SetFloat64(value);
- }
- void OnInt64(TInstant time, i64 value) override {
- Y_ENSURE(Sample_, "metric not started");
- NProto::TPoint* point = Sample_->AddPoints();
- point->SetTime(time.MilliSeconds());
- point->SetInt64(value);
- }
- void OnUint64(TInstant time, ui64 value) override {
- Y_ENSURE(Sample_, "metric not started");
- NProto::TPoint* point = Sample_->AddPoints();
- point->SetTime(time.MilliSeconds());
- point->SetUint64(value);
- }
- void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) override {
- Y_ENSURE(Sample_, "metric not started");
- NProto::TPoint* point = Sample_->AddPoints();
- point->SetTime(time.MilliSeconds());
- FillHistogram(*snapshot, point->MutableHistogram());
- }
- void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) override {
- Y_ENSURE(Sample_, "metric not started");
- NProto::TPoint* point = Sample_->AddPoints();
- point->SetTime(time.MilliSeconds());
- FillSummaryDouble(*snapshot, point->MutableSummaryDouble());
- }
- void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) override {
- Y_ENSURE(Sample_, "metric not started");
- NProto::TPoint* point = Sample_->AddPoints();
- point->SetTime(time.MilliSeconds());
- FillLogHistogram(*snapshot, point->MutableLogHistogram());
- }
- void Close() override {
- }
- private:
- NProto::TMultiSamplesList* Samples_;
- NProto::TMultiSample* Sample_;
- };
- }
- IMetricEncoderPtr EncoderProtobuf(NProto::TSingleSamplesList* samples) {
- return MakeHolder<TSingleSamplesEncoder>(samples);
- }
- IMetricEncoderPtr EncoderProtobuf(NProto::TMultiSamplesList* samples) {
- return MakeHolder<TMultiSamplesEncoder>(samples);
- }
- }
|