aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/dynamic_counters
diff options
context:
space:
mode:
authorSergey Polovko <sergey@polovko.me>2022-02-10 16:47:02 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:47:02 +0300
commit3e0b762a82514bac89c1dd6ea7211e381d8aa248 (patch)
treec2d1b379ecaf05ca8f11ed0b5da9d1a950e6e554 /library/cpp/monlib/dynamic_counters
parentab3783171cc30e262243a0227c86118f7080c896 (diff)
downloadydb-3e0b762a82514bac89c1dd6ea7211e381d8aa248.tar.gz
Restoring authorship annotation for Sergey Polovko <sergey@polovko.me>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/monlib/dynamic_counters')
-rw-r--r--library/cpp/monlib/dynamic_counters/contention_ut.cpp6
-rw-r--r--library/cpp/monlib/dynamic_counters/counters.cpp76
-rw-r--r--library/cpp/monlib/dynamic_counters/counters.h180
-rw-r--r--library/cpp/monlib/dynamic_counters/counters_ut.cpp222
-rw-r--r--library/cpp/monlib/dynamic_counters/encode.cpp214
-rw-r--r--library/cpp/monlib/dynamic_counters/encode.h26
-rw-r--r--library/cpp/monlib/dynamic_counters/encode_ut.cpp360
-rw-r--r--library/cpp/monlib/dynamic_counters/golovan_page.cpp8
-rw-r--r--library/cpp/monlib/dynamic_counters/golovan_page.h6
-rw-r--r--library/cpp/monlib/dynamic_counters/page.cpp84
-rw-r--r--library/cpp/monlib/dynamic_counters/page.h10
-rw-r--r--library/cpp/monlib/dynamic_counters/percentile/percentile_base.h2
-rw-r--r--library/cpp/monlib/dynamic_counters/percentile/ut/ya.make2
-rw-r--r--library/cpp/monlib/dynamic_counters/percentile/ya.make2
-rw-r--r--library/cpp/monlib/dynamic_counters/ut/ya.make30
-rw-r--r--library/cpp/monlib/dynamic_counters/ya.make38
16 files changed, 633 insertions, 633 deletions
diff --git a/library/cpp/monlib/dynamic_counters/contention_ut.cpp b/library/cpp/monlib/dynamic_counters/contention_ut.cpp
index 8798044ee3..9b6f8af254 100644
--- a/library/cpp/monlib/dynamic_counters/contention_ut.cpp
+++ b/library/cpp/monlib/dynamic_counters/contention_ut.cpp
@@ -28,7 +28,7 @@ Y_UNIT_TEST_SUITE(TDynamicCountersContentionTest) {
Thread.Join();
}
- void OnCounter(const TString& /*labelName*/, const TString& /*labelValue*/, const TCounterForPtr* /*counter*/) override {
+ void OnCounter(const TString& /*labelName*/, const TString& /*labelValue*/, const TCounterForPtr* /*counter*/) override {
Ev.Signal();
Response.Wait();
}
@@ -36,10 +36,10 @@ Y_UNIT_TEST_SUITE(TDynamicCountersContentionTest) {
void OnHistogram(const TString& /*labelName*/, const TString& /*labelValue*/, IHistogramSnapshotPtr /*snapshot*/, bool /*derivative*/) override {
}
- void OnGroupBegin(const TString& /*labelName*/, const TString& /*labelValue*/, const TDynamicCounters* /*group*/) override {
+ void OnGroupBegin(const TString& /*labelName*/, const TString& /*labelValue*/, const TDynamicCounters* /*group*/) override {
}
- void OnGroupEnd(const TString& /*labelName*/, const TString& /*labelValue*/, const TDynamicCounters* /*group*/) override {
+ void OnGroupEnd(const TString& /*labelName*/, const TString& /*labelValue*/, const TDynamicCounters* /*group*/) override {
}
private:
diff --git a/library/cpp/monlib/dynamic_counters/counters.cpp b/library/cpp/monlib/dynamic_counters/counters.cpp
index 3635d87d0d..96de74b3c9 100644
--- a/library/cpp/monlib/dynamic_counters/counters.cpp
+++ b/library/cpp/monlib/dynamic_counters/counters.cpp
@@ -1,7 +1,7 @@
-#include "counters.h"
-
-#include <library/cpp/monlib/service/pages/templates.h>
-
+#include "counters.h"
+
+#include <library/cpp/monlib/service/pages/templates.h>
+
#include <util/generic/cast.h>
using namespace NMonitoring;
@@ -45,14 +45,14 @@ namespace {
}
static constexpr TStringBuf INDENT = " ";
-
+
TDynamicCounters::TDynamicCounters(EVisibility vis)
{
Visibility_ = vis;
-}
+}
-TDynamicCounters::~TDynamicCounters() {
-}
+TDynamicCounters::~TDynamicCounters() {
+}
TDynamicCounters::TCounterPtr TDynamicCounters::GetExpiringCounter(const TString& value, bool derivative, EVisibility vis) {
return GetExpiringNamedCounter("sensor", value, derivative, vis);
@@ -72,20 +72,20 @@ TDynamicCounters::TCounterPtr TDynamicCounters::GetNamedCounter(const TString& n
THistogramPtr TDynamicCounters::GetHistogram(const TString& value, IHistogramCollectorPtr collector, bool derivative, EVisibility vis) {
return GetNamedHistogram("sensor", value, std::move(collector), derivative, vis);
-}
-
+}
+
THistogramPtr TDynamicCounters::GetNamedHistogram(const TString& name, const TString& value, IHistogramCollectorPtr collector, bool derivative, EVisibility vis) {
return AsHistogramRef(GetNamedCounterImpl<false, THistogramCounter>(name, value, std::move(collector), derivative, vis));
}
-
+
THistogramPtr TDynamicCounters::GetExpiringHistogram(const TString& value, IHistogramCollectorPtr collector, bool derivative, EVisibility vis) {
return GetExpiringNamedHistogram("sensor", value, std::move(collector), derivative, vis);
}
-
+
THistogramPtr TDynamicCounters::GetExpiringNamedHistogram(const TString& name, const TString& value, IHistogramCollectorPtr collector, bool derivative, EVisibility vis) {
return AsHistogramRef(GetNamedCounterImpl<true, TExpiringHistogramCounter>(name, value, std::move(collector), derivative, vis));
-}
-
+}
+
TDynamicCounters::TCounterPtr TDynamicCounters::FindCounter(const TString& value) const {
return FindNamedCounter("sensor", value);
}
@@ -113,7 +113,7 @@ void TDynamicCounters::RemoveNamedCounter(const TString& name, const TString &va
}
}
-TIntrusivePtr<TDynamicCounters> TDynamicCounters::GetSubgroup(const TString& name, const TString& value) {
+TIntrusivePtr<TDynamicCounters> TDynamicCounters::GetSubgroup(const TString& name, const TString& value) {
auto res = FindSubgroup(name, value);
if (!res) {
auto g = LockForUpdate("GetSubgroup", name, value);
@@ -128,7 +128,7 @@ TIntrusivePtr<TDynamicCounters> TDynamicCounters::GetSubgroup(const TString& nam
return res;
}
-TIntrusivePtr<TDynamicCounters> TDynamicCounters::FindSubgroup(const TString& name, const TString& value) const {
+TIntrusivePtr<TDynamicCounters> TDynamicCounters::FindSubgroup(const TString& name, const TString& value) const {
TReadGuard g(Lock);
const auto it = Counters.find({name, value});
return it != Counters.end() ? AsDynamicCounters(it->second) : nullptr;
@@ -183,7 +183,7 @@ void TDynamicCounters::RegisterSubgroup(const TString& name, const TString& valu
RegisterCountable(name, value, subgroup);
}
-void TDynamicCounters::OutputHtml(IOutputStream& os) const {
+void TDynamicCounters::OutputHtml(IOutputStream& os) const {
HTML(os) {
PRE() {
OutputPlainText(os);
@@ -191,7 +191,7 @@ void TDynamicCounters::OutputHtml(IOutputStream& os) const {
}
}
-void TDynamicCounters::EnumerateSubgroups(const std::function<void(const TString& name, const TString& value)>& output) const {
+void TDynamicCounters::EnumerateSubgroups(const std::function<void(const TString& name, const TString& value)>& output) const {
TReadGuard g(Lock);
for (const auto& [key, value] : Counters) {
if (AsDynamicCounters(value)) {
@@ -209,29 +209,29 @@ void TDynamicCounters::OutputPlainText(IOutputStream& os, const TString& indent)
for (const auto& [key, value] : snap) {
if (const auto counter = AsCounter(value)) {
- os << indent
+ os << indent
<< key.LabelName << '=' << key.LabelValue
- << ": " << counter->Val()
+ << ": " << counter->Val()
<< outputVisibilityMarker(counter->Visibility())
- << '\n';
+ << '\n';
} else if (const auto histogram = AsHistogram(value)) {
- os << indent
+ os << indent
<< key.LabelName << '=' << key.LabelValue
<< ":"
<< outputVisibilityMarker(histogram->Visibility())
<< "\n";
-
- auto snapshot = histogram->Snapshot();
- for (ui32 i = 0, count = snapshot->Count(); i < count; i++) {
+
+ auto snapshot = histogram->Snapshot();
+ for (ui32 i = 0, count = snapshot->Count(); i < count; i++) {
os << indent << INDENT << TStringBuf("bin=");
- TBucketBound bound = snapshot->UpperBound(i);
- if (bound == Max<TBucketBound>()) {
+ TBucketBound bound = snapshot->UpperBound(i);
+ if (bound == Max<TBucketBound>()) {
os << TStringBuf("inf");
- } else {
- os << bound;
- }
- os << ": " << snapshot->Value(i) << '\n';
- }
+ } else {
+ os << bound;
+ }
+ os << ": " << snapshot->Value(i) << '\n';
+ }
}
}
@@ -239,22 +239,22 @@ void TDynamicCounters::OutputPlainText(IOutputStream& os, const TString& indent)
if (const auto subgroup = AsDynamicCounters(value)) {
os << "\n";
os << indent << key.LabelName << "=" << key.LabelValue << ":\n";
- subgroup->OutputPlainText(os, indent + INDENT);
+ subgroup->OutputPlainText(os, indent + INDENT);
}
}
}
-void TDynamicCounters::Accept(const TString& labelName, const TString& labelValue, ICountableConsumer& consumer) const {
+void TDynamicCounters::Accept(const TString& labelName, const TString& labelValue, ICountableConsumer& consumer) const {
if (!IsVisible(Visibility(), consumer.Visibility())) {
return;
}
- consumer.OnGroupBegin(labelName, labelValue, this);
+ consumer.OnGroupBegin(labelName, labelValue, this);
for (auto& [key, value] : ReadSnapshot()) {
value->Accept(key.LabelName, key.LabelValue, consumer);
- }
- consumer.OnGroupEnd(labelName, labelValue, this);
-}
+ }
+ consumer.OnGroupEnd(labelName, labelValue, this);
+}
void TDynamicCounters::RemoveExpired() const {
if (AtomicGet(ExpiringCount) == 0) {
diff --git a/library/cpp/monlib/dynamic_counters/counters.h b/library/cpp/monlib/dynamic_counters/counters.h
index dc178cfbe0..bc78538ef8 100644
--- a/library/cpp/monlib/dynamic_counters/counters.h
+++ b/library/cpp/monlib/dynamic_counters/counters.h
@@ -1,24 +1,24 @@
#pragma once
-#include <library/cpp/monlib/counters/counters.h>
-#include <library/cpp/monlib/metrics/histogram_collector.h>
-
+#include <library/cpp/monlib/counters/counters.h>
+#include <library/cpp/monlib/metrics/histogram_collector.h>
+
#include <library/cpp/threading/light_rw_lock/lightrwlock.h>
#include <library/cpp/containers/stack_vector/stack_vec.h>
#include <util/generic/cast.h>
#include <util/generic/map.h>
#include <util/generic/ptr.h>
-#include <util/string/cast.h>
+#include <util/string/cast.h>
#include <util/system/rwlock.h>
-
+
#include <functional>
namespace NMonitoring {
- struct TCounterForPtr;
- struct TDynamicCounters;
- struct ICountableConsumer;
-
+ struct TCounterForPtr;
+ struct TDynamicCounters;
+ struct ICountableConsumer;
+
struct TCountableBase: public TAtomicRefCount<TCountableBase> {
// Private means that the object must not be serialized unless the consumer
@@ -31,12 +31,12 @@ namespace NMonitoring {
Private,
};
- virtual ~TCountableBase() {
- }
-
- virtual void Accept(
- const TString& labelName, const TString& labelValue,
- ICountableConsumer& consumer) const = 0;
+ virtual ~TCountableBase() {
+ }
+
+ virtual void Accept(
+ const TString& labelName, const TString& labelValue,
+ ICountableConsumer& consumer) const = 0;
virtual EVisibility Visibility() const {
return Visibility_;
@@ -56,64 +56,64 @@ namespace NMonitoring {
return true;
}
- struct ICountableConsumer {
- virtual ~ICountableConsumer() {
- }
-
- virtual void OnCounter(
- const TString& labelName, const TString& labelValue,
- const TCounterForPtr* counter) = 0;
-
- virtual void OnHistogram(
- const TString& labelName, const TString& labelValue,
+ struct ICountableConsumer {
+ virtual ~ICountableConsumer() {
+ }
+
+ virtual void OnCounter(
+ const TString& labelName, const TString& labelValue,
+ const TCounterForPtr* counter) = 0;
+
+ virtual void OnHistogram(
+ const TString& labelName, const TString& labelValue,
IHistogramSnapshotPtr snapshot, bool derivative) = 0;
-
- virtual void OnGroupBegin(
- const TString& labelName, const TString& labelValue,
- const TDynamicCounters* group) = 0;
-
- virtual void OnGroupEnd(
- const TString& labelName, const TString& labelValue,
- const TDynamicCounters* group) = 0;
+
+ virtual void OnGroupBegin(
+ const TString& labelName, const TString& labelValue,
+ const TDynamicCounters* group) = 0;
+
+ virtual void OnGroupEnd(
+ const TString& labelName, const TString& labelValue,
+ const TDynamicCounters* group) = 0;
virtual TCountableBase::EVisibility Visibility() const {
return TCountableBase::EVisibility::Unspecified;
}
- };
-
+ };
+
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4522) // multiple assignment operators specified
#endif // _MSC_VER
- struct TCounterForPtr: public TDeprecatedCounter, public TCountableBase {
+ struct TCounterForPtr: public TDeprecatedCounter, public TCountableBase {
TCounterForPtr(bool derivative = false, EVisibility vis = EVisibility::Public)
- : TDeprecatedCounter(0ULL, derivative)
- {
+ : TDeprecatedCounter(0ULL, derivative)
+ {
Visibility_ = vis;
- }
-
+ }
+
TCounterForPtr(const TCounterForPtr&) = delete;
TCounterForPtr& operator=(const TCounterForPtr& other) = delete;
- void Accept(
- const TString& labelName, const TString& labelValue,
- ICountableConsumer& consumer) const override {
+ void Accept(
+ const TString& labelName, const TString& labelValue,
+ ICountableConsumer& consumer) const override {
if (IsVisible(Visibility(), consumer.Visibility())) {
consumer.OnCounter(labelName, labelValue, this);
}
- }
-
+ }
+
TCountableBase::EVisibility Visibility() const override {
return Visibility_;
}
- using TDeprecatedCounter::operator++;
- using TDeprecatedCounter::operator--;
- using TDeprecatedCounter::operator+=;
- using TDeprecatedCounter::operator-=;
- using TDeprecatedCounter::operator=;
- using TDeprecatedCounter::operator!;
+ using TDeprecatedCounter::operator++;
+ using TDeprecatedCounter::operator--;
+ using TDeprecatedCounter::operator+=;
+ using TDeprecatedCounter::operator-=;
+ using TDeprecatedCounter::operator=;
+ using TDeprecatedCounter::operator!;
};
struct TExpiringCounter: public TCounterForPtr {
@@ -124,27 +124,27 @@ namespace NMonitoring {
}
void Reset() {
- TDeprecatedCounter::operator=(0);
+ TDeprecatedCounter::operator=(0);
}
};
- struct THistogramCounter: public TCountableBase {
+ struct THistogramCounter: public TCountableBase {
explicit THistogramCounter(
IHistogramCollectorPtr collector, bool derivative = true, EVisibility vis = EVisibility::Public)
- : Collector_(std::move(collector))
+ : Collector_(std::move(collector))
, Derivative_(derivative)
- {
+ {
Visibility_ = vis;
- }
-
- void Collect(i64 value) {
- Collector_->Collect(value);
- }
-
- void Collect(i64 value, ui32 count) {
- Collector_->Collect(value, count);
- }
-
+ }
+
+ void Collect(i64 value) {
+ Collector_->Collect(value);
+ }
+
+ void Collect(i64 value, ui32 count) {
+ Collector_->Collect(value, count);
+ }
+
void Collect(double value, ui32 count) {
Collector_->Collect(value, count);
}
@@ -153,34 +153,34 @@ namespace NMonitoring {
Collector_->Collect(snapshot);
}
- void Accept(
- const TString& labelName, const TString& labelValue,
- ICountableConsumer& consumer) const override
- {
+ void Accept(
+ const TString& labelName, const TString& labelValue,
+ ICountableConsumer& consumer) const override
+ {
if (IsVisible(Visibility(), consumer.Visibility())) {
consumer.OnHistogram(labelName, labelValue, Collector_->Snapshot(), Derivative_);
}
- }
-
+ }
+
void Reset() {
Collector_->Reset();
}
- IHistogramSnapshotPtr Snapshot() const {
- return Collector_->Snapshot();
- }
-
- private:
- IHistogramCollectorPtr Collector_;
+ IHistogramSnapshotPtr Snapshot() const {
+ return Collector_->Snapshot();
+ }
+
+ private:
+ IHistogramCollectorPtr Collector_;
bool Derivative_;
- };
-
+ };
+
struct TExpiringHistogramCounter: public THistogramCounter {
using THistogramCounter::THistogramCounter;
};
- using THistogramPtr = TIntrusivePtr<THistogramCounter>;
-
+ using THistogramPtr = TIntrusivePtr<THistogramCounter>;
+
#ifdef _MSC_VER
#pragma warning(pop)
#endif
@@ -203,8 +203,8 @@ namespace NMonitoring {
struct TChildId {
TString LabelName;
TString LabelValue;
- TChildId() {
- }
+ TChildId() {
+ }
TChildId(const TString& labelName, const TString& labelValue)
: LabelName(labelName)
, LabelValue(labelValue)
@@ -230,7 +230,7 @@ namespace NMonitoring {
/// XXX: hack for deferred removal of expired counters. Remove once Output* functions are not used for serialization
mutable TCounters Counters;
mutable TAtomic ExpiringCount = 0;
-
+
public:
TDynamicCounters(TCountableBase::EVisibility visibility = TCountableBase::EVisibility::Public);
@@ -306,7 +306,7 @@ namespace NMonitoring {
const TString& value,
bool derivative = false,
TCountableBase::EVisibility visibility = TCountableBase::EVisibility::Public);
-
+
THistogramPtr GetExpiringHistogram(
const TString& value,
IHistogramCollectorPtr collector,
@@ -344,15 +344,15 @@ namespace NMonitoring {
TIntrusivePtr<TDynamicCounters> subgroup);
void OutputHtml(IOutputStream& os) const;
- void EnumerateSubgroups(const std::function<void(const TString& name, const TString& value)>& output) const;
+ void EnumerateSubgroups(const std::function<void(const TString& name, const TString& value)>& output) const;
// mostly for debugging purposes -- use accept with encoder instead
void OutputPlainText(IOutputStream& os, const TString& indent = "") const;
- void Accept(
- const TString& labelName, const TString& labelValue,
- ICountableConsumer& consumer) const override;
-
+ void Accept(
+ const TString& labelName, const TString& labelValue,
+ ICountableConsumer& consumer) const override;
+
private:
TCounters Resign() {
TCounters counters;
diff --git a/library/cpp/monlib/dynamic_counters/counters_ut.cpp b/library/cpp/monlib/dynamic_counters/counters_ut.cpp
index 3591037e0a..512728be1d 100644
--- a/library/cpp/monlib/dynamic_counters/counters_ut.cpp
+++ b/library/cpp/monlib/dynamic_counters/counters_ut.cpp
@@ -1,101 +1,101 @@
-#include "counters.h"
-
+#include "counters.h"
+
#include <library/cpp/testing/unittest/registar.h>
-
-using namespace NMonitoring;
-
-class TCountersPrinter: public ICountableConsumer {
-public:
+
+using namespace NMonitoring;
+
+class TCountersPrinter: public ICountableConsumer {
+public:
TCountersPrinter(IOutputStream* out)
- : Out_(out)
- , Level_(0)
- {
- }
-
-private:
- void OnCounter(
- const TString& labelName, const TString& labelValue,
- const TCounterForPtr* counter) override {
- Indent(Out_, Level_)
- << labelName << ':' << labelValue
- << " = " << counter->Val() << '\n';
- }
-
- void OnHistogram(
- const TString& labelName, const TString& labelValue,
+ : Out_(out)
+ , Level_(0)
+ {
+ }
+
+private:
+ void OnCounter(
+ const TString& labelName, const TString& labelValue,
+ const TCounterForPtr* counter) override {
+ Indent(Out_, Level_)
+ << labelName << ':' << labelValue
+ << " = " << counter->Val() << '\n';
+ }
+
+ void OnHistogram(
+ const TString& labelName, const TString& labelValue,
IHistogramSnapshotPtr snapshot, bool /*derivative*/) override {
- Indent(Out_, Level_)
- << labelName << ':' << labelValue
- << " = " << *snapshot << '\n';
- }
-
- void OnGroupBegin(
- const TString& labelName, const TString& labelValue,
- const TDynamicCounters*) override {
- Indent(Out_, Level_++) << labelName << ':' << labelValue << " {\n";
- }
-
- void OnGroupEnd(
- const TString&, const TString&,
- const TDynamicCounters*) override {
- Indent(Out_, --Level_) << "}\n";
- }
-
+ Indent(Out_, Level_)
+ << labelName << ':' << labelValue
+ << " = " << *snapshot << '\n';
+ }
+
+ void OnGroupBegin(
+ const TString& labelName, const TString& labelValue,
+ const TDynamicCounters*) override {
+ Indent(Out_, Level_++) << labelName << ':' << labelValue << " {\n";
+ }
+
+ void OnGroupEnd(
+ const TString&, const TString&,
+ const TDynamicCounters*) override {
+ Indent(Out_, --Level_) << "}\n";
+ }
+
static IOutputStream& Indent(IOutputStream* out, int level) {
- for (int i = 0; i < level; i++) {
- out->Write(" ");
- }
- return *out;
- }
-
-private:
+ for (int i = 0; i < level; i++) {
+ out->Write(" ");
+ }
+ return *out;
+ }
+
+private:
IOutputStream* Out_;
- int Level_ = 0;
-};
-
+ int Level_ = 0;
+};
+
Y_UNIT_TEST_SUITE(TDynamicCountersTest) {
Y_UNIT_TEST(CountersConsumer) {
- TDynamicCounterPtr rootGroup(new TDynamicCounters());
-
- auto usersCounter = rootGroup->GetNamedCounter("users", "count");
- *usersCounter = 7;
-
- auto hostGroup = rootGroup->GetSubgroup("counters", "resources");
- auto cpuCounter = hostGroup->GetNamedCounter("resource", "cpu");
- *cpuCounter = 30;
-
- auto memGroup = hostGroup->GetSubgroup("resource", "mem");
- auto usedCounter = memGroup->GetCounter("used");
- auto freeCounter = memGroup->GetCounter("free");
- *usedCounter = 100;
- *freeCounter = 28;
-
- auto netGroup = hostGroup->GetSubgroup("resource", "net");
- auto rxCounter = netGroup->GetCounter("rx", true);
- auto txCounter = netGroup->GetCounter("tx", true);
- *rxCounter = 8;
- *txCounter = 9;
-
- TStringStream ss;
- TCountersPrinter printer(&ss);
- rootGroup->Accept("root", "counters", printer);
-
- UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
- "root:counters {\n"
- " counters:resources {\n"
- " resource:cpu = 30\n"
- " resource:mem {\n"
- " sensor:free = 28\n"
- " sensor:used = 100\n"
- " }\n"
- " resource:net {\n"
- " sensor:rx = 8\n"
- " sensor:tx = 9\n"
- " }\n"
- " }\n"
- " users:count = 7\n"
- "}\n");
- }
+ TDynamicCounterPtr rootGroup(new TDynamicCounters());
+
+ auto usersCounter = rootGroup->GetNamedCounter("users", "count");
+ *usersCounter = 7;
+
+ auto hostGroup = rootGroup->GetSubgroup("counters", "resources");
+ auto cpuCounter = hostGroup->GetNamedCounter("resource", "cpu");
+ *cpuCounter = 30;
+
+ auto memGroup = hostGroup->GetSubgroup("resource", "mem");
+ auto usedCounter = memGroup->GetCounter("used");
+ auto freeCounter = memGroup->GetCounter("free");
+ *usedCounter = 100;
+ *freeCounter = 28;
+
+ auto netGroup = hostGroup->GetSubgroup("resource", "net");
+ auto rxCounter = netGroup->GetCounter("rx", true);
+ auto txCounter = netGroup->GetCounter("tx", true);
+ *rxCounter = 8;
+ *txCounter = 9;
+
+ TStringStream ss;
+ TCountersPrinter printer(&ss);
+ rootGroup->Accept("root", "counters", printer);
+
+ UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
+ "root:counters {\n"
+ " counters:resources {\n"
+ " resource:cpu = 30\n"
+ " resource:mem {\n"
+ " sensor:free = 28\n"
+ " sensor:used = 100\n"
+ " }\n"
+ " resource:net {\n"
+ " sensor:rx = 8\n"
+ " sensor:tx = 9\n"
+ " }\n"
+ " }\n"
+ " users:count = 7\n"
+ "}\n");
+ }
Y_UNIT_TEST(MergeSubgroup) {
TDynamicCounterPtr rootGroup(new TDynamicCounters());
@@ -249,7 +249,7 @@ Y_UNIT_TEST_SUITE(TDynamicCountersTest) {
UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
"root:counters {\n"
"}\n");
- }
+ }
Y_UNIT_TEST(ExpiringCountersDiesAfterRegistry) {
TDynamicCounters::TCounterPtr ptr;
@@ -266,24 +266,24 @@ Y_UNIT_TEST_SUITE(TDynamicCountersTest) {
" sensor:foo = 0\n"
"}\n");
}
- }
-
- Y_UNIT_TEST(HistogramCounter) {
- TDynamicCounterPtr rootGroup(new TDynamicCounters());
-
- auto h = rootGroup->GetHistogram("timeMillis", ExponentialHistogram(4, 2));
- for (i64 i = 1; i < 100; i++) {
- h->Collect(i);
- }
-
- TStringStream ss;
- TCountersPrinter printer(&ss);
- rootGroup->Accept("root", "counters", printer);
- UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
- "root:counters {\n"
- " sensor:timeMillis = {1: 1, 2: 1, 4: 2, inf: 95}\n"
- "}\n");
- }
+ }
+
+ Y_UNIT_TEST(HistogramCounter) {
+ TDynamicCounterPtr rootGroup(new TDynamicCounters());
+
+ auto h = rootGroup->GetHistogram("timeMillis", ExponentialHistogram(4, 2));
+ for (i64 i = 1; i < 100; i++) {
+ h->Collect(i);
+ }
+
+ TStringStream ss;
+ TCountersPrinter printer(&ss);
+ rootGroup->Accept("root", "counters", printer);
+ UNIT_ASSERT_STRINGS_EQUAL(ss.Str(),
+ "root:counters {\n"
+ " sensor:timeMillis = {1: 1, 2: 1, 4: 2, inf: 95}\n"
+ "}\n");
+ }
Y_UNIT_TEST(CounterLookupCounter) {
TDynamicCounterPtr rootGroup(new TDynamicCounters());
@@ -339,4 +339,4 @@ Y_UNIT_TEST_SUITE(TDynamicCountersTest) {
histogram = rootGroup->FindNamedHistogram("name", "histogram2");
UNIT_ASSERT(histogram);
}
-}
+}
diff --git a/library/cpp/monlib/dynamic_counters/encode.cpp b/library/cpp/monlib/dynamic_counters/encode.cpp
index ffa48d276e..8a8b31314b 100644
--- a/library/cpp/monlib/dynamic_counters/encode.cpp
+++ b/library/cpp/monlib/dynamic_counters/encode.cpp
@@ -1,131 +1,131 @@
-#include "encode.h"
-
-#include <library/cpp/monlib/encode/encoder.h>
-#include <library/cpp/monlib/encode/json/json.h>
-#include <library/cpp/monlib/encode/spack/spack_v1.h>
+#include "encode.h"
+
+#include <library/cpp/monlib/encode/encoder.h>
+#include <library/cpp/monlib/encode/json/json.h>
+#include <library/cpp/monlib/encode/spack/spack_v1.h>
#include <library/cpp/monlib/encode/prometheus/prometheus.h>
-
-#include <util/stream/str.h>
-
-namespace NMonitoring {
- namespace {
- constexpr TInstant ZERO_TIME = TInstant::Zero();
-
- class TConsumer final: public ICountableConsumer {
- using TLabel = std::pair<TString, TString>; // name, value
-
- public:
- explicit TConsumer(NMonitoring::IMetricEncoderPtr encoderImpl, TCountableBase::EVisibility vis)
- : EncoderImpl_(std::move(encoderImpl))
+
+#include <util/stream/str.h>
+
+namespace NMonitoring {
+ namespace {
+ constexpr TInstant ZERO_TIME = TInstant::Zero();
+
+ class TConsumer final: public ICountableConsumer {
+ using TLabel = std::pair<TString, TString>; // name, value
+
+ public:
+ explicit TConsumer(NMonitoring::IMetricEncoderPtr encoderImpl, TCountableBase::EVisibility vis)
+ : EncoderImpl_(std::move(encoderImpl))
, Visibility_{vis}
- {
- }
-
- void OnCounter(
- const TString& labelName, const TString& labelValue,
- const TCounterForPtr* counter) override {
- NMonitoring::EMetricType metricType = counter->ForDerivative()
- ? NMonitoring::EMetricType::RATE
- : NMonitoring::EMetricType::GAUGE;
- EncoderImpl_->OnMetricBegin(metricType);
- EncodeLabels(labelName, labelValue);
-
- if (metricType == NMonitoring::EMetricType::GAUGE) {
- EncoderImpl_->OnDouble(ZERO_TIME, static_cast<double>(counter->Val()));
- } else {
- EncoderImpl_->OnUint64(ZERO_TIME, counter->Val());
- }
-
- EncoderImpl_->OnMetricEnd();
- }
-
- void OnHistogram(
- const TString& labelName, const TString& labelValue,
+ {
+ }
+
+ void OnCounter(
+ const TString& labelName, const TString& labelValue,
+ const TCounterForPtr* counter) override {
+ NMonitoring::EMetricType metricType = counter->ForDerivative()
+ ? NMonitoring::EMetricType::RATE
+ : NMonitoring::EMetricType::GAUGE;
+ EncoderImpl_->OnMetricBegin(metricType);
+ EncodeLabels(labelName, labelValue);
+
+ if (metricType == NMonitoring::EMetricType::GAUGE) {
+ EncoderImpl_->OnDouble(ZERO_TIME, static_cast<double>(counter->Val()));
+ } else {
+ EncoderImpl_->OnUint64(ZERO_TIME, counter->Val());
+ }
+
+ EncoderImpl_->OnMetricEnd();
+ }
+
+ void OnHistogram(
+ const TString& labelName, const TString& labelValue,
IHistogramSnapshotPtr snapshot, bool derivative) override {
NMonitoring::EMetricType metricType = derivative ? EMetricType::HIST_RATE : EMetricType::HIST;
EncoderImpl_->OnMetricBegin(metricType);
- EncodeLabels(labelName, labelValue);
- EncoderImpl_->OnHistogram(ZERO_TIME, snapshot);
- EncoderImpl_->OnMetricEnd();
- }
-
- void OnGroupBegin(
- const TString& labelName, const TString& labelValue,
- const TDynamicCounters*) override {
+ EncodeLabels(labelName, labelValue);
+ EncoderImpl_->OnHistogram(ZERO_TIME, snapshot);
+ EncoderImpl_->OnMetricEnd();
+ }
+
+ void OnGroupBegin(
+ const TString& labelName, const TString& labelValue,
+ const TDynamicCounters*) override {
if (labelName.empty() && labelValue.empty()) {
- // root group has empty label name and value
- EncoderImpl_->OnStreamBegin();
- } else {
- ParentLabels_.emplace_back(labelName, labelValue);
- }
- }
-
- void OnGroupEnd(
- const TString& labelName, const TString& labelValue,
- const TDynamicCounters*) override {
+ // root group has empty label name and value
+ EncoderImpl_->OnStreamBegin();
+ } else {
+ ParentLabels_.emplace_back(labelName, labelValue);
+ }
+ }
+
+ void OnGroupEnd(
+ const TString& labelName, const TString& labelValue,
+ const TDynamicCounters*) override {
if (labelName.empty() && labelValue.empty()) {
- // root group has empty label name and value
- EncoderImpl_->OnStreamEnd();
- EncoderImpl_->Close();
- } else {
- ParentLabels_.pop_back();
- }
- }
-
+ // root group has empty label name and value
+ EncoderImpl_->OnStreamEnd();
+ EncoderImpl_->Close();
+ } else {
+ ParentLabels_.pop_back();
+ }
+ }
+
TCountableBase::EVisibility Visibility() const override {
return Visibility_;
}
- private:
- void EncodeLabels(const TString& labelName, const TString& labelValue) {
- EncoderImpl_->OnLabelsBegin();
- for (const auto& label : ParentLabels_) {
- EncoderImpl_->OnLabel(label.first, label.second);
- }
- EncoderImpl_->OnLabel(labelName, labelValue);
- EncoderImpl_->OnLabelsEnd();
- }
-
- private:
- NMonitoring::IMetricEncoderPtr EncoderImpl_;
- TVector<TLabel> ParentLabels_;
+ private:
+ void EncodeLabels(const TString& labelName, const TString& labelValue) {
+ EncoderImpl_->OnLabelsBegin();
+ for (const auto& label : ParentLabels_) {
+ EncoderImpl_->OnLabel(label.first, label.second);
+ }
+ EncoderImpl_->OnLabel(labelName, labelValue);
+ EncoderImpl_->OnLabelsEnd();
+ }
+
+ private:
+ NMonitoring::IMetricEncoderPtr EncoderImpl_;
+ TVector<TLabel> ParentLabels_;
TCountableBase::EVisibility Visibility_;
- };
-
- }
-
+ };
+
+ }
+
THolder<ICountableConsumer> CreateEncoder(IOutputStream* out, EFormat format, TCountableBase::EVisibility vis) {
- switch (format) {
- case EFormat::JSON:
+ switch (format) {
+ case EFormat::JSON:
return MakeHolder<TConsumer>(NMonitoring::EncoderJson(out), vis);
- case EFormat::SPACK:
+ case EFormat::SPACK:
return MakeHolder<TConsumer>(NMonitoring::EncoderSpackV1(
- out,
- NMonitoring::ETimePrecision::SECONDS,
+ out,
+ NMonitoring::ETimePrecision::SECONDS,
NMonitoring::ECompression::ZSTD), vis);
case EFormat::PROMETHEUS:
return MakeHolder<TConsumer>(NMonitoring::EncoderPrometheus(
out), vis);
- default:
- ythrow yexception() << "unsupported metric encoding format: " << format;
- break;
- }
- }
-
+ default:
+ ythrow yexception() << "unsupported metric encoding format: " << format;
+ break;
+ }
+ }
+
THolder<ICountableConsumer> AsCountableConsumer(IMetricEncoderPtr encoder, TCountableBase::EVisibility visibility) {
return MakeHolder<TConsumer>(std::move(encoder), visibility);
}
- void ToJson(const TDynamicCounters& counters, IOutputStream* out) {
- TConsumer consumer{EncoderJson(out), TCountableBase::EVisibility::Public};
- counters.Accept(TString{}, TString{}, consumer);
- }
-
- TString ToJson(const TDynamicCounters& counters) {
- TStringStream ss;
- ToJson(counters, &ss);
- return ss.Str();
- }
-
-}
+ void ToJson(const TDynamicCounters& counters, IOutputStream* out) {
+ TConsumer consumer{EncoderJson(out), TCountableBase::EVisibility::Public};
+ counters.Accept(TString{}, TString{}, consumer);
+ }
+
+ TString ToJson(const TDynamicCounters& counters) {
+ TStringStream ss;
+ ToJson(counters, &ss);
+ return ss.Str();
+ }
+
+}
diff --git a/library/cpp/monlib/dynamic_counters/encode.h b/library/cpp/monlib/dynamic_counters/encode.h
index c79964d7cb..81e924a034 100644
--- a/library/cpp/monlib/dynamic_counters/encode.h
+++ b/library/cpp/monlib/dynamic_counters/encode.h
@@ -1,23 +1,23 @@
-#pragma once
-
-#include "counters.h"
-
+#pragma once
+
+#include "counters.h"
+
#include <library/cpp/monlib/encode/encoder.h>
-#include <library/cpp/monlib/encode/format.h>
-
-namespace NMonitoring {
-
+#include <library/cpp/monlib/encode/format.h>
+
+namespace NMonitoring {
+
THolder<ICountableConsumer> CreateEncoder(
IOutputStream* out,
EFormat format,
TCountableBase::EVisibility visibility = TCountableBase::EVisibility::Public
);
-
+
THolder<ICountableConsumer> AsCountableConsumer(
NMonitoring::IMetricEncoderPtr encoder,
TCountableBase::EVisibility visibility = TCountableBase::EVisibility::Public);
- void ToJson(const TDynamicCounters& counters, IOutputStream* out);
-
- TString ToJson(const TDynamicCounters& counters);
-}
+ void ToJson(const TDynamicCounters& counters, IOutputStream* out);
+
+ TString ToJson(const TDynamicCounters& counters);
+}
diff --git a/library/cpp/monlib/dynamic_counters/encode_ut.cpp b/library/cpp/monlib/dynamic_counters/encode_ut.cpp
index 52d77b6b41..9e5bd74f91 100644
--- a/library/cpp/monlib/dynamic_counters/encode_ut.cpp
+++ b/library/cpp/monlib/dynamic_counters/encode_ut.cpp
@@ -1,172 +1,172 @@
-#include "encode.h"
-
-#include <library/cpp/monlib/encode/json/json.h>
-#include <library/cpp/monlib/encode/spack/spack_v1.h>
-#include <library/cpp/monlib/encode/protobuf/protobuf.h>
-
-#include <library/cpp/monlib/encode/protobuf/protos/samples.pb.h>
+#include "encode.h"
+
+#include <library/cpp/monlib/encode/json/json.h>
+#include <library/cpp/monlib/encode/spack/spack_v1.h>
+#include <library/cpp/monlib/encode/protobuf/protobuf.h>
+
+#include <library/cpp/monlib/encode/protobuf/protos/samples.pb.h>
#include <library/cpp/testing/unittest/registar.h>
-
-#include <util/generic/buffer.h>
-#include <util/stream/buffer.h>
-
-namespace NMonitoring {
- struct TTestData: public TDynamicCounters {
- TTestData() {
- auto hostGroup = GetSubgroup("counters", "resources");
- {
- auto cpuCounter = hostGroup->GetNamedCounter("resource", "cpu");
- *cpuCounter = 30;
-
- auto memGroup = hostGroup->GetSubgroup("resource", "mem");
- auto usedCounter = memGroup->GetCounter("used");
- auto freeCounter = memGroup->GetCounter("free");
- *usedCounter = 100;
- *freeCounter = 28;
-
- auto netGroup = hostGroup->GetSubgroup("resource", "net");
- auto rxCounter = netGroup->GetCounter("rx", true);
- auto txCounter = netGroup->GetCounter("tx", true);
- *rxCounter = 8;
- *txCounter = 9;
- }
-
- auto usersCounter = GetNamedCounter("users", "count");
- *usersCounter = 7;
-
- auto responseTimeMillis = GetHistogram("responseTimeMillis", ExplicitHistogram({1, 5, 10, 15, 20, 100, 200}));
- for (i64 i = 0; i < 400; i++) {
- responseTimeMillis->Collect(i);
- }
- }
- };
-
- void AssertLabelsEqual(const NProto::TLabel& l, TStringBuf name, TStringBuf value) {
- UNIT_ASSERT_STRINGS_EQUAL(l.GetName(), name);
- UNIT_ASSERT_STRINGS_EQUAL(l.GetValue(), value);
- }
-
- void AssertResult(const NProto::TSingleSamplesList& samples) {
- UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 7);
-
- {
- auto s = samples.GetSamples(0);
- UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2);
- AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
- AssertLabelsEqual(s.GetLabels(1), "resource", "cpu");
- UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
- UNIT_ASSERT_DOUBLES_EQUAL(s.GetFloat64(), 30.0, Min<double>());
- }
- {
- auto s = samples.GetSamples(1);
- UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 3);
- AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
- AssertLabelsEqual(s.GetLabels(1), "resource", "mem");
- AssertLabelsEqual(s.GetLabels(2), "sensor", "free");
- UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
- UNIT_ASSERT_DOUBLES_EQUAL(s.GetFloat64(), 28.0, Min<double>());
- }
- {
- auto s = samples.GetSamples(2);
- UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 3);
- AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
- AssertLabelsEqual(s.GetLabels(1), "resource", "mem");
- AssertLabelsEqual(s.GetLabels(2), "sensor", "used");
- UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
- UNIT_ASSERT_DOUBLES_EQUAL(s.GetFloat64(), 100.0, Min<double>());
- }
- {
- auto s = samples.GetSamples(3);
- UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 3);
- AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
- AssertLabelsEqual(s.GetLabels(1), "resource", "net");
- AssertLabelsEqual(s.GetLabels(2), "sensor", "rx");
- UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE);
- UNIT_ASSERT_VALUES_EQUAL(s.GetUint64(), 8);
- }
- {
- auto s = samples.GetSamples(4);
- UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 3);
- AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
- AssertLabelsEqual(s.GetLabels(1), "resource", "net");
- AssertLabelsEqual(s.GetLabels(2), "sensor", "tx");
- UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE);
- UNIT_ASSERT_VALUES_EQUAL(s.GetUint64(), 9);
- }
- {
- auto s = samples.GetSamples(5);
- UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1);
- AssertLabelsEqual(s.GetLabels(0), "sensor", "responseTimeMillis");
- UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::HIST_RATE);
-
- const NProto::THistogram& h = s.GetHistogram();
-
- UNIT_ASSERT_EQUAL(h.BoundsSize(), 8);
- UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(0), 1, Min<double>());
- UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(1), 5, Min<double>());
- UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(2), 10, Min<double>());
- UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(3), 15, Min<double>());
- UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(4), 20, Min<double>());
- UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(5), 100, Min<double>());
- UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(6), 200, Min<double>());
- UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(7), Max<double>(), Min<double>());
-
- UNIT_ASSERT_EQUAL(h.ValuesSize(), 8);
- UNIT_ASSERT_EQUAL(h.GetValues(0), 2);
- UNIT_ASSERT_EQUAL(h.GetValues(1), 4);
- UNIT_ASSERT_EQUAL(h.GetValues(2), 5);
- UNIT_ASSERT_EQUAL(h.GetValues(3), 5);
- UNIT_ASSERT_EQUAL(h.GetValues(4), 5);
- UNIT_ASSERT_EQUAL(h.GetValues(5), 80);
- UNIT_ASSERT_EQUAL(h.GetValues(6), 100);
- UNIT_ASSERT_EQUAL(h.GetValues(7), 199);
- }
- {
- auto s = samples.GetSamples(6);
- UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1);
- AssertLabelsEqual(s.GetLabels(0), "users", "count");
- UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
- UNIT_ASSERT_DOUBLES_EQUAL(s.GetFloat64(), 7, Min<double>());
- }
- }
-
+
+#include <util/generic/buffer.h>
+#include <util/stream/buffer.h>
+
+namespace NMonitoring {
+ struct TTestData: public TDynamicCounters {
+ TTestData() {
+ auto hostGroup = GetSubgroup("counters", "resources");
+ {
+ auto cpuCounter = hostGroup->GetNamedCounter("resource", "cpu");
+ *cpuCounter = 30;
+
+ auto memGroup = hostGroup->GetSubgroup("resource", "mem");
+ auto usedCounter = memGroup->GetCounter("used");
+ auto freeCounter = memGroup->GetCounter("free");
+ *usedCounter = 100;
+ *freeCounter = 28;
+
+ auto netGroup = hostGroup->GetSubgroup("resource", "net");
+ auto rxCounter = netGroup->GetCounter("rx", true);
+ auto txCounter = netGroup->GetCounter("tx", true);
+ *rxCounter = 8;
+ *txCounter = 9;
+ }
+
+ auto usersCounter = GetNamedCounter("users", "count");
+ *usersCounter = 7;
+
+ auto responseTimeMillis = GetHistogram("responseTimeMillis", ExplicitHistogram({1, 5, 10, 15, 20, 100, 200}));
+ for (i64 i = 0; i < 400; i++) {
+ responseTimeMillis->Collect(i);
+ }
+ }
+ };
+
+ void AssertLabelsEqual(const NProto::TLabel& l, TStringBuf name, TStringBuf value) {
+ UNIT_ASSERT_STRINGS_EQUAL(l.GetName(), name);
+ UNIT_ASSERT_STRINGS_EQUAL(l.GetValue(), value);
+ }
+
+ void AssertResult(const NProto::TSingleSamplesList& samples) {
+ UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 7);
+
+ {
+ auto s = samples.GetSamples(0);
+ UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 2);
+ AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
+ AssertLabelsEqual(s.GetLabels(1), "resource", "cpu");
+ UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
+ UNIT_ASSERT_DOUBLES_EQUAL(s.GetFloat64(), 30.0, Min<double>());
+ }
+ {
+ auto s = samples.GetSamples(1);
+ UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 3);
+ AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
+ AssertLabelsEqual(s.GetLabels(1), "resource", "mem");
+ AssertLabelsEqual(s.GetLabels(2), "sensor", "free");
+ UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
+ UNIT_ASSERT_DOUBLES_EQUAL(s.GetFloat64(), 28.0, Min<double>());
+ }
+ {
+ auto s = samples.GetSamples(2);
+ UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 3);
+ AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
+ AssertLabelsEqual(s.GetLabels(1), "resource", "mem");
+ AssertLabelsEqual(s.GetLabels(2), "sensor", "used");
+ UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
+ UNIT_ASSERT_DOUBLES_EQUAL(s.GetFloat64(), 100.0, Min<double>());
+ }
+ {
+ auto s = samples.GetSamples(3);
+ UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 3);
+ AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
+ AssertLabelsEqual(s.GetLabels(1), "resource", "net");
+ AssertLabelsEqual(s.GetLabels(2), "sensor", "rx");
+ UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE);
+ UNIT_ASSERT_VALUES_EQUAL(s.GetUint64(), 8);
+ }
+ {
+ auto s = samples.GetSamples(4);
+ UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 3);
+ AssertLabelsEqual(s.GetLabels(0), "counters", "resources");
+ AssertLabelsEqual(s.GetLabels(1), "resource", "net");
+ AssertLabelsEqual(s.GetLabels(2), "sensor", "tx");
+ UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::RATE);
+ UNIT_ASSERT_VALUES_EQUAL(s.GetUint64(), 9);
+ }
+ {
+ auto s = samples.GetSamples(5);
+ UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1);
+ AssertLabelsEqual(s.GetLabels(0), "sensor", "responseTimeMillis");
+ UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::HIST_RATE);
+
+ const NProto::THistogram& h = s.GetHistogram();
+
+ UNIT_ASSERT_EQUAL(h.BoundsSize(), 8);
+ UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(0), 1, Min<double>());
+ UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(1), 5, Min<double>());
+ UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(2), 10, Min<double>());
+ UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(3), 15, Min<double>());
+ UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(4), 20, Min<double>());
+ UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(5), 100, Min<double>());
+ UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(6), 200, Min<double>());
+ UNIT_ASSERT_DOUBLES_EQUAL(h.GetBounds(7), Max<double>(), Min<double>());
+
+ UNIT_ASSERT_EQUAL(h.ValuesSize(), 8);
+ UNIT_ASSERT_EQUAL(h.GetValues(0), 2);
+ UNIT_ASSERT_EQUAL(h.GetValues(1), 4);
+ UNIT_ASSERT_EQUAL(h.GetValues(2), 5);
+ UNIT_ASSERT_EQUAL(h.GetValues(3), 5);
+ UNIT_ASSERT_EQUAL(h.GetValues(4), 5);
+ UNIT_ASSERT_EQUAL(h.GetValues(5), 80);
+ UNIT_ASSERT_EQUAL(h.GetValues(6), 100);
+ UNIT_ASSERT_EQUAL(h.GetValues(7), 199);
+ }
+ {
+ auto s = samples.GetSamples(6);
+ UNIT_ASSERT_VALUES_EQUAL(s.LabelsSize(), 1);
+ AssertLabelsEqual(s.GetLabels(0), "users", "count");
+ UNIT_ASSERT_EQUAL(s.GetMetricType(), NProto::GAUGE);
+ UNIT_ASSERT_DOUBLES_EQUAL(s.GetFloat64(), 7, Min<double>());
+ }
+ }
+
Y_UNIT_TEST_SUITE(TDynamicCountersEncodeTest) {
- TTestData Data;
-
+ TTestData Data;
+
Y_UNIT_TEST(Json) {
- TString result;
- {
- TStringOutput out(result);
- auto encoder = CreateEncoder(&out, EFormat::JSON);
- Data.Accept(TString(), TString(), *encoder);
- }
-
- NProto::TSingleSamplesList samples;
- {
- auto e = EncoderProtobuf(&samples);
- DecodeJson(result, e.Get());
- }
-
- AssertResult(samples);
- }
-
+ TString result;
+ {
+ TStringOutput out(result);
+ auto encoder = CreateEncoder(&out, EFormat::JSON);
+ Data.Accept(TString(), TString(), *encoder);
+ }
+
+ NProto::TSingleSamplesList samples;
+ {
+ auto e = EncoderProtobuf(&samples);
+ DecodeJson(result, e.Get());
+ }
+
+ AssertResult(samples);
+ }
+
Y_UNIT_TEST(Spack) {
- TBuffer result;
- {
- TBufferOutput out(result);
- auto encoder = CreateEncoder(&out, EFormat::SPACK);
- Data.Accept(TString(), TString(), *encoder);
- }
-
- NProto::TSingleSamplesList samples;
- {
- auto e = EncoderProtobuf(&samples);
- TBufferInput in(result);
- DecodeSpackV1(&in, e.Get());
- }
-
- AssertResult(samples);
- }
+ TBuffer result;
+ {
+ TBufferOutput out(result);
+ auto encoder = CreateEncoder(&out, EFormat::SPACK);
+ Data.Accept(TString(), TString(), *encoder);
+ }
+
+ NProto::TSingleSamplesList samples;
+ {
+ auto e = EncoderProtobuf(&samples);
+ TBufferInput in(result);
+ DecodeSpackV1(&in, e.Get());
+ }
+
+ AssertResult(samples);
+ }
Y_UNIT_TEST(PrivateSubgroupIsNotSerialized) {
TBuffer result;
@@ -209,18 +209,18 @@ namespace NMonitoring {
AssertResult(samples);
}
-
- Y_UNIT_TEST(ToJson) {
- TString result = ToJson(Data);
-
- NProto::TSingleSamplesList samples;
- {
- auto e = EncoderProtobuf(&samples);
- DecodeJson(result, e.Get());
- }
-
- AssertResult(samples);
- }
- }
-
-}
+
+ Y_UNIT_TEST(ToJson) {
+ TString result = ToJson(Data);
+
+ NProto::TSingleSamplesList samples;
+ {
+ auto e = EncoderProtobuf(&samples);
+ DecodeJson(result, e.Get());
+ }
+
+ AssertResult(samples);
+ }
+ }
+
+}
diff --git a/library/cpp/monlib/dynamic_counters/golovan_page.cpp b/library/cpp/monlib/dynamic_counters/golovan_page.cpp
index 49cf2d39bb..03a5b1a399 100644
--- a/library/cpp/monlib/dynamic_counters/golovan_page.cpp
+++ b/library/cpp/monlib/dynamic_counters/golovan_page.cpp
@@ -1,6 +1,6 @@
-#include "golovan_page.h"
+#include "golovan_page.h"
-#include <library/cpp/monlib/service/pages/templates.h>
+#include <library/cpp/monlib/service/pages/templates.h>
#include <util/string/split.h>
#include <util/system/tls.h>
@@ -40,8 +40,8 @@ public:
}
void OnHistogram(const TString&, const TString&, IHistogramSnapshotPtr, bool) override {
- }
-
+ }
+
void OnGroupBegin(const TString&, const TString& value, const TDynamicCounters*) override {
prefix += value;
if (!value.empty()) {
diff --git a/library/cpp/monlib/dynamic_counters/golovan_page.h b/library/cpp/monlib/dynamic_counters/golovan_page.h
index e1772c7734..855b4e9eab 100644
--- a/library/cpp/monlib/dynamic_counters/golovan_page.h
+++ b/library/cpp/monlib/dynamic_counters/golovan_page.h
@@ -1,8 +1,8 @@
#pragma once
-#include "counters.h"
+#include "counters.h"
-#include <library/cpp/monlib/service/pages/mon_page.h>
+#include <library/cpp/monlib/service/pages/mon_page.h>
#include <util/generic/ptr.h>
@@ -19,7 +19,7 @@ public:
TOutputCallback outputCallback = nullptr);
void Output(NMonitoring::IMonHttpRequest& request) override;
-
+
private:
TOutputCallback OutputCallback;
};
diff --git a/library/cpp/monlib/dynamic_counters/page.cpp b/library/cpp/monlib/dynamic_counters/page.cpp
index 5124a47bb3..e554d330ad 100644
--- a/library/cpp/monlib/dynamic_counters/page.cpp
+++ b/library/cpp/monlib/dynamic_counters/page.cpp
@@ -1,31 +1,31 @@
-#include "page.h"
-#include "encode.h"
-
-#include <library/cpp/monlib/service/pages/templates.h>
-#include <library/cpp/string_utils/quote/quote.h>
+#include "page.h"
+#include "encode.h"
+#include <library/cpp/monlib/service/pages/templates.h>
+#include <library/cpp/string_utils/quote/quote.h>
+
#include <util/string/split.h>
#include <util/system/tls.h>
using namespace NMonitoring;
-namespace {
- Y_POD_STATIC_THREAD(TDynamicCounters*)
- currentCounters(nullptr);
+namespace {
+ Y_POD_STATIC_THREAD(TDynamicCounters*)
+ currentCounters(nullptr);
}
TMaybe<EFormat> ParseFormat(TStringBuf str) {
if (str == TStringBuf("json")) {
- return EFormat::JSON;
+ return EFormat::JSON;
} else if (str == TStringBuf("spack")) {
- return EFormat::SPACK;
+ return EFormat::SPACK;
} else if (str == TStringBuf("prometheus")) {
return EFormat::PROMETHEUS;
- } else {
- return Nothing();
- }
-}
-
+ } else {
+ return Nothing();
+ }
+}
+
void TDynamicCountersPage::Output(NMonitoring::IMonHttpRequest& request) {
if (OutputCallback) {
OutputCallback();
@@ -41,10 +41,10 @@ void TDynamicCountersPage::Output(NMonitoring::IMonHttpRequest& request) {
.SkipEmpty()
.Collect(&parts);
- TMaybe<EFormat> format = !parts.empty() ? ParseFormat(parts.back()) : Nothing();
- if (format) {
+ TMaybe<EFormat> format = !parts.empty() ? ParseFormat(parts.back()) : Nothing();
+ if (format) {
parts.pop_back();
- }
+ }
if (!parts.empty() && parts.back() == TStringBuf("private")) {
visibility = TCountableBase::EVisibility::Private;
@@ -72,27 +72,27 @@ void TDynamicCountersPage::Output(NMonitoring::IMonHttpRequest& request) {
}
}
- if (!format) {
- currentCounters = counters.Get();
+ if (!format) {
+ currentCounters = counters.Get();
THtmlMonPage::Output(request);
- currentCounters = nullptr;
- return;
+ currentCounters = nullptr;
+ return;
}
-
- IOutputStream& out = request.Output();
- if (*format == EFormat::JSON) {
- out << HTTPOKJSON;
- } else if (*format == EFormat::SPACK) {
- out << HTTPOKSPACK;
+
+ IOutputStream& out = request.Output();
+ if (*format == EFormat::JSON) {
+ out << HTTPOKJSON;
+ } else if (*format == EFormat::SPACK) {
+ out << HTTPOKSPACK;
} else if (*format == EFormat::PROMETHEUS) {
out << HTTPOKPROMETHEUS;
- } else {
- ythrow yexception() << "unsupported metric encoding format: " << *format;
- }
-
- auto encoder = CreateEncoder(&out, *format, visibility);
- counters->Accept(TString(), TString(), *encoder);
- out.Flush();
+ } else {
+ ythrow yexception() << "unsupported metric encoding format: " << *format;
+ }
+
+ auto encoder = CreateEncoder(&out, *format, visibility);
+ counters->Accept(TString(), TString(), *encoder);
+ out.Flush();
}
void TDynamicCountersPage::HandleAbsentSubgroup(IMonHttpRequest& request) {
@@ -113,10 +113,10 @@ void TDynamicCountersPage::BeforePre(IMonHttpRequest& request) {
out << " for <a href='https://wiki.yandex-team.ru/solomon/'>Solomon</a>";
}
- H5() {
- out << "Counters subgroups";
+ H5() {
+ out << "Counters subgroups";
}
- UL() {
+ UL() {
currentCounters->EnumerateSubgroups([&](const TString& name, const TString& value) {
LI() {
TString pathPart = name + "=" + value;
@@ -124,11 +124,11 @@ void TDynamicCountersPage::BeforePre(IMonHttpRequest& request) {
out << "\n<a href='" << request.GetPath() << "/" << pathPart << "'>" << name << " " << value << "</a>";
}
});
- }
+ }
- H4() {
- out << "Counters as text";
- }
+ H4() {
+ out << "Counters as text";
+ }
}
}
diff --git a/library/cpp/monlib/dynamic_counters/page.h b/library/cpp/monlib/dynamic_counters/page.h
index 1f0ef6a5ea..159ddaf578 100644
--- a/library/cpp/monlib/dynamic_counters/page.h
+++ b/library/cpp/monlib/dynamic_counters/page.h
@@ -1,9 +1,9 @@
#pragma once
-#include "counters.h"
-
-#include <library/cpp/monlib/service/pages/pre_mon_page.h>
-
+#include "counters.h"
+
+#include <library/cpp/monlib/service/pages/pre_mon_page.h>
+
#include <util/generic/ptr.h>
#include <functional>
@@ -28,7 +28,7 @@ namespace NMonitoring {
public:
TDynamicCountersPage(const TString& path,
- const TString& title,
+ const TString& title,
TIntrusivePtr<TDynamicCounters> counters,
TOutputCallback outputCallback = nullptr)
: TPreMonPage(path, title)
diff --git a/library/cpp/monlib/dynamic_counters/percentile/percentile_base.h b/library/cpp/monlib/dynamic_counters/percentile/percentile_base.h
index d3c825c43d..ee242b1cdb 100644
--- a/library/cpp/monlib/dynamic_counters/percentile/percentile_base.h
+++ b/library/cpp/monlib/dynamic_counters/percentile/percentile_base.h
@@ -1,6 +1,6 @@
#pragma once
-#include <library/cpp/monlib/dynamic_counters/counters.h>
+#include <library/cpp/monlib/dynamic_counters/counters.h>
#include <util/string/printf.h>
diff --git a/library/cpp/monlib/dynamic_counters/percentile/ut/ya.make b/library/cpp/monlib/dynamic_counters/percentile/ut/ya.make
index f9f3564101..9859d87de2 100644
--- a/library/cpp/monlib/dynamic_counters/percentile/ut/ya.make
+++ b/library/cpp/monlib/dynamic_counters/percentile/ut/ya.make
@@ -1,4 +1,4 @@
-UNITTEST_FOR(library/cpp/monlib/dynamic_counters/percentile)
+UNITTEST_FOR(library/cpp/monlib/dynamic_counters/percentile)
OWNER(alexvru g:kikimr g:solomon)
diff --git a/library/cpp/monlib/dynamic_counters/percentile/ya.make b/library/cpp/monlib/dynamic_counters/percentile/ya.make
index cb52cdd9ad..3480330470 100644
--- a/library/cpp/monlib/dynamic_counters/percentile/ya.make
+++ b/library/cpp/monlib/dynamic_counters/percentile/ya.make
@@ -9,7 +9,7 @@ LIBRARY()
PEERDIR(
library/cpp/containers/stack_vector
- library/cpp/monlib/dynamic_counters
+ library/cpp/monlib/dynamic_counters
)
END()
diff --git a/library/cpp/monlib/dynamic_counters/ut/ya.make b/library/cpp/monlib/dynamic_counters/ut/ya.make
index 8242f2fe30..b0a7c7aa41 100644
--- a/library/cpp/monlib/dynamic_counters/ut/ya.make
+++ b/library/cpp/monlib/dynamic_counters/ut/ya.make
@@ -1,16 +1,16 @@
-UNITTEST_FOR(library/cpp/monlib/dynamic_counters)
-
-OWNER(jamel)
-
-SRCS(
+UNITTEST_FOR(library/cpp/monlib/dynamic_counters)
+
+OWNER(jamel)
+
+SRCS(
contention_ut.cpp
- counters_ut.cpp
- encode_ut.cpp
-)
-
-PEERDIR(
- library/cpp/monlib/encode/protobuf
- library/cpp/monlib/encode/json
-)
-
-END()
+ counters_ut.cpp
+ encode_ut.cpp
+)
+
+PEERDIR(
+ library/cpp/monlib/encode/protobuf
+ library/cpp/monlib/encode/json
+)
+
+END()
diff --git a/library/cpp/monlib/dynamic_counters/ya.make b/library/cpp/monlib/dynamic_counters/ya.make
index aafe1c34be..cd65c4a4c9 100644
--- a/library/cpp/monlib/dynamic_counters/ya.make
+++ b/library/cpp/monlib/dynamic_counters/ya.make
@@ -1,27 +1,27 @@
-LIBRARY()
-
+LIBRARY()
+
OWNER(
g:solomon
jamel
)
-
-NO_WSHADOW()
-
-SRCS(
- counters.cpp
- encode.cpp
- golovan_page.cpp
- page.cpp
-)
-
-PEERDIR(
+
+NO_WSHADOW()
+
+SRCS(
+ counters.cpp
+ encode.cpp
+ golovan_page.cpp
+ page.cpp
+)
+
+PEERDIR(
library/cpp/containers/stack_vector
- library/cpp/monlib/encode/json
- library/cpp/monlib/encode/spack
+ library/cpp/monlib/encode/json
+ library/cpp/monlib/encode/spack
library/cpp/monlib/encode/prometheus
- library/cpp/monlib/service/pages
+ library/cpp/monlib/service/pages
library/cpp/string_utils/quote
library/cpp/threading/light_rw_lock
-)
-
-END()
+)
+
+END()