Skip to content

Commit dedfdca

Browse files
committed
[Fix] coredump if add new c++ metrics
1 parent f5261e9 commit dedfdca

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)