Skip to content

Commit 12a69e2

Browse files
committed
add inner metrics
1 parent 1f1ba52 commit 12a69e2

File tree

10 files changed

+725
-51
lines changed

10 files changed

+725
-51
lines changed

include/ylt/metric.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
#define CINATRA_ENABLE_METRIC_JSON
1818
#include "metric/gauge.hpp"
1919
#include "metric/histogram.hpp"
20-
#include "metric/metric.hpp"
20+
#include "metric/system_metric.hpp"
2121
#include "metric/summary.hpp"
2222
#include "ylt/struct_json/json_writer.h"

include/ylt/metric/counter.hpp

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class counter_t : public metric_t {
2828
counter_t(std::string name, std::string help)
2929
: metric_t(MetricType::Counter, std::move(name), std::move(help)) {
3030
use_atomic_ = true;
31+
g_user_metric_count++;
3132
}
3233

3334
// static labels value, contains a map with atomic value.
@@ -36,16 +37,19 @@ class counter_t : public metric_t {
3637
: metric_t(MetricType::Counter, std::move(name), std::move(help),
3738
std::move(labels)) {
3839
atomic_value_map_.emplace(labels_value_, 0);
40+
g_user_metric_count++;
3941
use_atomic_ = true;
4042
}
4143

4244
// dynamic labels value
4345
counter_t(std::string name, std::string help,
4446
std::vector<std::string> labels_name)
4547
: metric_t(MetricType::Counter, std::move(name), std::move(help),
46-
std::move(labels_name)) {}
48+
std::move(labels_name)) {
49+
g_user_metric_count++;
50+
}
4751

48-
virtual ~counter_t() {}
52+
virtual ~counter_t() { g_user_metric_count--; }
4953

5054
double value() { return default_lable_value_; }
5155

@@ -82,20 +86,16 @@ class counter_t : public metric_t {
8286
return;
8387
}
8488

85-
serialize_head(str);
86-
std::string s;
87-
if (use_atomic_) {
88-
serialize_map(atomic_value_map_, s);
89-
}
90-
else {
91-
serialize_map(value_map_, s);
89+
auto map = value_map();
90+
if (map.empty()) {
91+
return;
9292
}
9393

94-
if (s.empty()) {
95-
str.clear();
96-
}
97-
else {
98-
str.append(s);
94+
std::string value_str;
95+
serialize_map(map, value_str);
96+
if (!value_str.empty()) {
97+
serialize_head(str);
98+
str.append(value_str);
9999
}
100100
}
101101

@@ -113,18 +113,17 @@ class counter_t : public metric_t {
113113
return;
114114
}
115115

116+
auto map = value_map();
116117
json_counter_t counter{name_, help_, std::string(metric_name())};
117-
if (use_atomic_) {
118-
to_json(counter, atomic_value_map_, str);
119-
}
120-
else {
121-
to_json(counter, value_map_, str);
122-
}
118+
to_json(counter, map, str);
123119
}
124120

125121
template <typename T>
126122
void to_json(json_counter_t &counter, T &map, std::string &str) {
127123
for (auto &[k, v] : map) {
124+
if (v == 0) {
125+
continue;
126+
}
128127
json_counter_metric_t metric;
129128
size_t index = 0;
130129
for (auto &label_value : k) {
@@ -133,7 +132,9 @@ class counter_t : public metric_t {
133132
metric.value = (int64_t)v;
134133
counter.metrics.push_back(std::move(metric));
135134
}
136-
iguana::to_json(counter, str);
135+
if (!counter.metrics.empty()) {
136+
iguana::to_json(counter, str);
137+
}
137138
}
138139
#endif
139140

@@ -164,10 +165,21 @@ class counter_t : public metric_t {
164165
}
165166
else {
166167
std::lock_guard lock(mtx_);
168+
stat_metric(labels_value);
167169
set_value<false>(value_map_[labels_value], value, op_type_t::INC);
168170
}
169171
}
170172

173+
void stat_metric(const std::vector<std::string> &labels_value) {
174+
if (!value_map_.contains(labels_value)) {
175+
for (auto &key : labels_value) {
176+
g_user_metric_memory->inc(key.size());
177+
}
178+
g_user_metric_memory->inc(8);
179+
g_user_metric_labels->inc();
180+
}
181+
}
182+
171183
void update(double value) { default_lable_value_ = value; }
172184

173185
void update(const std::vector<std::string> &labels_value, double value) {

include/ylt/metric/gauge.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class gauge_t : public counter_t {
4545
}
4646
else {
4747
std::lock_guard lock(mtx_);
48+
stat_metric(labels_value);
4849
set_value<false>(value_map_[labels_value], value, op_type_t::DEC);
4950
}
5051
}

include/ylt/metric/histogram.hpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ class histogram_t : public metric_t {
108108
return;
109109
}
110110

111+
if (sum_->value() == 0) {
112+
return;
113+
}
114+
111115
serialize_head(str);
112116
double count = 0;
113117
auto bucket_counts = get_bucket_counts();
@@ -146,6 +150,10 @@ class histogram_t : public metric_t {
146150
return;
147151
}
148152

153+
if (sum_->value() == 0) {
154+
return;
155+
}
156+
149157
json_histogram_t hist{name_, help_, std::string(metric_name())};
150158

151159
double count = 0;
@@ -183,11 +191,13 @@ class histogram_t : public metric_t {
183191
}
184192

185193
void serialize_with_labels(std::string &str) {
186-
serialize_head(str);
194+
auto value_map = sum_->value_map();
195+
if (value_map.empty()) {
196+
return;
197+
}
187198

199+
std::string value_str;
188200
auto bucket_counts = get_bucket_counts();
189-
190-
auto value_map = sum_->value_map();
191201
for (auto &[labels_value, value] : value_map) {
192202
if (value == 0) {
193203
continue;
@@ -196,24 +206,31 @@ class histogram_t : public metric_t {
196206
double count = 0;
197207
for (size_t i = 0; i < bucket_counts.size(); i++) {
198208
auto counter = bucket_counts[i];
199-
str.append(name_).append("_bucket{");
209+
value_str.append(name_).append("_bucket{");
200210
build_label_string(str, sum_->labels_name(), labels_value);
201-
str.append(",");
211+
value_str.append(",");
202212

203213
if (i == bucket_boundaries_.size()) {
204-
str.append("le=\"").append("+Inf").append("\"} ");
214+
value_str.append("le=\"").append("+Inf").append("\"} ");
205215
}
206216
else {
207-
str.append("le=\"")
217+
value_str.append("le=\"")
208218
.append(std::to_string(bucket_boundaries_[i]))
209219
.append("\"} ");
210220
}
211221

212222
count += counter->value(labels_value);
213-
str.append(std::to_string(count));
214-
str.append("\n");
223+
value_str.append(std::to_string(count));
224+
value_str.append("\n");
225+
}
226+
227+
if (value_str.empty()) {
228+
return;
215229
}
216230

231+
serialize_head(str);
232+
str.append(value_str);
233+
217234
str.append(name_);
218235
str.append("_sum{");
219236
build_label_string(str, sum_->labels_name(), labels_value);
@@ -237,10 +254,14 @@ class histogram_t : public metric_t {
237254

238255
#ifdef CINATRA_ENABLE_METRIC_JSON
239256
void serialize_to_json_with_labels(std::string &str) {
257+
auto value_map = sum_->value_map();
258+
if (value_map.empty()) {
259+
return;
260+
}
261+
240262
json_histogram_t hist{name_, help_, std::string(metric_name())};
241263
auto bucket_counts = get_bucket_counts();
242264

243-
auto value_map = sum_->value_map();
244265
for (auto &[labels_value, value] : value_map) {
245266
if (value == 0) {
246267
continue;
@@ -272,7 +293,9 @@ class histogram_t : public metric_t {
272293
hist.metrics.push_back(std::move(metric));
273294
}
274295

275-
iguana::to_json(hist, str);
296+
if (!hist.metrics.empty()) {
297+
iguana::to_json(hist, str);
298+
}
276299
}
277300
#endif
278301

include/ylt/metric/metric.hpp

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,22 @@ class metric_t {
201201
std::chrono::system_clock::time_point metric_created_time_{};
202202
};
203203

204-
template <size_t ID = 0>
204+
template <typename Tag>
205+
struct metric_manager_t;
206+
207+
struct ylt_system_tag_t {};
208+
using system_metric_manager = metric_manager_t<ylt_system_tag_t>;
209+
210+
class counter_t;
211+
inline auto g_user_metric_memory =
212+
std::make_shared<counter_t>("ylt_user_metric_memory", "");
213+
inline auto g_user_metric_labels =
214+
std::make_shared<counter_t>("ylt_user_metric_labels", "");
215+
inline auto g_summary_failed_count =
216+
std::make_shared<counter_t>("ylt_summary_failed_count", "");
217+
inline std::atomic<int64_t> g_user_metric_count = 0;
218+
219+
template <typename Tag>
205220
struct metric_manager_t {
206221
struct null_mutex_t {
207222
void lock() {}
@@ -263,6 +278,15 @@ struct metric_manager_t {
263278
return r;
264279
}
265280

281+
static auto get_metrics() {
282+
if (need_lock_) {
283+
return collect<true>();
284+
}
285+
else {
286+
return collect<false>();
287+
}
288+
}
289+
266290
static auto metric_map_static() { return metric_map_impl<false>(); }
267291
static auto metric_map_dynamic() { return metric_map_impl<true>(); }
268292

@@ -580,5 +604,34 @@ struct metric_manager_t {
580604
static inline std::once_flag flag_;
581605
};
582606

583-
using default_metric_manager = metric_manager_t<0>;
607+
struct ylt_default_metric_tag_t {};
608+
using default_metric_manager = metric_manager_t<ylt_default_metric_tag_t>;
609+
610+
template <typename... Args>
611+
struct metric_collector_t {
612+
static std::string serialize() {
613+
auto vec = get_all_metrics();
614+
return default_metric_manager::serialize(vec);
615+
}
616+
617+
#ifdef CINATRA_ENABLE_METRIC_JSON
618+
static std::string serialize_to_json() {
619+
auto vec = get_all_metrics();
620+
return default_metric_manager::serialize_to_json(vec);
621+
}
622+
#endif
623+
624+
static std::vector<std::shared_ptr<metric_t>> get_all_metrics() {
625+
std::vector<std::shared_ptr<metric_t>> vec;
626+
(append_vector<Args>(vec), ...);
627+
return vec;
628+
}
629+
630+
private:
631+
template <typename T>
632+
static void append_vector(std::vector<std::shared_ptr<metric_t>>& vec) {
633+
auto v = T::get_metrics();
634+
vec.insert(vec.end(), v.begin(), v.end());
635+
}
636+
};
584637
} // namespace ylt::metric

include/ylt/metric/summary.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class summary_t : public metric_t {
109109
throw std::invalid_argument("not a default label metric");
110110
}
111111
if (block_->sample_queue_.size_approx() >= 20000000) {
112-
// TODO: record failed count.
112+
g_summary_failed_count->inc();
113113
return;
114114
}
115115
block_->sample_queue_.enqueue(value);
@@ -131,7 +131,7 @@ class summary_t : public metric_t {
131131
}
132132
}
133133
if (labels_block_->sample_queue_.size_approx() >= 20000000) {
134-
// TODO: record failed count.
134+
g_summary_failed_count->inc();
135135
return;
136136
}
137137
labels_block_->sample_queue_.enqueue({std::move(labels_value), value});

0 commit comments

Comments
 (0)