perf: 修复 Agent 校验 CPU 热点并实现指标分层聚合与频率下发
CPU 根因:每个 Agent 请求都做一次 bcrypt 校验(实测 72ms), 30 台主机 18 req/s × 72ms ≈ 1.35 核,实测容器 CPU 135%。 - agentauth: 校验结果按 Token 哈希缓存(TTL 5min,轮换即失效), last_used_at 写入按 60s 节流。实测 CPU 135% → 1.6%,请求延迟 103ms → 1.6ms - metricagg: 新增后台逐级降采样聚合 raw→1m/5m→1h→6h/1d,取代前端手动触发 - metrics_aggregate: 补 uniq_metric_agg 唯一索引(原 idx_agg 非唯一导致 upsert 插重复行) - agent 合并配置接口 GET /agent/config:一次返回任务+探针+端口策略+频率, 配置轮询 5s→30s;服务端可下发 report_interval,Agent 热更新 ticker 无需重装 - retention: agent_reports 保留期 7d→1d(趋势由聚合表承担) - redis: 增加 maxmemory 512mb + volatile-lru - fix(upgrade): 升级脚本改为 fail-fast,修复下载失败仍报 UPGRADE_OK 的静默谎报 // Logan.Li
This commit is contained in:
1 parent
93abf81e40
commit
59320aa410
23 files changed
+902
-29
No files matched your search
+1
-1
@@ -164,4 +164,4 @@ cd agent
|
||||
make # CGO_ENABLED=0 静态编译当前平台
|
||||
```
|
||||
|
||||
顶层 `./run.sh build agent` 会交叉编译 `linux-amd64`、`linux-arm64`、`darwin-arm64`、`windows-amd64.exe`。Docker 镜像会携带当前目标架构 Linux Agent 和 Windows AMD64 Agent。// Logan.Li
|
||||
顶层 `./run.sh build agent` 会交叉编译 `linux-amd64`、`linux-arm64`、`windows-amd64.exe`。Docker 镜像内置同样的三个平台 Agent 二进制,覆盖主流服务器架构。// Logan.Li
|
||||
@@ -106,7 +106,7 @@ curl -fSL -o /tmp/aiops-agent "${BINARY_URL}" || {
|
||||
echo "❌ 下载失败: ${BINARY_URL}"
|
||||
echo " 请检查: 1) 后端地址是否正确 2) 该平台是否有对应二进制"
|
||||
if [ "$OS_LOWER" = "darwin" ]; then
|
||||
echo " 提示: macOS 二进制需要后端编译 darwin 版本 (make build-darwin-arm64)"
|
||||
echo " 提示: 平台不再提供 macOS Agent 二进制,服务器采集请使用 Linux/Windows 主机"
|
||||
fi
|
||||
exit 1
|
||||
}
|
||||
|
||||
+72
-13
@@ -35,7 +35,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
version = "v2.1.0"
|
||||
version = "v2.2.0"
|
||||
buildTime = "unknown"
|
||||
)
|
||||
|
||||
@@ -292,33 +292,92 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
const pollInterval = 5 * time.Second
|
||||
// ── 配置轮询 + 上报循环(v2.2)────────────────────────────────
|
||||
//
|
||||
// 旧版每 5 秒分别拉取 tasks/poll、health-probes/config、port-policy 三个接口,
|
||||
// 配置类请求量是上报本身的 6 倍;现改为单次 GET /agent/config,
|
||||
// 并由服务端下发 report_interval,Agent 热更新 ticker、无需重装。 // Logan.Li
|
||||
const minInterval = 5 * time.Second
|
||||
const fallbackConfigPollInterval = 30 * time.Second
|
||||
intervalUpdates := make(chan time.Duration, 1)
|
||||
|
||||
configTicker := time.NewTicker(fallbackConfigPollInterval)
|
||||
go func() {
|
||||
pollAndExecute()
|
||||
pullProbes()
|
||||
pullPortPolicy()
|
||||
ticker := time.NewTicker(pollInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
defer configTicker.Stop()
|
||||
runConfig := func() {
|
||||
merged, err := taskClient.PollConfig(ctx)
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
// 服务端尚未升级(或暂时失败)时回退旧的分接口模式,保证兼容。 // Logan.Li
|
||||
pollAndExecute()
|
||||
pullProbes()
|
||||
pullPortPolicy()
|
||||
return
|
||||
}
|
||||
|
||||
// 执行服务端下发的任务。 // Logan.Li
|
||||
for _, t := range merged.Tasks {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
result := taskExecutor.ExecuteTask(ctx, t)
|
||||
if err := taskClient.Submit(ctx, result); err != nil {
|
||||
logger.Printf("任务 %s 结果回传失败: %v", t.TaskID, err)
|
||||
}
|
||||
}
|
||||
if len(merged.HealthProbes) > 0 {
|
||||
probeConfigs = merged.HealthProbes
|
||||
}
|
||||
if merged.PortPolicy != nil {
|
||||
portCollector.SetPolicy(merged.PortPolicy)
|
||||
}
|
||||
|
||||
// 服务端下发的上报间隔 → 通知主循环热更新 ticker。 // Logan.Li
|
||||
if merged.ReportInterval > 0 {
|
||||
next := time.Duration(merged.ReportInterval) * time.Second
|
||||
if next < minInterval {
|
||||
next = minInterval
|
||||
}
|
||||
select {
|
||||
case intervalUpdates <- next:
|
||||
default:
|
||||
}
|
||||
}
|
||||
// 服务端下发的配置轮询间隔 → 调整本循环节奏。 // Logan.Li
|
||||
if merged.ConfigPollInterval > 0 {
|
||||
next := time.Duration(merged.ConfigPollInterval) * time.Second
|
||||
if next >= minInterval && next != fallbackConfigPollInterval {
|
||||
configTicker.Reset(next)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
runConfig()
|
||||
for {
|
||||
select {
|
||||
case <-configTicker.C:
|
||||
runConfig()
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// 上报循环:间隔可被服务端热更新。 // Logan.Li
|
||||
time.Sleep(startupJitter)
|
||||
collectAndReport()
|
||||
ticker := time.NewTicker(time.Duration(cfg.Interval) * time.Second)
|
||||
defer ticker.Stop()
|
||||
reportTicker := time.NewTicker(time.Duration(cfg.Interval) * time.Second)
|
||||
defer reportTicker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
case <-reportTicker.C:
|
||||
collectAndReport()
|
||||
case next := <-intervalUpdates:
|
||||
// 重新抖动,避免多台主机在服务端下发同值后被对齐成同一时刻上报。 // Logan.Li
|
||||
reportTicker.Reset(next)
|
||||
logger.Printf("服务端下发新的上报间隔: %s", next)
|
||||
case <-ctx.Done():
|
||||
logger.Println("收到退出信号,Agent 正常关闭")
|
||||
return
|
||||
|
||||
@@ -20,6 +20,8 @@ const (
|
||||
resultPathFmt = "/api/v1/agent/tasks/%s/result"
|
||||
probePath = "/api/v1/agent/health-probes/config"
|
||||
portPolicyPath = "/api/v1/agent/port-policy"
|
||||
// configPath 合并配置接口(v2.2):一次拉取任务+探针+端口策略+采集频率。 // Logan.Li
|
||||
configPath = "/api/v1/agent/config"
|
||||
)
|
||||
|
||||
// Client 负责与后端任务/探针接口通信。
|
||||
@@ -90,3 +92,32 @@ func (c *Client) PollPortPolicy(ctx context.Context) (*model.PortPolicy, error)
|
||||
}
|
||||
return &wrapper.Data, nil
|
||||
}
|
||||
|
||||
// AgentConfig 合并配置响应(v2.2)。
|
||||
//
|
||||
// 旧版 Agent 每 5 秒要分别请求 tasks/poll、health-probes/config、port-policy
|
||||
// 三个接口,配置类请求量是上报本身的 6 倍;合并后单次请求即可拿到全部配置。 // Logan.Li
|
||||
type AgentConfig struct {
|
||||
ConfigVersion string `json:"config_version"`
|
||||
ReportInterval int `json:"report_interval"`
|
||||
ConfigPollInterval int `json:"config_poll_interval"`
|
||||
Tasks []Task `json:"tasks"`
|
||||
HealthProbes []model.ProbeConfig `json:"health_probes"`
|
||||
PortPolicy *model.PortPolicy `json:"port_policy,omitempty"`
|
||||
}
|
||||
|
||||
// PollConfig 一次性拉取任务 + 探针 + 端口策略 + 采集频率(v2.2 合并接口)。
|
||||
// 若服务端尚未升级(404),调用方应回退到分接口拉取。 // Logan.Li
|
||||
func (c *Client) PollConfig(ctx context.Context) (*AgentConfig, error) {
|
||||
path := configPath + "?hostname=" + url.QueryEscape(c.hostname)
|
||||
|
||||
var wrapper struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data AgentConfig `json:"data"`
|
||||
}
|
||||
if err := c.client.DoJSON(ctx, "GET", path, nil, &wrapper); err != nil {
|
||||
return nil, fmt.Errorf("配置拉取失败: %w", err)
|
||||
}
|
||||
return &wrapper.Data, nil
|
||||
}
|
||||
Reference in new issue
Block a user