8000 Add System Load collector by WRPStephanie · Pull Request #2231 · alibaba/loongcollector · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add System Load collector #2231

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

Merged
merged 1 commit into from
Jun 19, 2025
Merged

Add System Load collector #2231

merged 1 commit into from
Jun 19, 2025

Conversation

WRPStephanie
Copy link
Contributor
@WRPStephanie WRPStephanie commented May 22, 2025

新增采集系统负载的collector,通过读取/proc/loadavg的内容采集系统负载,和对应的ut
新增一个函数GetHostSystemStatWithPath,读取指定文件的内容,和对应ut

@WRPStephanie WRPStephanie force-pushed the pr branch 3 times, most recently from aeeef02 to d606f34 Compare May 26, 2025 09:28
struct MetricDef {
const char* name;
double* value;
} metrics[] = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CPU这样写是用constexpr + 数组定义一个schema。这里如果是存的具体的指标数据的话,直接用vector就行。下面有个18可以改成 metrics.size()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CPU这样写是用constexpr + 数组定义一个schema。这里如果是存的具体的指标数据的话,直接用vector就行。下面有个18可以改成 metrics.size()

已修改

8000

Init();
}
int SystemCollector::Init(int totalCount) {
mTotalCount = totalCount;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个mTotalCount其实是指要发送的周期?这个名字表示的含义不太清楚

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个mTotalCount其实是指要发送的周期?这个名字表示的含义不太清楚

是的,采集周期和发送周期不一定一致,mTotalCount=发送周期/采集周期,意思是采集多少轮以后发送一次。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成mCountPerReport或mCountPerStat是不是更好

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成mCountPerReport或mCountPerStat是不是更好

已修改

std::vector<std::string> loadMetric;
boost::split(loadMetric, loadLines[0], boost::is_any_of(" "), boost::token_compress_on);

systemload.load1 = std::atof(loadMetric[0].c_str());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

外部输入做好保护,下标访问合法性判断

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

外部输入做好保护,下标访问合法性判断

已修改

Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new system load collector that reads /proc/loadavg to capture system load metrics and introduces corresponding unit tests. Key changes include:

  • Adding a new collector (SystemCollector) and its implementation.
  • Creating new unit tests for GetHostSystemStatWithPath and the collector’s logic.
  • Updating CMakeLists.txt and related files to include the new collector.

Reviewed Changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
core/unittest/host_monitor/SystemInformationToolsUnittest.cpp Updated unit tests to use the new GetHostSystemStatWithPath function; legacy test code is commented out.
core/unittest/host_monitor/SystemCollectorUnittest.cpp Added tests for system load statistic collection and metric event validation.
core/host_monitor/collector/SystemCollector.h / SystemCollector.cpp Added the SystemCollector implementation for reading load data and calculating per-core metrics.
core/host_monitor/Constants.h / Constants.cpp Introduced PROCESS_LOADAVG constant to support file path changes.
Other files (CMakeLists, InputHostMonitor.cpp, etc.) Integrated the new collector into the existing test and plugin infrastructure.
Comments suppressed due to low confidence (1)

core/host_monitor/collector/SystemCollector.cpp:112

  • Verify that the argument passed to SetValue is correct; passing the metric event itself may not be intended and could lead to unexpected behavior.
metricEvent->SetValue<UntypedMultiDoubleValues>(metricEvent);

@@ -26,23 +26,36 @@ namespace logtail {

class SystemInformationToolsUnittest : public testing::Test {
public:
void TestGetHostSystemStat() const;
// void TestGetHostSystemStat() const;
Copy link
Preview
Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] If the legacy test is no longer necessary, consider removing the commented-out TestGetHostSystemStat code to reduce clutter.

Suggested change
// void TestGetHostSystemStat() const;

Copilot uses AI. Check for mistakes.

}
};

void SystemInformationToolsUnittest::TestGetHostSystemStat() const {
// void SystemInformationToolsUnittest::TestGetHostSystemStat() const {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个直接删除吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个直接删除吧

已删除

systemload.load5 = std::stod(loadMetric[1]);
systemload.load15 = std::stod(loadMetric[2]);

auto cpuCoreCount = static_cast<double>(std::thread::hardware_concurrency());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto cpuCoreCount = static_cast<double>(std::thread::hardware_concurrency());
cpuCoreCount = cpuCoreCount < 1 ? 1.0 : cpuCoreCount;

这两行放到init里吧

@WRPStephanie WRPStephanie force-pushed the pr branch 2 times, most recently from 087e975 to fe2597c Compare June 10, 2025 12:47
private:
int mTotalCount = 0;
int mCount = 0;
double cpuCoreCount = 1.0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

命名规范统一

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

命名规范统一

已修改


namespace logtail {
9E81
extern const uint32_t kMinInterval;
Copy link
Collaborator
@yyuuttaaoo yyuuttaaoo Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个到底是哪个kMinInterval,代码里有两个,一个在InputHostMeta,一个在InputHostMonitor。
请用constexpr定义在头文件中,不要用隐式依赖。
原来的文件如已有问题,请帮忙一并改正。
下面量变量同理

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个到底是哪个kMinInterval,代码里有两个,一个在InputHostMeta,一个在InputHostMonitor。 请用constexpr定义在头文件中,不要用隐式依赖。 原来的文件如已有问题,请帮忙一并改正。 下面量变量同理

已修改


extern const uint32_t kHostMonitorMinInterval;
extern const uint32_t kHostMonitorDefaultInterval;
extern std::filesystem::path PROC_LOADAVG;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个好像没用到

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个好像没用到

最开始用了,后面修改后忘记删除了。已删除。

@WRPStephanie WRPStephanie force-pushed the pr branch 2 times, most recently from c0bb150 to b8a494d Compare June 19, 2025 02:32
@yyuuttaaoo yyuuttaaoo merged commit d8a913d into alibaba:main Jun 19, 2025
29 checks passed
xiongyunn pushed a commit to xiongyunn/loongcollector that referenced this pull request Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0