Forráskód Böngészése

Feature #TASK_QT-19234 EBPF-euspace增加心跳

Tom 1 éve
szülő
commit
0e6df2ac04
2 módosított fájl, 46 hozzáadás és 0 törlés
  1. 41 0
      containers/apm_heartbeat.go
  2. 5 0
      main.go

+ 41 - 0
containers/apm_heartbeat.go

@@ -0,0 +1,41 @@
+package containers
+
+import (
+	log "github.com/sirupsen/logrus"
+	"os"
+	"strconv"
+	"time"
+)
+
+var hbStopChan = make(chan struct{})
+
+func DoHeartbeat(hbFilePath string) {
+	defer func() {
+		if err := recover(); err != nil {
+			log.Errorf("DoHeartbeat panic: %v", err)
+		}
+	}()
+
+	doHeartbeat(hbFilePath)
+	ticker := time.NewTicker(time.Second * 30)
+	for {
+		select {
+		case <-ticker.C:
+			doHeartbeat(hbFilePath)
+		case <-hbStopChan:
+			return
+		}
+	}
+}
+
+func doHeartbeat(hbFilePath string) {
+	timeUnix := time.Now().Unix()
+	timeStr := strconv.FormatInt(timeUnix, 10)
+	if err := os.WriteFile(hbFilePath, []byte(timeStr), 0644); err != nil {
+		log.Errorf("write heartbeat to file [%s] occurs error: %v", hbFilePath, err.Error())
+	}
+}
+
+func StopHeartbeat() {
+	close(hbStopChan)
+}

+ 5 - 0
main.go

@@ -9,6 +9,7 @@ import (
 	"github.com/coroot/coroot-node-agent/utils"
 	"github.com/coroot/coroot-node-agent/utils/enums"
 	"github.com/coroot/coroot-node-agent/utils/namedpipe"
+	"github.com/coroot/coroot-node-agent/utils/try"
 	dto "github.com/prometheus/client_model/go"
 	log "github.com/sirupsen/logrus"
 	"io"
@@ -16,6 +17,7 @@ import (
 	_ "net/http/pprof"
 	"os"
 	"path"
+	"path/filepath"
 	"regexp"
 	"runtime"
 	"strconv"
@@ -218,6 +220,9 @@ func main() {
 		npCtl.AcceptAndDisposeMsg(cr)
 	}
 
+	//heartbeat
+	try.GoParams(containers.DoHeartbeat, utils.CatchFn, filepath.Join(utils.GetRootPath(), "heartbeat"))
+
 	//profiling.Start()
 	//defer profiling.Stop()
 	// 创建一个/metrics路由处理函数