Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jul 30, 2024
1 parent f7576e3 commit 9ea0141
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
28 changes: 16 additions & 12 deletions include/ylt/metric/metric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ struct metric_manager_t {
}
auto lock = get_lock<true>();
for (auto& name : names) {
metric_map_.erase(name);
metric_map_->erase(name);
}
}

Expand All @@ -290,7 +290,7 @@ struct metric_manager_t {
}
auto lock = get_lock<true>();
for (auto& metric : metrics) {
metric_map_.erase(std::string(metric->name()));
metric_map_->erase(std::string(metric->name()));
}
}

Expand Down Expand Up @@ -532,7 +532,11 @@ struct metric_manager_t {
<< g_user_metric_count;
return false;
}
bool r = metric_map_.emplace(name, std::move(metric)).second;
if (metric_map_ == nullptr) {
metric_map_ = std::make_shared<
std::unordered_map<std::string, std::shared_ptr<metric_t>>>();
}
bool r = metric_map_->emplace(name, std::move(metric)).second;
if (!r) {
CINATRA_LOG_ERROR << "duplicate registered metric name: " << name;
}
Expand All @@ -542,27 +546,27 @@ struct metric_manager_t {
template <bool need_lock>
static size_t remove_metric_impl(const std::string& name) {
auto lock = get_lock<need_lock>();
return metric_map_.erase(name);
return metric_map_->erase(name);
}

template <bool need_lock>
static auto metric_map_impl() {
auto lock = get_lock<need_lock>();
return metric_map_;
return *metric_map_;
}

template <bool need_lock>
static size_t metric_count_impl() {
auto lock = get_lock<need_lock>();
return metric_map_.size();
return metric_map_->size();
}

template <bool need_lock>
static std::vector<std::string> metric_keys_impl() {
std::vector<std::string> keys;
{
auto lock = get_lock<need_lock>();
for (auto& pair : metric_map_) {
for (auto& pair : *metric_map_) {
keys.push_back(pair.first);
}
}
Expand All @@ -573,8 +577,8 @@ struct metric_manager_t {
template <bool need_lock>
static std::shared_ptr<metric_t> get_metric_impl(const std::string& name) {
auto lock = get_lock<need_lock>();
auto it = metric_map_.find(name);
if (it == metric_map_.end()) {
auto it = metric_map_->find(name);
if (it == metric_map_->end()) {
return nullptr;
}
return it->second;
Expand All @@ -585,7 +589,7 @@ struct metric_manager_t {
std::vector<std::shared_ptr<metric_t>> metrics;
{
auto lock = get_lock<need_lock>();
for (auto& pair : metric_map_) {
for (auto& pair : *metric_map_) {
metrics.push_back(pair.second);
}
}
Expand Down Expand Up @@ -653,8 +657,8 @@ struct metric_manager_t {
}

static inline std::mutex mtx_;
static inline std::unordered_map<std::string, std::shared_ptr<metric_t>>
metric_map_;
static inline auto metric_map_ = std::make_shared<
std::unordered_map<std::string, std::shared_ptr<metric_t>>>();

static inline null_mutex_t null_mtx_;
static inline std::atomic_bool need_lock_ = true;
Expand Down
16 changes: 13 additions & 3 deletions src/metric/tests/test_metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@
using namespace ylt;
using namespace ylt::metric;

template <size_t id>
struct test_id_t {};

using my_metric_mgr = metric_manager_t<test_id_t<1>>;

auto g_counter = my_metric_mgr::create_metric_dynamic<counter_t>("test1", "");

TEST_CASE("test no lable") {
{
g_counter->inc();
g_counter->inc();
CHECK(g_counter->value() == 2);
}

{
gauge_t g{"test_gauge", "help"};
g.inc();
Expand Down Expand Up @@ -303,9 +316,6 @@ TEST_CASE("test register metric") {
}
}

template <size_t id>
struct test_id_t {};

TEST_CASE("test remove metric and serialize metrics") {
using metric_mgr = metric_manager_t<test_id_t<1>>;
metric_mgr::create_metric_dynamic<counter_t>("test_counter", "");
Expand Down

0 comments on commit 9ea0141

Please sign in to comment.