summaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/metrics
diff options
context:
space:
mode:
authormiktorius <[email protected]>2025-03-03 12:16:59 +0300
committermiktorius <[email protected]>2025-03-03 12:35:24 +0300
commite5e00e2a402f3028369f177f5919182949a71ba6 (patch)
treed06a7353708c3ed2fdf2db2a20eab8e7c6f24746 /library/cpp/monlib/metrics
parent5fc9035f13cbbee5e75a9f7933bb877454a40c24 (diff)
monlib : adding memOnly flag support for cpp lib
commit_hash:cffc55ecd6d0ea22c3c2ce52f21e6aba6da16a15
Diffstat (limited to 'library/cpp/monlib/metrics')
-rw-r--r--library/cpp/monlib/metrics/metric_consumer.h4
-rw-r--r--library/cpp/monlib/metrics/metric_registry.cpp182
-rw-r--r--library/cpp/monlib/metrics/metric_registry.h131
-rw-r--r--library/cpp/monlib/metrics/metric_registry_ut.cpp72
-rw-r--r--library/cpp/monlib/metrics/metric_sub_registry.h49
5 files changed, 391 insertions, 47 deletions
diff --git a/library/cpp/monlib/metrics/metric_consumer.h b/library/cpp/monlib/metrics/metric_consumer.h
index f7a727585ad..1152b51c4af 100644
--- a/library/cpp/monlib/metrics/metric_consumer.h
+++ b/library/cpp/monlib/metrics/metric_consumer.h
@@ -33,6 +33,10 @@ namespace NMonitoring {
virtual void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) = 0;
virtual void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) = 0;
virtual void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) = 0;
+
+ virtual void OnMemOnly(bool isMemOnly) {
+ Y_UNUSED(isMemOnly);
+ }
};
using IMetricConsumerPtr = THolder<IMetricConsumer>;
diff --git a/library/cpp/monlib/metrics/metric_registry.cpp b/library/cpp/monlib/metrics/metric_registry.cpp
index c747ae2b74f..245f65702d1 100644
--- a/library/cpp/monlib/metrics/metric_registry.cpp
+++ b/library/cpp/monlib/metrics/metric_registry.cpp
@@ -11,16 +11,17 @@ namespace NMonitoring {
}
template <typename TLabelsConsumer>
- void ConsumeMetric(TInstant time, IMetricConsumer* consumer, IMetric* metric, TLabelsConsumer&& labelsConsumer) {
+ void ConsumeMetric(TInstant time, IMetricConsumer* consumer, IMetric* metric, TLabelsConsumer&& labelsConsumer, TMetricOpts opts = {}) {
consumer->OnMetricBegin(metric->Type());
// (1) add labels
consumer->OnLabelsBegin();
labelsConsumer();
consumer->OnLabelsEnd();
-
// (2) add time and value
metric->Accept(time, consumer);
+ // (3) add flag
+ consumer->OnMemOnly(opts.MemOnly);
consumer->OnMetricEnd();
}
}
@@ -55,104 +56,177 @@ namespace NMonitoring {
}
TGauge* TMetricRegistry::Gauge(TLabels labels) {
- return Metric<TGauge, EMetricType::GAUGE>(std::move(labels));
+ return GaugeWithOpts(std::move(labels));
+ }
+ TGauge* TMetricRegistry::GaugeWithOpts(TLabels labels, TMetricOpts opts) {
+ return Metric<TGauge, EMetricType::GAUGE>(std::move(labels), std::move(opts));
}
TGauge* TMetricRegistry::Gauge(ILabelsPtr labels) {
- return Metric<TGauge, EMetricType::GAUGE>(std::move(labels));
+ return GaugeWithOpts(std::move(labels));
+ }
+ TGauge* TMetricRegistry::GaugeWithOpts(ILabelsPtr labels, TMetricOpts opts) {
+ return Metric<TGauge, EMetricType::GAUGE>(std::move(labels), std::move(opts));
}
TLazyGauge* TMetricRegistry::LazyGauge(TLabels labels, std::function<double()> supplier) {
- return Metric<TLazyGauge, EMetricType::GAUGE>(std::move(labels), std::move(supplier));
+ return LazyGaugeWithOpts(std::move(labels), std::move(supplier));
+ }
+ TLazyGauge* TMetricRegistry::LazyGaugeWithOpts(TLabels labels, std::function<double()> supplier, TMetricOpts opts) {
+ return Metric<TLazyGauge, EMetricType::GAUGE>(std::move(labels),std::move(opts), std::move(supplier));
}
TLazyGauge* TMetricRegistry::LazyGauge(ILabelsPtr labels, std::function<double()> supplier) {
- return Metric<TLazyGauge, EMetricType::GAUGE>(std::move(labels), std::move(supplier));
+ return LazyGaugeWithOpts(std::move(labels), std::move(supplier));
+ }
+ TLazyGauge* TMetricRegistry::LazyGaugeWithOpts(ILabelsPtr labels, std::function<double()> supplier, TMetricOpts opts) {
+ return Metric<TLazyGauge, EMetricType::GAUGE>(std::move(labels), std::move(opts), std::move(supplier));
}
TIntGauge* TMetricRegistry::IntGauge(TLabels labels) {
- return Metric<TIntGauge, EMetricType::IGAUGE>(std::move(labels));
+ return IntGaugeWithOpts(std::move(labels));
+ }
+ TIntGauge* TMetricRegistry::IntGaugeWithOpts(TLabels labels, TMetricOpts opts) {
+ return Metric<TIntGauge, EMetricType::IGAUGE>(std::move(labels), std::move(opts));
}
TIntGauge* TMetricRegistry::IntGauge(ILabelsPtr labels) {
- return Metric<TIntGauge, EMetricType::IGAUGE>(std::move(labels));
+ return IntGaugeWithOpts(std::move(labels));
+ }
+ TIntGauge* TMetricRegistry::IntGaugeWithOpts(ILabelsPtr labels, TMetricOpts opts) {
+ return Metric<TIntGauge, EMetricType::IGAUGE>(std::move(labels), std::move(opts));
}
TLazyIntGauge* TMetricRegistry::LazyIntGauge(TLabels labels, std::function<i64()> supplier) {
- return Metric<TLazyIntGauge, EMetricType::IGAUGE>(std::move(labels), std::move(supplier));
+ return LazyIntGaugeWithOpts(std::move(labels), std::move(supplier));
+ }
+ TLazyIntGauge* TMetricRegistry::LazyIntGaugeWithOpts(TLabels labels, std::function<i64()> supplier, TMetricOpts opts) {
+ return Metric<TLazyIntGauge, EMetricType::IGAUGE>(std::move(labels), std::move(opts), std::move(supplier));
}
TLazyIntGauge* TMetricRegistry::LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) {
- return Metric<TLazyIntGauge, EMetricType::IGAUGE>(std::move(labels), std::move(supplier));
+ return LazyIntGaugeWithOpts(std::move(labels), std::move(supplier));
+ }
+ TLazyIntGauge* TMetricRegistry::LazyIntGaugeWithOpts(ILabelsPtr labels, std::function<i64()> supplier, TMetricOpts opts) {
+ return Metric<TLazyIntGauge, EMetricType::IGAUGE>(std::move(labels), std::move(opts), std::move(supplier));
}
TCounter* TMetricRegistry::Counter(TLabels labels) {
- return Metric<TCounter, EMetricType::COUNTER>(std::move(labels));
+ return CounterWithOpts(std::move(labels));
+ }
+ TCounter* TMetricRegistry::CounterWithOpts(TLabels labels, TMetricOpts opts) {
+ return Metric<TCounter, EMetricType::COUNTER>(std::move(labels), std::move(opts));
}
TCounter* TMetricRegistry::Counter(ILabelsPtr labels) {
- return Metric<TCounter, EMetricType::COUNTER>(std::move(labels));
+ return CounterWithOpts(std::move(labels));
+ }
+ TCounter* TMetricRegistry::CounterWithOpts(ILabelsPtr labels, TMetricOpts opts) {
+ return Metric<TCounter, EMetricType::COUNTER>(std::move(labels), std::move(opts));
}
TLazyCounter* TMetricRegistry::LazyCounter(TLabels labels, std::function<ui64()> supplier) {
- return Metric<TLazyCounter, EMetricType::COUNTER>(std::move(labels), std::move(supplier));
+ return LazyCounterWithOpts(std::move(labels), std::move(supplier));
+ }
+ TLazyCounter* TMetricRegistry::LazyCounterWithOpts(TLabels labels, std::function<ui64()> supplier, TMetricOpts opts) {
+ return Metric<TLazyCounter, EMetricType::COUNTER>(std::move(labels), std::move(opts), std::move(supplier));
}
TLazyCounter* TMetricRegistry::LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) {
- return Metric<TLazyCounter, EMetricType::COUNTER>(std::move(labels), std::move(supplier));
+ return LazyCounterWithOpts(std::move(labels), std::move(supplier));
+ }
+ TLazyCounter* TMetricRegistry::LazyCounterWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts) {
+ return Metric<TLazyCounter, EMetricType::COUNTER>(std::move(labels), std::move(opts), std::move(supplier));
}
TRate* TMetricRegistry::Rate(TLabels labels) {
- return Metric<TRate, EMetricType::RATE>(std::move(labels));
+ return RateWithOpts(std::move(labels));
+ }
+ TRate* TMetricRegistry::RateWithOpts(TLabels labels, TMetricOpts opts) {
+ return Metric<TRate, EMetricType::RATE>(std::move(labels), std::move(opts));
}
TRate* TMetricRegistry::Rate(ILabelsPtr labels) {
- return Metric<TRate, EMetricType::RATE>(std::move(labels));
+ return RateWithOpts(std::move(labels));
+ }
+ TRate* TMetricRegistry::RateWithOpts(ILabelsPtr labels, TMetricOpts opts) {
+ return Metric<TRate, EMetricType::RATE>(std::move(labels), std::move(opts));
}
TLazyRate* TMetricRegistry::LazyRate(TLabels labels, std::function<ui64()> supplier) {
- return Metric<TLazyRate, EMetricType::RATE>(std::move(labels), std::move(supplier));
+ return LazyRateWithOpts(std::move(labels), std::move(supplier));
+ }
+ TLazyRate* TMetricRegistry::LazyRateWithOpts(TLabels labels, std::function<ui64()> supplier, TMetricOpts opts) {
+ return Metric<TLazyRate, EMetricType::RATE>(std::move(labels), std::move(opts), std::move(supplier));
}
TLazyRate* TMetricRegistry::LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) {
- return Metric<TLazyRate, EMetricType::RATE>(std::move(labels), std::move(supplier));
+ return LazyRateWithOpts(std::move(labels), std::move(supplier));
+ }
+ TLazyRate* TMetricRegistry::LazyRateWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts) {
+ return Metric<TLazyRate, EMetricType::RATE>(std::move(labels), std::move(opts), std::move(supplier));
}
THistogram* TMetricRegistry::HistogramCounter(TLabels labels, IHistogramCollectorPtr collector) {
- return Metric<THistogram, EMetricType::HIST>(std::move(labels), std::move(collector), false);
+ return HistogramCounterWithOpts(std::move(labels), std::move(collector));
+ }
+ THistogram* TMetricRegistry::HistogramCounterWithOpts(TLabels labels, IHistogramCollectorPtr collector, TMetricOpts opts) {
+ return Metric<THistogram, EMetricType::HIST>(std::move(labels), std::move(opts), std::move(collector), false);
}
THistogram* TMetricRegistry::HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) {
- return Metric<THistogram, EMetricType::HIST>(std::move(labels), std::move(collector), false);
+ return HistogramCounterWithOpts(std::move(labels), std::move(collector));
+ }
+ THistogram* TMetricRegistry::HistogramCounterWithOpts(ILabelsPtr labels, IHistogramCollectorPtr collector, TMetricOpts opts) {
+ return Metric<THistogram, EMetricType::HIST>(std::move(labels), std::move(opts), std::move(collector), false);
}
THistogram* TMetricRegistry::HistogramCounter(TLabels labels, std::function<IHistogramCollectorPtr()> supplier) {
- return Metric<THistogram, EMetricType::HIST>(std::move(labels), std::move(supplier), false);
+ return HistogramCounterWithOpts(std::move(labels), std::move(supplier));
+ }
+ THistogram* TMetricRegistry::HistogramCounterWithOpts(TLabels labels, std::function<IHistogramCollectorPtr()> supplier, TMetricOpts opts) {
+ return Metric<THistogram, EMetricType::HIST>(std::move(labels), std::move(opts), std::move(supplier), false);
}
THistogram* TMetricRegistry::HistogramCounter(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> supplier) {
- return Metric<THistogram, EMetricType::HIST>(std::move(labels), std::move(supplier), false);
+ return HistogramCounterWithOpts(std::move(labels), std::move(supplier));
+ }
+ THistogram* TMetricRegistry::HistogramCounterWithOpts(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> supplier, TMetricOpts opts) {
+ return Metric<THistogram, EMetricType::HIST>(std::move(labels), std::move(opts), std::move(supplier), false);
}
THistogram* TMetricRegistry::HistogramRate(TLabels labels, IHistogramCollectorPtr collector) {
- return Metric<THistogram, EMetricType::HIST_RATE>(std::move(labels), std::move(collector), true);
+ return HistogramRateWithOpts(std::move(labels), std::move(collector));
+ }
+ THistogram* TMetricRegistry::HistogramRateWithOpts(TLabels labels, IHistogramCollectorPtr collector, TMetricOpts opts) {
+ return Metric<THistogram, EMetricType::HIST_RATE>(std::move(labels), std::move(opts), std::move(collector), true);
}
THistogram* TMetricRegistry::HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) {
- return Metric<THistogram, EMetricType::HIST_RATE>(std::move(labels), std::move(collector), true);
+ return HistogramRateWithOpts(std::move(labels), std::move(collector));
+ }
+ THistogram* TMetricRegistry::HistogramRateWithOpts(ILabelsPtr labels, IHistogramCollectorPtr collector, TMetricOpts opts) {
+ return Metric<THistogram, EMetricType::HIST_RATE>(std::move(labels), std::move(opts), std::move(collector), true);
}
THistogram* TMetricRegistry::HistogramRate(TLabels labels, std::function<IHistogramCollectorPtr()> supplier) {
- return Metric<THistogram, EMetricType::HIST_RATE>(std::move(labels), std::move(supplier), true);
+ return HistogramRateWithOpts(std::move(labels), std::move(supplier));
+ }
+ THistogram* TMetricRegistry::HistogramRateWithOpts(TLabels labels, std::function<IHistogramCollectorPtr()> supplier, TMetricOpts opts) {
+ return Metric<THistogram, EMetricType::HIST_RATE>(std::move(labels), std::move(opts), std::move(supplier), true);
}
THistogram* TMetricRegistry::HistogramRate(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> supplier) {
- return Metric<THistogram, EMetricType::HIST_RATE>(std::move(labels), std::move(supplier), true);
+ return HistogramRateWithOpts(std::move(labels), std::move(supplier));
+ }
+ THistogram* TMetricRegistry::HistogramRateWithOpts(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> supplier, TMetricOpts opts) {
+ return Metric<THistogram, EMetricType::HIST_RATE>(std::move(labels), std::move(opts), std::move(supplier), true);
}
void TMetricRegistry::Reset() {
TWriteGuard g{*Lock_};
- for (auto& [label, metric] : Metrics_) {
+ for (auto& [label, metricValue] : Metrics_) {
+ auto metric = metricValue.Metric;
switch (metric->Type()) {
case EMetricType::GAUGE:
static_cast<TGauge*>(metric.Get())->Set(.0);
@@ -184,16 +258,19 @@ namespace NMonitoring {
}
template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args>
- TMetric* TMetricRegistry::Metric(TLabelsType&& labels, Args&&... args) {
+ TMetric* TMetricRegistry::Metric(TLabelsType&& labels, TMetricOpts&& opts, Args&&... args) {
{
TReadGuard g{*Lock_};
auto it = Metrics_.find(labels);
if (it != Metrics_.end()) {
- Y_ENSURE(it->second->Type() == type, "cannot create metric " << labels
+ Y_ENSURE(it->second.Metric->Type() == type, "cannot create metric " << labels
<< " with type " << MetricTypeToStr(type)
- << ", because registry already has same metric with type " << MetricTypeToStr(it->second->Type()));
- return static_cast<TMetric*>(it->second.Get());
+ << ", because registry already has same metric with type " << MetricTypeToStr(it->second.Metric->Type()));
+ Y_ENSURE(it->second.Opts.MemOnly == opts.MemOnly,"cannot create metric " << labels
+ << " with memOnly=" << opts.MemOnly
+ << ", because registry already has same metric with memOnly=" << it->second.Opts.MemOnly);
+ return static_cast<TMetric*>(it->second.Metric.Get());
}
}
@@ -202,14 +279,15 @@ namespace NMonitoring {
TWriteGuard g{*Lock_};
// decltype(Metrics_)::iterator breaks build on windows
- THashMap<ILabelsPtr, IMetricPtr>::iterator it;
+ THashMap<ILabelsPtr, TMetricValue>::iterator it;
+ TMetricValue metricValue = {metric, opts};
if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) {
- it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metric)).first;
+ it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metricValue)).first;
} else {
- it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metric)).first;
+ it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metricValue)).first;
}
- return static_cast<TMetric*>(it->second.Get());
+ return static_cast<TMetric*>(it->second.Metric.Get());
}
}
@@ -227,7 +305,7 @@ namespace NMonitoring {
consumer->OnLabelsEnd();
}
- TVector<std::pair<ILabelsPtr, IMetricPtr>> tmpMetrics;
+ TVector<std::pair<ILabelsPtr, TMetricValue>> tmpMetrics;
{
TReadGuard g{*Lock_};
@@ -239,10 +317,17 @@ namespace NMonitoring {
for (const auto& it: tmpMetrics) {
ILabels* labels = it.first.Get();
- IMetric* metric = it.second.Get();
- ConsumeMetric(time, consumer, metric, [&]() {
- ConsumeLabels(consumer, *labels);
- });
+ IMetric* metric = it.second.Metric.Get();
+ TMetricOpts opts = it.second.Opts;
+ ConsumeMetric(
+ time,
+ consumer,
+ metric,
+ [&]() {
+ ConsumeLabels(consumer, *labels);
+ },
+ opts
+ );
}
consumer->OnStreamEnd();
@@ -253,11 +338,18 @@ namespace NMonitoring {
for (const auto& it: Metrics_) {
ILabels* labels = it.first.Get();
- IMetric* metric = it.second.Get();
- ConsumeMetric(time, consumer, metric, [&]() {
- ConsumeLabels(consumer, CommonLabels_);
- ConsumeLabels(consumer, *labels);
- });
+ IMetric* metric = it.second.Metric.Get();
+ TMetricOpts opts = it.second.Opts;
+ ConsumeMetric(
+ time,
+ consumer,
+ metric,
+ [&]() {
+ ConsumeLabels(consumer, CommonLabels_);
+ ConsumeLabels(consumer, *labels);
+ },
+ opts
+ );
}
}
}
diff --git a/library/cpp/monlib/metrics/metric_registry.h b/library/cpp/monlib/metrics/metric_registry.h
index 7b3a7aab70e..f60467cf912 100644
--- a/library/cpp/monlib/metrics/metric_registry.h
+++ b/library/cpp/monlib/metrics/metric_registry.h
@@ -9,35 +9,109 @@
namespace NMonitoring {
+
+ struct TMetricOpts {
+ bool MemOnly = false;
+ };
+
class IMetricFactory {
public:
virtual ~IMetricFactory() = default;
virtual IGauge* Gauge(ILabelsPtr labels) = 0;
+ virtual IGauge* GaugeWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) {
+ Y_UNUSED(opts);
+ return Gauge(std::move(labels));
+ }
+
virtual ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) = 0;
+ virtual ILazyGauge* LazyGaugeWithOpts(ILabelsPtr labels, std::function<double()> supplier, TMetricOpts opts = {}) {
+ Y_UNUSED(opts);
+ return LazyGauge(std::move(labels), std::move(supplier));
+ }
+
virtual IIntGauge* IntGauge(ILabelsPtr labels) = 0;
+ virtual IIntGauge* IntGaugeWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) {
+ Y_UNUSED(opts);
+ return IntGauge(std::move(labels));
+ }
virtual ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) = 0;
+ virtual ILazyIntGauge* LazyIntGaugeWithOpts(ILabelsPtr labels, std::function<i64()> supplier, TMetricOpts opts = {}) {
+ Y_UNUSED(opts);
+ return LazyIntGauge(std::move(labels), std::move(supplier));
+ }
+
virtual ICounter* Counter(ILabelsPtr labels) = 0;
+ virtual ICounter* CounterWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) {
+ Y_UNUSED(opts);
+ return Counter(std::move(labels));
+ }
+
virtual ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) = 0;
+ virtual ILazyCounter* LazyCounterWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts = {}) {
+ Y_UNUSED(opts);
+ return LazyCounter(std::move(labels), std::move(supplier));
+ }
virtual IRate* Rate(ILabelsPtr labels) = 0;
+ virtual IRate* RateWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) {
+ Y_UNUSED(opts);
+ return Rate(std::move(labels));
+ }
+
virtual ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) = 0;
+ virtual ILazyRate* LazyRateWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts = {}) {
+ Y_UNUSED(opts);
+ return LazyRate(std::move(labels), std::move(supplier));
+ }
virtual IHistogram* HistogramCounter(
ILabelsPtr labels,
IHistogramCollectorPtr collector) = 0;
+ virtual IHistogram* HistogramCounterWithOpts(
+ ILabelsPtr labels,
+ IHistogramCollectorPtr collector,
+ TMetricOpts opts = {})
+ {
+ Y_UNUSED(opts);
+ return HistogramCounter(std::move(labels), std::move(collector));
+ }
virtual IHistogram* HistogramRate(
ILabelsPtr labels,
IHistogramCollectorPtr collector) = 0;
+ virtual IHistogram* HistogramRateWithOpts(
+ ILabelsPtr labels,
+ IHistogramCollectorPtr collector,
+ TMetricOpts opts = {})
+ {
+ Y_UNUSED(opts);
+ return HistogramRate(std::move(labels), std::move(collector));
+ }
virtual IHistogram* HistogramCounter(
ILabelsPtr labels,
std::function<IHistogramCollectorPtr()> makeHistogramCollector) = 0;
+ virtual IHistogram* HistogramCounterWithOpts(
+ ILabelsPtr labels,
+ std::function<IHistogramCollectorPtr()> makeHistogramCollector,
+ TMetricOpts opts = {})
+ {
+ Y_UNUSED(opts);
+ return HistogramCounter(std::move(labels), std::move(makeHistogramCollector));
+ }
virtual IHistogram* HistogramRate(
ILabelsPtr labels,
std::function<IHistogramCollectorPtr()> makeHistogramCollector) = 0;
+ virtual IHistogram* HistogramRateWithOpts(
+ ILabelsPtr labels,
+ std::function<IHistogramCollectorPtr()> makeHistogramCollector,
+ TMetricOpts opts = {})
+ {
+ Y_UNUSED(opts);
+ return HistogramRate(std::move(labels), std::move(makeHistogramCollector));
+ }
};
class IMetricSupplier {
@@ -79,29 +153,53 @@ namespace NMonitoring {
static std::shared_ptr<TMetricRegistry> SharedInstance();
TGauge* Gauge(TLabels labels);
+ TGauge* GaugeWithOpts(TLabels labels, TMetricOpts opts = {});
TLazyGauge* LazyGauge(TLabels labels, std::function<double()> supplier);
+ TLazyGauge* LazyGaugeWithOpts(TLabels labels, std::function<double()> supplier, TMetricOpts opts = {});
TIntGauge* IntGauge(TLabels labels);
+ TIntGauge* IntGaugeWithOpts(TLabels labels, TMetricOpts opts = {});
TLazyIntGauge* LazyIntGauge(TLabels labels, std::function<i64()> supplier);
+ TLazyIntGauge* LazyIntGaugeWithOpts(TLabels labels, std::function<i64()> supplier, TMetricOpts opts = {});
TCounter* Counter(TLabels labels);
+ TCounter* CounterWithOpts(TLabels labels, TMetricOpts opts = {});
TLazyCounter* LazyCounter(TLabels labels, std::function<ui64()> supplier);
+ TLazyCounter* LazyCounterWithOpts(TLabels labels, std::function<ui64()> supplier, TMetricOpts opts = {});
TRate* Rate(TLabels labels);
+ TRate* RateWithOpts(TLabels labels, TMetricOpts opts = {});
TLazyRate* LazyRate(TLabels labels, std::function<ui64()> supplier);
+ TLazyRate* LazyRateWithOpts(TLabels labels, std::function<ui64()> supplier, TMetricOpts opts = {});
THistogram* HistogramCounter(
TLabels labels,
IHistogramCollectorPtr collector);
+ THistogram* HistogramCounterWithOpts(
+ TLabels labels,
+ IHistogramCollectorPtr collector,
+ TMetricOpts opts = {});
THistogram* HistogramRate(
TLabels labels,
IHistogramCollectorPtr collector);
+ THistogram* HistogramRateWithOpts(
+ TLabels labels,
+ IHistogramCollectorPtr collector,
+ TMetricOpts opts = {});
THistogram* HistogramCounter(
TLabels labels,
std::function<IHistogramCollectorPtr()> makeHistogramCollector);
+ THistogram* HistogramCounterWithOpts(
+ TLabels labels,
+ std::function<IHistogramCollectorPtr()> makeHistogramCollector,
+ TMetricOpts opts = {});
THistogram* HistogramRate(
TLabels labels,
std::function<IHistogramCollectorPtr()> makeHistogramCollector);
+ THistogram* HistogramRateWithOpts(
+ TLabels labels,
+ std::function<IHistogramCollectorPtr()> makeHistogramCollector,
+ TMetricOpts opts = {});
/**
* Set all registered metrics to zero
@@ -123,36 +221,65 @@ namespace NMonitoring {
private:
TGauge* Gauge(ILabelsPtr labels) override;
+ TGauge* GaugeWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override;
TLazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override;
+ TLazyGauge* LazyGaugeWithOpts(ILabelsPtr labels, std::function<double()> supplier, TMetricOpts opts = {}) override;
TIntGauge* IntGauge(ILabelsPtr labels) override;
+ TIntGauge* IntGaugeWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override;
TLazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override;
+ TLazyIntGauge* LazyIntGaugeWithOpts(ILabelsPtr labels, std::function<i64()> supplier, TMetricOpts opts = {}) override;
TCounter* Counter(ILabelsPtr labels) override;
+ TCounter* CounterWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override;
TLazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override;
+ TLazyCounter* LazyCounterWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts = {}) override;
TRate* Rate(ILabelsPtr labels) override;
+ TRate* RateWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override;
TLazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override;
+ TLazyRate* LazyRateWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts = {}) override;
THistogram* HistogramCounter(
ILabelsPtr labels,
IHistogramCollectorPtr collector) override;
+ THistogram* HistogramCounterWithOpts(
+ ILabelsPtr labels,
+ IHistogramCollectorPtr collector,
+ TMetricOpts opts = {}) override;
THistogram* HistogramRate(
ILabelsPtr labels,
IHistogramCollectorPtr collector) override;
+ THistogram* HistogramRateWithOpts(
+ ILabelsPtr labels,
+ IHistogramCollectorPtr collector,
+ TMetricOpts opts = {}) override;
THistogram* HistogramCounter(
ILabelsPtr labels,
std::function<IHistogramCollectorPtr()> makeHistogramCollector) override;
+ THistogram* HistogramCounterWithOpts(
+ ILabelsPtr labels,
+ std::function<IHistogramCollectorPtr()> makeHistogramCollector,
+ TMetricOpts opts = {}) override;
THistogram* HistogramRate(
ILabelsPtr labels,
std::function<IHistogramCollectorPtr()> makeHistogramCollector) override;
+ THistogram* HistogramRateWithOpts(
+ ILabelsPtr labels,
+ std::function<IHistogramCollectorPtr()> makeHistogramCollector,
+ TMetricOpts opts = {}) override;
+
+ struct TMetricValue {
+ IMetricPtr Metric;
+ TMetricOpts Opts;
+ };
private:
THolder<TRWMutex> Lock_ = MakeHolder<TRWMutex>();
- THashMap<ILabelsPtr, IMetricPtr> Metrics_;
+ THashMap<ILabelsPtr, TMetricValue> Metrics_;
template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args>
- TMetric* Metric(TLabelsType&& labels, Args&&... args);
+ TMetric* Metric(TLabelsType&& labels, TMetricOpts&& opts, Args&&... args);
TLabels CommonLabels_;
};
diff --git a/library/cpp/monlib/metrics/metric_registry_ut.cpp b/library/cpp/monlib/metrics/metric_registry_ut.cpp
index 802c423264c..89e6d56146f 100644
--- a/library/cpp/monlib/metrics/metric_registry_ut.cpp
+++ b/library/cpp/monlib/metrics/metric_registry_ut.cpp
@@ -396,4 +396,76 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) {
UNIT_ASSERT(commonLabels[0].GetName() == "common");
UNIT_ASSERT(commonLabels[0].GetValue() == "label");
}
+
+ Y_UNIT_TEST(MemOnlyMetric) {
+ TMetricRegistry registry;
+ i64 int_val = 0;
+ double double_val = 0;
+
+ registry.GaugeWithOpts({{"some", "gauge"}}, {true});
+ UNIT_ASSERT_EXCEPTION(registry.Gauge({{"some", "gauge"}}), yexception);
+
+ registry.LazyGaugeWithOpts(
+ {{"some", "lazy_gauge"}},
+ [&double_val](){return double_val;},
+ {true});
+ UNIT_ASSERT_EXCEPTION(
+ registry.LazyGauge(
+ {{"some", "lazy_gauge"}},
+ [&double_val](){return double_val;}),
+ yexception);
+
+ registry.IntGaugeWithOpts({{"some", "int_gauge"}}, {true});
+ UNIT_ASSERT_EXCEPTION(registry.IntGauge({{"some", "int_gauge"}}), yexception);
+
+ registry.LazyIntGaugeWithOpts(
+ {{"some", "lazy_int_gauge"}},
+ [&int_val](){return int_val;},
+ {true});
+ UNIT_ASSERT_EXCEPTION(
+ registry.LazyIntGauge(
+ {{"some", "lazy_int_gauge"}},
+ [&int_val](){return int_val;}),
+ yexception);
+
+ registry.CounterWithOpts({{"some", "counter"}}, {true});
+ UNIT_ASSERT_EXCEPTION(registry.Counter({{"some", "counter"}}), yexception);
+
+ registry.LazyCounterWithOpts({{"some", "lazy_counter"}}, [&int_val](){return int_val;}, {true});
+ UNIT_ASSERT_EXCEPTION(
+ registry.LazyCounter(
+ {{"some", "lazy_counter"}},
+ [&int_val](){return int_val;}),
+ yexception);
+
+ registry.RateWithOpts({{"some", "rate"}}, {true});
+ UNIT_ASSERT_EXCEPTION(registry.Rate({{"some", "rate"}}), yexception);
+
+ registry.LazyRateWithOpts({{"some", "lazy_rate"}}, [&double_val](){return double_val;}, {true});
+ UNIT_ASSERT_EXCEPTION(
+ registry.LazyRate(
+ {{"some", "lazy_rate"}},
+ [&double_val](){return double_val;}),
+ yexception);
+
+ registry.HistogramCounterWithOpts(
+ {{"some", "histogram_counter"}},
+ ExplicitHistogram({1, 5, 15, 20, 25}),
+ {true});
+ UNIT_ASSERT_EXCEPTION(
+ registry.HistogramCounter(
+ {{"some", "histogram_counter"}},
+ ExplicitHistogram({1, 5, 15, 20, 25})),
+ yexception);
+
+ registry.HistogramRateWithOpts(
+ {{"some", "histogram_rate"}},
+ ExponentialHistogram(5, 2),
+ {true});
+ UNIT_ASSERT_EXCEPTION(
+ registry.HistogramRate(
+ {{"some", "histogram_rate"}},
+ ExponentialHistogram(5, 2)),
+ yexception);
+ }
}
diff --git a/library/cpp/monlib/metrics/metric_sub_registry.h b/library/cpp/monlib/metrics/metric_sub_registry.h
index d1860c00a91..c3481f338fd 100644
--- a/library/cpp/monlib/metrics/metric_sub_registry.h
+++ b/library/cpp/monlib/metrics/metric_sub_registry.h
@@ -33,61 +33,110 @@ public:
AddCommonLabels(labels.Get());
return DelegatePtr_->Gauge(std::move(labels));
}
+ IGauge* GaugeWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override {
+ AddCommonLabels(labels.Get());
+ return DelegatePtr_->GaugeWithOpts(std::move(labels), std::move(opts));
+ }
ILazyGauge* LazyGauge(ILabelsPtr labels, std::function<double()> supplier) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->LazyGauge(std::move(labels), std::move(supplier));
}
+ ILazyGauge* LazyGaugeWithOpts(ILabelsPtr labels, std::function<double()> supplier, TMetricOpts opts = {}) override {
+ AddCommonLabels(labels.Get());
+ return DelegatePtr_->LazyGaugeWithOpts(std::move(labels), std::move(supplier), std::move(opts));
+ }
IIntGauge* IntGauge(ILabelsPtr labels) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->IntGauge(std::move(labels));
}
+ IIntGauge* IntGaugeWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override {
+ AddCommonLabels(labels.Get());
+ return DelegatePtr_->IntGaugeWithOpts(std::move(labels), std::move(opts));
+ }
ILazyIntGauge* LazyIntGauge(ILabelsPtr labels, std::function<i64()> supplier) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->LazyIntGauge(std::move(labels), std::move(supplier));
}
+ ILazyIntGauge* LazyIntGaugeWithOpts(ILabelsPtr labels, std::function<i64()> supplier, TMetricOpts opts = {}) override {
+ AddCommonLabels(labels.Get());
+ return DelegatePtr_->LazyIntGaugeWithOpts(std::move(labels), std::move(supplier), std::move(opts));
+ }
ICounter* Counter(ILabelsPtr labels) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->Counter(std::move(labels));
}
+ ICounter* CounterWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override {
+ AddCommonLabels(labels.Get());
+ return DelegatePtr_->CounterWithOpts(std::move(labels), std::move(opts));
+ }
ILazyCounter* LazyCounter(ILabelsPtr labels, std::function<ui64()> supplier) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->LazyCounter(std::move(labels), std::move(supplier));
}
+ ILazyCounter* LazyCounterWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts = {}) override {
+ AddCommonLabels(labels.Get());
+ return DelegatePtr_->LazyCounterWithOpts(std::move(labels), std::move(supplier), std::move(opts));
+ }
IRate* Rate(ILabelsPtr labels) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->Rate(std::move(labels));
}
+ IRate* RateWithOpts(ILabelsPtr labels, TMetricOpts opts = {}) override {
+ AddCommonLabels(labels.Get());
+ return DelegatePtr_->RateWithOpts(std::move(labels), std::move(opts));
+ }
+
ILazyRate* LazyRate(ILabelsPtr labels, std::function<ui64()> supplier) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->LazyRate(std::move(labels), std::move(supplier));
}
+ ILazyRate* LazyRateWithOpts(ILabelsPtr labels, std::function<ui64()> supplier, TMetricOpts opts = {}) override {
+ AddCommonLabels(labels.Get());
+ return DelegatePtr_->LazyRateWithOpts(std::move(labels), std::move(supplier), std::move(opts));
+ }
IHistogram* HistogramCounter(ILabelsPtr labels, IHistogramCollectorPtr collector) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->HistogramCounter(std::move(labels), std::move(collector));
}
+ IHistogram* HistogramCounterWithOpts(ILabelsPtr labels, IHistogramCollectorPtr collector, TMetricOpts opts = {}) override {
+ AddCommonLabels(labels.Get());
+ return DelegatePtr_->HistogramCounterWithOpts(std::move(labels), std::move(collector), std::move(opts));
+ }
IHistogram* HistogramRate(ILabelsPtr labels, IHistogramCollectorPtr collector) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->HistogramRate(std::move(labels), std::move(collector));
}
+ IHistogram* HistogramRateWithOpts(ILabelsPtr labels, IHistogramCollectorPtr collector, TMetricOpts opts = {}) override {
+ AddCommonLabels(labels.Get());
+ return DelegatePtr_->HistogramRateWithOpts(std::move(labels), std::move(collector), std::move(opts));
+ }
IHistogram* HistogramCounter(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> collector) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->HistogramCounter(std::move(labels), std::move(collector));
}
+ IHistogram* HistogramCounterWithOpts(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> collector, TMetricOpts opts = {}) override {
+ AddCommonLabels(labels.Get());
+ return DelegatePtr_->HistogramCounterWithOpts(std::move(labels), std::move(collector), std::move(opts));
+ }
IHistogram* HistogramRate(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> collector) override {
AddCommonLabels(labels.Get());
return DelegatePtr_->HistogramRate(std::move(labels), std::move(collector));
}
+ IHistogram* HistogramRateWithOpts(ILabelsPtr labels, std::function<IHistogramCollectorPtr()> collector, TMetricOpts opts) override {
+ AddCommonLabels(labels.Get());
+ return DelegatePtr_->HistogramRateWithOpts(std::move(labels), std::move(collector), std::move(opts));
+ }
void Accept(TInstant time, IMetricConsumer* consumer) const override {
DelegatePtr_->Accept(time, consumer);