-
Notifications
You must be signed in to change notification settings - Fork 434
Feat: Reporting metrics upon first startup #2356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,15 +37,18 @@ PipelineConfigWatcher::PipelineConfigWatcher() | |
| mTaskPipelineManager(TaskPipelineManager::GetInstance()) { | ||
| } | ||
|
|
||
| pair<CollectionConfigDiff, TaskConfigDiff> PipelineConfigWatcher::CheckConfigDiff() { | ||
| pair<CollectionConfigDiff, TaskConfigDiff> PipelineConfigWatcher::CheckConfigDiff(bool builtinOnly) { | ||
| CollectionConfigDiff pDiff; | ||
| TaskConfigDiff tDiff; | ||
| unordered_set<string> configSet; | ||
| SingletonConfigCache singletonCache; | ||
| // builtin pipeline configs | ||
| InsertBuiltInPipelines(pDiff, tDiff, configSet, singletonCache); | ||
| // file pipeline configs | ||
| InsertPipelines(pDiff, tDiff, configSet, singletonCache); | ||
|
|
||
| if (!builtinOnly) { | ||
| // file pipeline configs | ||
| InsertPipelines(pDiff, tDiff, configSet, singletonCache); | ||
| } | ||
|
|
||
| CheckSingletonInput(pDiff, singletonCache); | ||
| for (const auto& name : mCollectionPipelineManager->GetAllConfigNames()) { | ||
|
|
@@ -100,6 +103,13 @@ void PipelineConfigWatcher::InsertBuiltInPipelines(CollectionConfigDiff& pDiff, | |
| for (const auto& pipeline : builtInPipelines) { | ||
| const string& pipelineName = pipeline.first; | ||
| const string& pipleineDetail = pipeline.second; | ||
| if (configSet.find(pipelineName) != configSet.end()) { | ||
| LOG_WARNING(sLogger, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 什么时候会出现 |
||
| ("more than 1 config with the same name is found", "skip current config")("inner pipeline", | ||
| pipelineName)); | ||
| continue; | ||
| } | ||
| configSet.insert(pipelineName); | ||
|
|
||
| string errorMsg; | ||
| auto iter = mInnerConfigMap.find(pipelineName); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,8 +16,14 @@ | |
|
|
||
| #include "monitor/SelfMonitorServer.h" | ||
|
|
||
| #include <json/json.h> | ||
|
|
||
| #include <fstream> | ||
|
|
||
| #include "MetricConstants.h" | ||
| #include "Monitor.h" | ||
| #include "app_config/AppConfig.h" | ||
| #include "common/FileSystemUtil.h" | ||
| #include "runner/ProcessorRunner.h" | ||
|
|
||
| using namespace std; | ||
|
|
@@ -100,7 +106,7 @@ void SelfMonitorServer::RemoveMetricPipeline() { | |
| LOG_INFO(sLogger, ("self-monitor metrics pipeline", "removed")); | ||
| } | ||
|
|
||
| void SelfMonitorServer::SendMetrics() { | ||
| void SelfMonitorServer::SendMetrics(bool updateAppInfo) { | ||
| ReadMetrics::GetInstance()->UpdateMetrics(); | ||
|
|
||
| ReadLock lock(mMetricPipelineLock); | ||
|
|
@@ -117,6 +123,10 @@ void SelfMonitorServer::SendMetrics() { | |
| pipelineEventGroup.SetMetadata(EventGroupMetaKey::INTERNAL_DATA_TYPE, INTERNAL_DATA_TYPE_METRIC); | ||
| ReadAsPipelineEventGroup(pipelineEventGroup); | ||
|
|
||
| if (updateAppInfo) { | ||
| UpdateAppInfoJson(pipelineEventGroup); | ||
| } | ||
|
|
||
| if (pipelineEventGroup.GetEvents().size() > 0) { | ||
| ProcessorRunner::GetInstance()->PushQueue( | ||
| mMetricPipelineCtx->GetProcessQueueKey(), mMetricInputIndex, std::move(pipelineEventGroup)); | ||
|
|
@@ -213,4 +223,79 @@ void SelfMonitorServer::SendAlarms() { | |
| } | ||
| } | ||
|
|
||
| void SelfMonitorServer::SendStartMetric() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 尽量时序上去保证先后顺序 |
||
| // 等待mMetricPipelineCtx存在,最多等待1秒 | ||
| auto startTime = std::chrono::steady_clock::now(); | ||
| while (true) { | ||
| ReadLock lock(mMetricPipelineLock); | ||
| if (mMetricPipelineCtx != nullptr && mSelfMonitorMetricRules != nullptr) { | ||
| auto endTime = std::chrono::steady_clock::now(); | ||
| auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime); | ||
| LOG_INFO(sLogger, | ||
| ("send start metric", "wait metric pipeline ready")("cost milliseconds", duration.count())); | ||
| break; | ||
| } | ||
| lock.unlock(); | ||
|
|
||
| // 检查是否超时 | ||
| auto currentTime = std::chrono::steady_clock::now(); | ||
| auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime); | ||
| if (elapsed.count() >= 1000) { // 1秒超时,实测正常启动应该1ms内完成 | ||
| LOG_WARNING(sLogger, ("send start metric", "wait metric pipeline ready timeout, skip")); | ||
| return; | ||
| } | ||
|
|
||
| std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||
| } | ||
| // 启动时需要立刻更新一下cpu和memory指标,因为正常启动时,cpu和memory指标会等30s更新一次 | ||
| LogtailMonitor::GetInstance()->UpdateCpuMem(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 都没加载配置,cpu、内存此时没有什么意义 |
||
|
|
||
| // 调用SendMetrics,并启用UpdateAppInfo | ||
| // TODO: SendMetrics(true); | ||
| SendMetrics(); | ||
| LOG_INFO(sLogger, ("send start metric", "sent successfully")); | ||
| } | ||
|
|
||
| void SelfMonitorServer::UpdateAppInfoJson(const PipelineEventGroup& pipelineEventGroup) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 没有什么有意义的信息,就不要李留这些代码 |
||
| // 读取现有的app_info.json | ||
| string appInfoFile = GetAgentAppInfoFile(); | ||
| Json::Value appInfoJson; | ||
|
|
||
| // 尝试读取现有文件 | ||
| ifstream file(appInfoFile); | ||
| if (file.is_open()) { | ||
| Json::CharReaderBuilder builder; | ||
| string errors; | ||
| if (Json::parseFromStream(builder, file, &appInfoJson, &errors)) { | ||
| file.close(); | ||
| } else { | ||
| file.close(); | ||
| LOG_WARNING(sLogger, ("failed to parse existing app_info.json", errors)); | ||
| } | ||
| } | ||
|
|
||
| const auto& events = pipelineEventGroup.GetEvents(); | ||
| for (const auto& eventPtr : events) { | ||
| if (!eventPtr.Is<MetricEvent>()) { | ||
| continue; | ||
| } | ||
|
|
||
| const auto& metricEvent = eventPtr.Cast<MetricEvent>(); | ||
| const std::string metricName = metricEvent.GetName().to_string(); | ||
|
|
||
| // 检查是否是agent指标 | ||
| if (metricName == MetricCategory::METRIC_CATEGORY_AGENT) { | ||
| // TODO: 从metricEvent中提取agent指标设置到appInfoJson | ||
| } | ||
| // 检查是否是runner指标 | ||
| else if (metricName == MetricCategory::METRIC_CATEGORY_RUNNER) { | ||
| // TODO: 从metricEvent中提取runner指标,例如所有已经启动的runner的名字 | ||
| } | ||
| } | ||
| // 写入更新后的app_info.json | ||
| string appInfo = appInfoJson.toStyledString(); | ||
| OverwriteFile(appInfoFile, appInfo); | ||
| LOG_INFO(sLogger, ("updated app_info.json with additional metric info", appInfo)); | ||
| } | ||
|
|
||
| } // namespace logtail | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
开源版调用会有什么影响?尽量少引入特殊的控制。