Skip to content

Commit 3ccbd66

Browse files
authored
[Fix] coredump if add new c++ metrics (#666)
<!-- Thanks for sending a pull request! BEFORE SUBMITTING, PLEASE READ OUR OFFICIAL WEBSITE. --> # Purpose What this PR does / why we need it? <!-- - Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. If possible, please consider writing useful notes for better and faster reviews in your PR. - Please clarify why the changes are needed. For instance, the use case and bug description. - Fixes # --> When adding new metric in c++, it would core dump and print "Please call SetUp() first!" # Modifications Does this PR introduce _any_ user-facing change? <!-- Note that it means *any* user-facing change including all aspects such as API, interface or other behavior changes. Documentation-only updates are not considered user-facing changes. --> No using changes # Test How was this patch tested? <!-- CI passed with new added/existing test. If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future. If tests were not added, please describe why they were not added and/or why it was difficult to add. --> Tested with adding new metrics and online services ![img_v3_02u6_5dd8aa32-f951-44dd-8de8-fc22d634f34g](https://github.com/user-attachments/assets/31d12621-a885-44c2-97e9-43decc130ad8) ![img_v3_02u6_e73f6c7e-7f3f-416a-8852-f6f0eacdd6dg](https://github.com/user-attachments/assets/bdc1fc96-0ad8-49ec-8f64-8def75fa7a88)
1 parent 015a128 commit 3ccbd66

4 files changed

Lines changed: 10 additions & 11 deletions

File tree

ucm/shared/metrics/cc/api/metrics_api.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "metrics_api.h"
2525
namespace UC::Metrics {
2626

27-
void SetUp(size_t maxVectorLen) { Metrics::SetUp(maxVectorLen); }
27+
void SetUp(size_t maxVectorLen) { Metrics::GetInstance().SetUp(maxVectorLen); }
2828

2929
void CreateStats(const std::string& name, const std::string& type)
3030
{

ucm/shared/metrics/cc/domain/metrics.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ thread_local std::shared_ptr<MetricBuffer> Metrics::threadBuffer_ =
2929
std::make_shared<MetricBuffer>();
3030
thread_local bool Metrics::isRegisteredThread_ = false;
3131

32-
std::atomic<bool> Metrics::isInited_{false};
33-
size_t Metrics::maxVectorLen_{10000};
34-
3532
void Metrics::CreateStats(const std::string& name, const std::string& type)
3633
{
37-
std::unique_lock<std::shared_mutex> lock(mutex_);
34+
if (!isInited_.load(std::memory_order_acquire)) {
35+
throw std::runtime_error("Please call SetUp() first!");
36+
}
3837
std::string typeUpper = type;
3938
std::transform(typeUpper.begin(), typeUpper.end(), typeUpper.begin(), ::toupper);
39+
std::unique_lock<std::shared_mutex> lock(mutex_);
4040
if (statsType_.count(name)) {
4141
return;
4242
} else {

ucm/shared/metrics/cc/domain/metrics.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <tuple>
3636
#include <unordered_map>
3737
#include <vector>
38-
3938
namespace UC::Metrics {
4039
struct MetricBuffer {
4140
struct InnerBuffer {
@@ -74,13 +73,13 @@ class Metrics {
7473
public:
7574
static Metrics& GetInstance()
7675
{
77-
if (!isInited_) { throw std::runtime_error("Please call SetUp() first!"); }
7876
static Metrics inst;
7977
return inst;
8078
}
8179

82-
static void SetUp(size_t maxVectorLen)
80+
void SetUp(size_t maxVectorLen)
8381
{
82+
std::unique_lock<std::shared_mutex> lock(mutex_);
8483
if (isInited_.load(std::memory_order_acquire)) { return; }
8584
bool expected = false;
8685
if (isInited_.compare_exchange_strong(expected, true, std::memory_order_release,
@@ -113,8 +112,8 @@ class Metrics {
113112
Metrics() = default;
114113
Metrics(const Metrics&) = delete;
115114
Metrics& operator=(const Metrics&) = delete;
116-
static std::atomic<bool> isInited_;
117-
static size_t maxVectorLen_;
115+
std::atomic<bool> isInited_{false};
116+
size_t maxVectorLen_{10000};
118117
};
119118
} // namespace UC::Metrics
120119

ucm/shared/test/case/metrics/metrics_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class UCMetricsUT : public testing::Test {
3535
void SetUp() override
3636
{
3737
try {
38-
Metrics::SetUp(1000000);
38+
UC::Metrics::SetUp(1000000);
3939
CreateStats("stats1", "counter");
4040
CreateStats("stats2", "gauge");
4141
CreateStats("stats3", "histogram");

0 commit comments

Comments
 (0)