From 1634754a81b42de7a83282c5ce9bcd9e963f53d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Thu, 28 Nov 2024 03:52:56 +0000 Subject: [PATCH 1/5] init --- core/CMakeLists.txt | 2 +- core/app_config/AppConfig.cpp | 17 -- core/app_config/AppConfig.h | 2 - core/application/Application.cpp | 3 - core/ebpf/SelfMonitor.h | 4 +- core/ebpf/eBPFServer.cpp | 2 +- core/ebpf/eBPFServer.h | 2 +- core/ebpf/handler/AbstractHandler.h | 2 +- core/file_server/FileServer.h | 2 +- core/file_server/event_handler/LogInput.cpp | 15 +- core/file_server/polling/PollingDirFile.cpp | 12 +- core/file_server/polling/PollingModify.cpp | 4 +- core/logger/Logger.cpp | 8 - core/monitor/MetricManager.cpp | 158 --------------- core/monitor/MetricManager.h | 47 +---- core/monitor/MetricStore.cpp | 81 -------- core/monitor/MetricStore.h | 58 ------ core/monitor/Monitor.cpp | 126 +----------- core/monitor/Monitor.h | 7 +- .../{ => metric_models}/MetricRecord.cpp | 0 .../{ => metric_models}/MetricRecord.h | 0 .../monitor/{ => metric_models}/MetricTypes.h | 0 .../ReentrantMetricsRecord.cpp} | 2 +- .../ReentrantMetricsRecord.h} | 0 .../metric_models/SelfMonitorMetricEvent.cpp | 182 ++++++++++++++++++ .../metric_models/SelfMonitorMetricEvent.h | 64 ++++++ .../plugin/instance/FlusherInstance.h | 2 +- core/plugin/input/InputContainerStdio.h | 2 +- core/plugin/input/InputFile.h | 2 +- core/plugin/input/InputFileSecurity.h | 2 +- core/plugin/input/InputNetworkObserver.cpp | 2 +- core/plugin/input/InputNetworkObserver.h | 2 +- core/plugin/input/InputNetworkSecurity.h | 2 +- core/plugin/input/InputProcessSecurity.h | 2 +- core/prometheus/PromSelfMonitor.cpp | 2 +- core/prometheus/PromSelfMonitor.h | 2 +- core/prometheus/PrometheusInputRunner.h | 2 +- core/prometheus/schedulers/ScrapeScheduler.h | 2 +- .../monitor/PluginMetricManagerUnittest.cpp | 2 +- 39 files changed, 280 insertions(+), 546 deletions(-) delete mode 100644 core/monitor/MetricStore.cpp delete mode 100644 core/monitor/MetricStore.h rename core/monitor/{ => metric_models}/MetricRecord.cpp (100%) rename core/monitor/{ => metric_models}/MetricRecord.h (100%) rename core/monitor/{ => metric_models}/MetricTypes.h (100%) rename core/monitor/{PluginMetricManager.cpp => metric_models/ReentrantMetricsRecord.cpp} (99%) rename core/monitor/{PluginMetricManager.h => metric_models/ReentrantMetricsRecord.h} (100%) create mode 100644 core/monitor/metric_models/SelfMonitorMetricEvent.cpp create mode 100644 core/monitor/metric_models/SelfMonitorMetricEvent.h diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index b1b91aaf73..3dd6af2c93 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -114,7 +114,7 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/plugin/flusher/links.cmake) # Subdirectories (modules). except for common, input, processor, flusher, observer, helper, spl, and provider. set(SUB_DIRECTORIES_LIST - application app_config checkpoint container_manager metadata logger go_pipeline monitor monitor/metric_constants monitor/profile_sender models + application app_config checkpoint container_manager metadata logger go_pipeline monitor monitor/metric_constants monitor/metric_models monitor/profile_sender models config config/watcher constants pipeline pipeline/batch pipeline/limiter pipeline/plugin pipeline/plugin/creator pipeline/plugin/instance pipeline/plugin/interface pipeline/queue pipeline/route pipeline/serializer task_pipeline diff --git a/core/app_config/AppConfig.cpp b/core/app_config/AppConfig.cpp index d492af6b7b..202d9f67f1 100644 --- a/core/app_config/AppConfig.cpp +++ b/core/app_config/AppConfig.cpp @@ -430,22 +430,6 @@ string GetAgentLogName() { } } -string GetAgentSnapshotDir() { - if (BOOL_FLAG(logtail_mode)) { - return GetProcessExecutionDir() + STRING_FLAG(logtail_snapshot_dir); - } else { - return GetAgentLogDir() + "snapshot"; - } -} - -string GetAgentStatusLogName() { - if (BOOL_FLAG(logtail_mode)) { - return "ilogtail_status.LOG"; - } else { - return "loongcollector_status.LOG"; - } -} - string GetObserverEbpfHostPath() { if (BOOL_FLAG(logtail_mode)) { return STRING_FLAG(sls_observer_ebpf_host_path); @@ -909,7 +893,6 @@ void AppConfig::LoadResourceConf(const Json::Value& confJson) { mSendRequestConcurrency = confJson["send_request_concurrency"].asInt(); else mSendRequestConcurrency = INT32_FLAG(send_request_concurrency); - LogtailMonitor::GetInstance()->UpdateConstMetric("send_request_concurrency", mSendRequestConcurrency); if (confJson.isMember("process_thread_count") && confJson["process_thread_count"].isInt()) mProcessThreadCount = confJson["process_thread_count"].asInt(); diff --git a/core/app_config/AppConfig.h b/core/app_config/AppConfig.h index 25cdb0fdcb..a1f0af7ec5 100644 --- a/core/app_config/AppConfig.h +++ b/core/app_config/AppConfig.h @@ -48,8 +48,6 @@ std::string GetLocalEventDataFileName(); std::string GetInotifyWatcherDirsDumpFileName(); std::string GetAgentLoggersPrefix(); std::string GetAgentLogName(); -std::string GetAgentSnapshotDir(); -std::string GetAgentStatusLogName(); std::string GetObserverEbpfHostPath(); std::string GetSendBufferFileNamePrefix(); std::string GetLegacyUserLocalConfigFilePath(); diff --git a/core/application/Application.cpp b/core/application/Application.cpp index 09901cced9..34564e238a 100644 --- a/core/application/Application.cpp +++ b/core/application/Application.cpp @@ -133,7 +133,6 @@ void Application::Init() { const string& configIP = AppConfig::GetInstance()->GetConfigIP(); if (!configIP.empty()) { LoongCollectorMonitor::mIpAddr = configIP; - LogtailMonitor::GetInstance()->UpdateConstMetric("logtail_ip", GetHostIp()); } else if (!interface.empty()) { LoongCollectorMonitor::mIpAddr = GetHostIp(interface); if (LoongCollectorMonitor::mIpAddr.empty()) { @@ -151,7 +150,6 @@ void Application::Init() { const string& configHostName = AppConfig::GetInstance()->GetConfigHostName(); if (!configHostName.empty()) { LoongCollectorMonitor::mHostname = configHostName; - LogtailMonitor::GetInstance()->UpdateConstMetric("logtail_hostname", GetHostName()); } GenerateInstanceId(); @@ -198,7 +196,6 @@ void Application::Init() { void Application::Start() { // GCOVR_EXCL_START LoongCollectorMonitor::mStartTime = GetTimeStamp(time(NULL), "%Y-%m-%d %H:%M:%S"); - LogtailMonitor::GetInstance()->UpdateConstMetric("start_time", LoongCollectorMonitor::mStartTime); #if defined(__ENTERPRISE__) && defined(_MSC_VER) InitWindowsSignalObject(); diff --git a/core/ebpf/SelfMonitor.h b/core/ebpf/SelfMonitor.h index 48847f8229..7189f3e9c4 100644 --- a/core/ebpf/SelfMonitor.h +++ b/core/ebpf/SelfMonitor.h @@ -19,9 +19,9 @@ #include #include "ebpf/include/export.h" -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" #include "common/Lock.h" -#include "monitor/MetricTypes.h" +#include "monitor/metric_models/MetricTypes.h" #include "monitor/metric_constants/MetricConstants.h" namespace logtail { diff --git a/core/ebpf/eBPFServer.cpp b/core/ebpf/eBPFServer.cpp index 501f833806..c87c14f63b 100644 --- a/core/ebpf/eBPFServer.cpp +++ b/core/ebpf/eBPFServer.cpp @@ -25,7 +25,7 @@ #include "ebpf/include/export.h" #include "common/LogtailCommonFlags.h" #include "common/MachineInfoUtil.h" -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" #include "common/Lock.h" DEFINE_FLAG_INT64(kernel_min_version_for_ebpf, diff --git a/core/ebpf/eBPFServer.h b/core/ebpf/eBPFServer.h index 24f79347ea..0f21fcdf07 100644 --- a/core/ebpf/eBPFServer.h +++ b/core/ebpf/eBPFServer.h @@ -29,7 +29,7 @@ #include "ebpf/handler/AbstractHandler.h" #include "ebpf/handler/ObserveHandler.h" #include "ebpf/handler/SecurityHandler.h" -#include "monitor/MetricTypes.h" +#include "monitor/metric_models/MetricTypes.h" #include "ebpf/SelfMonitor.h" namespace logtail { diff --git a/core/ebpf/handler/AbstractHandler.h b/core/ebpf/handler/AbstractHandler.h index b71924be33..140efb83c5 100644 --- a/core/ebpf/handler/AbstractHandler.h +++ b/core/ebpf/handler/AbstractHandler.h @@ -17,7 +17,7 @@ #include #include "pipeline/PipelineContext.h" -#include "monitor/MetricTypes.h" +#include "monitor/metric_models/MetricTypes.h" #include "monitor/MetricManager.h" namespace logtail{ diff --git a/core/file_server/FileServer.h b/core/file_server/FileServer.h index ec539dabdf..e7c30e14ba 100644 --- a/core/file_server/FileServer.h +++ b/core/file_server/FileServer.h @@ -25,7 +25,7 @@ #include "file_server/MultilineOptions.h" #include "file_server/reader/FileReaderOptions.h" #include "monitor/MetricManager.h" -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" #include "pipeline/PipelineContext.h" diff --git a/core/file_server/event_handler/LogInput.cpp b/core/file_server/event_handler/LogInput.cpp index 6765717922..340a6b6763 100644 --- a/core/file_server/event_handler/LogInput.cpp +++ b/core/file_server/event_handler/LogInput.cpp @@ -349,21 +349,10 @@ void LogInput::ProcessEvent(EventDispatcher* dispatcher, Event* ev) { } void LogInput::UpdateCriticalMetric(int32_t curTime) { - LogtailMonitor::GetInstance()->UpdateMetric("last_read_event_time", - GetTimeStamp(mLastReadEventTime, "%Y-%m-%d %H:%M:%S")); mLastRunTime->Set(mLastReadEventTime.load()); - - LogtailMonitor::GetInstance()->UpdateMetric("event_tps", - 1.0 * mEventProcessCount / (curTime - mLastUpdateMetricTime)); - int32_t openFdTotal = GloablFileDescriptorManager::GetInstance()->GetOpenedFilePtrSize(); - LogtailMonitor::GetInstance()->UpdateMetric("open_fd", openFdTotal); - LoongCollectorMonitor::GetInstance()->SetAgentOpenFdTotal(openFdTotal); - size_t handlerCount = EventDispatcher::GetInstance()->GetHandlerCount(); - LogtailMonitor::GetInstance()->UpdateMetric("register_handler", handlerCount); - mRegisterdHandlersTotal->Set(handlerCount); - LogtailMonitor::GetInstance()->UpdateMetric("reader_count", CheckPointManager::Instance()->GetReaderCount()); + LoongCollectorMonitor::GetInstance()->SetAgentOpenFdTotal(GloablFileDescriptorManager::GetInstance()->GetOpenedFilePtrSize()); + mRegisterdHandlersTotal->Set(EventDispatcher::GetInstance()->GetHandlerCount()); mActiveReadersTotal->Set(CheckPointManager::Instance()->GetReaderCount()); - LogtailMonitor::GetInstance()->UpdateMetric("multi_config", AppConfig::GetInstance()->IsAcceptMultiConfig()); mEventProcessCount = 0; } diff --git a/core/file_server/polling/PollingDirFile.cpp b/core/file_server/polling/PollingDirFile.cpp index 0b106605ff..8ec9cf6f4e 100644 --- a/core/file_server/polling/PollingDirFile.cpp +++ b/core/file_server/polling/PollingDirFile.cpp @@ -160,17 +160,11 @@ void PollingDirFile::PollingIteration() { } sort(sortedConfigs.begin(), sortedConfigs.end(), FileDiscoveryOptions::CompareByPathLength); - size_t configTotal = nameConfigMap.size(); - LogtailMonitor::GetInstance()->UpdateMetric("config_count", configTotal); - LoongCollectorMonitor::GetInstance()->SetAgentConfigTotal(configTotal); + LoongCollectorMonitor::GetInstance()->SetAgentConfigTotal(nameConfigMap.size()); { ScopedSpinLock lock(mCacheLock); - size_t pollingDirCacheSize = mDirCacheMap.size(); - LogtailMonitor::GetInstance()->UpdateMetric("polling_dir_cache", pollingDirCacheSize); - mPollingDirCacheSize->Set(pollingDirCacheSize); - size_t pollingFileCacheSize = mFileCacheMap.size(); - LogtailMonitor::GetInstance()->UpdateMetric("polling_file_cache", pollingFileCacheSize); - mPollingFileCacheSize->Set(pollingFileCacheSize); + mPollingDirCacheSize->Set( mDirCacheMap.size()); + mPollingFileCacheSize->Set(mFileCacheMap.size()); } // Iterate all normal configs, make sure stat count will not exceed limit. diff --git a/core/file_server/polling/PollingModify.cpp b/core/file_server/polling/PollingModify.cpp index 33b7f3fad8..fcb2bb8eff 100644 --- a/core/file_server/polling/PollingModify.cpp +++ b/core/file_server/polling/PollingModify.cpp @@ -262,9 +262,7 @@ void PollingModify::PollingIteration() { vector deletedFileVec; vector pollingEventVec; int32_t statCount = 0; - size_t pollingModifySizeTotal = mModifyCacheMap.size(); - LogtailMonitor::GetInstance()->UpdateMetric("polling_modify_size", pollingModifySizeTotal); - mPollingModifySize->Set(pollingModifySizeTotal); + mPollingModifySize->Set(mModifyCacheMap.size()); for (auto iter = mModifyCacheMap.begin(); iter != mModifyCacheMap.end(); ++iter) { if (!mRuningFlag || mHoldOnFlag) break; diff --git a/core/logger/Logger.cpp b/core/logger/Logger.cpp index 701fa2738d..237a33511b 100644 --- a/core/logger/Logger.cpp +++ b/core/logger/Logger.cpp @@ -414,14 +414,6 @@ void Logger::LoadAllDefaultConfigs(std::map& loggerCf LoadDefaultConfig(loggerCfgs, sinkCfgs); loggerCfgs.insert({GetAgentLoggersPrefix(), LoggerConfig{"AsyncFileSink", level::info}}); - loggerCfgs.insert({GetAgentLoggersPrefix() + "/status", LoggerConfig{"AsyncFileSinkStatus", level::info}}); - - std::string dirPath = GetAgentSnapshotDir(); - if (!Mkdir(dirPath)) { - LogMsg(std::string("Create snapshot dir error ") + dirPath + ", error" + ErrnoToString(GetErrno())); - } - sinkCfgs.insert( - {"AsyncFileSinkStatus", SinkConfig{"AsyncFile", 61, 1, 1, dirPath + PATH_SEPARATOR + GetAgentStatusLogName()}}); } void Logger::EnsureSnapshotDirExist(std::map& sinkCfgs) { diff --git a/core/monitor/MetricManager.cpp b/core/monitor/MetricManager.cpp index be425a9ce7..21aa50c68a 100644 --- a/core/monitor/MetricManager.cpp +++ b/core/monitor/MetricManager.cpp @@ -18,10 +18,6 @@ #include "Monitor.h" #include "app_config/AppConfig.h" -#include "common/HashUtil.h" -#include "common/JsonUtil.h" -#include "common/StringTools.h" -#include "common/TimeUtil.h" #include "go_pipeline/LogtailPlugin.h" #include "logger/Logger.h" #include "provider/Provider.h" @@ -37,159 +33,6 @@ const string METRIC_KEY_LABEL = "label"; const string METRIC_TOPIC_TYPE = "loongcollector_metric"; const string METRIC_EXPORT_TYPE_GO = "direct"; const string METRIC_EXPORT_TYPE_CPP = "cpp_provided"; -const string METRIC_GO_KEY_LABELS = "labels"; -const string METRIC_GO_KEY_COUNTERS = "counters"; -const string METRIC_GO_KEY_GAUGES = "gauges"; - -SelfMonitorMetricEvent::SelfMonitorMetricEvent() { -} - -SelfMonitorMetricEvent::SelfMonitorMetricEvent(MetricsRecord* metricRecord) { - // category - mCategory = metricRecord->GetCategory(); - // labels - for (auto item = metricRecord->GetLabels()->begin(); item != metricRecord->GetLabels()->end(); ++item) { - pair pair = *item; - mLabels[pair.first] = pair.second; - } - for (auto item = metricRecord->GetDynamicLabels()->begin(); item != metricRecord->GetDynamicLabels()->end(); - ++item) { - pair> pair = *item; - string value = pair.second(); - mLabels[pair.first] = value; - } - // counters - for (auto& item : metricRecord->GetCounters()) { - mCounters[item->GetName()] = item->GetValue(); - } - for (auto& item : metricRecord->GetTimeCounters()) { - mCounters[item->GetName()] = item->GetValue(); - } - // gauges - for (auto& item : metricRecord->GetIntGauges()) { - mGauges[item->GetName()] = item->GetValue(); - } - for (auto& item : metricRecord->GetDoubleGauges()) { - mGauges[item->GetName()] = item->GetValue(); - } - CreateKey(); -} - -SelfMonitorMetricEvent::SelfMonitorMetricEvent(const std::map& metricRecord) { - Json::Value labels, counters, gauges; - string errMsg; - ParseJsonTable(metricRecord.at(METRIC_GO_KEY_LABELS), labels, errMsg); - ParseJsonTable(metricRecord.at(METRIC_GO_KEY_COUNTERS), counters, errMsg); - ParseJsonTable(metricRecord.at(METRIC_GO_KEY_GAUGES), gauges, errMsg); - // category - if (labels.isMember("metric_category")) { - mCategory = labels["metric_category"].asString(); - labels.removeMember("metric_category"); - } else { - mCategory = MetricCategory::METRIC_CATEGORY_UNKNOWN; - LOG_ERROR(sLogger, ("parse go metric", "labels")("err", "metric_category not found")); - } - // labels - for (Json::Value::const_iterator itr = labels.begin(); itr != labels.end(); ++itr) { - if (itr->isString()) { - mLabels[itr.key().asString()] = itr->asString(); - } - } - // counters - for (Json::Value::const_iterator itr = counters.begin(); itr != counters.end(); ++itr) { - if (itr->isUInt64()) { - mCounters[itr.key().asString()] = itr->asUInt64(); - } - if (itr->isDouble()) { - mCounters[itr.key().asString()] = static_cast(itr->asDouble()); - } - if (itr->isString()) { - try { - mCounters[itr.key().asString()] = static_cast(std::stod(itr->asString())); - } catch (...) { - mCounters[itr.key().asString()] = 0; - } - } - } - // gauges - for (Json::Value::const_iterator itr = gauges.begin(); itr != gauges.end(); ++itr) { - if (itr->isDouble()) { - mGauges[itr.key().asString()] = itr->asDouble(); - } - if (itr->isString()) { - try { - double value = std::stod(itr->asString()); - mGauges[itr.key().asString()] = value; - } catch (...) { - mGauges[itr.key().asString()] = 0; - } - } - } - CreateKey(); -} - -void SelfMonitorMetricEvent::CreateKey() { - string key = "category:" + mCategory; - for (auto label : mLabels) { - key += (";" + label.first + ":" + label.second); - } - mKey = HashString(key); - mUpdatedFlag = true; -} - -void SelfMonitorMetricEvent::SetInterval(size_t interval) { - mLastSendInterval = 0; - mSendInterval = interval; -} - -void SelfMonitorMetricEvent::Merge(SelfMonitorMetricEvent& event) { - if (mSendInterval != event.mSendInterval) { - mSendInterval = event.mSendInterval; - mLastSendInterval = 0; - } - for (auto counter = event.mCounters.begin(); counter != event.mCounters.end(); counter++) { - if (mCounters.find(counter->first) != mCounters.end()) - mCounters[counter->first] += counter->second; - else - mCounters[counter->first] = counter->second; - } - for (auto gauge = event.mGauges.begin(); gauge != event.mGauges.end(); gauge++) { - mGauges[gauge->first] = gauge->second; - } - mUpdatedFlag = true; -} - -bool SelfMonitorMetricEvent::ShouldSend() { - mLastSendInterval++; - return (mLastSendInterval >= mSendInterval) && mUpdatedFlag; -} - -bool SelfMonitorMetricEvent::ShouldDelete() { - return (mLastSendInterval >= mSendInterval) && !mUpdatedFlag; -} - -void SelfMonitorMetricEvent::ReadAsMetricEvent(MetricEvent* metricEventPtr) { - // time - metricEventPtr->SetTimestamp(GetCurrentLogtailTime().tv_sec); - // __tag__ - for (auto label = mLabels.begin(); label != mLabels.end(); label++) { - metricEventPtr->SetTag(label->first, label->second); - } - // name - metricEventPtr->SetName(mCategory); - // values - metricEventPtr->SetValue({}); - for (auto counter = mCounters.begin(); counter != mCounters.end(); counter++) { - metricEventPtr->MutableValue()->SetValue(counter->first, counter->second); - counter->second = 0; - } - for (auto gauge = mGauges.begin(); gauge != mGauges.end(); gauge++) { - metricEventPtr->MutableValue()->SetValue(gauge->first, gauge->second); - } - // set flags - mLastSendInterval = 0; - mUpdatedFlag = false; -} WriteMetrics::~WriteMetrics() { Clear(); @@ -391,7 +234,6 @@ void ReadMetrics::UpdateGoCppProvidedMetrics(vector>& metric if (metric.first == METRIC_AGENT_GO_ROUTINES_TOTAL) { LoongCollectorMonitor::GetInstance()->SetAgentGoRoutinesTotal(stoi(metric.second)); } - LogtailMonitor::GetInstance()->UpdateMetric(metric.first, metric.second); } } } diff --git a/core/monitor/MetricManager.h b/core/monitor/MetricManager.h index e87a022da1..3f28b9b477 100644 --- a/core/monitor/MetricManager.h +++ b/core/monitor/MetricManager.h @@ -22,57 +22,16 @@ #include #include -#include "MetricRecord.h" #include "common/Lock.h" #include "models/PipelineEventGroup.h" -#include "protobuf/sls/sls_logs.pb.h" +#include "monitor/metric_constants/MetricConstants.h" +#include "monitor/metric_models/MetricRecord.h" +#include "monitor/metric_models/SelfMonitorMetricEvent.h" namespace logtail { extern const std::string METRIC_TOPIC_TYPE; -struct SelfMonitorMetricRule { - bool mEnable; - size_t mInterval; -}; - -struct SelfMonitorMetricRules { - SelfMonitorMetricRule mAgentMetricsRule; - SelfMonitorMetricRule mRunnerMetricsRule; - SelfMonitorMetricRule mPipelineMetricsRule; - SelfMonitorMetricRule mPluginSourceMetricsRule; - SelfMonitorMetricRule mPluginMetricsRule; - SelfMonitorMetricRule mComponentMetricsRule; -}; - -using SelfMonitorMetricEventKey = int64_t; -class SelfMonitorMetricEvent { -public: - SelfMonitorMetricEvent(); - SelfMonitorMetricEvent(MetricsRecord* metricRecord); - SelfMonitorMetricEvent(const std::map& metricRecord); - - void SetInterval(size_t interval); - void Merge(SelfMonitorMetricEvent& event); - - bool ShouldSend(); - bool ShouldDelete(); - void ReadAsMetricEvent(MetricEvent* metricEventPtr); - - SelfMonitorMetricEventKey mKey; // labels + category - std::string mCategory; // category -private: - void CreateKey(); - - std::unordered_map mLabels; - std::unordered_map mCounters; - std::unordered_map mGauges; - int32_t mSendInterval; - int32_t mLastSendInterval; - bool mUpdatedFlag; -}; -using SelfMonitorMetricEventMap = std::unordered_map; - class WriteMetrics { private: WriteMetrics() = default; diff --git a/core/monitor/MetricStore.cpp b/core/monitor/MetricStore.cpp deleted file mode 100644 index 8594329259..0000000000 --- a/core/monitor/MetricStore.cpp +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2022 iLogtail Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "MetricStore.h" -#include - -namespace logtail { - -std::string MetricStore::MetricToString() { - Json::Value rootValue; - ScopedSpinLock lock(mMonitorMetricLock); - auto iter = mLogtailMetric.begin(); - for (; iter != mLogtailMetric.end(); ++iter) { - rootValue[iter->first] = Json::Value(iter->second); - } - for (iter = mConstLogtailMetric.begin(); iter != mConstLogtailMetric.end(); ++iter) { - rootValue[iter->first] = Json::Value(iter->second); - } - return rootValue.toStyledString(); -} - -std::string MetricStore::CheckLogtailStatus() { - std::string metricStr; - int32_t processFull = 0; - int32_t sendFull = 0; - int32_t senderInvalid = 0; - int32_t openFdCount = 0; - double processTps = 0.; - - ScopedSpinLock lock(mMonitorMetricLock); - auto iter = mLogtailMetric.find("process_queue_full"); - if (iter != mLogtailMetric.end()) { - processFull = StringTo(iter->second); - } - - iter = mLogtailMetric.find("send_queue_full"); - if (iter != mLogtailMetric.end()) { - sendFull = StringTo(iter->second); - } - - iter = mLogtailMetric.find("sender_invalid"); - if (iter != mLogtailMetric.end()) { - senderInvalid = StringTo(iter->second); - } - - iter = mLogtailMetric.find("open_fd"); - if (iter != mLogtailMetric.end()) { - openFdCount = StringTo(iter->second); - } - - iter = mLogtailMetric.find("process_tps"); - if (iter != mLogtailMetric.end()) { - processTps = StringTo(iter->second); - } - - metricStr = "ok"; - if (processTps > 20.) - metricStr = "busy"; - if (openFdCount > 1000) - metricStr = "many_log_files"; - if (processFull > 0) - metricStr = "process_block"; - if (sendFull > 0) - metricStr = "send_block"; - if (senderInvalid > 0) - metricStr = "send_error"; - return metricStr; -} - -} // namespace logtail \ No newline at end of file diff --git a/core/monitor/MetricStore.h b/core/monitor/MetricStore.h deleted file mode 100644 index 5d3868d3ff..0000000000 --- a/core/monitor/MetricStore.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2022 iLogtail Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once -#include -#include "common/StringTools.h" -#include "common/Lock.h" - -namespace logtail { - -class MetricStore { -public: - template - void UpdateMetric(const std::string key, const T& val) { - ScopedSpinLock lock(mMonitorMetricLock); - mLogtailMetric[key] = ToString(val); - } - - template - void UpdateConstMetric(const std::string key, const T& val) { - ScopedSpinLock lock(mMonitorMetricLock); - mConstLogtailMetric[key] = ToString(val); - } - -protected: - // Clear mutable metrics. - void ClearMetric() { - ScopedSpinLock lock(mMonitorMetricLock); - mLogtailMetric.clear(); - } - - // MetricToString dumps stored metrics to string in JSON format. - std::string MetricToString(); - - // CheckLogtailStatus checks metrics status and return a string reprensent status. - std::string CheckLogtailStatus(); - - // Metrics and corresponding lock. - SpinLock mMonitorMetricLock; - // mConstLogtailMetric is used to store immutable metrics (assumption), such as IP. - std::unordered_map mConstLogtailMetric; - std::unordered_map mLogtailMetric; -}; - -} // namespace logtail \ No newline at end of file diff --git a/core/monitor/Monitor.cpp b/core/monitor/Monitor.cpp index 21a51952de..6ec7779bb4 100644 --- a/core/monitor/Monitor.cpp +++ b/core/monitor/Monitor.cpp @@ -229,28 +229,15 @@ void LogtailMonitor::Monitor() { SendStatusProfile(true); } -template -static void AddLogContent(sls_logs::Log* log, const char* key, const T& val) { - auto content = log->add_contents(); - content->set_key(key); - content->set_value(ToString(val)); -} - bool LogtailMonitor::SendStatusProfile(bool suicide) { mStatusCount++; - string category; - if (suicide) - category = "logtail_suicide_profile"; - else if (mStatusCount % 2 == 0) - category = "logtail_status_profile"; - else + if (!suicide && mStatusCount % 2 != 0) return false; - auto now = GetCurrentLogtailTime(); // Check input thread. int32_t lastReadEventTime = LogInput::GetInstance()->GetLastReadEventTime(); if (lastReadEventTime > 0 - && (now.tv_sec - lastReadEventTime > AppConfig::GetInstance()->GetForceQuitReadTimeout())) { + && (GetCurrentLogtailTime().tv_sec - lastReadEventTime > AppConfig::GetInstance()->GetForceQuitReadTimeout())) { LOG_ERROR(sLogger, ("last read event time is too old", lastReadEventTime)("prepare force exit", "")); AlarmManager::GetInstance()->SendAlarm( LOGTAIL_CRASH_ALARM, "last read event time is too old: " + ToString(lastReadEventTime) + " force exit"); @@ -258,100 +245,12 @@ bool LogtailMonitor::SendStatusProfile(bool suicide) { sleep(10); _exit(1); } - - // the unique id of current instance - std::string id = sdk::Base64Enconde(LoongCollectorMonitor::mHostname + LoongCollectorMonitor::mIpAddr - + ILOGTAIL_VERSION + GetProcessExecutionDir()); - - // Collect status information to send. - LogGroup logGroup; - logGroup.set_category(category); - logGroup.set_source(LoongCollectorMonitor::mIpAddr); - Log* logPtr = logGroup.add_logs(); - SetLogTime(logPtr, AppConfig::GetInstance()->EnableLogTimeAutoAdjust() ? now.tv_sec + GetTimeDelta() : now.tv_sec); // CPU usage of Logtail process. - AddLogContent(logPtr, "cpu", mCpuStat.mCpuUsage); LoongCollectorMonitor::GetInstance()->SetAgentCpu(mCpuStat.mCpuUsage); -#if defined(__linux__) // TODO: Remove this if auto scale is available on Windows. - // CPU usage of system. - AddLogContent(logPtr, "os_cpu", mOsCpuStatForScale.mOsCpuUsage); -#endif // Memory usage of Logtail process. - AddLogContent(logPtr, "mem", mMemStat.mRss); LoongCollectorMonitor::GetInstance()->SetAgentMemory(mMemStat.mRss); - // The version, uuid of Logtail. - AddLogContent(logPtr, "version", ILOGTAIL_VERSION); - AddLogContent(logPtr, "uuid", Application::GetInstance()->GetUUID()); -#ifdef __ENTERPRISE__ - AddLogContent(logPtr, "user_defined_id", EnterpriseConfigProvider::GetInstance()->GetUserDefinedIdSet()); - AddLogContent(logPtr, "aliuids", EnterpriseConfigProvider::GetInstance()->GetAliuidSet()); -#endif - AddLogContent(logPtr, "projects", FlusherSLS::GetAllProjects()); - AddLogContent(logPtr, "instance_id", Application::GetInstance()->GetInstanceId()); - AddLogContent(logPtr, "instance_key", id); - // Host informations. - AddLogContent(logPtr, "ip", LoongCollectorMonitor::mIpAddr); - AddLogContent(logPtr, "hostname", LoongCollectorMonitor::mHostname); - AddLogContent(logPtr, "os", OS_NAME); - AddLogContent(logPtr, "os_detail", LoongCollectorMonitor::mOsDetail); - AddLogContent(logPtr, "user", LoongCollectorMonitor::mUsername); -#if defined(__linux__) - AddLogContent(logPtr, "load", GetLoadAvg()); -#endif - AddLogContent(logPtr, "plugin_stats", PipelineManager::GetInstance()->GetPluginStatistics()); - // Metrics. - vector allProfileRegion; - GetProfileSender()->GetAllProfileRegion(allProfileRegion); - UpdateMetric("region", allProfileRegion); -#ifdef __ENTERPRISE__ - UpdateMetric("config_update_count", EnterpriseConfigProvider::GetInstance()->GetConfigUpdateTotalCount()); - UpdateMetric("config_update_item_count", EnterpriseConfigProvider::GetInstance()->GetConfigUpdateItemTotalCount()); - UpdateMetric("config_update_last_time", - GetTimeStamp(EnterpriseConfigProvider::GetInstance()->GetLastConfigUpdateTime(), "%Y-%m-%d %H:%M:%S")); - UpdateMetric("config_get_last_time", - GetTimeStamp(EnterpriseConfigProvider::GetInstance()->GetLastConfigGetTime(), "%Y-%m-%d %H:%M:%S")); -#endif - UpdateMetric("config_prefer_real_ip", BOOL_FLAG(send_prefer_real_ip)); - UpdateMetric("plugin_enabled", LogtailPlugin::GetInstance()->IsPluginOpened()); - const std::vector& envTags = AppConfig::GetInstance()->GetEnvTags(); - if (!envTags.empty()) { - UpdateMetric("env_config_count", envTags.size()); - } - int32_t usedSendingConcurrency = FlusherRunner::GetInstance()->GetSendingBufferCount(); - UpdateMetric("used_sending_concurrency", usedSendingConcurrency); - - AddLogContent(logPtr, "metric_json", MetricToString()); - AddLogContent(logPtr, "status", CheckLogtailStatus()); - AddLogContent(logPtr, "ecs_instance_id", LoongCollectorMonitor::mECSInstanceID); - AddLogContent(logPtr, "ecs_user_id", LoongCollectorMonitor::mECSUserID); - AddLogContent(logPtr, "ecs_regioon_id", LoongCollectorMonitor::mECSRegionID); - ClearMetric(); - - if (!mIsThreadRunning) - return false; - - // Dump to local and send to enabled regions. - DumpToLocal(logGroup); - for (size_t i = 0; i < allProfileRegion.size(); ++i) { - if (BOOL_FLAG(check_profile_region) && !FlusherSLS::IsRegionContainingConfig(allProfileRegion[i])) { - LOG_DEBUG(sLogger, ("region does not contain config for this instance", allProfileRegion[i])); - continue; - } - // Check if the region is disabled. - if (!FlusherSLS::GetRegionStatus(allProfileRegion[i])) { - LOG_DEBUG(sLogger, ("disabled region, do not send status profile to region", allProfileRegion[i])); - continue; - } - - if (i == allProfileRegion.size() - 1) { - GetProfileSender()->SendToProfileProject(allProfileRegion[i], logGroup); - } else { - LogGroup copyLogGroup = logGroup; - GetProfileSender()->SendToProfileProject(allProfileRegion[i], copyLogGroup); - } - } - return true; + return mIsThreadRunning; } bool LogtailMonitor::GetMemStat() { @@ -473,24 +372,6 @@ bool LogtailMonitor::CheckHardMemLimit() { return mMemStat.mRss > 5 * AppConfig::GetInstance()->GetMemUsageUpLimit(); } -void LogtailMonitor::DumpToLocal(const sls_logs::LogGroup& logGroup) { - string dumpStr = "\n####logtail status####\n"; - for (int32_t logIdx = 0; logIdx < logGroup.logs_size(); ++logIdx) { - Json::Value category; - const Log& log = logGroup.logs(logIdx); - for (int32_t conIdx = 0; conIdx < log.contents_size(); ++conIdx) { - const Log_Content& content = log.contents(conIdx); - const string& key = content.key(); - const string& value = content.value(); - dumpStr.append(key).append(":").append(value).append("\n"); - } - } - dumpStr += "####status end####\n"; - - static auto gMonitorLogger = Logger::Instance().GetLogger(GetAgentLoggersPrefix() + "/status"); - LOG_INFO(gMonitorLogger, ("\n", dumpStr)); -} - bool LogtailMonitor::DumpMonitorInfo(time_t monitorTime) { string path = GetAgentLogDir() + GetMonitorInfoFileName(); ofstream outfile(path.c_str(), ofstream::app); @@ -756,7 +637,6 @@ void LoongCollectorMonitor::Init() { void LoongCollectorMonitor::Stop() { SelfMonitorServer::GetInstance()->Stop(); LOG_INFO(sLogger, ("LoongCollector monitor", "stopped successfully")); - } const string LoongCollectorMonitor::GetInnerSelfMonitorMetricPipeline() { diff --git a/core/monitor/Monitor.h b/core/monitor/Monitor.h index 372407d8f1..631e36fc83 100644 --- a/core/monitor/Monitor.h +++ b/core/monitor/Monitor.h @@ -21,9 +21,7 @@ #include #include -#include "MetricConstants.h" #include "MetricManager.h" -#include "MetricStore.h" #if defined(_MSC_VER) #include @@ -78,7 +76,7 @@ struct OsCpuStat { } }; -class LogtailMonitor : public MetricStore { +class LogtailMonitor { public: LogtailMonitor(const LogtailMonitor&) = delete; LogtailMonitor& operator=(const LogtailMonitor&) = delete; @@ -124,9 +122,6 @@ class LogtailMonitor : public MetricStore { // several seconds after calling this method and before _exit(1). bool SendStatusProfile(bool suicide); - // DumpToLocal dumps the @logGroup to local status log. - void DumpToLocal(const sls_logs::LogGroup& logGroup); - // DumpMonitorInfo dumps simple monitor information to local. bool DumpMonitorInfo(time_t monitorTime); diff --git a/core/monitor/MetricRecord.cpp b/core/monitor/metric_models/MetricRecord.cpp similarity index 100% rename from core/monitor/MetricRecord.cpp rename to core/monitor/metric_models/MetricRecord.cpp diff --git a/core/monitor/MetricRecord.h b/core/monitor/metric_models/MetricRecord.h similarity index 100% rename from core/monitor/MetricRecord.h rename to core/monitor/metric_models/MetricRecord.h diff --git a/core/monitor/MetricTypes.h b/core/monitor/metric_models/MetricTypes.h similarity index 100% rename from core/monitor/MetricTypes.h rename to core/monitor/metric_models/MetricTypes.h diff --git a/core/monitor/PluginMetricManager.cpp b/core/monitor/metric_models/ReentrantMetricsRecord.cpp similarity index 99% rename from core/monitor/PluginMetricManager.cpp rename to core/monitor/metric_models/ReentrantMetricsRecord.cpp index 58719241d9..33b9a7a5f4 100644 --- a/core/monitor/PluginMetricManager.cpp +++ b/core/monitor/metric_models/ReentrantMetricsRecord.cpp @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "PluginMetricManager.h" +#include "ReentrantMetricsRecord.h" namespace logtail { diff --git a/core/monitor/PluginMetricManager.h b/core/monitor/metric_models/ReentrantMetricsRecord.h similarity index 100% rename from core/monitor/PluginMetricManager.h rename to core/monitor/metric_models/ReentrantMetricsRecord.h diff --git a/core/monitor/metric_models/SelfMonitorMetricEvent.cpp b/core/monitor/metric_models/SelfMonitorMetricEvent.cpp new file mode 100644 index 0000000000..85410f564d --- /dev/null +++ b/core/monitor/metric_models/SelfMonitorMetricEvent.cpp @@ -0,0 +1,182 @@ +/* + * Copyright 2023 iLogtail Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "SelfMonitorMetricEvent.h" + +#include "common/HashUtil.h" +#include "common/JsonUtil.h" +#include "common/TimeUtil.h" +#include "logger/Logger.h" + +using namespace std; + +namespace logtail { + +const string METRIC_GO_KEY_LABELS = "labels"; +const string METRIC_GO_KEY_COUNTERS = "counters"; +const string METRIC_GO_KEY_GAUGES = "gauges"; + +SelfMonitorMetricEvent::SelfMonitorMetricEvent() { +} + +SelfMonitorMetricEvent::SelfMonitorMetricEvent(MetricsRecord* metricRecord) { + // category + mCategory = metricRecord->GetCategory(); + // labels + for (auto item = metricRecord->GetLabels()->begin(); item != metricRecord->GetLabels()->end(); ++item) { + pair pair = *item; + mLabels[pair.first] = pair.second; + } + for (auto item = metricRecord->GetDynamicLabels()->begin(); item != metricRecord->GetDynamicLabels()->end(); + ++item) { + pair> pair = *item; + string value = pair.second(); + mLabels[pair.first] = value; + } + // counters + for (auto& item : metricRecord->GetCounters()) { + mCounters[item->GetName()] = item->GetValue(); + } + for (auto& item : metricRecord->GetTimeCounters()) { + mCounters[item->GetName()] = item->GetValue(); + } + // gauges + for (auto& item : metricRecord->GetIntGauges()) { + mGauges[item->GetName()] = item->GetValue(); + } + for (auto& item : metricRecord->GetDoubleGauges()) { + mGauges[item->GetName()] = item->GetValue(); + } + CreateKey(); +} + +SelfMonitorMetricEvent::SelfMonitorMetricEvent(const std::map& metricRecord) { + Json::Value labels, counters, gauges; + string errMsg; + ParseJsonTable(metricRecord.at(METRIC_GO_KEY_LABELS), labels, errMsg); + ParseJsonTable(metricRecord.at(METRIC_GO_KEY_COUNTERS), counters, errMsg); + ParseJsonTable(metricRecord.at(METRIC_GO_KEY_GAUGES), gauges, errMsg); + // category + if (labels.isMember("metric_category")) { + mCategory = labels["metric_category"].asString(); + labels.removeMember("metric_category"); + } else { + mCategory = MetricCategory::METRIC_CATEGORY_UNKNOWN; + LOG_ERROR(sLogger, ("parse go metric", "labels")("err", "metric_category not found")); + } + // labels + for (Json::Value::const_iterator itr = labels.begin(); itr != labels.end(); ++itr) { + if (itr->isString()) { + mLabels[itr.key().asString()] = itr->asString(); + } + } + // counters + for (Json::Value::const_iterator itr = counters.begin(); itr != counters.end(); ++itr) { + if (itr->isUInt64()) { + mCounters[itr.key().asString()] = itr->asUInt64(); + } + if (itr->isDouble()) { + mCounters[itr.key().asString()] = static_cast(itr->asDouble()); + } + if (itr->isString()) { + try { + mCounters[itr.key().asString()] = static_cast(std::stod(itr->asString())); + } catch (...) { + mCounters[itr.key().asString()] = 0; + } + } + } + // gauges + for (Json::Value::const_iterator itr = gauges.begin(); itr != gauges.end(); ++itr) { + if (itr->isDouble()) { + mGauges[itr.key().asString()] = itr->asDouble(); + } + if (itr->isString()) { + try { + double value = std::stod(itr->asString()); + mGauges[itr.key().asString()] = value; + } catch (...) { + mGauges[itr.key().asString()] = 0; + } + } + } + CreateKey(); +} + +void SelfMonitorMetricEvent::CreateKey() { + string key = "category:" + mCategory; + for (auto label : mLabels) { + key += (";" + label.first + ":" + label.second); + } + mKey = HashString(key); + mUpdatedFlag = true; +} + +void SelfMonitorMetricEvent::SetInterval(size_t interval) { + mLastSendInterval = 0; + mSendInterval = interval; +} + +void SelfMonitorMetricEvent::Merge(SelfMonitorMetricEvent& event) { + if (mSendInterval != event.mSendInterval) { + mSendInterval = event.mSendInterval; + mLastSendInterval = 0; + } + for (auto counter = event.mCounters.begin(); counter != event.mCounters.end(); counter++) { + if (mCounters.find(counter->first) != mCounters.end()) + mCounters[counter->first] += counter->second; + else + mCounters[counter->first] = counter->second; + } + for (auto gauge = event.mGauges.begin(); gauge != event.mGauges.end(); gauge++) { + mGauges[gauge->first] = gauge->second; + } + mUpdatedFlag = true; +} + +bool SelfMonitorMetricEvent::ShouldSend() { + mLastSendInterval++; + return (mLastSendInterval >= mSendInterval) && mUpdatedFlag; +} + +bool SelfMonitorMetricEvent::ShouldDelete() { + return (mLastSendInterval >= mSendInterval) && !mUpdatedFlag; +} + +void SelfMonitorMetricEvent::ReadAsMetricEvent(MetricEvent* metricEventPtr) { + // time + metricEventPtr->SetTimestamp(GetCurrentLogtailTime().tv_sec); + // __tag__ + for (auto label = mLabels.begin(); label != mLabels.end(); label++) { + metricEventPtr->SetTag(label->first, label->second); + } + // name + metricEventPtr->SetName(mCategory); + // values + metricEventPtr->SetValue({}); + for (auto counter = mCounters.begin(); counter != mCounters.end(); counter++) { + metricEventPtr->MutableValue()->SetValue(counter->first, counter->second); + counter->second = 0; + } + for (auto gauge = mGauges.begin(); gauge != mGauges.end(); gauge++) { + metricEventPtr->MutableValue()->SetValue(gauge->first, gauge->second); + } + // set flags + mLastSendInterval = 0; + mUpdatedFlag = false; +} + +} // namespace logtail \ No newline at end of file diff --git a/core/monitor/metric_models/SelfMonitorMetricEvent.h b/core/monitor/metric_models/SelfMonitorMetricEvent.h new file mode 100644 index 0000000000..2cc256c39d --- /dev/null +++ b/core/monitor/metric_models/SelfMonitorMetricEvent.h @@ -0,0 +1,64 @@ +/* + * Copyright 2023 iLogtail Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "MetricRecord.h" +#include "models/PipelineEventGroup.h" + +namespace logtail { + +struct SelfMonitorMetricRule { + bool mEnable; + size_t mInterval; +}; + +struct SelfMonitorMetricRules { + SelfMonitorMetricRule mAgentMetricsRule; + SelfMonitorMetricRule mRunnerMetricsRule; + SelfMonitorMetricRule mPipelineMetricsRule; + SelfMonitorMetricRule mPluginSourceMetricsRule; + SelfMonitorMetricRule mPluginMetricsRule; + SelfMonitorMetricRule mComponentMetricsRule; +}; + +using SelfMonitorMetricEventKey = int64_t; +class SelfMonitorMetricEvent { +public: + SelfMonitorMetricEvent(); + SelfMonitorMetricEvent(MetricsRecord* metricRecord); + SelfMonitorMetricEvent(const std::map& metricRecord); + + void SetInterval(size_t interval); + void Merge(SelfMonitorMetricEvent& event); + + bool ShouldSend(); + bool ShouldDelete(); + void ReadAsMetricEvent(MetricEvent* metricEventPtr); + + SelfMonitorMetricEventKey mKey; // labels + category + std::string mCategory; // category +private: + void CreateKey(); + + std::unordered_map mLabels; + std::unordered_map mCounters; + std::unordered_map mGauges; + int32_t mSendInterval; + int32_t mLastSendInterval; + bool mUpdatedFlag; +}; +using SelfMonitorMetricEventMap = std::unordered_map; + +} // namespace logtail \ No newline at end of file diff --git a/core/pipeline/plugin/instance/FlusherInstance.h b/core/pipeline/plugin/instance/FlusherInstance.h index 68089f60b1..4102616541 100644 --- a/core/pipeline/plugin/instance/FlusherInstance.h +++ b/core/pipeline/plugin/instance/FlusherInstance.h @@ -21,7 +21,7 @@ #include #include "models/PipelineEventGroup.h" -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" #include "pipeline/PipelineContext.h" #include "pipeline/plugin/instance/PluginInstance.h" #include "pipeline/plugin/interface/Flusher.h" diff --git a/core/plugin/input/InputContainerStdio.h b/core/plugin/input/InputContainerStdio.h index a9d1e51aed..f5955b4fa6 100644 --- a/core/plugin/input/InputContainerStdio.h +++ b/core/plugin/input/InputContainerStdio.h @@ -21,7 +21,7 @@ #include "container_manager/ContainerDiscoveryOptions.h" #include "file_server/FileDiscoveryOptions.h" #include "file_server/MultilineOptions.h" -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" #include "pipeline/plugin/interface/Input.h" #include "file_server/reader/FileReaderOptions.h" diff --git a/core/plugin/input/InputFile.h b/core/plugin/input/InputFile.h index ee8275ef7c..4d966f1e67 100644 --- a/core/plugin/input/InputFile.h +++ b/core/plugin/input/InputFile.h @@ -21,7 +21,7 @@ #include "container_manager/ContainerDiscoveryOptions.h" #include "file_server/FileDiscoveryOptions.h" #include "file_server/MultilineOptions.h" -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" #include "pipeline/plugin/interface/Input.h" #include "file_server/reader/FileReaderOptions.h" diff --git a/core/plugin/input/InputFileSecurity.h b/core/plugin/input/InputFileSecurity.h index fea0b459fc..ebff74842f 100644 --- a/core/plugin/input/InputFileSecurity.h +++ b/core/plugin/input/InputFileSecurity.h @@ -21,7 +21,7 @@ #include "ebpf/config.h" #include "pipeline/plugin/interface/Input.h" #include "ebpf/eBPFServer.h" -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" namespace logtail { diff --git a/core/plugin/input/InputNetworkObserver.cpp b/core/plugin/input/InputNetworkObserver.cpp index ce9c4218ff..564d2244f0 100644 --- a/core/plugin/input/InputNetworkObserver.cpp +++ b/core/plugin/input/InputNetworkObserver.cpp @@ -18,7 +18,7 @@ #include "ebpf/eBPFServer.h" #include "ebpf/config.h" #include "logger/Logger.h" -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" using namespace std; diff --git a/core/plugin/input/InputNetworkObserver.h b/core/plugin/input/InputNetworkObserver.h index 7f204a2c90..33bf4ab0cc 100644 --- a/core/plugin/input/InputNetworkObserver.h +++ b/core/plugin/input/InputNetworkObserver.h @@ -21,7 +21,7 @@ #include "ebpf/config.h" #include "pipeline/plugin/interface/Input.h" #include "ebpf/include/export.h" -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" namespace logtail { diff --git a/core/plugin/input/InputNetworkSecurity.h b/core/plugin/input/InputNetworkSecurity.h index cda3a7c170..18023fc28e 100644 --- a/core/plugin/input/InputNetworkSecurity.h +++ b/core/plugin/input/InputNetworkSecurity.h @@ -20,7 +20,7 @@ #include "ebpf/config.h" #include "pipeline/plugin/interface/Input.h" -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" namespace logtail { diff --git a/core/plugin/input/InputProcessSecurity.h b/core/plugin/input/InputProcessSecurity.h index d26d7a95e3..d731b2b8fb 100644 --- a/core/plugin/input/InputProcessSecurity.h +++ b/core/plugin/input/InputProcessSecurity.h @@ -20,7 +20,7 @@ #include "ebpf/config.h" #include "pipeline/plugin/interface/Input.h" -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" namespace logtail { diff --git a/core/prometheus/PromSelfMonitor.cpp b/core/prometheus/PromSelfMonitor.cpp index 17f2e701e1..410195acaf 100644 --- a/core/prometheus/PromSelfMonitor.cpp +++ b/core/prometheus/PromSelfMonitor.cpp @@ -4,7 +4,7 @@ #include #include -#include "monitor/MetricTypes.h" +#include "monitor/metric_models/MetricTypes.h" #include "monitor/metric_constants/MetricConstants.h" using namespace std; diff --git a/core/prometheus/PromSelfMonitor.h b/core/prometheus/PromSelfMonitor.h index 002e8f88ef..b762533791 100644 --- a/core/prometheus/PromSelfMonitor.h +++ b/core/prometheus/PromSelfMonitor.h @@ -5,7 +5,7 @@ #include #include "monitor/MetricManager.h" -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" namespace logtail { diff --git a/core/prometheus/PrometheusInputRunner.h b/core/prometheus/PrometheusInputRunner.h index 2823d562a4..996caf163e 100644 --- a/core/prometheus/PrometheusInputRunner.h +++ b/core/prometheus/PrometheusInputRunner.h @@ -22,7 +22,7 @@ #include "common/Lock.h" #include "common/timer/Timer.h" -#include "monitor/MetricTypes.h" +#include "monitor/metric_models/MetricTypes.h" #include "prometheus/schedulers/TargetSubscriberScheduler.h" #include "runner/InputRunner.h" #include "sdk/Common.h" diff --git a/core/prometheus/schedulers/ScrapeScheduler.h b/core/prometheus/schedulers/ScrapeScheduler.h index 00ac2d989a..78018325bd 100644 --- a/core/prometheus/schedulers/ScrapeScheduler.h +++ b/core/prometheus/schedulers/ScrapeScheduler.h @@ -22,7 +22,7 @@ #include "BaseScheduler.h" #include "common/http/HttpResponse.h" #include "models/PipelineEventGroup.h" -#include "monitor/MetricTypes.h" +#include "monitor/metric_models/MetricTypes.h" #include "pipeline/queue/QueueKey.h" #include "prometheus/PromSelfMonitor.h" #include "prometheus/Utils.h" diff --git a/core/unittest/monitor/PluginMetricManagerUnittest.cpp b/core/unittest/monitor/PluginMetricManagerUnittest.cpp index 41ef503c75..77246a322b 100644 --- a/core/unittest/monitor/PluginMetricManagerUnittest.cpp +++ b/core/unittest/monitor/PluginMetricManagerUnittest.cpp @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "monitor/PluginMetricManager.h" +#include "monitor/metric_models/ReentrantMetricsRecord.h" #include "monitor/metric_constants/MetricConstants.h" #include "unittest/Unittest.h" From 26c80eccc6cd501ffcc809eb429f3acd69722b20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Thu, 28 Nov 2024 06:08:11 +0000 Subject: [PATCH 2/5] polish --- core/monitor/Monitor.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/monitor/Monitor.cpp b/core/monitor/Monitor.cpp index 6ec7779bb4..e5818e7b2b 100644 --- a/core/monitor/Monitor.cpp +++ b/core/monitor/Monitor.cpp @@ -234,10 +234,11 @@ bool LogtailMonitor::SendStatusProfile(bool suicide) { if (!suicide && mStatusCount % 2 != 0) return false; + auto now = GetCurrentLogtailTime(); // Check input thread. int32_t lastReadEventTime = LogInput::GetInstance()->GetLastReadEventTime(); if (lastReadEventTime > 0 - && (GetCurrentLogtailTime().tv_sec - lastReadEventTime > AppConfig::GetInstance()->GetForceQuitReadTimeout())) { + && (now.tv_sec - lastReadEventTime > AppConfig::GetInstance()->GetForceQuitReadTimeout())) { LOG_ERROR(sLogger, ("last read event time is too old", lastReadEventTime)("prepare force exit", "")); AlarmManager::GetInstance()->SendAlarm( LOGTAIL_CRASH_ALARM, "last read event time is too old: " + ToString(lastReadEventTime) + " force exit"); From 00291cb80556465732f01c38861d1ee499de2d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Mon, 2 Dec 2024 08:09:37 +0000 Subject: [PATCH 3/5] polish --- core/file_server/polling/PollingDirFile.cpp | 2 +- .../metric_models/SelfMonitorMetricEvent.cpp | 8 +- .../metric_models/SelfMonitorMetricEvent.h | 5 + core/unittest/monitor/CMakeLists.txt | 4 + .../SelfMonitorMetricEventUnittest.cpp | 205 ++++++++++++++++++ 5 files changed, 216 insertions(+), 8 deletions(-) create mode 100644 core/unittest/monitor/SelfMonitorMetricEventUnittest.cpp diff --git a/core/file_server/polling/PollingDirFile.cpp b/core/file_server/polling/PollingDirFile.cpp index 8ec9cf6f4e..f0f37b120e 100644 --- a/core/file_server/polling/PollingDirFile.cpp +++ b/core/file_server/polling/PollingDirFile.cpp @@ -163,7 +163,7 @@ void PollingDirFile::PollingIteration() { LoongCollectorMonitor::GetInstance()->SetAgentConfigTotal(nameConfigMap.size()); { ScopedSpinLock lock(mCacheLock); - mPollingDirCacheSize->Set( mDirCacheMap.size()); + mPollingDirCacheSize->Set(mDirCacheMap.size()); mPollingFileCacheSize->Set(mFileCacheMap.size()); } diff --git a/core/monitor/metric_models/SelfMonitorMetricEvent.cpp b/core/monitor/metric_models/SelfMonitorMetricEvent.cpp index 85410f564d..367163a400 100644 --- a/core/monitor/metric_models/SelfMonitorMetricEvent.cpp +++ b/core/monitor/metric_models/SelfMonitorMetricEvent.cpp @@ -85,12 +85,6 @@ SelfMonitorMetricEvent::SelfMonitorMetricEvent(const std::mapisUInt64()) { - mCounters[itr.key().asString()] = itr->asUInt64(); - } - if (itr->isDouble()) { - mCounters[itr.key().asString()] = static_cast(itr->asDouble()); - } if (itr->isString()) { try { mCounters[itr.key().asString()] = static_cast(std::stod(itr->asString())); @@ -166,7 +160,7 @@ void SelfMonitorMetricEvent::ReadAsMetricEvent(MetricEvent* metricEventPtr) { // name metricEventPtr->SetName(mCategory); // values - metricEventPtr->SetValue({}); + metricEventPtr->SetValue(UntypedMultiDoubleValues{{}, nullptr}); for (auto counter = mCounters.begin(); counter != mCounters.end(); counter++) { metricEventPtr->MutableValue()->SetValue(counter->first, counter->second); counter->second = 0; diff --git a/core/monitor/metric_models/SelfMonitorMetricEvent.h b/core/monitor/metric_models/SelfMonitorMetricEvent.h index 2cc256c39d..ff0a8cc7ea 100644 --- a/core/monitor/metric_models/SelfMonitorMetricEvent.h +++ b/core/monitor/metric_models/SelfMonitorMetricEvent.h @@ -14,6 +14,7 @@ * limitations under the License. */ +#pragma once #include "MetricRecord.h" #include "models/PipelineEventGroup.h" @@ -58,6 +59,10 @@ class SelfMonitorMetricEvent { int32_t mSendInterval; int32_t mLastSendInterval; bool mUpdatedFlag; + +#ifdef APSARA_UNIT_TEST_MAIN + friend class SelfMonitorMetricEventUnittest; +#endif }; using SelfMonitorMetricEventMap = std::unordered_map; diff --git a/core/unittest/monitor/CMakeLists.txt b/core/unittest/monitor/CMakeLists.txt index aa6544d052..d3e7c760f2 100644 --- a/core/unittest/monitor/CMakeLists.txt +++ b/core/unittest/monitor/CMakeLists.txt @@ -21,6 +21,10 @@ target_link_libraries(metric_manager_unittest ${UT_BASE_TARGET}) add_executable(plugin_metric_manager_unittest PluginMetricManagerUnittest.cpp) target_link_libraries(plugin_metric_manager_unittest ${UT_BASE_TARGET}) +add_executable(self_monitor_metric_event_unittest SelfMonitorMetricEventUnittest.cpp) +target_link_libraries(self_monitor_metric_event_unittest ${UT_BASE_TARGET}) + include(GoogleTest) gtest_discover_tests(metric_manager_unittest) gtest_discover_tests(plugin_metric_manager_unittest) +gtest_discover_tests(self_monitor_metric_event_unittest) diff --git a/core/unittest/monitor/SelfMonitorMetricEventUnittest.cpp b/core/unittest/monitor/SelfMonitorMetricEventUnittest.cpp new file mode 100644 index 0000000000..495026420d --- /dev/null +++ b/core/unittest/monitor/SelfMonitorMetricEventUnittest.cpp @@ -0,0 +1,205 @@ +// Copyright 2024 iLogtail Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "monitor/MetricManager.h" +#include "monitor/metric_models/SelfMonitorMetricEvent.h" +#include "unittest/Unittest.h" + +namespace logtail { + +class SelfMonitorMetricEventUnittest : public ::testing::Test { +public: + void SetUp() {} + void TearDown() {} + + void TestCreateFromMetricEvent(); + void TestCreateFromGoMetricMap(); + void TestMerge(); + void TestSendInterval(); + +private: + std::shared_ptr mSourceBuffer; + std::unique_ptr mEventGroup; + std::unique_ptr mMetricEvent; +}; + +APSARA_UNIT_TEST_CASE(SelfMonitorMetricEventUnittest, TestCreateFromMetricEvent, 0); +APSARA_UNIT_TEST_CASE(SelfMonitorMetricEventUnittest, TestCreateFromGoMetricMap, 1); +APSARA_UNIT_TEST_CASE(SelfMonitorMetricEventUnittest, TestMerge, 2); +APSARA_UNIT_TEST_CASE(SelfMonitorMetricEventUnittest, TestSendInterval, 3); + +void SelfMonitorMetricEventUnittest::TestCreateFromMetricEvent() { + std::vector> labels; + labels.emplace_back(std::make_pair("plugin_type", "input_file")); + labels.emplace_back(std::make_pair("plugin_id", "1")); + labels.emplace_back(std::make_pair("pipeline_name", "pipeline_test")); + labels.emplace_back(std::make_pair("project", "project_a")); + + MetricsRecord* pluginMetric = new MetricsRecord(MetricCategory::METRIC_CATEGORY_PLUGIN, + std::make_shared(labels), + std::make_shared()); + + CounterPtr outSizeBytes = pluginMetric->CreateCounter("out_size_bytes"); + outSizeBytes->Add(100); + CounterPtr outEventTotal = pluginMetric->CreateCounter("out_event_total"); + outEventTotal->Add(1024); + IntGaugePtr monitorFileTotal = pluginMetric->CreateIntGauge("monitor_file_total"); + monitorFileTotal->Set(10); + + SelfMonitorMetricEvent event(pluginMetric); + + APSARA_TEST_EQUAL(MetricCategory::METRIC_CATEGORY_PLUGIN, event.mCategory); + APSARA_TEST_EQUAL(4U, event.mLabels.size()); + APSARA_TEST_EQUAL("input_file", event.mLabels["plugin_type"]); + APSARA_TEST_EQUAL("1", event.mLabels["plugin_id"]); + APSARA_TEST_EQUAL("pipeline_test", event.mLabels["pipeline_name"]); + APSARA_TEST_EQUAL("project_a", event.mLabels["project"]); + APSARA_TEST_EQUAL(2U, event.mCounters.size()); + APSARA_TEST_EQUAL(100U, event.mCounters["out_size_bytes"]); + APSARA_TEST_EQUAL(1024U, event.mCounters["out_event_total"]); + APSARA_TEST_EQUAL(1U, event.mGauges.size()); + APSARA_TEST_EQUAL(10, event.mGauges["monitor_file_total"]); +} + +void SelfMonitorMetricEventUnittest::TestCreateFromGoMetricMap() { + std::map pluginMetric; + pluginMetric["labels"] = R"( + { + "metric_category":"plugin", + "plugin_type":"input_file", + "plugin_id":"1", + "pipeline_name":"pipeline_test", + "project":"project_a" + } + )"; + pluginMetric["counters"] = R"( + { + "out_size_bytes": "100", + "out_event_total": "1024" + } + )"; + pluginMetric["gauges"] = R"( + { + "monitor_file_total": "10" + } + )"; + SelfMonitorMetricEvent event(pluginMetric); + + APSARA_TEST_EQUAL(MetricCategory::METRIC_CATEGORY_PLUGIN, event.mCategory); + APSARA_TEST_EQUAL(4U, event.mLabels.size()); + APSARA_TEST_EQUAL("input_file", event.mLabels["plugin_type"]); + APSARA_TEST_EQUAL("1", event.mLabels["plugin_id"]); + APSARA_TEST_EQUAL("pipeline_test", event.mLabels["pipeline_name"]); + APSARA_TEST_EQUAL("project_a", event.mLabels["project"]); + APSARA_TEST_EQUAL(2U, event.mCounters.size()); + APSARA_TEST_EQUAL(100U, event.mCounters["out_size_bytes"]); + APSARA_TEST_EQUAL(1024U, event.mCounters["out_event_total"]); + APSARA_TEST_EQUAL(1U, event.mGauges.size()); + APSARA_TEST_EQUAL(10, event.mGauges["monitor_file_total"]); +} + +void SelfMonitorMetricEventUnittest::TestMerge() { + { + SelfMonitorMetricEvent event1; + SelfMonitorMetricEvent event2; + + // 初始化 event1 和 event2 + event1.mCounters["counter1"] = 100; + event1.mGauges["gauge1"] = 1.5; + event2.mCounters["counter1"] = 200; + event2.mGauges["gauge1"] = 2.5; + + event1.mUpdatedFlag = false; + event2.mUpdatedFlag = true; + + event1.Merge(event2); + + // 检验是否正确合并 + APSARA_TEST_EQUAL(300, event1.mCounters["counter1"]); + APSARA_TEST_EQUAL(2.5, event1.mGauges["gauge1"]); + APSARA_TEST_TRUE(event1.mUpdatedFlag); + } + // 含有不重叠键值的情况 + { + SelfMonitorMetricEvent event1; + SelfMonitorMetricEvent event2; + + // 初始化 event1 和 event2 + event1.mCounters["counter1"] = 100; + event2.mCounters["counter2"] = 200; + event1.mGauges["gauge1"] = 1.5; + event2.mGauges["gauge2"] = 2.5; + + event1.Merge(event2); + + // 检验是否正确合并 + APSARA_TEST_EQUAL(100, event1.mCounters["counter1"]); + APSARA_TEST_EQUAL(200, event1.mCounters["counter2"]); + APSARA_TEST_EQUAL(1.5, event1.mGauges["gauge1"]); + APSARA_TEST_EQUAL(2.5, event1.mGauges["gauge2"]); + } + // 不同发送间隔 + { + SelfMonitorMetricEvent event1; + SelfMonitorMetricEvent event2; + + event1.SetInterval(5); + event2.SetInterval(10); + + event1.mCounters["counter1"] = 100; + event2.mCounters["counter1"] = 200; + + event1.Merge(event2); + + // 检验间隔是否被设置为 event2 的间隔 + APSARA_TEST_EQUAL(0, event1.mLastSendInterval); + APSARA_TEST_EQUAL(10, event1.mSendInterval); + // 检验计数器是否正确合并 + APSARA_TEST_EQUAL(300, event1.mCounters["counter1"]); + } +} + +void SelfMonitorMetricEventUnittest::TestSendInterval() { + SelfMonitorMetricEvent event; + mSourceBuffer.reset(new SourceBuffer); + mEventGroup.reset(new PipelineEventGroup(mSourceBuffer)); + mMetricEvent = mEventGroup->CreateMetricEvent(); + + event.mUpdatedFlag = true; + event.SetInterval(3); + APSARA_TEST_FALSE(event.ShouldSend()); + APSARA_TEST_FALSE(event.ShouldDelete()); + APSARA_TEST_FALSE(event.ShouldSend()); // 模拟两次调用,间隔计数为2 + APSARA_TEST_FALSE(event.ShouldDelete()); + APSARA_TEST_TRUE(event.ShouldSend()); // 第三次调用,间隔计数达到3,应返回true + APSARA_TEST_FALSE(event.ShouldDelete()); + event.ReadAsMetricEvent(mMetricEvent.get()); + APSARA_TEST_FALSE(event.ShouldDelete()); + + event.mUpdatedFlag = false; + APSARA_TEST_FALSE(event.ShouldSend()); + APSARA_TEST_FALSE(event.ShouldDelete()); + APSARA_TEST_FALSE(event.ShouldSend()); + APSARA_TEST_FALSE(event.ShouldDelete()); + APSARA_TEST_FALSE(event.ShouldSend()); + APSARA_TEST_TRUE(event.ShouldDelete()); // 第三次调用,间隔计数达到3,应返回true +} + +} // namespace logtail + +int main(int argc, char** argv) { + logtail::Logger::Instance().InitGlobalLoggers(); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file From 38f1c8490d548ea6f4371ebe4f34c1b4cfded59a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Mon, 2 Dec 2024 08:56:32 +0000 Subject: [PATCH 4/5] polish --- core/unittest/monitor/SelfMonitorMetricEventUnittest.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/unittest/monitor/SelfMonitorMetricEventUnittest.cpp b/core/unittest/monitor/SelfMonitorMetricEventUnittest.cpp index 495026420d..1b22386701 100644 --- a/core/unittest/monitor/SelfMonitorMetricEventUnittest.cpp +++ b/core/unittest/monitor/SelfMonitorMetricEventUnittest.cpp @@ -70,6 +70,8 @@ void SelfMonitorMetricEventUnittest::TestCreateFromMetricEvent() { APSARA_TEST_EQUAL(1024U, event.mCounters["out_event_total"]); APSARA_TEST_EQUAL(1U, event.mGauges.size()); APSARA_TEST_EQUAL(10, event.mGauges["monitor_file_total"]); + + delete pluginMetric; } void SelfMonitorMetricEventUnittest::TestCreateFromGoMetricMap() { From 010e3ef2105fd092ffe6a00f4964d457477e6e47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Tue, 3 Dec 2024 14:44:09 +0800 Subject: [PATCH 5/5] polish --- .../metric_models/SelfMonitorMetricEvent.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/core/monitor/metric_models/SelfMonitorMetricEvent.cpp b/core/monitor/metric_models/SelfMonitorMetricEvent.cpp index 367163a400..e35698e359 100644 --- a/core/monitor/metric_models/SelfMonitorMetricEvent.cpp +++ b/core/monitor/metric_models/SelfMonitorMetricEvent.cpp @@ -67,8 +67,19 @@ SelfMonitorMetricEvent::SelfMonitorMetricEvent(const std::mapisString()) { try { mCounters[itr.key().asString()] = static_cast(std::stod(itr->asString())); - } catch (...) { + } catch (...) { // catch std::invalid_argument & std::out_of_range mCounters[itr.key().asString()] = 0; } }