Skip to content

Commit f414482

Browse files
author
golubi-kuryat
committed
Make WriteMetric protected
Появляется возможность зарегистрировать новую тип метрики с кастомным поведением, который наследуется от IMetric Пример использования HIDDEN_URL commit_hash:82e3997427ef61d8017d6a1c1eca848131b03d7b
1 parent 0b2bfef commit f414482

File tree

4 files changed

+57
-65
lines changed

4 files changed

+57
-65
lines changed

library/cpp/monlib/metrics/fake.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ namespace NMonitoring {
8282
i64 Get() const noexcept override {
8383
return 0;
8484
}
85+
86+
void Reset() noexcept override {}
8587
};
8688

8789
struct TFakeRate final: public TFakeAcceptor<IRate> {
@@ -102,6 +104,8 @@ namespace NMonitoring {
102104
ui64 Get() const noexcept override {
103105
return 0;
104106
}
107+
108+
void Reset() noexcept override {}
105109
};
106110

107111
struct TFakeGauge final: public TFakeAcceptor<IGauge> {
@@ -117,12 +121,16 @@ namespace NMonitoring {
117121
double Get() const noexcept override {
118122
return 0;
119123
}
124+
125+
void Reset() noexcept override {}
120126
};
121127

122128
struct TFakeLazyGauge final: public TFakeAcceptor<ILazyGauge> {
123129
double Get() const noexcept override {
124130
return 0;
125131
}
132+
133+
void Reset() noexcept override {}
126134
};
127135

128136
struct TFakeHistogram final: public IHistogram {
@@ -169,5 +177,7 @@ namespace NMonitoring {
169177
ui64 Get() const noexcept override {
170178
return 0;
171179
}
180+
181+
void Reset() noexcept override {}
172182
};
173183
} // namespace NMonitoring

library/cpp/monlib/metrics/metric.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace NMonitoring {
1515

1616
virtual EMetricType Type() const noexcept = 0;
1717
virtual void Accept(TInstant time, IMetricConsumer* consumer) const = 0;
18+
virtual void Reset() noexcept = 0;
1819
};
1920

2021
using IMetricPtr = TIntrusivePtr<IMetric>;
@@ -28,7 +29,7 @@ namespace NMonitoring {
2829
virtual double Add(double n) noexcept = 0;
2930
virtual void Set(double n) noexcept = 0;
3031
virtual double Get() const noexcept = 0;
31-
virtual void Reset() noexcept {
32+
void Reset() noexcept override {
3233
Set(0);
3334
}
3435
};
@@ -58,7 +59,7 @@ namespace NMonitoring {
5859

5960
virtual void Set(i64 value) noexcept = 0;
6061
virtual i64 Get() const noexcept = 0;
61-
virtual void Reset() noexcept {
62+
void Reset() noexcept override {
6263
Set(0);
6364
}
6465
};
@@ -84,7 +85,6 @@ namespace NMonitoring {
8485

8586
virtual ui64 Add(ui64 n) noexcept = 0;
8687
virtual ui64 Get() const noexcept = 0;
87-
virtual void Reset() noexcept = 0;
8888
};
8989

9090
class ILazyCounter: public IMetric {
@@ -108,7 +108,6 @@ namespace NMonitoring {
108108

109109
virtual ui64 Add(ui64 n) noexcept = 0;
110110
virtual ui64 Get() const noexcept = 0;
111-
virtual void Reset() noexcept = 0;
112111
};
113112

114113
class ILazyRate: public IMetric {
@@ -134,7 +133,6 @@ namespace NMonitoring {
134133
virtual void Record(double value) noexcept = 0;
135134
virtual void Record(double value, ui32 count) noexcept = 0;
136135
virtual IHistogramSnapshotPtr TakeSnapshot() const = 0;
137-
virtual void Reset() noexcept = 0;
138136

139137
protected:
140138
const bool IsRate_;
@@ -194,6 +192,8 @@ namespace NMonitoring {
194192
consumer->OnDouble(time, Get());
195193
}
196194

195+
void Reset() noexcept override {}
196+
197197
private:
198198
std::function<double()> Supplier_;
199199
};
@@ -245,6 +245,8 @@ namespace NMonitoring {
245245
consumer->OnInt64(time, Get());
246246
}
247247

248+
void Reset() noexcept override {}
249+
248250
private:
249251
std::function<i64()> Supplier_;
250252
};
@@ -296,6 +298,8 @@ namespace NMonitoring {
296298
consumer->OnUint64(time, Get());
297299
}
298300

301+
void Reset() noexcept override {}
302+
299303
private:
300304
std::function<ui64()> Supplier_;
301305
};
@@ -347,6 +351,8 @@ namespace NMonitoring {
347351
consumer->OnUint64(time, Get());
348352
}
349353

354+
void Reset() noexcept override {}
355+
350356
private:
351357
std::function<ui64()> Supplier_;
352358
};

library/cpp/monlib/metrics/metric_registry.cpp

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -226,29 +226,7 @@ namespace NMonitoring {
226226
void TMetricRegistry::Reset() {
227227
TWriteGuard g{*Lock_};
228228
for (auto& [label, metricValue] : Metrics_) {
229-
auto metric = metricValue.Metric;
230-
switch (metric->Type()) {
231-
case EMetricType::GAUGE:
232-
static_cast<TGauge*>(metric.Get())->Set(.0);
233-
break;
234-
case EMetricType::IGAUGE:
235-
static_cast<TIntGauge*>(metric.Get())->Set(0);
236-
break;
237-
case EMetricType::COUNTER:
238-
static_cast<TCounter*>(metric.Get())->Reset();
239-
break;
240-
case EMetricType::RATE:
241-
static_cast<TRate*>(metric.Get())->Reset();
242-
break;
243-
case EMetricType::HIST:
244-
case EMetricType::HIST_RATE:
245-
static_cast<THistogram*>(metric.Get())->Reset();
246-
break;
247-
case EMetricType::UNKNOWN:
248-
case EMetricType::DSUMMARY:
249-
case EMetricType::LOGHIST:
250-
break;
251-
}
229+
metricValue.Metric->Reset();
252230
}
253231
}
254232

@@ -257,40 +235,6 @@ namespace NMonitoring {
257235
Metrics_.clear();
258236
}
259237

260-
template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args>
261-
TMetric* TMetricRegistry::Metric(TLabelsType&& labels, TMetricOpts&& opts, Args&&... args) {
262-
{
263-
TReadGuard g{*Lock_};
264-
265-
auto it = Metrics_.find(labels);
266-
if (it != Metrics_.end()) {
267-
Y_ENSURE(it->second.Metric->Type() == type, "cannot create metric " << labels
268-
<< " with type " << MetricTypeToStr(type)
269-
<< ", because registry already has same metric with type " << MetricTypeToStr(it->second.Metric->Type()));
270-
Y_ENSURE(it->second.Opts.MemOnly == opts.MemOnly,"cannot create metric " << labels
271-
<< " with memOnly=" << opts.MemOnly
272-
<< ", because registry already has same metric with memOnly=" << it->second.Opts.MemOnly);
273-
return static_cast<TMetric*>(it->second.Metric.Get());
274-
}
275-
}
276-
277-
{
278-
IMetricPtr metric = MakeIntrusive<TMetric>(std::forward<Args>(args)...);
279-
280-
TWriteGuard g{*Lock_};
281-
// decltype(Metrics_)::iterator breaks build on windows
282-
THashMap<ILabelsPtr, TMetricValue>::iterator it;
283-
TMetricValue metricValue = {metric, opts};
284-
if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) {
285-
it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metricValue)).first;
286-
} else {
287-
it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metricValue)).first;
288-
}
289-
290-
return static_cast<TMetric*>(it->second.Metric.Get());
291-
}
292-
}
293-
294238
void TMetricRegistry::RemoveMetric(const ILabels& labels) noexcept {
295239
TWriteGuard g{*Lock_};
296240
Metrics_.erase(labels);

library/cpp/monlib/metrics/metric_registry.h

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,45 @@ namespace NMonitoring {
274274
TMetricOpts Opts;
275275
};
276276

277+
protected:
278+
template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args>
279+
TMetric* Metric(TLabelsType&& labels, TMetricOpts&& opts, Args&&... args) {
280+
{
281+
TReadGuard g{*Lock_};
282+
283+
auto it = Metrics_.find(labels);
284+
if (it != Metrics_.end()) {
285+
Y_ENSURE(it->second.Metric->Type() == type, "cannot create metric " << labels
286+
<< " with type " << MetricTypeToStr(type)
287+
<< ", because registry already has same metric with type " << MetricTypeToStr(it->second.Metric->Type()));
288+
Y_ENSURE(it->second.Opts.MemOnly == opts.MemOnly,"cannot create metric " << labels
289+
<< " with memOnly=" << opts.MemOnly
290+
<< ", because registry already has same metric with memOnly=" << it->second.Opts.MemOnly);
291+
return static_cast<TMetric*>(it->second.Metric.Get());
292+
}
293+
}
294+
295+
{
296+
IMetricPtr metric = MakeIntrusive<TMetric>(std::forward<Args>(args)...);
297+
298+
TWriteGuard g{*Lock_};
299+
// decltype(Metrics_)::iterator breaks build on windows
300+
THashMap<ILabelsPtr, TMetricValue>::iterator it;
301+
TMetricValue metricValue = {metric, opts};
302+
if constexpr (!std::is_convertible_v<TLabelsType, ILabelsPtr>) {
303+
it = Metrics_.emplace(new TLabels{std::forward<TLabelsType>(labels)}, std::move(metricValue)).first;
304+
} else {
305+
it = Metrics_.emplace(std::forward<TLabelsType>(labels), std::move(metricValue)).first;
306+
}
307+
308+
return static_cast<TMetric*>(it->second.Metric.Get());
309+
}
310+
}
311+
277312
private:
278313
THolder<TRWMutex> Lock_ = MakeHolder<TRWMutex>();
279314
THashMap<ILabelsPtr, TMetricValue> Metrics_;
280315

281-
template <typename TMetric, EMetricType type, typename TLabelsType, typename... Args>
282-
TMetric* Metric(TLabelsType&& labels, TMetricOpts&& opts, Args&&... args);
283-
284316
TLabels CommonLabels_;
285317
};
286318

0 commit comments

Comments
 (0)