aboutsummaryrefslogtreecommitdiffstats
path: root/library
diff options
context:
space:
mode:
authorarcadia-devtools <arcadia-devtools@yandex-team.ru>2022-06-21 11:31:57 +0300
committerarcadia-devtools <arcadia-devtools@yandex-team.ru>2022-06-21 11:31:57 +0300
commit444fb65b1a79c2c2ce6753cdfe18372203e1b44f (patch)
tree4314c153eff2e535c91c373cde41d486546514e3 /library
parent184eb704ed3f6064598640cc8cf23ab5af942a97 (diff)
downloadydb-444fb65b1a79c2c2ce6753cdfe18372203e1b44f.tar.gz
intermediate changes
ref:98c26ee2d3cd643aba98b8222f9f8a4c1773449a
Diffstat (limited to 'library')
-rw-r--r--library/cpp/monlib/metrics/metric.h6
-rw-r--r--library/cpp/monlib/metrics/metric_registry.cpp16
-rw-r--r--library/cpp/monlib/metrics/metric_registry.h16
-rw-r--r--library/cpp/monlib/metrics/metric_registry_ut.cpp41
4 files changed, 79 insertions, 0 deletions
diff --git a/library/cpp/monlib/metrics/metric.h b/library/cpp/monlib/metrics/metric.h
index d655ba44ec..cf736e8d80 100644
--- a/library/cpp/monlib/metrics/metric.h
+++ b/library/cpp/monlib/metrics/metric.h
@@ -362,6 +362,12 @@ namespace NMonitoring {
{
}
+ THistogram(std::function<IHistogramCollectorPtr()> makeHistogramCollector, bool isRate)
+ : IHistogram(isRate)
+ , Collector_(makeHistogramCollector())
+ {
+ }
+
void Record(double value) override {
Collector_->Collect(value);
}
diff --git a/library/cpp/monlib/metrics/metric_registry.cpp b/library/cpp/monlib/metrics/metric_registry.cpp
index 2c64a23ead..9a09a78d6b 100644
--- a/library/cpp/monlib/metrics/metric_registry.cpp
+++ b/library/cpp/monlib/metrics/metric_registry.cpp
@@ -124,6 +124,14 @@ namespace NMonitoring {
return Metric<THistogram, EMetricType::HIST>(std::move(labels), 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);
+ }
+
+ THistogram* TMetricRegistry::HistogramCounter(TLabels labels, nullptr_t) {
+ return HistogramCounter(std::move(labels), IHistogramCollectorPtr(nullptr));
+ }
+
THistogram* TMetricRegistry::HistogramRate(TLabels labels, IHistogramCollectorPtr collector) {
return Metric<THistogram, EMetricType::HIST_RATE>(std::move(labels), std::move(collector), true);
}
@@ -132,6 +140,14 @@ namespace NMonitoring {
return Metric<THistogram, EMetricType::HIST_RATE>(std::move(labels), 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);
+ }
+
+ THistogram* TMetricRegistry::HistogramRate(TLabels labels, nullptr_t) {
+ return HistogramRate(std::move(labels), IHistogramCollectorPtr(nullptr));
+ }
+
void TMetricRegistry::Reset() {
TWriteGuard g{*Lock_};
for (auto& [label, metric] : Metrics_) {
diff --git a/library/cpp/monlib/metrics/metric_registry.h b/library/cpp/monlib/metrics/metric_registry.h
index faba19e845..4000ed6803 100644
--- a/library/cpp/monlib/metrics/metric_registry.h
+++ b/library/cpp/monlib/metrics/metric_registry.h
@@ -86,6 +86,22 @@ namespace NMonitoring {
TLabels labels,
IHistogramCollectorPtr collector);
+ THistogram* HistogramCounter(
+ TLabels labels,
+ std::function<IHistogramCollectorPtr()> makeHistogramCollector);
+
+ THistogram* HistogramRate(
+ TLabels labels,
+ std::function<IHistogramCollectorPtr()> makeHistogramCollector);
+
+ THistogram* HistogramCounter(
+ TLabels labels,
+ nullptr_t);
+
+ THistogram* HistogramRate(
+ TLabels labels,
+ nullptr_t);
+
/**
* Set all registered metrics to zero
*/
diff --git a/library/cpp/monlib/metrics/metric_registry_ut.cpp b/library/cpp/monlib/metrics/metric_registry_ut.cpp
index 65fb2b3b38..0f0dbf0e6e 100644
--- a/library/cpp/monlib/metrics/metric_registry_ut.cpp
+++ b/library/cpp/monlib/metrics/metric_registry_ut.cpp
@@ -239,6 +239,47 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) {
UNIT_ASSERT_NO_DIFF(ss.Str(), NResource::Find("/histograms.json"));
}
+ Y_UNIT_TEST(HistogramsFabric) {
+ TMetricRegistry registry(TLabels{{"common", "label"}});
+ bool called = false;
+
+ auto collector = [&]() {
+ called = true;
+ return ExponentialHistogram(5, 2);
+ };
+
+ THistogram* h1 = registry.HistogramCounter(
+ {{"sensor", "readTimeMillis"}},
+ collector);
+
+ UNIT_ASSERT_VALUES_EQUAL(called, true);
+ called = false;
+
+ h1 = registry.HistogramCounter(
+ {{"sensor", "readTimeMillis"}},
+ collector);
+
+ UNIT_ASSERT_VALUES_EQUAL(called, false);
+
+ THistogram* h2 = registry.HistogramRate(
+ {{"sensor", "writeTimeMillis"}},
+ ExplicitHistogram({1, 5, 15, 20, 25}));
+
+ for (i64 i = 0; i < 100; i++) {
+ h1->Record(i);
+ h2->Record(i);
+ }
+
+ TStringStream ss;
+ {
+ auto encoder = EncoderJson(&ss, 2);
+ registry.Accept(TInstant::Zero(), encoder.Get());
+ }
+ ss << '\n';
+
+ UNIT_ASSERT_NO_DIFF(ss.Str(), NResource::Find("/histograms.json"));
+ }
+
Y_UNIT_TEST(StreamingEncoderTest) {
const TString expected {
"{\"commonLabels\":{\"common\":\"label\"},"