Skip to content

Commit 2e37dcc

Browse files
authored
sync metric (#656)
1 parent aa315d1 commit 2e37dcc

10 files changed

+433
-287
lines changed

include/cinatra/ylt/metric/counter.hpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,18 @@ class basic_dynamic_counter
180180

181181
// }
182182
// else {
183+
size_t count = 0;
183184
std::vector<std::string_view> vec;
184185
for (auto &lb_name : labels_name) {
185186
if (auto i = labels.find(lb_name); i != labels.end()) {
186187
vec.push_back(i->second);
187188
}
188189
else {
189190
vec.push_back("");
191+
count++;
190192
}
191193
}
192-
if (vec.empty()) {
194+
if (count == labels_name.size()) {
193195
return;
194196
}
195197
Base::erase_if([&](auto &pair) {
@@ -238,6 +240,10 @@ class basic_dynamic_counter
238240
bool has_label_value(const std::vector<std::string> &label_value) override {
239241
std::array<std::string, N> arr{};
240242
size_t size = (std::min)((size_t)N, label_value.size());
243+
if (label_value.size() > N) {
244+
return false;
245+
}
246+
241247
for (size_t i = 0; i < size; i++) {
242248
arr[i] = label_value[i];
243249
}

include/cinatra/ylt/metric/dynamic_metric.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "metric.hpp"
55
#include "thread_local_value.hpp"
6-
#if __has_include("ylt/util/type_traits.h")
6+
#if __has_include("ylt/util/map_sharded.hpp")
77
#include "ylt/util/map_sharded.hpp"
88
#else
99
#include "../util/map_sharded.hpp"
@@ -131,4 +131,4 @@ class dynamic_metric_impl : public dynamic_metric {
131131
my_hash<137>>
132132
map_{std::min<unsigned>(128u, std::thread::hardware_concurrency())};
133133
};
134-
} // namespace ylt::metric
134+
} // namespace ylt::metric

include/cinatra/ylt/metric/gauge.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <chrono>
44

55
#include "counter.hpp"
6+
#include "metric.hpp"
67

78
namespace ylt::metric {
89

include/cinatra/ylt/metric/histogram.hpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,6 @@ class basic_dynamic_histogram : public dynamic_metric {
242242
value_str.append("\n");
243243
}
244244

245-
if (value_str.empty()) {
246-
return;
247-
}
248-
249245
str.append(value_str);
250246

251247
str.append(name_);
@@ -262,6 +258,9 @@ class basic_dynamic_histogram : public dynamic_metric {
262258
str.append(std::to_string(count));
263259
str.append("\n");
264260
}
261+
if (value_str.empty()) {
262+
str.clear();
263+
}
265264
}
266265

267266
#ifdef CINATRA_ENABLE_METRIC_JSON

include/cinatra/ylt/metric/metric.hpp

+1-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "async_simple/coro/Lazy.h"
1515
#include "async_simple/coro/SyncAwait.h"
1616
#include "cinatra/cinatra_log_wrapper.hpp"
17-
#include "thread_local_value.hpp"
1817
#if __has_include("ylt/coro_io/coro_io.hpp")
1918
#include "ylt/coro_io/coro_io.hpp"
2019
#else
@@ -53,6 +52,7 @@ struct metric_filter_options {
5352
class metric_t {
5453
public:
5554
static inline std::atomic<int64_t> g_user_metric_count = 0;
55+
5656
metric_t() = default;
5757
metric_t(MetricType type, std::string name, std::string help)
5858
: type_(type),
@@ -92,8 +92,6 @@ class metric_t {
9292

9393
MetricType metric_type() { return type_; }
9494

95-
auto get_created_time() { return metric_created_time_; }
96-
9795
std::string_view metric_name() {
9896
switch (type_) {
9997
case MetricType::Counter:
@@ -197,16 +195,10 @@ inline std::chrono::seconds ylt_label_max_age{0};
197195
inline std::chrono::seconds ylt_label_check_expire_duration{60};
198196

199197
inline std::atomic<int64_t> ylt_metric_capacity = 10000000;
200-
inline int64_t ylt_label_capacity = 20000000;
201-
202198
inline void set_metric_capacity(int64_t max_count) {
203199
ylt_metric_capacity = max_count;
204200
}
205201

206-
inline void set_label_capacity(int64_t max_label_count) {
207-
ylt_label_capacity = max_label_count;
208-
}
209-
210202
inline void set_label_max_age(
211203
std::chrono::seconds max_age,
212204
std::chrono::seconds check_duration = std::chrono::seconds{60}) {

include/cinatra/ylt/metric/metric_manager.hpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
#include <utility>
55

66
#include "metric.hpp"
7+
#if __has_include("ylt/util/map_sharded.hpp")
8+
#include "ylt/util/map_sharded.hpp"
9+
#else
10+
#include "../util/map_sharded.hpp"
11+
#endif
712

813
namespace ylt::metric {
914
class manager_helper {
@@ -36,9 +41,6 @@ class manager_helper {
3641
#ifdef CINATRA_ENABLE_METRIC_JSON
3742
static std::string serialize_to_json(
3843
const std::vector<std::shared_ptr<metric_t>>& metrics) {
39-
if (metrics.empty()) {
40-
return "";
41-
}
4244
std::string str;
4345
str.append("[");
4446
for (auto& m : metrics) {
@@ -49,7 +51,10 @@ class manager_helper {
4951
}
5052

5153
if (str.size() == 1) {
52-
return "";
54+
str.append("]");
55+
}
56+
else {
57+
str.back() = ']';
5358
}
5459

5560
str.back() = ']';
@@ -139,6 +144,9 @@ class manager_helper {
139144
static void filter_by_label_name(
140145
std::vector<std::shared_ptr<metric_t>>& filtered_metrics,
141146
std::shared_ptr<metric_t> m, const metric_filter_options& options) {
147+
if (!options.label_regex) {
148+
return;
149+
}
142150
const auto& labels_name = m->labels_name();
143151
for (auto& label_name : labels_name) {
144152
if (std::regex_match(label_name, *options.label_regex)) {

include/cinatra/ylt/metric/summary_impl.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ class summary_impl {
7373
fltInt16 |= (fltInt32 >> 15) & 0xff;
7474

7575
auto i = fltInt16 >> (8 - frac_bit);
76-
auto j = decode_impl(i);
7776
return i;
7877
}
7978

0 commit comments

Comments
 (0)