From d37328df7a147b144ffb83bce06e916583a65ce0 Mon Sep 17 00:00:00 2001 From: yiqiwang-17 <66986742+yiqiwang-17@users.noreply.github.com> Date: Wed, 6 Nov 2024 19:56:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E4=B8=BB?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=8F=98=E6=9B=B4=E9=87=8D=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beater/beater.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/beater/beater.go b/beater/beater.go index c440e0c..10df816 100644 --- a/beater/beater.go +++ b/beater/beater.go @@ -28,7 +28,9 @@ package beater import ( "context" + "encoding/json" "fmt" + "github.com/TencentBlueKing/bkunifylogbeat/utils" "time" "github.com/TencentBlueKing/bkmonitor-datalink/pkg/libgse/beat" @@ -40,9 +42,12 @@ import ( _ "github.com/TencentBlueKing/bkunifylogbeat/include" "github.com/TencentBlueKing/bkunifylogbeat/registrar" "github.com/pkg/errors" + + "github.com/elastic/beats/libbeat/cfgfile" ) var Registrar *registrar.Registrar +var lastTaskHash string // LogBeat package cadvisor type LogBeat struct { @@ -118,6 +123,7 @@ func (bt *LogBeat) Run() error { } reloadTicker := time.NewTicker(10 * time.Second) + diffTaskTicker := time.NewTicker(10 * time.Second) defer reloadTicker.Stop() for { select { @@ -130,6 +136,11 @@ func (bt *LogBeat) Run() error { bt.Reload(config) } } + // 处理采集器主配置是否变更,变更则发送重加载信号 + case <-diffTaskTicker.C: + if err = bt.checkNeedReload(); err != nil { + logp.L.Error(err) + } case <-beat.ReloadChan: bt.isReload = true // 处理采集器框架发送的结束采集器的信号(常由SIGINT引起),关闭采集器 @@ -148,6 +159,42 @@ func (bt *LogBeat) Stop() { close(bt.done) } +// Main config diff check +func (bt *LogBeat) checkNeedReload() error { + rawConfig, err := cfgfile.Load("", nil) + if err != nil { + return err + } + if !rawConfig.HasField("bkunifylogbeat") { + return errors.New("no bkunifylogbeat field found") + } + + beatConfig, err := rawConfig.Child("bkunifylogbeat", -1) + if err != nil { + return err + } + config, err := cfg.Parse(beatConfig) + if err != nil { + return err + } + b, err := json.Marshal(config) + if err != nil { + return err + } + + currentTaskHash := utils.Md5(string(b)) + if len(lastTaskHash) == 0 { + lastTaskHash = currentTaskHash + } + if lastTaskHash != currentTaskHash { + lastTaskHash = currentTaskHash + bt.Reload(beatConfig) + logp.L.Info("Reload main config task.") + } + + return nil +} + // Close cadvisor storage interface func (bt *LogBeat) Close() error { return nil From 3c08bf0d37324f02be9ffe47772767c14a2971a2 Mon Sep 17 00:00:00 2001 From: yiqiwang-17 <66986742+yiqiwang-17@users.noreply.github.com> Date: Wed, 6 Nov 2024 20:22:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E4=B8=BB?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=8F=98=E6=9B=B4=E9=87=8D=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beater/beater.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beater/beater.go b/beater/beater.go index 10df816..4b0d53e 100644 --- a/beater/beater.go +++ b/beater/beater.go @@ -123,7 +123,7 @@ func (bt *LogBeat) Run() error { } reloadTicker := time.NewTicker(10 * time.Second) - diffTaskTicker := time.NewTicker(10 * time.Second) + diffTaskTicker := time.NewTicker(60 * time.Second) defer reloadTicker.Stop() for { select {