diff options
author | monster <monster@ydb.tech> | 2022-07-07 14:41:37 +0300 |
---|---|---|
committer | monster <monster@ydb.tech> | 2022-07-07 14:41:37 +0300 |
commit | 06e5c21a835c0e923506c4ff27929f34e00761c2 (patch) | |
tree | 75efcbc6854ef9bd476eb8bf00cc5c900da436a2 /library/cpp/monlib | |
parent | 03f024c4412e3aa613bb543cf1660176320ba8f4 (diff) | |
download | ydb-06e5c21a835c0e923506c4ff27929f34e00761c2.tar.gz |
fix ya.make
Diffstat (limited to 'library/cpp/monlib')
22 files changed, 109 insertions, 2688 deletions
diff --git a/library/cpp/monlib/consumers/collecting_consumer.cpp b/library/cpp/monlib/consumers/collecting_consumer.cpp new file mode 100644 index 00000000000..28ad400f9ee --- /dev/null +++ b/library/cpp/monlib/consumers/collecting_consumer.cpp @@ -0,0 +1 @@ +#include "collecting_consumer.h" diff --git a/library/cpp/monlib/consumers/collecting_consumer.h b/library/cpp/monlib/consumers/collecting_consumer.h new file mode 100644 index 00000000000..0b061fce0bc --- /dev/null +++ b/library/cpp/monlib/consumers/collecting_consumer.h @@ -0,0 +1,108 @@ +#pragma once + +#include <library/cpp/monlib/metrics/labels.h> +#include <library/cpp/monlib/metrics/metric_value.h> +#include <library/cpp/monlib/metrics/metric_consumer.h> + +#include <util/datetime/base.h> + + +namespace NMonitoring { + // TODO(ivanzhukov@): very similar to https://nda.ya.ru/t/ST-KDJAH3W3cfn. Merge them later + struct TMetricData { + TMetricData() + : Values{new NMonitoring::TMetricTimeSeries} + { + } + + TMetricData(NMonitoring::TLabels labels, NMonitoring::EMetricType type, THolder<NMonitoring::TMetricTimeSeries> s) + : Labels{std::move(labels)} + , Kind{type} + , Values{std::move(s)} + { + } + + NMonitoring::TLabels Labels; + // TODO(ivanzhukov@): rename to Type + NMonitoring::EMetricType Kind{NMonitoring::EMetricType::UNKNOWN}; + THolder<NMonitoring::TMetricTimeSeries> Values; + }; + + template <typename TLabelsImpl> + struct TCollectingConsumerImpl: NMonitoring::IMetricConsumer { + TCollectingConsumerImpl() = default; + explicit TCollectingConsumerImpl(bool doMergeCommonLabels) + : DoMergeCommonLabels{doMergeCommonLabels} + {} + + void OnStreamBegin() override {} + void OnStreamEnd() override {} + + void OnCommonTime(TInstant time) override { + CommonTime = time; + } + + void OnMetricBegin(NMonitoring::EMetricType kind) override { + auto& metric = Metrics.emplace_back(); + metric.Kind = kind; + InsideSensor = true; + } + + void OnMetricEnd() override { + InsideSensor = false; + } + + void OnLabelsBegin() override {} + void OnLabelsEnd() override { + if (DoMergeCommonLabels) { + for (auto& cl : CommonLabels) { + Metrics.back().Labels.Add(cl); + } + } + } + + void OnLabel(TStringBuf key, TStringBuf value) override { + if (InsideSensor) { + Metrics.back().Labels.Add(key, value); + } else { + CommonLabels.Add(key, value); + } + } + + void OnDouble(TInstant time, double value) override { + Metrics.back().Values->Add(time, value); + } + + void OnInt64(TInstant time, i64 value) override { + Metrics.back().Values->Add(time, value); + } + + void OnUint64(TInstant time, ui64 value) override { + Metrics.back().Values->Add(time, value); + } + + void OnHistogram(TInstant time, NMonitoring::IHistogramSnapshotPtr snapshot) override { + auto& val = Metrics.back().Values; + val->Add(time, snapshot.Get()); + } + + void OnSummaryDouble(TInstant time, NMonitoring::ISummaryDoubleSnapshotPtr snapshot) override { + auto& val = Metrics.back().Values; + val->Add(time, snapshot.Get()); + } + + void OnLogHistogram(TInstant time, NMonitoring::TLogHistogramSnapshotPtr snapshot) override { + auto& val = Metrics.back().Values; + val->Add(time, snapshot.Get()); + } + + bool DoMergeCommonLabels{false}; + TVector<TMetricData> Metrics; + TLabelsImpl CommonLabels; + TInstant CommonTime; + bool InsideSensor{false}; + }; + + using TCollectingConsumer = TCollectingConsumerImpl<NMonitoring::TLabels>; + +} // namespace NMonitoring diff --git a/library/cpp/monlib/counters/counters.cpp b/library/cpp/monlib/counters/counters.cpp deleted file mode 100644 index 50dca4c577a..00000000000 --- a/library/cpp/monlib/counters/counters.cpp +++ /dev/null @@ -1,49 +0,0 @@ - -#include "counters.h" - -namespace NMonitoring { - char* PrettyNumShort(i64 val, char* buf, size_t size) { - static const char shorts[] = {' ', 'K', 'M', 'G', 'T', 'P', 'E'}; - unsigned i = 0; - i64 major = val; - i64 minor = 0; - const unsigned imax = sizeof(shorts) / sizeof(char); - for (i = 0; i < imax; i++) { - if (major >> 10 == 0) - break; - else { - minor = major - (major >> 10 << 10); - major = major >> 10; - } - } - minor = (minor * 10) >> 10; - - if (i == 0 || i >= imax) - *buf = '\0'; - else - snprintf(buf, size, "%" PRId64 ".%" PRId64 "%c", major, minor, shorts[i]); - - return buf; - } - - char* PrettyNum(i64 val, char* buf, size_t size) { - Y_ASSERT(buf); - if (size < 4) { - buf[0] = 0; - return buf; - } - PrettyNumShort(val, buf + 2, size - 3); - if (buf[2] == 0) { - *buf = '\0'; - } else { - size_t len = 2 + strnlen(buf + 2, size - 4); - Y_ASSERT(len < size); - buf[0] = ' '; - buf[1] = '('; - buf[len] = ')'; - buf[len + 1] = '\0'; - } - - return buf; - } -} diff --git a/library/cpp/monlib/counters/counters_ut.cpp b/library/cpp/monlib/counters/counters_ut.cpp deleted file mode 100644 index 2845efb97ba..00000000000 --- a/library/cpp/monlib/counters/counters_ut.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include "counters.h" - -#include <library/cpp/testing/unittest/registar.h> - -#include <util/generic/set.h> -#include <util/thread/pool.h> - -using namespace NMonitoring; - -Y_UNIT_TEST_SUITE(TDeprecatedCountersTest) { - Y_UNIT_TEST(CounterGroupsAreThreadSafe) { - const static ui32 GROUPS_COUNT = 1000; - const static ui32 THREADS_COUNT = 10; - - TDeprecatedCounterGroups<ui32, ui32> groups; - - auto adder = [&groups]() { - for (ui32 id = 0; id < GROUPS_COUNT; id++) { - groups.Get(id); - - // adds contention - ::NanoSleep(42); - } - }; - - TThreadPool q; - q.Start(THREADS_COUNT); - for (ui32 i = 0; i < THREADS_COUNT; i++) { - q.SafeAddFunc(adder); - } - q.Stop(); - - // each group id is present - for (ui32 id = 0; id < GROUPS_COUNT; id++) { - UNIT_ASSERT(groups.Has(id)); - } - - // group names contains only appropriate ids - auto ids = groups.GetGroupsNames(); - for (ui32 id : *ids) { - UNIT_ASSERT(id < GROUPS_COUNT); - } - - // no duplication in group names - TSet<ui32> uniqueIds(ids->begin(), ids->end()); - UNIT_ASSERT_EQUAL(ids->size(), uniqueIds.size()); - UNIT_ASSERT_EQUAL(ids->size(), GROUPS_COUNT); - } -} diff --git a/library/cpp/monlib/counters/histogram.cpp b/library/cpp/monlib/counters/histogram.cpp deleted file mode 100644 index 46cf4e6ec8d..00000000000 --- a/library/cpp/monlib/counters/histogram.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "histogram.h" - -namespace NMonitoring { - void THistogramSnapshot::Print(IOutputStream* out) const { - (*out) << "mean: " << Mean - << ", stddev: " << StdDeviation - << ", min: " << Min - << ", max: " << Max - << ", 50%: " << Percentile50 - << ", 75%: " << Percentile75 - << ", 90%: " << Percentile90 - << ", 95%: " << Percentile95 - << ", 98%: " << Percentile98 - << ", 99%: " << Percentile99 - << ", 99.9%: " << Percentile999 - << ", count: " << TotalCount; - } - - void THdrHistogram::TakeSnaphot(THistogramSnapshot* snapshot) { - with_lock (Lock_) { - // TODO: get data with single traverse - snapshot->Mean = Data_.GetMean(); - snapshot->StdDeviation = Data_.GetStdDeviation(); - snapshot->Min = Data_.GetMin(); - snapshot->Max = Data_.GetMax(); - snapshot->Percentile50 = Data_.GetValueAtPercentile(50.0); - snapshot->Percentile75 = Data_.GetValueAtPercentile(75.0); - snapshot->Percentile90 = Data_.GetValueAtPercentile(90.0); - snapshot->Percentile95 = Data_.GetValueAtPercentile(95.0); - snapshot->Percentile98 = Data_.GetValueAtPercentile(98.0); - snapshot->Percentile99 = Data_.GetValueAtPercentile(99.0); - snapshot->Percentile999 = Data_.GetValueAtPercentile(99.9); - snapshot->TotalCount = Data_.GetTotalCount(); - - // cleanup histogram data - Data_.Reset(); - } - } - -} diff --git a/library/cpp/monlib/counters/histogram.h b/library/cpp/monlib/counters/histogram.h deleted file mode 100644 index 96361b00238..00000000000 --- a/library/cpp/monlib/counters/histogram.h +++ /dev/null @@ -1,189 +0,0 @@ -#pragma once - -#include <library/cpp/histogram/hdr/histogram.h> - -#include <util/system/spinlock.h> -#include <util/stream/output.h> - -namespace NMonitoring { - /** - * A statistical snapshot of values recorded in histogram. - */ - struct THistogramSnapshot { - double Mean; - double StdDeviation; - i64 Min; - i64 Max; - i64 Percentile50; - i64 Percentile75; - i64 Percentile90; - i64 Percentile95; - i64 Percentile98; - i64 Percentile99; - i64 Percentile999; - i64 TotalCount; - - void Print(IOutputStream* out) const; - }; - - /** - * Special counter which calculates the distribution of a value. - */ - class THdrHistogram { - public: - /** - * Construct a histogram given the Lowest and Highest values to be tracked - * and a number of significant decimal digits. Providing a - * lowestDiscernibleValue is useful in situations where the units used for - * the histogram's values are much smaller that the minimal accuracy - * required. E.g. when tracking time values stated in nanosecond units, - * where the minimal accuracy required is a microsecond, the proper value - * for lowestDiscernibleValue would be 1000. - * - * @param lowestDiscernibleValue The lowest value that can be discerned - * (distinguished from 0) by the histogram. Must be a positive - * integer that is >= 1. May be internally rounded down to nearest - * power of 2. - * - * @param highestTrackableValue The highest value to be tracked by the - * histogram. Must be a positive integer that is - * >= (2 * lowestDiscernibleValue). - * - * @param numberOfSignificantValueDigits Specifies the precision to use. - * This is the number of significant decimal digits to which the - * histogram will maintain value resolution and separation. Must be - * a non-negative integer between 0 and 5. - */ - THdrHistogram(i64 lowestDiscernibleValue, i64 highestTrackableValue, - i32 numberOfSignificantValueDigits) - : Data_(lowestDiscernibleValue, highestTrackableValue, - numberOfSignificantValueDigits) { - } - - /** - * Records a value in the histogram, will round this value of to a - * precision at or better than the NumberOfSignificantValueDigits specified - * at construction time. - * - * @param value Value to add to the histogram - * @return false if the value is larger than the HighestTrackableValue - * and can't be recorded, true otherwise. - */ - bool RecordValue(i64 value) { - with_lock (Lock_) { - return Data_.RecordValue(value); - } - } - - /** - * Records count values in the histogram, will round this value of to a - * precision at or better than the NumberOfSignificantValueDigits specified - * at construction time. - * - * @param value Value to add to the histogram - * @param count Number of values to add to the histogram - * @return false if the value is larger than the HighestTrackableValue - * and can't be recorded, true otherwise. - */ - bool RecordValues(i64 value, i64 count) { - with_lock (Lock_) { - return Data_.RecordValues(value, count); - } - } - - /** - * Records a value in the histogram and backfill based on an expected - * interval. Value will be rounded this to a precision at or better - * than the NumberOfSignificantValueDigits specified at contruction time. - * This is specifically used for recording latency. If the value is larger - * than the expectedInterval then the latency recording system has - * experienced co-ordinated omission. This method fills in the values that - * would have occured had the client providing the load not been blocked. - * - * @param value Value to add to the histogram - * @param expectedInterval The delay between recording values - * @return false if the value is larger than the HighestTrackableValue - * and can't be recorded, true otherwise. - */ - bool RecordValueWithExpectedInterval(i64 value, i64 expectedInterval) { - with_lock (Lock_) { - return Data_.RecordValueWithExpectedInterval(value, expectedInterval); - } - } - - /** - * Record a value in the histogram count times. Applies the same correcting - * logic as {@link THdrHistogram::RecordValueWithExpectedInterval}. - * - * @param value Value to add to the histogram - * @param count Number of values to add to the histogram - * @param expectedInterval The delay between recording values. - * @return false if the value is larger than the HighestTrackableValue - * and can't be recorded, true otherwise. - */ - bool RecordValuesWithExpectedInterval( - i64 value, i64 count, i64 expectedInterval) { - with_lock (Lock_) { - return Data_.RecordValuesWithExpectedInterval( - value, count, expectedInterval); - } - } - - /** - * @return The configured lowestDiscernibleValue - */ - i64 GetLowestDiscernibleValue() const { - with_lock (Lock_) { - return Data_.GetLowestDiscernibleValue(); - } - } - - /** - * @return The configured highestTrackableValue - */ - i64 GetHighestTrackableValue() const { - with_lock (Lock_) { - return Data_.GetHighestTrackableValue(); - } - } - - /** - * @return The configured numberOfSignificantValueDigits - */ - i32 GetNumberOfSignificantValueDigits() const { - with_lock (Lock_) { - return Data_.GetNumberOfSignificantValueDigits(); - } - } - - /** - * @return The total count of all recorded values in the histogram - */ - i64 GetTotalCount() const { - with_lock (Lock_) { - return Data_.GetTotalCount(); - } - } - - /** - * Place a copy of the value counts accumulated since the last snapshot - * was taken into {@code snapshot}. Calling this member-function will - * reset the value counts, and start accumulating value counts for the - * next interval. - * - * @param snapshot the structure into which the values should be copied. - */ - void TakeSnaphot(THistogramSnapshot* snapshot); - - private: - mutable TSpinLock Lock_; - NHdr::THistogram Data_; - }; - -} - -template <> -inline void Out<NMonitoring::THistogramSnapshot>( - IOutputStream& out, const NMonitoring::THistogramSnapshot& snapshot) { - snapshot.Print(&out); -} diff --git a/library/cpp/monlib/counters/histogram_ut.cpp b/library/cpp/monlib/counters/histogram_ut.cpp deleted file mode 100644 index 5a0800505ab..00000000000 --- a/library/cpp/monlib/counters/histogram_ut.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "histogram.h" - -#include <library/cpp/testing/unittest/registar.h> - -using namespace NMonitoring; - -Y_UNIT_TEST_SUITE(THistorgamTest) { - Y_UNIT_TEST(TakeSnapshot) { - THdrHistogram h(1, 10, 3); - UNIT_ASSERT(h.RecordValue(1)); - UNIT_ASSERT(h.RecordValue(2)); - UNIT_ASSERT(h.RecordValues(3, 10)); - UNIT_ASSERT(h.RecordValue(4)); - UNIT_ASSERT(h.RecordValue(5)); - - UNIT_ASSERT_EQUAL(h.GetTotalCount(), 14); - - THistogramSnapshot snapshot; - h.TakeSnaphot(&snapshot); - - UNIT_ASSERT_EQUAL(h.GetTotalCount(), 0); - - UNIT_ASSERT_EQUAL(snapshot.Min, 1); - UNIT_ASSERT_EQUAL(snapshot.Max, 5); - - // >>> a = [1, 2] + [3 for i in range(10)] + [4, 5] - // >>> numpy.mean(a) - // 3.0 - UNIT_ASSERT_DOUBLES_EQUAL(snapshot.Mean, 3.0, 1e-6); - - // >>> numpy.std(a) - // 0.84515425472851657 - UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.84515425472851657, 1e-6); - - // >>> [(p, round(numpy.percentile(a, p))) for p in [50, 75, 90, 95, 98, 99, 99.9, 100]] - // [(50, 3.0), (75, 3.0), (90, 4.0), (95, 4.0), (98, 5.0), (99, 5.0), (99.9, 5.0), (100, 5.0)] - UNIT_ASSERT_EQUAL(snapshot.Percentile50, 3); - UNIT_ASSERT_EQUAL(snapshot.Percentile75, 3); - UNIT_ASSERT_EQUAL(snapshot.Percentile90, 4); - UNIT_ASSERT_EQUAL(snapshot.Percentile95, 4); - UNIT_ASSERT_EQUAL(snapshot.Percentile98, 5); - UNIT_ASSERT_EQUAL(snapshot.Percentile99, 5); - UNIT_ASSERT_EQUAL(snapshot.Percentile999, 5); - - UNIT_ASSERT_EQUAL(snapshot.TotalCount, 14); - } -} diff --git a/library/cpp/monlib/counters/meter.cpp b/library/cpp/monlib/counters/meter.cpp deleted file mode 100644 index 6f15f173d11..00000000000 --- a/library/cpp/monlib/counters/meter.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#include "meter.h" - -namespace NMonitoring { -} diff --git a/library/cpp/monlib/counters/meter.h b/library/cpp/monlib/counters/meter.h deleted file mode 100644 index 12c10b4ca6a..00000000000 --- a/library/cpp/monlib/counters/meter.h +++ /dev/null @@ -1,285 +0,0 @@ -#pragma once - -#include <util/system/types.h> -#include <util/generic/noncopyable.h> -#include <library/cpp/deprecated/atomic/atomic.h> - -#include <chrono> -#include <cstdlib> -#include <cmath> - -namespace NMonitoring { - /** - * An exponentially-weighted moving average. - * - * @see <a href="http://www.teamquest.com/pdfs/whitepaper/ldavg1.pdf"> - * UNIX Load Average Part 1: How It Works</a> - * @see <a href="http://www.teamquest.com/pdfs/whitepaper/ldavg2.pdf"> - * UNIX Load Average Part 2: Not Your Average Average</a> - * @see <a href="http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average">EMA</a> - */ - class TMovingAverage { - public: - enum { - INTERVAL = 5 // in seconds - }; - - public: - /** - * Creates a new EWMA which is equivalent to the UNIX one minute load - * average and which expects to be ticked every 5 seconds. - * - * @return a one-minute EWMA - */ - static TMovingAverage OneMinute() { - static const double M1_ALPHA = 1 - std::exp(-INTERVAL / 60.0 / 1); - return {M1_ALPHA, std::chrono::seconds(INTERVAL)}; - } - - /** - * Creates a new EWMA which is equivalent to the UNIX five minute load - * average and which expects to be ticked every 5 seconds. - * - * @return a five-minute EWMA - */ - static TMovingAverage FiveMinutes() { - static const double M5_ALPHA = 1 - std::exp(-INTERVAL / 60.0 / 5); - return {M5_ALPHA, std::chrono::seconds(INTERVAL)}; - } - - /** - * Creates a new EWMA which is equivalent to the UNIX fifteen minute load - * average and which expects to be ticked every 5 seconds. - * - * @return a fifteen-minute EWMA - */ - static TMovingAverage FifteenMinutes() { - static const double M15_ALPHA = 1 - std::exp(-INTERVAL / 60.0 / 15); - return {M15_ALPHA, std::chrono::seconds(INTERVAL)}; - } - - /** - * Create a new EWMA with a specific smoothing constant. - * - * @param alpha the smoothing constant - * @param interval the expected tick interval - */ - TMovingAverage(double alpha, std::chrono::seconds interval) - : Initialized_(0) - , Rate_(0) - , Uncounted_(0) - , Alpha_(alpha) - , Interval_(std::chrono::nanoseconds(interval).count()) - { - } - - TMovingAverage(const TMovingAverage& rhs) - : Initialized_(AtomicGet(rhs.Initialized_)) - , Rate_(AtomicGet(rhs.Rate_)) - , Uncounted_(AtomicGet(rhs.Uncounted_)) - , Alpha_(rhs.Alpha_) - , Interval_(rhs.Interval_) - { - } - - TMovingAverage& operator=(const TMovingAverage& rhs) { - AtomicSet(Initialized_, AtomicGet(Initialized_)); - AtomicSet(Rate_, AtomicGet(rhs.Rate_)); - AtomicSet(Uncounted_, AtomicGet(rhs.Uncounted_)); - Alpha_ = rhs.Alpha_; - Interval_ = rhs.Interval_; - return *this; - } - - /** - * Update the moving average with a new value. - * - * @param n the new value - */ - void Update(ui64 n = 1) { - AtomicAdd(Uncounted_, n); - } - - /** - * Mark the passage of time and decay the current rate accordingly. - */ - void Tick() { - double instantRate = AtomicSwap(&Uncounted_, 0) / Interval_; - if (AtomicGet(Initialized_)) { - double rate = AsDouble(AtomicGet(Rate_)); - rate += (Alpha_ * (instantRate - rate)); - AtomicSet(Rate_, AsAtomic(rate)); - } else { - AtomicSet(Rate_, AsAtomic(instantRate)); - AtomicSet(Initialized_, 1); - } - } - - /** - * @return the rate in the seconds - */ - double GetRate() const { - double rate = AsDouble(AtomicGet(Rate_)); - return rate * std::nano::den; - } - - private: - static double AsDouble(TAtomicBase val) { - union { - double D; - TAtomicBase A; - } doubleAtomic; - doubleAtomic.A = val; - return doubleAtomic.D; - } - - static TAtomicBase AsAtomic(double val) { - union { - double D; - TAtomicBase A; - } doubleAtomic; - doubleAtomic.D = val; - return doubleAtomic.A; - } - - private: - TAtomic Initialized_; - TAtomic Rate_; - TAtomic Uncounted_; - double Alpha_; - double Interval_; - }; - - /** - * A meter metric which measures mean throughput and one-, five-, and - * fifteen-minute exponentially-weighted moving average throughputs. - */ - template <typename TClock> - class TMeterImpl: private TNonCopyable { - public: - TMeterImpl() - : StartTime_(TClock::now()) - , LastTick_(StartTime_.time_since_epoch().count()) - , Count_(0) - , OneMinuteRate_(TMovingAverage::OneMinute()) - , FiveMinutesRate_(TMovingAverage::FiveMinutes()) - , FifteenMinutesRate_(TMovingAverage::FifteenMinutes()) - { - } - - /** - * Mark the occurrence of events. - * - * @param n the number of events - */ - void Mark(ui64 n = 1) { - TickIfNecessary(); - AtomicAdd(Count_, n); - OneMinuteRate_.Update(n); - FiveMinutesRate_.Update(n); - FifteenMinutesRate_.Update(n); - } - - /** - * Returns the one-minute exponentially-weighted moving average rate at - * which events have occurred since the meter was created. - * - * This rate has the same exponential decay factor as the one-minute load - * average in the top Unix command. - * - * @return the one-minute exponentially-weighted moving average rate at - * which events have occurred since the meter was created - */ - double GetOneMinuteRate() const { - return OneMinuteRate_.GetRate(); - } - - /** - * Returns the five-minute exponentially-weighted moving average rate at - * which events have occurred since the meter was created. - * - * This rate has the same exponential decay factor as the five-minute load - * average in the top Unix command. - * - * @return the five-minute exponentially-weighted moving average rate at - * which events have occurred since the meter was created - */ - double GetFiveMinutesRate() const { - return FiveMinutesRate_.GetRate(); - } - - /** - * Returns the fifteen-minute exponentially-weighted moving average rate - * at which events have occurred since the meter was created. - * - * This rate has the same exponential decay factor as the fifteen-minute - * load average in the top Unix command. - * - * @return the fifteen-minute exponentially-weighted moving average rate - * at which events have occurred since the meter was created - */ - double GetFifteenMinutesRate() const { - return FifteenMinutesRate_.GetRate(); - } - - /** - * @return the mean rate at which events have occurred since the meter - * was created - */ - double GetMeanRate() const { - if (GetCount() == 0) { - return 0.0; - } - - auto now = TClock::now(); - std::chrono::duration<double> elapsedSeconds = now - StartTime_; - return GetCount() / elapsedSeconds.count(); - } - - /** - * @return the number of events which have been marked - */ - ui64 GetCount() const { - return AtomicGet(Count_); - } - - private: - void TickIfNecessary() { - static ui64 TICK_INTERVAL_NS = - std::chrono::nanoseconds( - std::chrono::seconds(TMovingAverage::INTERVAL)) - .count(); - - auto oldTickNs = AtomicGet(LastTick_); - auto newTickNs = TClock::now().time_since_epoch().count(); - ui64 elapsedNs = std::abs(newTickNs - oldTickNs); - - if (elapsedNs > TICK_INTERVAL_NS) { - // adjust to interval begining - newTickNs -= elapsedNs % TICK_INTERVAL_NS; - if (AtomicCas(&LastTick_, newTickNs, oldTickNs)) { - ui64 requiredTicks = elapsedNs / TICK_INTERVAL_NS; - for (ui64 i = 0; i < requiredTicks; ++i) { - OneMinuteRate_.Tick(); - FiveMinutesRate_.Tick(); - FifteenMinutesRate_.Tick(); - } - } - } - } - - private: - const typename TClock::time_point StartTime_; - TAtomic LastTick_; - TAtomic Count_; - TMovingAverage OneMinuteRate_; - TMovingAverage FiveMinutesRate_; - TMovingAverage FifteenMinutesRate_; - }; - - using TSystemMeter = TMeterImpl<std::chrono::system_clock>; - using TSteadyMeter = TMeterImpl<std::chrono::steady_clock>; - using THighResMeter = TMeterImpl<std::chrono::high_resolution_clock>; - using TMeter = THighResMeter; - -} diff --git a/library/cpp/monlib/counters/meter_ut.cpp b/library/cpp/monlib/counters/meter_ut.cpp deleted file mode 100644 index b507d16fbdd..00000000000 --- a/library/cpp/monlib/counters/meter_ut.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "meter.h" - -#include <library/cpp/testing/unittest/registar.h> - -using namespace NMonitoring; - -struct TMockClock { - using duration = std::chrono::nanoseconds; - using rep = duration::rep; - using period = duration::period; - using time_point = std::chrono::time_point<TMockClock, duration>; - - static time_point now() noexcept { - static int index = 0; - return index++ < 2 ? time_point() : time_point(std::chrono::seconds(10)); - } -}; - -using TMockMeter = TMeterImpl<TMockClock>; - -Y_UNIT_TEST_SUITE(TMeterTest) { - Y_UNIT_TEST(StartsOutWithNoRatesOrCount) { - TMeter meter; - UNIT_ASSERT_EQUAL(meter.GetCount(), 0L); - UNIT_ASSERT_DOUBLES_EQUAL(meter.GetMeanRate(), 0.0, 0.0001); - UNIT_ASSERT_DOUBLES_EQUAL(meter.GetOneMinuteRate(), 0.0, 0.0001); - UNIT_ASSERT_DOUBLES_EQUAL(meter.GetFiveMinutesRate(), 0.0, 0.0001); - UNIT_ASSERT_DOUBLES_EQUAL(meter.GetFifteenMinutesRate(), 0.0, 0.0001); - } - - Y_UNIT_TEST(MarksEventsAndUpdatesRatesAndCount) { - TMockMeter meter; - meter.Mark(); - meter.Mark(2); - UNIT_ASSERT_EQUAL(meter.GetCount(), 3L); - UNIT_ASSERT_DOUBLES_EQUAL(meter.GetMeanRate(), 0.3, 0.001); - UNIT_ASSERT_DOUBLES_EQUAL(meter.GetOneMinuteRate(), 0.1840, 0.0001); - UNIT_ASSERT_DOUBLES_EQUAL(meter.GetFiveMinutesRate(), 0.1966, 0.0001); - UNIT_ASSERT_DOUBLES_EQUAL(meter.GetFifteenMinutesRate(), 0.1988, 0.0001); - } -} diff --git a/library/cpp/monlib/counters/timer.h b/library/cpp/monlib/counters/timer.h deleted file mode 100644 index 03dfb35337f..00000000000 --- a/library/cpp/monlib/counters/timer.h +++ /dev/null @@ -1,176 +0,0 @@ -#pragma once - -#include "histogram.h" - -#include <util/generic/scope.h> - -#include <chrono> - -namespace NMonitoring { - /** - * A timer counter which aggregates timing durations and provides duration - * statistics in selected time resolution. - */ - template <typename TResolution> - class TTimerImpl { - public: - /** - * Construct a timer given the Lowest and Highest values to be tracked - * and a number of significant decimal digits. Providing a - * lowestDiscernibleValue is useful in situations where the units used for - * the timer's values are much smaller that the minimal accuracy - * required. E.g. when tracking time values stated in nanosecond units, - * where the minimal accuracy required is a microsecond, the proper value - * for lowestDiscernibleValue would be 1000. - * - * @param min The lowest value that can be discerned (distinguished from - * 0) by the timer. Must be a positive integer that is >= 1. - * May be internally rounded down to nearest power of 2. - * - * @param max The highest value to be tracked by the timer. Must be a - * positive integer that is >= (2 * min). - * - * @param numberOfSignificantValueDigits Specifies the precision to use. - * This is the number of significant decimal digits to which the - * timer will maintain value resolution and separation. Must be - * a non-negative integer between 0 and 5. - */ - TTimerImpl(ui64 min, ui64 max, i32 numberOfSignificantValueDigits = 3) - : TTimerImpl(TResolution(min), TResolution(max), - numberOfSignificantValueDigits) { - } - - /** - * Construct a timer given the Lowest and Highest values to be tracked - * and a number of significant decimal digits. - * - * @param min The lowest value that can be discerned (distinguished from - * 0) by the timer. - * - * @param max The highest value to be tracked by the histogram. Must be a - * positive integer that is >= (2 * min). - * - * @param numberOfSignificantValueDigits Specifies the precision to use. - */ - template <typename TDurationMin, typename TDurationMax> - TTimerImpl(TDurationMin min, TDurationMax max, - i32 numberOfSignificantValueDigits = 3) - : Histogram_(std::chrono::duration_cast<TResolution>(min).count(), - std::chrono::duration_cast<TResolution>(max).count(), - numberOfSignificantValueDigits) { - } - - /** - * Records a value in the timer with timer resulution. Recorded value will - * be rounded to a precision at or better than the - * NumberOfSignificantValueDigits specified at construction time. - * - * @param duration duration to add to the timer - * @return false if the value is larger than the max and can't be recorded, - * true otherwise. - */ - bool RecordValue(ui64 duration) { - return Histogram_.RecordValue(duration); - } - - /** - * Records a duration in the timer. Recorded value will be converted to - * the timer resulution and rounded to a precision at or better than the - * NumberOfSignificantValueDigits specified at construction time. - * - * @param duration duration to add to the timer - * @return false if the value is larger than the max and can't be recorded, - * true otherwise. - */ - template <typename TDuration> - bool RecordValue(TDuration duration) { - auto count = static_cast<ui64>( - std::chrono::duration_cast<TResolution>(duration).count()); - return RecordValue(count); - } - - /** - * Records count values in the timer with timer resulution. Recorded value will - * be rounded to a precision at or better than the - * NumberOfSignificantValueDigits specified at construction time. - * - * @param duration duration to add to the timer - * @param count number of values to add to the histogram - * @return false if the value is larger than the max and can't be recorded, - * true otherwise. - */ - bool RecordValues(ui64 duration, ui64 count) { - return Histogram_.RecordValues(duration, count); - } - - /** - * Measures a time of functor execution. - * - * @param fn functor whose duration should be timed - */ - template <typename TFunc> - void Measure(TFunc&& fn) { - using TClock = std::chrono::high_resolution_clock; - - auto start = TClock::now(); - - Y_SCOPE_EXIT(this, start) { - RecordValue(TClock::now() - start); - }; - - fn(); - } - - /** - * Place a copy of the value counts accumulated since the last snapshot - * was taken into {@code snapshot}. Calling this member-function will - * reset the value counts, and start accumulating value counts for the - * next interval. - * - * @param snapshot the structure into which the values should be copied. - */ - void TakeSnapshot(THistogramSnapshot* snapshot) { - Histogram_.TakeSnaphot(snapshot); - } - - private: - THdrHistogram Histogram_; - }; - - /** - * Timer template instantiations for certain time resolutions. - */ - using TTimerNs = TTimerImpl<std::chrono::nanoseconds>; - using TTimerUs = TTimerImpl<std::chrono::microseconds>; - using TTimerMs = TTimerImpl<std::chrono::milliseconds>; - using TTimerS = TTimerImpl<std::chrono::seconds>; - - /** - * A timing scope to record elapsed time since creation. - */ - template <typename TTimer, typename TFunc = std::function<void(std::chrono::high_resolution_clock::duration)>> - class TTimerScope { - using TClock = std::chrono::high_resolution_clock; - - public: - explicit TTimerScope(TTimer* timer, TFunc* callback = nullptr) - : Timer_(timer) - , StartTime_(TClock::now()) - , Callback_(callback) - { - } - - ~TTimerScope() { - TClock::duration duration = TClock::now() - StartTime_; - if (Callback_) { - (*Callback_)(duration); - } - Timer_->RecordValue(duration); - } - - private: - TTimer* Timer_; - TClock::time_point StartTime_; - TFunc* Callback_; - }; -} diff --git a/library/cpp/monlib/counters/timer_ut.cpp b/library/cpp/monlib/counters/timer_ut.cpp deleted file mode 100644 index c5cd07e89d5..00000000000 --- a/library/cpp/monlib/counters/timer_ut.cpp +++ /dev/null @@ -1,81 +0,0 @@ -#include "timer.h" - -#include <library/cpp/testing/unittest/registar.h> - -using namespace NMonitoring; -using namespace std::literals::chrono_literals; - -class TCallback { -public: - explicit TCallback(int value) - : Value_(value){}; - void operator()(std::chrono::high_resolution_clock::duration duration) { - Value_ = duration.count(); - }; - - int Value_; -}; - -Y_UNIT_TEST_SUITE(TTimerTest) { - Y_UNIT_TEST(RecordValue) { - TTimerNs timerNs(1ns, 1s); - UNIT_ASSERT(timerNs.RecordValue(10us)); - - TTimerUs timerUs(1us, 1s); - UNIT_ASSERT(timerUs.RecordValue(10us)); - - THistogramSnapshot snapshot; - timerNs.TakeSnapshot(&snapshot); - UNIT_ASSERT_EQUAL(snapshot.Min, 10000); - UNIT_ASSERT_EQUAL(snapshot.Max, 10007); - UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6); - - timerUs.TakeSnapshot(&snapshot); - UNIT_ASSERT_EQUAL(snapshot.Min, 10); - UNIT_ASSERT_EQUAL(snapshot.Max, 10); - UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6); - } - - Y_UNIT_TEST(Measure) { - TTimerNs timer(1ns, 1s); - timer.Measure([]() { - Sleep(TDuration::MilliSeconds(1)); - }); - THistogramSnapshot snapshot; - timer.TakeSnapshot(&snapshot); - - UNIT_ASSERT(snapshot.Min > std::chrono::nanoseconds(1ms).count()); - UNIT_ASSERT(snapshot.Max > std::chrono::nanoseconds(1ms).count()); - UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6); - } - - Y_UNIT_TEST(TimerScope) { - TTimerUs timer(1us, 1000s); - { - TTimerScope<TTimerUs> scope(&timer); - Sleep(TDuration::MilliSeconds(10)); - } - THistogramSnapshot snapshot; - timer.TakeSnapshot(&snapshot); - - UNIT_ASSERT(snapshot.Min > std::chrono::microseconds(10ms).count()); - UNIT_ASSERT(snapshot.Max > std::chrono::microseconds(10ms).count()); - UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6); - } - - Y_UNIT_TEST(TimerScopeWithCallback) { - TCallback callback(0); - TTimerUs timer(1us, 1000s); - { - TTimerScope<TTimerUs, TCallback> scope(&timer, &callback); - Sleep(TDuration::MilliSeconds(10)); - } - THistogramSnapshot snapshot; - timer.TakeSnapshot(&snapshot); - - UNIT_ASSERT(snapshot.Min > std::chrono::microseconds(10ms).count()); - UNIT_ASSERT(snapshot.Max > std::chrono::microseconds(10ms).count()); - UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6); - UNIT_ASSERT(callback.Value_ > std::chrono::microseconds(10ms).count()); - } -} diff --git a/library/cpp/monlib/encode/legacy_protobuf/legacy_proto_decoder.cpp b/library/cpp/monlib/encode/legacy_protobuf/legacy_proto_decoder.cpp deleted file mode 100644 index f87a2d7e8f4..00000000000 --- a/library/cpp/monlib/encode/legacy_protobuf/legacy_proto_decoder.cpp +++ /dev/null @@ -1,527 +0,0 @@ -#include "legacy_protobuf.h" - -#include <library/cpp/monlib/encode/legacy_protobuf/protos/metric_meta.pb.h> -#include <library/cpp/monlib/metrics/metric_consumer.h> -#include <library/cpp/monlib/metrics/labels.h> - -#include <util/generic/yexception.h> -#include <util/generic/maybe.h> -#include <util/datetime/base.h> -#include <util/string/split.h> - -#include <google/protobuf/reflection.h> - -#include <algorithm> - -#ifdef LEGACY_PB_TRACE -#define TRACE(msg) \ - Cerr << msg << Endl -#else -#define TRACE(...) ; -#endif - -namespace NMonitoring { - namespace { - using TMaybeMeta = TMaybe<NMonProto::TMetricMeta>; - - TString ReadLabelValue(const NProtoBuf::Message& msg, const NProtoBuf::FieldDescriptor* d, const NProtoBuf::Reflection& r) { - using namespace NProtoBuf; - - switch (d->type()) { - case FieldDescriptor::TYPE_UINT32: - return ::ToString(r.GetUInt32(msg, d)); - case FieldDescriptor::TYPE_UINT64: - return ::ToString(r.GetUInt64(msg, d)); - case FieldDescriptor::TYPE_STRING: - return r.GetString(msg, d); - case FieldDescriptor::TYPE_ENUM: { - auto val = r.GetEnumValue(msg, d); - auto* valDesc = d->enum_type()->FindValueByNumber(val); - return valDesc->name(); - } - - default: - ythrow yexception() << "type " << d->type_name() << " cannot be used as a field value"; - } - - return {}; - } - - double ReadFieldAsDouble(const NProtoBuf::Message& msg, const NProtoBuf::FieldDescriptor* d, const NProtoBuf::Reflection& r) { - using namespace NProtoBuf; - - switch (d->type()) { - case FieldDescriptor::TYPE_DOUBLE: - return r.GetDouble(msg, d); - case FieldDescriptor::TYPE_BOOL: - return r.GetBool(msg, d) ? 1 : 0; - case FieldDescriptor::TYPE_INT32: - return r.GetInt32(msg, d); - case FieldDescriptor::TYPE_INT64: - return r.GetInt64(msg, d); - case FieldDescriptor::TYPE_UINT32: - return r.GetUInt32(msg, d); - case FieldDescriptor::TYPE_UINT64: - return r.GetUInt64(msg, d); - case FieldDescriptor::TYPE_SINT32: - return r.GetInt32(msg, d); - case FieldDescriptor::TYPE_SINT64: - return r.GetInt64(msg, d); - case FieldDescriptor::TYPE_FIXED32: - return r.GetUInt32(msg, d); - case FieldDescriptor::TYPE_FIXED64: - return r.GetUInt64(msg, d); - case FieldDescriptor::TYPE_SFIXED32: - return r.GetInt32(msg, d); - case FieldDescriptor::TYPE_SFIXED64: - return r.GetInt64(msg, d); - case FieldDescriptor::TYPE_FLOAT: - return r.GetFloat(msg, d); - case FieldDescriptor::TYPE_ENUM: - return r.GetEnumValue(msg, d); - default: - ythrow yexception() << "type " << d->type_name() << " cannot be used as a field value"; - } - - return std::numeric_limits<double>::quiet_NaN(); - } - - double ReadRepeatedAsDouble(const NProtoBuf::Message& msg, const NProtoBuf::FieldDescriptor* d, const NProtoBuf::Reflection& r, size_t i) { - using namespace NProtoBuf; - - switch (d->type()) { - case FieldDescriptor::TYPE_DOUBLE: - return r.GetRepeatedDouble(msg, d, i); - case FieldDescriptor::TYPE_BOOL: - return r.GetRepeatedBool(msg, d, i) ? 1 : 0; - case FieldDescriptor::TYPE_INT32: - return r.GetRepeatedInt32(msg, d, i); - case FieldDescriptor::TYPE_INT64: - return r.GetRepeatedInt64(msg, d, i); - case FieldDescriptor::TYPE_UINT32: - return r.GetRepeatedUInt32(msg, d, i); - case FieldDescriptor::TYPE_UINT64: - return r.GetRepeatedUInt64(msg, d, i); - case FieldDescriptor::TYPE_SINT32: - return r.GetRepeatedInt32(msg, d, i); - case FieldDescriptor::TYPE_SINT64: - return r.GetRepeatedInt64(msg, d, i); - case FieldDescriptor::TYPE_FIXED32: - return r.GetRepeatedUInt32(msg, d, i); - case FieldDescriptor::TYPE_FIXED64: - return r.GetRepeatedUInt64(msg, d, i); - case FieldDescriptor::TYPE_SFIXED32: - return r.GetRepeatedInt32(msg, d, i); - case FieldDescriptor::TYPE_SFIXED64: - return r.GetRepeatedInt64(msg, d, i); - case FieldDescriptor::TYPE_FLOAT: - return r.GetRepeatedFloat(msg, d, i); - case FieldDescriptor::TYPE_ENUM: - return r.GetRepeatedEnumValue(msg, d, i); - default: - ythrow yexception() << "type " << d->type_name() << " cannot be used as a field value"; - } - - return std::numeric_limits<double>::quiet_NaN(); - } - - TString LabelFromField(const NProtoBuf::Message& msg, const TString& name) { - const auto* fieldDesc = msg.GetDescriptor()->FindFieldByName(name); - const auto* reflection = msg.GetReflection(); - Y_ENSURE(fieldDesc && reflection, "Unable to get meta for field " << name); - - auto s = ReadLabelValue(msg, fieldDesc, *reflection); - std::replace(std::begin(s), s.vend(), ' ', '_'); - - return s; - } - - TMaybeMeta MaybeGetMeta(const NProtoBuf::FieldOptions& opts) { - if (opts.HasExtension(NMonProto::Metric)) { - return opts.GetExtension(NMonProto::Metric); - } - - return Nothing(); - } - - class ILabelGetter: public TThrRefBase { - public: - enum class EType { - Fixed = 1, - Lazy = 2, - }; - - virtual TLabel Get(const NProtoBuf::Message&) = 0; - virtual EType Type() const = 0; - }; - - class TFixedLabel: public ILabelGetter { - public: - explicit TFixedLabel(TLabel&& l) - : Label_{std::move(l)} - { - TRACE("found fixed label " << l); - } - - EType Type() const override { - return EType::Fixed; - } - TLabel Get(const NProtoBuf::Message&) override { - return Label_; - } - - private: - TLabel Label_; - }; - - using TFunction = std::function<TLabel(const NProtoBuf::Message&)>; - - class TLazyLabel: public ILabelGetter { - public: - TLazyLabel(TFunction&& fn) - : Fn_{std::move(fn)} - { - TRACE("found lazy label"); - } - - EType Type() const override { - return EType::Lazy; - } - TLabel Get(const NProtoBuf::Message& msg) override { - return Fn_(msg); - } - - private: - TFunction Fn_; - }; - - class TDecoderContext { - public: - void Init(const NProtoBuf::Message* msg) { - Message_ = msg; - Y_ENSURE(Message_); - Reflection_ = msg->GetReflection(); - Y_ENSURE(Reflection_); - - for (auto it = Labels_.begin(); it != Labels_.end(); ++it) { - if ((*it)->Type() == ILabelGetter::EType::Lazy) { - auto l = (*it)->Get(Message()); - *it = ::MakeIntrusive<TFixedLabel>(std::move(l)); - } else { - auto l = (*it)->Get(Message()); - } - } - } - - void Clear() noexcept { - Message_ = nullptr; - Reflection_ = nullptr; - } - - TDecoderContext CreateChildFromMeta(const NMonProto::TMetricMeta& metricMeta, const TString& name, i64 repeatedIdx = -1) { - TDecoderContext child{*this}; - child.Clear(); - - if (metricMeta.HasCustomPath()) { - if (const auto& nodePath = metricMeta.GetCustomPath()) { - child.AppendPath(nodePath); - } - } else if (metricMeta.GetPath()) { - child.AppendPath(name); - } - - if (metricMeta.HasKeys()) { - child.ParseKeys(metricMeta.GetKeys(), repeatedIdx); - } - - return child; - } - - TDecoderContext CreateChildFromRepeatedScalar(const NMonProto::TMetricMeta& metricMeta, i64 repeatedIdx = -1) { - TDecoderContext child{*this}; - child.Clear(); - - if (metricMeta.HasKeys()) { - child.ParseKeys(metricMeta.GetKeys(), repeatedIdx); - } - - return child; - } - - TDecoderContext CreateChildFromEls(const TString& name, const NMonProto::TExtraLabelMetrics& metrics, size_t idx, TMaybeMeta maybeMeta) { - TDecoderContext child{*this}; - child.Clear(); - - auto usePath = [&maybeMeta] { - return !maybeMeta->HasPath() || maybeMeta->GetPath(); - }; - - if (!name.empty() && (!maybeMeta || usePath())) { - child.AppendPath(name); - } - - child.Labels_.push_back(::MakeIntrusive<TLazyLabel>( - [ labelName = metrics.GetlabelName(), idx, &metrics ](const auto&) { - const auto& val = metrics.Getvalues(idx); - TString labelVal; - const auto uintLabel = val.GetlabelValueUint(); - - if (uintLabel) { - labelVal = ::ToString(uintLabel); - } else { - labelVal = val.GetlabelValue(); - } - - return TLabel{labelName, labelVal}; - })); - - return child; - } - - void ParseKeys(TStringBuf keys, i64 repeatedIdx = -1) { - auto parts = StringSplitter(keys) - .Split(' ') - .SkipEmpty(); - - for (auto part : parts) { - auto str = part.Token(); - - TStringBuf lhs, rhs; - - const bool isDynamic = str.TrySplit(':', lhs, rhs); - const bool isIndexing = isDynamic && rhs == TStringBuf("#"); - - if (isIndexing) { - TRACE("parsed index labels"); - - // <label_name>:# means that we should use index of the repeated - // field as label value - Y_ENSURE(repeatedIdx != -1); - Labels_.push_back(::MakeIntrusive<TLazyLabel>([=](const auto&) { - return TLabel{lhs, ::ToString(repeatedIdx)}; - })); - } else if (isDynamic) { - TRACE("parsed dynamic labels"); - - // <label_name>:<field_name> means that we need to take label value - // later from message's field - Labels_.push_back(::MakeIntrusive<TLazyLabel>([=](const auto& msg) { - return TLabel{lhs, LabelFromField(msg, TString{rhs})}; - })); - } else if (str.TrySplit('=', lhs, rhs)) { - TRACE("parsed static labels"); - - // <label_name>=<label_value> stands for constant label - Labels_.push_back(::MakeIntrusive<TFixedLabel>(TLabel{lhs, rhs})); - } else { - ythrow yexception() << "Incorrect Keys format"; - } - } - } - - void AppendPath(TStringBuf fieldName) { - Path_ += '/'; - Path_ += fieldName; - } - - const TString& Path() const { - return Path_; - } - - TLabels Labels() const { - TLabels result; - for (auto&& l : Labels_) { - result.Add(l->Get(Message())); - } - - return result; - } - - const NProtoBuf::Message& Message() const { - Y_VERIFY_DEBUG(Message_); - return *Message_; - } - - const NProtoBuf::Reflection& Reflection() const { - return *Reflection_; - } - - private: - const NProtoBuf::Message* Message_{nullptr}; - const NProtoBuf::Reflection* Reflection_{nullptr}; - - TString Path_; - TVector<TIntrusivePtr<ILabelGetter>> Labels_; - }; - - class TDecoder { - public: - TDecoder(IMetricConsumer* consumer, const NProtoBuf::Message& message, TInstant timestamp) - : Consumer_{consumer} - , Message_{message} - , Timestamp_{timestamp} - { - } - - void Decode() const { - Consumer_->OnStreamBegin(); - DecodeToStream(); - Consumer_->OnStreamEnd(); - } - - void DecodeToStream() const { - DecodeImpl(Message_, {}); - } - - private: - static const NMonProto::TExtraLabelMetrics& ExtractExtraMetrics(TDecoderContext& ctx, const NProtoBuf::FieldDescriptor& f) { - const auto& parent = ctx.Message(); - const auto& reflection = ctx.Reflection(); - auto& subMessage = reflection.GetMessage(parent, &f); - - return dynamic_cast<const NMonProto::TExtraLabelMetrics&>(subMessage); - } - - void DecodeImpl(const NProtoBuf::Message& msg, TDecoderContext ctx) const { - std::vector<const NProtoBuf::FieldDescriptor*> fields; - - ctx.Init(&msg); - - ctx.Reflection().ListFields(msg, &fields); - - for (const auto* f : fields) { - Y_ENSURE(f); - - const auto& opts = f->options(); - const auto isMessage = f->type() == NProtoBuf::FieldDescriptor::TYPE_MESSAGE; - const auto isExtraLabelMetrics = isMessage && f->message_type()->full_name() == "NMonProto.TExtraLabelMetrics"; - const auto maybeMeta = MaybeGetMeta(opts); - - if (!(maybeMeta || isExtraLabelMetrics)) { - continue; - } - - if (isExtraLabelMetrics) { - const auto& extra = ExtractExtraMetrics(ctx, *f); - RecurseExtraLabelMetrics(ctx, extra, f->name(), maybeMeta); - } else if (isMessage) { - RecurseMessage(ctx, *maybeMeta, *f); - } else if (f->is_repeated()) { - RecurseRepeatedScalar(ctx, *maybeMeta, *f); - } else if (maybeMeta->HasType()) { - const auto val = ReadFieldAsDouble(msg, f, ctx.Reflection()); - const bool isRate = maybeMeta->GetType() == NMonProto::EMetricType::RATE; - WriteMetric(val, ctx, f->name(), isRate); - } - } - } - - void RecurseRepeatedScalar(TDecoderContext ctx, const NMonProto::TMetricMeta& meta, const NProtoBuf::FieldDescriptor& f) const { - auto&& msg = ctx.Message(); - auto&& reflection = ctx.Reflection(); - const bool isRate = meta.GetType() == NMonProto::EMetricType::RATE; - - // this is a repeated scalar field, which makes metric only if it's indexing - for (auto i = 0; i < reflection.FieldSize(msg, &f); ++i) { - auto subCtx = ctx.CreateChildFromRepeatedScalar(meta, i); - subCtx.Init(&msg); - auto val = ReadRepeatedAsDouble(msg, &f, reflection, i); - WriteMetric(val, subCtx, f.name(), isRate); - } - } - - void RecurseExtraLabelMetrics(TDecoderContext ctx, const NMonProto::TExtraLabelMetrics& msg, const TString& name, const TMaybeMeta& meta) const { - auto i = 0; - for (const auto& val : msg.Getvalues()) { - auto subCtx = ctx.CreateChildFromEls(name, msg, i++, meta); - subCtx.Init(&val); - - const bool isRate = val.Hastype() - ? val.Gettype() == NMonProto::EMetricType::RATE - : meta->GetType() == NMonProto::EMetricType::RATE; - - double metricVal{0}; - if (isRate) { - metricVal = val.GetlongValue(); - } else { - metricVal = val.GetdoubleValue(); - } - - WriteMetric(metricVal, subCtx, "", isRate); - - for (const auto& child : val.Getchildren()) { - RecurseExtraLabelMetrics(subCtx, child, "", meta); - } - } - } - - void RecurseMessage(TDecoderContext ctx, const NMonProto::TMetricMeta& metricMeta, const NProtoBuf::FieldDescriptor& f) const { - const auto& msg = ctx.Message(); - const auto& reflection = ctx.Reflection(); - - if (f.is_repeated()) { - TRACE("recurse into repeated message " << f.name()); - for (auto i = 0; i < reflection.FieldSize(msg, &f); ++i) { - auto& subMessage = reflection.GetRepeatedMessage(msg, &f, i); - DecodeImpl(subMessage, ctx.CreateChildFromMeta(metricMeta, f.name(), i)); - } - } else { - TRACE("recurse into message " << f.name()); - auto& subMessage = reflection.GetMessage(msg, &f); - DecodeImpl(subMessage, ctx.CreateChildFromMeta(metricMeta, f.name())); - } - } - - inline void WriteValue(ui64 value) const { - Consumer_->OnUint64(Timestamp_, value); - } - - inline void WriteValue(double value) const { - Consumer_->OnDouble(Timestamp_, value); - } - - void WriteMetric(double value, const TDecoderContext& ctx, const TString& name, bool isRate) const { - if (isRate) { - Consumer_->OnMetricBegin(EMetricType::RATE); - WriteValue(static_cast<ui64>(value)); - } else { - Consumer_->OnMetricBegin(EMetricType::GAUGE); - WriteValue(static_cast<double>(value)); - } - - Consumer_->OnLabelsBegin(); - - for (const auto& label : ctx.Labels()) { - Consumer_->OnLabel(label.Name(), label.Value()); - } - - const auto fullPath = name.empty() - ? ctx.Path() - : ctx.Path() + '/' + name; - - if (fullPath) { - Consumer_->OnLabel("path", fullPath); - } - - Consumer_->OnLabelsEnd(); - Consumer_->OnMetricEnd(); - } - - private: - IMetricConsumer* Consumer_{nullptr}; - const NProtoBuf::Message& Message_; - TInstant Timestamp_; - }; - - } - - void DecodeLegacyProto(const NProtoBuf::Message& data, IMetricConsumer* consumer, TInstant ts) { - Y_ENSURE(consumer); - TDecoder(consumer, data, ts).Decode(); - } - - void DecodeLegacyProtoToStream(const NProtoBuf::Message& data, IMetricConsumer* consumer, TInstant ts) { - Y_ENSURE(consumer); - TDecoder(consumer, data, ts).DecodeToStream(); - } -} diff --git a/library/cpp/monlib/encode/legacy_protobuf/legacy_protobuf.h b/library/cpp/monlib/encode/legacy_protobuf/legacy_protobuf.h deleted file mode 100644 index 7cf8985d656..00000000000 --- a/library/cpp/monlib/encode/legacy_protobuf/legacy_protobuf.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include <google/protobuf/message.h> -#include <util/datetime/base.h> - -namespace NMonitoring { - // Unsupported features of the original format: - // - histograms; - // - memOnly; - // - dropHost/ignorePath - - void DecodeLegacyProto(const NProtoBuf::Message& data, class IMetricConsumer* c, TInstant ts = TInstant::Zero()); - - /// Does not open/close consumer stream unlike the above function. - void DecodeLegacyProtoToStream(const NProtoBuf::Message& data, class IMetricConsumer* c, TInstant ts = TInstant::Zero()); -} diff --git a/library/cpp/monlib/encode/legacy_protobuf/legacy_protobuf_ut.cpp b/library/cpp/monlib/encode/legacy_protobuf/legacy_protobuf_ut.cpp deleted file mode 100644 index 53683cb39c6..00000000000 --- a/library/cpp/monlib/encode/legacy_protobuf/legacy_protobuf_ut.cpp +++ /dev/null @@ -1,422 +0,0 @@ -#include "legacy_protobuf.h" - -#include <library/cpp/testing/unittest/registar.h> - -#include <library/cpp/monlib/encode/legacy_protobuf/ut/test_cases.pb.h> -#include <library/cpp/monlib/encode/legacy_protobuf/protos/metric_meta.pb.h> - -#include <library/cpp/monlib/encode/protobuf/protobuf.h> -#include <library/cpp/monlib/encode/text/text.h> -#include <library/cpp/monlib/metrics/labels.h> - -#include <util/generic/algorithm.h> -#include <util/generic/hash_set.h> - -using namespace NMonitoring; - -TSimple MakeSimpleMessage() { - TSimple msg; - - msg.SetFoo(1); - msg.SetBar(2.); - msg.SetBaz(42.); - - return msg; -} - -IMetricEncoderPtr debugPrinter = EncoderText(&Cerr); - -namespace NMonitoring { - inline bool operator<(const TLabel& lhs, const TLabel& rhs) { - return lhs.Name() < rhs.Name() || - (lhs.Name() == rhs.Name() && lhs.Value() < rhs.Value()); - } - -} - -void SetLabelValue(NMonProto::TExtraLabelMetrics::TValue& val, TString s) { - val.SetlabelValue(s); -} - -void SetLabelValue(NMonProto::TExtraLabelMetrics::TValue& val, ui64 u) { - val.SetlabelValueUint(u); -} - -template <typename T, typename V> -NMonProto::TExtraLabelMetrics MakeExtra(TString labelName, V labelValue, T value, bool isDeriv) { - NMonProto::TExtraLabelMetrics metric; - auto* val = metric.Addvalues(); - - metric.SetlabelName(labelName); - SetLabelValue(*val, labelValue); - - if (isDeriv) { - val->SetlongValue(value); - } else { - val->SetdoubleValue(value); - } - - return metric; -} - -void AssertLabels(const TLabels& expected, const NProto::TMultiSample& actual) { - UNIT_ASSERT_EQUAL(actual.LabelsSize(), expected.Size()); - - TSet<TLabel> actualSet; - TSet<TLabel> expectedSet; - Transform(expected.begin(), expected.end(), std::inserter(expectedSet, expectedSet.end()), [] (auto&& l) { - return TLabel{l.Name(), l.Value()}; - }); - - const auto& l = actual.GetLabels(); - Transform(std::begin(l), std::end(l), std::inserter(actualSet, std::begin(actualSet)), - [](auto&& elem) -> TLabel { - return {elem.GetName(), elem.GetValue()}; - }); - - TVector<TLabel> diff; - SetSymmetricDifference(std::begin(expectedSet), std::end(expectedSet), - std::begin(actualSet), std::end(actualSet), std::back_inserter(diff)); - - if (diff.size() > 0) { - for (auto&& l : diff) { - Cerr << l << Endl; - } - - UNIT_FAIL("Labels don't match"); - } -} - -void AssertSimpleMessage(const NProto::TMultiSamplesList& samples, TString pathPrefix = "/") { - UNIT_ASSERT_EQUAL(samples.SamplesSize(), 3); - - THashSet<TString> expectedValues{pathPrefix + "Foo", pathPrefix + "Bar", pathPrefix + "Baz"}; - - for (const auto& s : samples.GetSamples()) { - UNIT_ASSERT_EQUAL(s.LabelsSize(), 1); - UNIT_ASSERT_EQUAL(s.PointsSize(), 1); - - const auto labelVal = s.GetLabels(0).GetValue(); - UNIT_ASSERT(expectedValues.contains(labelVal)); - - if (labelVal == pathPrefix + "Foo") { - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_DOUBLES_EQUAL(s.GetPoints(0).GetFloat64(), 1, 1e-6); - } else if (labelVal == pathPrefix + "Bar") { - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_DOUBLES_EQUAL(s.GetPoints(0).GetFloat64(), 2, 1e-6); - } else if (labelVal == pathPrefix + "Baz") { - UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE); - UNIT_ASSERT_EQUAL(s.GetPoints(0).GetUint64(), 42); - } - } -} - -Y_UNIT_TEST_SUITE(TLegacyProtoDecoderTest) { - Y_UNIT_TEST(SimpleProto) { - NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - auto msg = MakeSimpleMessage(); - DecodeLegacyProto(msg, e.Get()); - - AssertSimpleMessage(samples); - }; - - Y_UNIT_TEST(RepeatedProto) { - NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - auto simple = MakeSimpleMessage(); - TRepeated msg; - msg.AddMessages()->CopyFrom(simple); - - DecodeLegacyProto(msg, e.Get()); - - AssertSimpleMessage(samples); - } - - Y_UNIT_TEST(RepeatedProtoWithPath) { - NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - auto simple = MakeSimpleMessage(); - TRepeatedWithPath msg; - msg.AddNamespace()->CopyFrom(simple); - - DecodeLegacyProto(msg, e.Get()); - - AssertSimpleMessage(samples, "/Namespace/"); - } - - Y_UNIT_TEST(DeepNesting) { - NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - auto simple = MakeSimpleMessage(); - TRepeatedWithPath internal; - internal.AddNamespace()->CopyFrom(simple); - - TDeepNesting msg; - msg.MutableNested()->CopyFrom(internal); - - DecodeLegacyProto(msg, e.Get()); - - AssertSimpleMessage(samples, "/Namespace/"); - } - - Y_UNIT_TEST(Keys) { - NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - auto simple = MakeSimpleMessage(); - simple.SetLabel("my_label_value"); - - TNestedWithKeys msg; - msg.AddNamespace()->CopyFrom(simple); - - DecodeLegacyProto(msg, e.Get()); - - auto i = 0; - for (const auto& s : samples.GetSamples()) { - UNIT_ASSERT_EQUAL(s.LabelsSize(), 4); - - bool foundLabel = false; - bool foundFixed = false; - bool foundNumbered = false; - - for (const auto& label : s.GetLabels()) { - if (label.GetName() == "my_label") { - foundLabel = true; - UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "my_label_value"); - } else if (label.GetName() == "fixed_label") { - foundFixed = true; - UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "fixed_value"); - } else if (label.GetName() == "numbered") { - foundNumbered = true; - UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), ::ToString(i)); - } - } - - UNIT_ASSERT(foundLabel); - UNIT_ASSERT(foundFixed); - UNIT_ASSERT(foundNumbered); - } - } - - Y_UNIT_TEST(NonStringKeys) { - NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TNonStringKeys msg; - msg.SetFoo(42); - msg.SetEnum(ENUM); - msg.SetInt(43); - - TRepeatedNonStringKeys msgs; - msgs.AddNested()->CopyFrom(msg); - - DecodeLegacyProto(msgs, e.Get()); - - for (const auto& s : samples.GetSamples()) { - bool foundEnum = false; - bool foundInt = false; - - for (const auto& label : s.GetLabels()) { - if (label.GetName() == "enum") { - foundEnum = true; - UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "ENUM"); - } else if (label.GetName() == "int") { - foundInt = true; - UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "43"); - } - } - - UNIT_ASSERT(foundEnum); - UNIT_ASSERT(foundInt); - - UNIT_ASSERT_DOUBLES_EQUAL(s.GetPoints(0).GetFloat64(), 42, 1e-6); - } - } - - Y_UNIT_TEST(KeysFromNonLeafNodes) { - NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - auto simple = MakeSimpleMessage(); - simple.SetLabel("label_value"); - - TRepeatedWithName nested; - nested.SetName("my_name"); - nested.AddNested()->CopyFrom(simple); - - TKeysFromNonLeaf msg; - msg.AddNested()->CopyFrom(nested); - - DecodeLegacyProto(msg, e.Get()); - - AssertLabels({{"my_label", "label_value"}, {"path", "/Nested/Nested/Foo"}, {"name", "my_name"}}, samples.GetSamples(0)); - } - - Y_UNIT_TEST(SpacesAreGetReplaced) { - NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - auto simple = MakeSimpleMessage(); - simple.SetLabel("my label_value"); - - TNestedWithKeys msg; - msg.AddNamespace()->CopyFrom(simple); - - DecodeLegacyProto(msg, e.Get()); - - for (const auto& s : samples.GetSamples()) { - UNIT_ASSERT_EQUAL(s.LabelsSize(), 4); - - bool foundLabel = false; - - for (const auto& label : s.GetLabels()) { - if (label.GetName() == "my_label") { - foundLabel = true; - UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "my_label_value"); - } - } - - UNIT_ASSERT(foundLabel); - } - } - - Y_UNIT_TEST(ExtraLabels) { - NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TExtraLabels msg; - msg.MutableExtraAsIs()->CopyFrom(MakeExtra("label", "foo", 42, false)); - msg.MutableExtraDeriv()->CopyFrom(MakeExtra("deriv_label", "deriv_foo", 43, true)); - - DecodeLegacyProto(msg, e.Get()); - - UNIT_ASSERT_EQUAL(samples.SamplesSize(), 2); - { - auto s = samples.GetSamples(0); - AssertLabels({{"label", "foo"}, {"path", "/ExtraAsIs"}}, s); - - UNIT_ASSERT_EQUAL(s.PointsSize(), 1); - auto point = s.GetPoints(0); - UNIT_ASSERT_DOUBLES_EQUAL(point.GetFloat64(), 42, 1e-6); - } - - { - auto s = samples.GetSamples(1); - AssertLabels({{"deriv_label", "deriv_foo"}, {"path", "/ExtraDeriv"}}, s); - - UNIT_ASSERT_EQUAL(s.PointsSize(), 1); - auto point = s.GetPoints(0); - UNIT_ASSERT_EQUAL(point.GetUint64(), 43); - } - } - - Y_UNIT_TEST(NestedExtraLabels) { - NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TExtraLabels msg; - auto extra = MakeExtra("label", "foo", 42, false); - auto* val = extra.Mutablevalues(0); - { - auto child = MakeExtra("child1", "label1", 24, true); - child.Mutablevalues(0)->Settype(NMonProto::EMetricType::RATE); - val->Addchildren()->CopyFrom(child); - } - - { - auto child = MakeExtra("child2", 34, 23, false); - val->Addchildren()->CopyFrom(child); - } - msg.MutableExtraAsIs()->CopyFrom(extra); - - DecodeLegacyProto(msg, e.Get()); - - UNIT_ASSERT_EQUAL(samples.SamplesSize(), 3); - { - auto s = samples.GetSamples(0); - AssertLabels({{"label", "foo"}, {"path", "/ExtraAsIs"}}, s); - - UNIT_ASSERT_EQUAL(s.PointsSize(), 1); - auto point = s.GetPoints(0); - UNIT_ASSERT_DOUBLES_EQUAL(point.GetFloat64(), 42, 1e-6); - } - - { - auto s = samples.GetSamples(1); - AssertLabels({{"label", "foo"}, {"child1", "label1"}, {"path", "/ExtraAsIs"}}, s); - - UNIT_ASSERT_EQUAL(s.PointsSize(), 1); - auto point = s.GetPoints(0); - UNIT_ASSERT_EQUAL(point.GetUint64(), 24); - } - - { - auto s = samples.GetSamples(2); - AssertLabels({{"label", "foo"}, {"child2", "34"}, {"path", "/ExtraAsIs"}}, s); - - UNIT_ASSERT_EQUAL(s.PointsSize(), 1); - auto point = s.GetPoints(0); - UNIT_ASSERT_EQUAL(point.GetFloat64(), 23); - } - } - - Y_UNIT_TEST(RobotLabels) { - NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TNamedCounter responses; - responses.SetName("responses"); - responses.SetCount(42); - - TCrawlerCounters::TStatusCounters statusCounters; - statusCounters.AddZoraResponses()->CopyFrom(responses); - - TCrawlerCounters::TPolicyCounters policyCounters; - policyCounters.SetSubComponent("mySubComponent"); - policyCounters.SetName("myComponentName"); - policyCounters.SetZone("myComponentZone"); - policyCounters.MutableStatusCounters()->CopyFrom(statusCounters); - - TCrawlerCounters counters; - counters.SetComponent("myComponent"); - counters.AddPoliciesCounters()->CopyFrom(policyCounters); - - DecodeLegacyProto(counters, e.Get()); - UNIT_ASSERT_EQUAL(samples.SamplesSize(), 1); - auto s = samples.GetSamples(0); - AssertLabels({ - {"SubComponent", "mySubComponent"}, {"Policy", "myComponentName"}, {"Zone", "myComponentZone"}, - {"ZoraResponse", "responses"}, {"path", "/PoliciesCounters/StatusCounters/ZoraResponses/Count"}}, s); - } - - Y_UNIT_TEST(ZoraLabels) { - NProto::TMultiSamplesList samples; - IMetricEncoderPtr e = EncoderProtobuf(&samples); - - TTimeLogHist hist; - hist.AddBuckets(42); - hist.AddBuckets(0); - - TKiwiCounters counters; - counters.MutableTimes()->CopyFrom(hist); - - DecodeLegacyProto(counters, e.Get()); - - UNIT_ASSERT_EQUAL(samples.SamplesSize(), 2); - - auto s = samples.GetSamples(0); - AssertLabels({{"slot", "0"}, {"path", "/Times/Buckets"}}, s); - UNIT_ASSERT_EQUAL(s.PointsSize(), 1); - UNIT_ASSERT_EQUAL(s.GetPoints(0).GetUint64(), 42); - - s = samples.GetSamples(1); - AssertLabels({{"slot", "1"}, {"path", "/Times/Buckets"}}, s); - UNIT_ASSERT_EQUAL(s.GetPoints(0).GetUint64(), 0); - } -} diff --git a/library/cpp/monlib/encode/legacy_protobuf/ut/test_cases.proto b/library/cpp/monlib/encode/legacy_protobuf/ut/test_cases.proto deleted file mode 100644 index 37e901de48d..00000000000 --- a/library/cpp/monlib/encode/legacy_protobuf/ut/test_cases.proto +++ /dev/null @@ -1,90 +0,0 @@ -import "library/cpp/monlib/encode/legacy_protobuf/protos/metric_meta.proto"; - -message TSimple { - optional uint64 Foo = 1 [ (NMonProto.Metric).Type = GAUGE ]; - optional double Bar = 2 [ (NMonProto.Metric).Type = GAUGE ]; - optional double Baz = 3 [ (NMonProto.Metric).Type = RATE ]; - optional string Label = 4; -} - -message TRepeated { - repeated TSimple Messages = 1 [ (NMonProto.Metric).Path = false ]; -}; - -message TRepeatedWithPath { - repeated TSimple Namespace = 1 [ (NMonProto.Metric).Path = true ]; -}; - -message TNestedWithKeys { - repeated TSimple Namespace = 1 [ (NMonProto.Metric).Path = true, (NMonProto.Metric).Keys = "my_label:Label fixed_label=fixed_value numbered:#" ]; -}; - -message TDeepNesting { - optional TRepeatedWithPath Nested = 1 [ (NMonProto.Metric).Path = false ]; -}; - -enum EEnum { - MY = 1; - ENUM = 2; -}; - -message TNonStringKeys { - optional uint32 Foo = 1 [ (NMonProto.Metric).Type = GAUGE ]; - optional EEnum Enum = 2; - optional uint32 Int = 3; -}; - -message TRepeatedNonStringKeys { - repeated TNonStringKeys Nested = 1 [ (NMonProto.Metric).Path = true, (NMonProto.Metric).Keys = "enum:Enum int:Int" ]; -}; - -message TExtraLabels { - optional NMonProto.TExtraLabelMetrics ExtraAsIs = 1 [ (NMonProto.Metric).Type = GAUGE ]; - optional NMonProto.TExtraLabelMetrics ExtraDeriv = 2 [ (NMonProto.Metric).Type = RATE ]; -}; - -message TRepeatedWithName { - optional string Name = 1; - repeated TSimple Nested = 2 [ (NMonProto.Metric).Path = true, (NMonProto.Metric).Keys = "my_label:Label" ]; -}; - -message TKeysFromNonLeaf { - repeated TRepeatedWithName Nested = 1 [ (NMonProto.Metric).Path = true, (NMonProto.Metric).Keys = "name:Name" ]; -}; - - -message TNamedCounter { - optional string Name = 1; - optional uint64 Count = 2 [ (NMonProto.Metric).Type = RATE ]; -} - -message TCrawlerCounters { - message TStatusCounters { - repeated TNamedCounter ZoraResponses = 3 [ (NMonProto.Metric).Path = true, (NMonProto.Metric).Keys = "ZoraResponse:Name" ]; - repeated TNamedCounter FetcherResponses = 4 [ (NMonProto.Metric).Path = true, (NMonProto.Metric).Keys = "SpiderResponse:Name" ]; - repeated TNamedCounter RotorResponses = 5 [ (NMonProto.Metric).Path = true, (NMonProto.Metric).Keys = "SpiderResponse:Name" ]; - repeated TNamedCounter PDFetchResponses = 6 [ (NMonProto.Metric).Path = true, (NMonProto.Metric).Keys = "PDFetchResponse:Name" ]; - repeated TNamedCounter CalcResponses = 7 [ (NMonProto.Metric).Path = true, (NMonProto.Metric).Keys = "CalcResponse:Name" ]; - repeated TNamedCounter Responses = 8 [ (NMonProto.Metric).Path = true, (NMonProto.Metric).Keys = "AggregatedResponse:Name" ]; - } - - message TPolicyCounters { - optional string SubComponent = 1; - optional string Name = 2; - optional string Zone = 3; - - optional TStatusCounters StatusCounters = 4 [ (NMonProto.Metric).Path = true ]; - } - - optional string Component = 1; - repeated TPolicyCounters PoliciesCounters = 3 [ (NMonProto.Metric).Path = true, (NMonProto.Metric).Keys = "SubComponent:SubComponent Policy:Name Zone:Zone" ]; -} - -message TTimeLogHist { - optional uint32 MinBucketMillisec = 1; - repeated uint64 Buckets = 2 [ (NMonProto.Metric).Type = RATE, (NMonProto.Metric).Keys = "slot:#" ]; -} - -message TKiwiCounters { - optional TTimeLogHist Times = 22 [ (NMonProto.Metric).Path = true ]; -} diff --git a/library/cpp/monlib/encode/unistat/unistat.h b/library/cpp/monlib/encode/unistat/unistat.h deleted file mode 100644 index 300fb6270fa..00000000000 --- a/library/cpp/monlib/encode/unistat/unistat.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include <util/generic/fwd.h> -#include <util/datetime/base.h> - -namespace NMonitoring { - /// Decodes unistat-style metrics - /// https://wiki.yandex-team.ru/golovan/stat-handle - void DecodeUnistat(TStringBuf data, class IMetricConsumer* c, TStringBuf metricNameLabel = "sensor", TInstant ts = TInstant::Zero()); - - /// Assumes consumer's stream is open by the caller - void DecodeUnistatToStream(TStringBuf data, class IMetricConsumer* c, TStringBuf metricNameLabel = "sensor", TInstant ts = TInstant::Zero()); -} diff --git a/library/cpp/monlib/encode/unistat/unistat_decoder.cpp b/library/cpp/monlib/encode/unistat/unistat_decoder.cpp deleted file mode 100644 index 8c34dbefc0b..00000000000 --- a/library/cpp/monlib/encode/unistat/unistat_decoder.cpp +++ /dev/null @@ -1,255 +0,0 @@ -#include "unistat.h" - -#include <library/cpp/monlib/metrics/histogram_collector.h> -#include <library/cpp/monlib/metrics/labels.h> -#include <library/cpp/monlib/metrics/metric_type.h> -#include <library/cpp/monlib/metrics/metric_value.h> -#include <library/cpp/monlib/metrics/metric_consumer.h> - -#include <library/cpp/json/json_reader.h> - -#include <util/datetime/base.h> -#include <util/string/split.h> - -#include <contrib/libs/re2/re2/re2.h> - -using namespace NJson; - -const re2::RE2 NAME_RE{R"((?:[a-zA-Z0-9\.\-/@_]+_)+(?:[ad][vehmntx]{3}|summ|hgram|max))"}; - -namespace NMonitoring { - namespace { - bool IsNumber(const NJson::TJsonValue& j) { - switch (j.GetType()) { - case EJsonValueType::JSON_INTEGER: - case EJsonValueType::JSON_UINTEGER: - case EJsonValueType::JSON_DOUBLE: - return true; - - default: - return false; - } - } - - template <typename T> - T ExtractNumber(const TJsonValue& val) { - switch (val.GetType()) { - case EJsonValueType::JSON_INTEGER: - return static_cast<T>(val.GetInteger()); - case EJsonValueType::JSON_UINTEGER: - return static_cast<T>(val.GetUInteger()); - case EJsonValueType::JSON_DOUBLE: - return static_cast<T>(val.GetDouble()); - - default: - ythrow yexception() << "Expected number, but found " << val.GetType(); - } - } - - auto ExtractDouble = ExtractNumber<double>; - auto ExtractUi64 = ExtractNumber<ui64>; - - class THistogramBuilder { - public: - void Add(TBucketBound bound, TBucketValue value) { - /// XXX: yasm uses left-closed intervals, while in monlib we use right-closed ones, - /// so (-inf; 0) [0, 100) [100; +inf) - /// becomes (-inf; 0] (0, 100] (100; +inf) - /// but since we've already lost some information these no way to avoid this kind of error here - Bounds_.push_back(bound); - - /// this will always be 0 for the first bucket, - /// since there's no way to make (-inf; N) bucket in yasm - Values_.push_back(NextValue_); - - /// we will write this value into the next bucket so that [[0, 10], [100, 20], [200, 50]] - /// becomes (-inf; 0] -> 0; (0; 100] -> 10; (100; 200] -> 20; (200; +inf) -> 50 - NextValue_ = value; - } - - IHistogramSnapshotPtr Finalize() { - Bounds_.push_back(std::numeric_limits<TBucketBound>::max()); - Values_.push_back(NextValue_); - - Y_ENSURE(Bounds_.size() <= HISTOGRAM_MAX_BUCKETS_COUNT, - "Histogram is only allowed to have " << HISTOGRAM_MAX_BUCKETS_COUNT << " buckets, but has " << Bounds_.size()); - - return ExplicitHistogramSnapshot(Bounds_, Values_); - } - - public: - TBucketValue NextValue_ {0}; - TBucketBounds Bounds_; - TBucketValues Values_; - }; - - class TDecoderUnistat { - private: - public: - explicit TDecoderUnistat(IMetricConsumer* consumer, IInputStream* is, TStringBuf metricNameLabel, TInstant ts) - : Consumer_{consumer}, - MetricNameLabel(metricNameLabel), - Timestamp_{ts} { - ReadJsonTree(is, &Json_, /* throw */ true); - } - - void Decode() { - Y_ENSURE(Json_.IsArray(), "Expected array at the top level, but found " << Json_.GetType()); - - for (auto&& metric : Json_.GetArray()) { - Y_ENSURE(metric.IsArray(), "Metric must be an array"); - auto&& arr = metric.GetArray(); - Y_ENSURE(arr.size() == 2, "Metric must be an array of 2 elements"); - auto&& name = arr[0]; - auto&& value = arr[1]; - MetricContext_ = {}; - - ParseName(name.GetString()); - - if (value.IsArray()) { - OnHistogram(value); - } else if (IsNumber(value)) { - OnScalar(value); - } else { - ythrow yexception() << "Expected list or number, but found " << value.GetType(); - } - - WriteValue(); - } - } - - private: - void OnScalar(const TJsonValue& jsonValue) { - if (MetricContext_.IsDeriv) { - MetricContext_.Type = EMetricType::RATE; - MetricContext_.Value = TMetricValue{ExtractUi64(jsonValue)}; - } else { - MetricContext_.Type = EMetricType::GAUGE; - MetricContext_.Value = TMetricValue{ExtractDouble(jsonValue)}; - } - } - - void OnHistogram(const TJsonValue& jsonHist) { - if (MetricContext_.IsDeriv) { - MetricContext_.Type = EMetricType::HIST_RATE; - } else { - MetricContext_.Type = EMetricType::HIST; - } - - auto histogramBuilder = THistogramBuilder(); - - for (auto&& bucket : jsonHist.GetArray()) { - Y_ENSURE(bucket.IsArray(), "Expected an array, but found " << bucket.GetType()); - auto&& arr = bucket.GetArray(); - Y_ENSURE(arr.size() == 2, "Histogram bucket must be an array of 2 elements"); - const auto bound = ExtractDouble(arr[0]); - const auto weight = ExtractUi64(arr[1]); - histogramBuilder.Add(bound, weight); - } - - MetricContext_.Histogram = histogramBuilder.Finalize(); - MetricContext_.Value = TMetricValue{MetricContext_.Histogram.Get()}; - } - - bool IsDeriv(TStringBuf name) { - TStringBuf ignore, suffix; - name.RSplit('_', ignore, suffix); - - Y_ENSURE(suffix.size() >= 3 && suffix.size() <= 5, "Disallowed suffix value: " << suffix); - - if (suffix == TStringBuf("summ") || suffix == TStringBuf("hgram")) { - return true; - } else if (suffix == TStringBuf("max")) { - return false; - } - - return suffix[0] == 'd'; - } - - void ParseName(TStringBuf value) { - TVector<TStringBuf> parts; - StringSplitter(value).Split(';').SkipEmpty().Collect(&parts); - - Y_ENSURE(parts.size() >= 1 && parts.size() <= 16); - - TStringBuf name = parts.back(); - parts.pop_back(); - - Y_ENSURE(RE2::FullMatch(re2::StringPiece{name.data(), name.size()}, NAME_RE), - "Metric name " << name << " doesn't match regex " << NAME_RE.pattern()); - - MetricContext_.Name = name; - MetricContext_.IsDeriv = IsDeriv(MetricContext_.Name); - - for (auto tag : parts) { - TStringBuf n, v; - tag.Split('=', n, v); - Y_ENSURE(n && v, "Unexpected tag format in " << tag); - MetricContext_.Labels.Add(n, v); - } - } - - private: - void WriteValue() { - Consumer_->OnMetricBegin(MetricContext_.Type); - - Consumer_->OnLabelsBegin(); - Consumer_->OnLabel(MetricNameLabel, TString{MetricContext_.Name}); - for (auto&& l : MetricContext_.Labels) { - Consumer_->OnLabel(l.Name(), l.Value()); - } - - Consumer_->OnLabelsEnd(); - - switch (MetricContext_.Type) { - case EMetricType::GAUGE: - Consumer_->OnDouble(Timestamp_, MetricContext_.Value.AsDouble()); - break; - case EMetricType::RATE: - Consumer_->OnUint64(Timestamp_, MetricContext_.Value.AsUint64()); - break; - case EMetricType::HIST: - case EMetricType::HIST_RATE: - Consumer_->OnHistogram(Timestamp_, MetricContext_.Value.AsHistogram()); - break; - case EMetricType::LOGHIST: - case EMetricType::DSUMMARY: - case EMetricType::IGAUGE: - case EMetricType::COUNTER: - case EMetricType::UNKNOWN: - ythrow yexception() << "Unexpected metric type: " << MetricContext_.Type; - } - - Consumer_->OnMetricEnd(); - } - - private: - IMetricConsumer* Consumer_; - NJson::TJsonValue Json_; - TStringBuf MetricNameLabel; - TInstant Timestamp_; - - struct { - TStringBuf Name; - EMetricType Type{EMetricType::UNKNOWN}; - TMetricValue Value; - bool IsDeriv{false}; - TLabels Labels; - IHistogramSnapshotPtr Histogram; - } MetricContext_; - }; - - } - - void DecodeUnistat(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel, TInstant ts) { - c->OnStreamBegin(); - DecodeUnistatToStream(data, c, metricNameLabel, ts); - c->OnStreamEnd(); - } - - void DecodeUnistatToStream(TStringBuf data, IMetricConsumer* c, TStringBuf metricNameLabel, TInstant ts) { - TMemoryInput in{data.data(), data.size()}; - TDecoderUnistat decoder(c, &in, metricNameLabel, ts); - decoder.Decode(); - } -} diff --git a/library/cpp/monlib/encode/unistat/unistat_ut.cpp b/library/cpp/monlib/encode/unistat/unistat_ut.cpp deleted file mode 100644 index 04c84d30f68..00000000000 --- a/library/cpp/monlib/encode/unistat/unistat_ut.cpp +++ /dev/null @@ -1,238 +0,0 @@ -#include "unistat.h" - -#include <library/cpp/monlib/encode/protobuf/protobuf.h> -#include <library/cpp/monlib/metrics/labels.h> - -#include <library/cpp/testing/unittest/registar.h> - -using namespace NMonitoring; - -Y_UNIT_TEST_SUITE(TUnistatDecoderTest) { - Y_UNIT_TEST(MetricNameLabel) { - constexpr auto input = TStringBuf(R"([["something_axxx", 42]])"); - - NProto::TMultiSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - - DecodeUnistat(input, encoder.Get(), "metric_name_label"); - - UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1); - auto sample = samples.GetSamples(0); - - auto label = sample.GetLabels(0); - UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "metric_name_label"); - } - - Y_UNIT_TEST(ScalarMetric) { - constexpr auto input = TStringBuf(R"([["something_axxx", 42]])"); - - NProto::TMultiSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - - DecodeUnistat(input, encoder.Get()); - - UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1); - auto sample = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1); - UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1); - - auto label = sample.GetLabels(0); - auto point = sample.GetPoints(0); - UNIT_ASSERT_VALUES_EQUAL(point.GetFloat64(), 42.); - UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "sensor"); - UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "something_axxx"); - } - - Y_UNIT_TEST(OverriddenTags) { - constexpr auto input = TStringBuf(R"([["ctype=foo;prj=bar;custom_tag=qwe;something_axxx", 42]])"); - - NProto::TMultiSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - - DecodeUnistat(input, encoder.Get()); - - UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1); - auto sample = samples.GetSamples(0); - UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1); - UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 4); - - const auto& labels = sample.GetLabels(); - TLabels actual; - for (auto&& l : labels) { - actual.Add(l.GetName(), l.GetValue()); - } - - TLabels expected{{"ctype", "foo"}, {"prj", "bar"}, {"custom_tag", "qwe"}, {"sensor", "something_axxx"}}; - - UNIT_ASSERT_VALUES_EQUAL(actual.size(), expected.size()); - for (auto&& l : actual) { - UNIT_ASSERT(expected.Extract(l.Name())->Value() == l.Value()); - } - } - - Y_UNIT_TEST(ThrowsOnTopLevelObject) { - constexpr auto input = TStringBuf(R"({["something_axxx", 42]})"); - - NProto::TMultiSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - - UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception); - } - - Y_UNIT_TEST(ThrowsOnUnwrappedMetric) { - constexpr auto input = TStringBuf(R"(["something_axxx", 42])"); - - NProto::TMultiSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - - UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception); - } - - Y_UNIT_TEST(HistogramMetric) { - constexpr auto input = TStringBuf(R"([["something_hgram", [[0, 1], [200, 2], [500, 3]] ]])"); - - NProto::TMultiSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - - DecodeUnistat(input, encoder.Get()); - - auto sample = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::HIST_RATE); - UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1); - UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1); - - auto label = sample.GetLabels(0); - const auto point = sample.GetPoints(0); - const auto histogram = point.GetHistogram(); - const auto size = histogram.BoundsSize(); - UNIT_ASSERT_VALUES_EQUAL(size, 4); - - const TVector<double> expectedBounds {0, 200, 500, std::numeric_limits<double>::max()}; - const TVector<ui64> expectedValues {0, 1, 2, 3}; - - for (auto i = 0; i < 4; ++i) { - UNIT_ASSERT_VALUES_EQUAL(histogram.GetBounds(i), expectedBounds[i]); - UNIT_ASSERT_VALUES_EQUAL(histogram.GetValues(i), expectedValues[i]); - } - - UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "sensor"); - UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "something_hgram"); - } - - Y_UNIT_TEST(AbsoluteHistogram) { - constexpr auto input = TStringBuf(R"([["something_ahhh", [[0, 1], [200, 2], [500, 3]] ]])"); - - NProto::TMultiSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - - DecodeUnistat(input, encoder.Get()); - - auto sample = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::HISTOGRAM); - UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1); - UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1); - } - - Y_UNIT_TEST(AllowedMetricNames) { - NProto::TMultiSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - - { - constexpr auto input = TStringBuf(R"([["a/A-b/c_D/__G_dmmm", [[0, 1], [200, 2], [500, 3]] ]])"); - UNIT_ASSERT_NO_EXCEPTION(DecodeUnistat(input, encoder.Get())); - } - } - - Y_UNIT_TEST(DisallowedMetricNames) { - NProto::TMultiSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - - { - constexpr auto input = TStringBuf(R"([["someth!ng_ahhh", [[0, 1], [200, 2], [500, 3]] ]])"); - UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception); - } - - { - constexpr auto input = TStringBuf(R"([["foo_a", [[0, 1], [200, 2], [500, 3]] ]])"); - UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception); - } - - { - constexpr auto input = TStringBuf(R"([["foo_ahhh;tag=value", [[0, 1], [200, 2], [500, 3]] ]])"); - UNIT_ASSERT_EXCEPTION(DecodeUnistat(input, encoder.Get()), yexception); - } - } - - Y_UNIT_TEST(MultipleMetrics) { - constexpr auto input = TStringBuf(R"([["something_axxx", 42], ["some-other_dhhh", 53]])"); - - NProto::TMultiSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - - DecodeUnistat(input, encoder.Get()); - - UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 2); - auto sample = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1); - UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1); - - auto label = sample.GetLabels(0); - auto point = sample.GetPoints(0); - UNIT_ASSERT_VALUES_EQUAL(point.GetFloat64(), 42.); - UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "sensor"); - UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "something_axxx"); - - sample = samples.GetSamples(1); - UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::RATE); - UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1); - UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1); - - label = sample.GetLabels(0); - point = sample.GetPoints(0); - UNIT_ASSERT_VALUES_EQUAL(point.GetUint64(), 53); - UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "sensor"); - UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "some-other_dhhh"); - } - - Y_UNIT_TEST(UnderscoreName) { - constexpr auto input = TStringBuf(R"([["something_anything_dmmm", 42]])"); - - NProto::TMultiSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - DecodeUnistat(input, encoder.Get()); - - UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1); - auto sample = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::RATE); - UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1); - UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1); - - auto label = sample.GetLabels(0); - auto point = sample.GetPoints(0); - UNIT_ASSERT_VALUES_EQUAL(point.GetUint64(), 42); - UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "sensor"); - UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "something_anything_dmmm"); - } - - Y_UNIT_TEST(MaxAggr) { - constexpr auto input = TStringBuf(R"([["something_anything_max", 42]])"); - - NProto::TMultiSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - DecodeUnistat(input, encoder.Get()); - - UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 1); - auto sample = samples.GetSamples(0); - UNIT_ASSERT_EQUAL(sample.GetMetricType(), NProto::GAUGE); - UNIT_ASSERT_VALUES_EQUAL(sample.PointsSize(), 1); - UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1); - - auto label = sample.GetLabels(0); - auto point = sample.GetPoints(0); - UNIT_ASSERT_VALUES_EQUAL(point.GetFloat64(), 42.); - UNIT_ASSERT_VALUES_EQUAL(label.GetName(), "sensor"); - UNIT_ASSERT_VALUES_EQUAL(label.GetValue(), "something_anything_max"); - } -} diff --git a/library/cpp/monlib/metrics/fwd.h b/library/cpp/monlib/metrics/fwd.h deleted file mode 100644 index b4327ee5d5e..00000000000 --- a/library/cpp/monlib/metrics/fwd.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -namespace NMonitoring { - - struct ILabel; - struct ILabels; - - class ICounter; - class IGauge; - class IHistogram; - class IIntGauge; - class ILazyCounter; - class ILazyGauge; - class ILazyIntGauge; - class ILazyRate; - class IMetric; - class IRate; - class TCounter; - class TGauge; - class THistogram; - class TIntGauge; - class TLazyCounter; - class TLazyGauge; - class TLazyIntGauge; - class TLazyRate; - class TRate; - - class IMetricSupplier; - class IMetricFactory; - class IMetricConsumer; - - class IMetricRegistry; - class TMetricRegistry; - - class IHistogramCollector; - class IHistogramSnapshot; - - class IExpMovingAverage; - -} // namespace NMonitoring diff --git a/library/cpp/monlib/service/auth/tvm/auth.cpp b/library/cpp/monlib/service/auth/tvm/auth.cpp deleted file mode 100644 index e071c11ebc8..00000000000 --- a/library/cpp/monlib/service/auth/tvm/auth.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include "auth.h" - -#include <util/generic/hash_set.h> - - -using namespace NTvmAuth; - - -namespace NMonitoring { -namespace { - template <class TTvmClientPtr = THolder<TTvmClient>> - class TTvmManager final: public ITvmManager { - public: - TTvmManager(NTvmApi::TClientSettings settings, TVector<TTvmId> clients, TLoggerPtr logger) - : AllowedClients_{clients.begin(), clients.end()} - , Tvm_(new TTvmClient{std::move(settings), std::move(logger)}) - { - } - - TTvmManager(NTvmTool::TClientSettings settings, TVector<TTvmId> clients, TLoggerPtr logger) - : AllowedClients_{clients.begin(), clients.end()} - , Tvm_(new TTvmClient{std::move(settings), std::move(logger)}) - { - } - - TTvmManager(TTvmClientPtr tvm, TVector<TTvmId> clients) - : AllowedClients_{clients.begin(), clients.end()} - , Tvm_(std::move(tvm)) - { - } - - bool IsAllowedClient(TTvmId clientId) override { - return AllowedClients_.contains(clientId); - } - - TCheckedServiceTicket CheckServiceTicket(TStringBuf ticket) override { - return Tvm_->CheckServiceTicket(ticket); - } - - private: - THashSet<TTvmId> AllowedClients_; - TTvmClientPtr Tvm_; - }; - - class TTvmAuthProvider final: public IAuthProvider { - public: - TTvmAuthProvider(THolder<ITvmManager> manager) - : TvmManager_{std::move(manager)} - { - } - - TAuthResult Check(const IHttpRequest& req) override { - auto ticketHeader = req.GetHeaders().FindHeader("X-Ya-Service-Ticket"); - if (!ticketHeader) { - return TAuthResult::NoCredentials(); - } - - const auto ticket = TvmManager_->CheckServiceTicket(ticketHeader->Value()); - if (!ticket) { - return TAuthResult::Denied(); - } - - return TvmManager_->IsAllowedClient(ticket.GetSrc()) - ? TAuthResult::Ok() - : TAuthResult::Denied(); - } - - private: - THolder<ITvmManager> TvmManager_; - }; -} // namespace - -THolder<ITvmManager> CreateDefaultTvmManager(NTvmApi::TClientSettings settings, TVector<TTvmId> allowedClients, TLoggerPtr logger) { - return MakeHolder<TTvmManager<>>(std::move(settings), std::move(allowedClients), std::move(logger)); -} - -THolder<ITvmManager> CreateDefaultTvmManager(NTvmTool::TClientSettings settings, TVector<TTvmId> allowedClients, TLoggerPtr logger) { - return MakeHolder<TTvmManager<>>(std::move(settings), std::move(allowedClients), std::move(logger)); -} - -THolder<ITvmManager> CreateDefaultTvmManager(TAtomicSharedPtr<NTvmAuth::TTvmClient> client, TVector<TTvmId> allowedClients) { - return MakeHolder<TTvmManager<TAtomicSharedPtr<NTvmAuth::TTvmClient>>>(std::move(client), std::move(allowedClients)); -} - -THolder<ITvmManager> CreateDefaultTvmManager(std::shared_ptr<NTvmAuth::TTvmClient> client, TVector<TTvmId> allowedClients) { - return MakeHolder<TTvmManager<std::shared_ptr<NTvmAuth::TTvmClient>>>(std::move(client), std::move(allowedClients)); -} - -THolder<IAuthProvider> CreateTvmAuth(THolder<ITvmManager> manager) { - return MakeHolder<TTvmAuthProvider>(std::move(manager)); -} - -} // namespace NMonitoring diff --git a/library/cpp/monlib/service/auth/tvm/auth.h b/library/cpp/monlib/service/auth/tvm/auth.h deleted file mode 100644 index 432beff9d6d..00000000000 --- a/library/cpp/monlib/service/auth/tvm/auth.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include <library/cpp/monlib/service/mon_service_http_request.h> -#include <library/cpp/monlib/service/auth.h> -#include <library/cpp/tvmauth/client/facade.h> - -namespace NMonitoring { - struct ITvmManager { - virtual ~ITvmManager() = default; - virtual bool IsAllowedClient(NTvmAuth::TTvmId clientId) = 0; - virtual NTvmAuth::TCheckedServiceTicket CheckServiceTicket(TStringBuf ticket) = 0; - }; - - THolder<ITvmManager> CreateDefaultTvmManager( - NTvmAuth::NTvmApi::TClientSettings settings, - TVector<NTvmAuth::TTvmId> allowedClients, - NTvmAuth::TLoggerPtr logger = NTvmAuth::TDevNullLogger::IAmBrave()); - - THolder<ITvmManager> CreateDefaultTvmManager( - NTvmAuth::NTvmTool::TClientSettings settings, - TVector<NTvmAuth::TTvmId> allowedClients, - NTvmAuth::TLoggerPtr logger = NTvmAuth::TDevNullLogger::IAmBrave()); - - THolder<ITvmManager> CreateDefaultTvmManager( - TAtomicSharedPtr<NTvmAuth::TTvmClient> client, - TVector<NTvmAuth::TTvmId> allowedClients); - - THolder<ITvmManager> CreateDefaultTvmManager( - std::shared_ptr<NTvmAuth::TTvmClient> client, - TVector<NTvmAuth::TTvmId> allowedClients); - - THolder<IAuthProvider> CreateTvmAuth(THolder<ITvmManager> tvmManager); -} // namespace NMonitoring |