Skip to content

Commit

Permalink
sync metric (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 3, 2024
1 parent aa315d1 commit 2e37dcc
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 287 deletions.
8 changes: 7 additions & 1 deletion include/cinatra/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,18 @@ class basic_dynamic_counter

// }
// else {
size_t count = 0;
std::vector<std::string_view> vec;
for (auto &lb_name : labels_name) {
if (auto i = labels.find(lb_name); i != labels.end()) {
vec.push_back(i->second);
}
else {
vec.push_back("");
count++;
}
}
if (vec.empty()) {
if (count == labels_name.size()) {
return;
}
Base::erase_if([&](auto &pair) {
Expand Down Expand Up @@ -238,6 +240,10 @@ class basic_dynamic_counter
bool has_label_value(const std::vector<std::string> &label_value) override {
std::array<std::string, N> arr{};
size_t size = (std::min)((size_t)N, label_value.size());
if (label_value.size() > N) {
return false;
}

for (size_t i = 0; i < size; i++) {
arr[i] = label_value[i];
}
Expand Down
4 changes: 2 additions & 2 deletions include/cinatra/ylt/metric/dynamic_metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "metric.hpp"
#include "thread_local_value.hpp"
#if __has_include("ylt/util/type_traits.h")
#if __has_include("ylt/util/map_sharded.hpp")
#include "ylt/util/map_sharded.hpp"
#else
#include "../util/map_sharded.hpp"
Expand Down Expand Up @@ -131,4 +131,4 @@ class dynamic_metric_impl : public dynamic_metric {
my_hash<137>>
map_{std::min<unsigned>(128u, std::thread::hardware_concurrency())};
};
} // namespace ylt::metric
} // namespace ylt::metric
1 change: 1 addition & 0 deletions include/cinatra/ylt/metric/gauge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <chrono>

#include "counter.hpp"
#include "metric.hpp"

namespace ylt::metric {

Expand Down
7 changes: 3 additions & 4 deletions include/cinatra/ylt/metric/histogram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,6 @@ class basic_dynamic_histogram : public dynamic_metric {
value_str.append("\n");
}

if (value_str.empty()) {
return;
}

str.append(value_str);

str.append(name_);
Expand All @@ -262,6 +258,9 @@ class basic_dynamic_histogram : public dynamic_metric {
str.append(std::to_string(count));
str.append("\n");
}
if (value_str.empty()) {
str.clear();
}
}

#ifdef CINATRA_ENABLE_METRIC_JSON
Expand Down
10 changes: 1 addition & 9 deletions include/cinatra/ylt/metric/metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "async_simple/coro/Lazy.h"
#include "async_simple/coro/SyncAwait.h"
#include "cinatra/cinatra_log_wrapper.hpp"
#include "thread_local_value.hpp"
#if __has_include("ylt/coro_io/coro_io.hpp")
#include "ylt/coro_io/coro_io.hpp"
#else
Expand Down Expand Up @@ -53,6 +52,7 @@ struct metric_filter_options {
class metric_t {
public:
static inline std::atomic<int64_t> g_user_metric_count = 0;

metric_t() = default;
metric_t(MetricType type, std::string name, std::string help)
: type_(type),
Expand Down Expand Up @@ -92,8 +92,6 @@ class metric_t {

MetricType metric_type() { return type_; }

auto get_created_time() { return metric_created_time_; }

std::string_view metric_name() {
switch (type_) {
case MetricType::Counter:
Expand Down Expand Up @@ -197,16 +195,10 @@ inline std::chrono::seconds ylt_label_max_age{0};
inline std::chrono::seconds ylt_label_check_expire_duration{60};

inline std::atomic<int64_t> ylt_metric_capacity = 10000000;
inline int64_t ylt_label_capacity = 20000000;

inline void set_metric_capacity(int64_t max_count) {
ylt_metric_capacity = max_count;
}

inline void set_label_capacity(int64_t max_label_count) {
ylt_label_capacity = max_label_count;
}

inline void set_label_max_age(
std::chrono::seconds max_age,
std::chrono::seconds check_duration = std::chrono::seconds{60}) {
Expand Down
16 changes: 12 additions & 4 deletions include/cinatra/ylt/metric/metric_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <utility>

#include "metric.hpp"
#if __has_include("ylt/util/map_sharded.hpp")
#include "ylt/util/map_sharded.hpp"
#else
#include "../util/map_sharded.hpp"
#endif

namespace ylt::metric {
class manager_helper {
Expand Down Expand Up @@ -36,9 +41,6 @@ class manager_helper {
#ifdef CINATRA_ENABLE_METRIC_JSON
static std::string serialize_to_json(
const std::vector<std::shared_ptr<metric_t>>& metrics) {
if (metrics.empty()) {
return "";
}
std::string str;
str.append("[");
for (auto& m : metrics) {
Expand All @@ -49,7 +51,10 @@ class manager_helper {
}

if (str.size() == 1) {
return "";
str.append("]");
}
else {
str.back() = ']';
}

str.back() = ']';
Expand Down Expand Up @@ -139,6 +144,9 @@ class manager_helper {
static void filter_by_label_name(
std::vector<std::shared_ptr<metric_t>>& filtered_metrics,
std::shared_ptr<metric_t> m, const metric_filter_options& options) {
if (!options.label_regex) {
return;
}
const auto& labels_name = m->labels_name();
for (auto& label_name : labels_name) {
if (std::regex_match(label_name, *options.label_regex)) {
Expand Down
1 change: 0 additions & 1 deletion include/cinatra/ylt/metric/summary_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class summary_impl {
fltInt16 |= (fltInt32 >> 15) & 0xff;

auto i = fltInt16 >> (8 - frac_bit);
auto j = decode_impl(i);
return i;
}

Expand Down
Loading

0 comments on commit 2e37dcc

Please sign in to comment.