Skip to content

Commit efef604

Browse files
committed
feat: controller and analyzer add state counter
1 parent dc3f931 commit efef604

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

server/controller/db/metadb/model/model.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,22 @@ type Controller struct {
117117
Lcuuid string `gorm:"column:lcuuid;type:char(64);not null" json:"LCUUID"`
118118
}
119119

120+
func (c Controller) GetLcuuid() string {
121+
return c.Lcuuid
122+
}
123+
124+
func (c Controller) GetName() string {
125+
return c.Name
126+
}
127+
128+
func (c Controller) GetIP() string {
129+
return c.IP
130+
}
131+
132+
func (c Controller) GetState() int {
133+
return c.State
134+
}
135+
120136
type AZControllerConnection struct {
121137
ID int `gorm:"primaryKey;column:id;type:int;not null" json:"ID"`
122138
AZ string `gorm:"column:az;type:char(64);default:ALL" json:"AZ"`
@@ -153,6 +169,22 @@ type Analyzer struct {
153169
Lcuuid string `gorm:"column:lcuuid;type:char(64);not null" json:"LCUUID"`
154170
}
155171

172+
func (a Analyzer) GetLcuuid() string {
173+
return a.Lcuuid
174+
}
175+
176+
func (a Analyzer) GetName() string {
177+
return a.Name
178+
}
179+
180+
func (a Analyzer) GetIP() string {
181+
return a.IP
182+
}
183+
184+
func (a Analyzer) GetState() int {
185+
return a.State
186+
}
187+
156188
type AZAnalyzerConnection struct {
157189
ID int `gorm:"primaryKey;column:id;type:int;not null" json:"ID"`
158190
AZ string `gorm:"column:az;type:char(64);default:ALL" json:"AZ"`

server/controller/monitor/analyzer.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ import (
3030
"github.com/deepflowio/deepflow/server/controller/http/service/rebalance"
3131
mconfig "github.com/deepflowio/deepflow/server/controller/monitor/config"
3232
"github.com/deepflowio/deepflow/server/controller/trisolaris/refresh"
33+
"github.com/deepflowio/deepflow/server/libs/stats"
3334
)
3435

3536
type AnalyzerCheck struct {
37+
stateCounter *SysStateCounter[metadbmodel.Analyzer]
38+
3639
cCtx context.Context
3740
cCancel context.CancelFunc
3841
cfg mconfig.MonitorConfig
@@ -45,9 +48,15 @@ type AnalyzerCheck struct {
4548

4649
func NewAnalyzerCheck(cfg *config.ControllerConfig, ctx context.Context) *AnalyzerCheck {
4750
cCtx, cCancel := context.WithCancel(ctx)
51+
stateCounter := NewSysStateCounter[metadbmodel.Analyzer]()
52+
err := stats.RegisterCountableWithModulePrefix("controller_", "analyzer", stateCounter, stats.OptionStatTags{"type": "analyzer"})
53+
if err != nil {
54+
log.Error(err)
55+
}
4856
return &AnalyzerCheck{
4957
cCtx: cCtx,
5058
cCancel: cCancel,
59+
stateCounter: stateCounter,
5160
cfg: cfg.MonitorCfg,
5261
healthCheckPort: cfg.ListenPort,
5362
healthCheckNodePort: cfg.ListenNodePort,
@@ -231,6 +240,14 @@ func (c *AnalyzerCheck) healthCheck(orgDB *metadb.DB) {
231240
}
232241
}
233242
}
243+
244+
analyzers = []metadbmodel.Analyzer{}
245+
if err := metadb.DefaultDB.Find(&analyzers).Error; err != nil {
246+
log.Errorf("get analyzer state from db error: %s", err.Error())
247+
} else {
248+
c.stateCounter.AddSysStateCounters(analyzers)
249+
}
250+
234251
log.Info("analyzer health check end")
235252
}
236253

server/controller/monitor/common.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package monitor
1919
import (
2020
"fmt"
2121
"net/http"
22+
"sync"
2223
"time"
2324

2425
"github.com/deepflowio/deepflow/server/controller/common"
@@ -92,3 +93,61 @@ func getIPMap(hostType string) (map[string]bool, error) {
9293
}
9394
return res, nil
9495
}
96+
97+
type SysComponent interface {
98+
GetLcuuid() string
99+
GetName() string
100+
GetIP() string
101+
GetState() int
102+
}
103+
104+
type SysState struct {
105+
Name string `statsd:"name"`
106+
IP string `statsd:"ip"`
107+
State uint64 `statsd:"state"`
108+
}
109+
110+
type SysStateCounter[T SysComponent] struct {
111+
mu sync.Mutex
112+
counters map[string]*SysState
113+
}
114+
115+
func NewSysStateCounter[T SysComponent]() *SysStateCounter[T] {
116+
return &SysStateCounter[T]{
117+
counters: make(map[string]*SysState),
118+
}
119+
}
120+
121+
func (s *SysStateCounter[T]) GetCounter() interface{} {
122+
s.mu.Lock()
123+
counters := s.counters
124+
counterSize := len(counters)
125+
s.counters = make(map[string]*SysState, counterSize)
126+
s.mu.Unlock()
127+
128+
if counterSize == 0 {
129+
return nil
130+
}
131+
132+
result := make([]*SysState, 0, counterSize)
133+
for _, counter := range counters {
134+
result = append(result, counter)
135+
}
136+
return result
137+
}
138+
139+
func (s *SysStateCounter[T]) AddSysStateCounters(sysComponents []T) {
140+
s.mu.Lock()
141+
defer s.mu.Unlock()
142+
for _, sysComponent := range sysComponents {
143+
s.counters[sysComponent.GetLcuuid()] = &SysState{
144+
Name: sysComponent.GetName(),
145+
IP: sysComponent.GetIP(),
146+
State: uint64(sysComponent.GetState()),
147+
}
148+
}
149+
}
150+
151+
func (s *SysStateCounter[T]) Closed() bool {
152+
return false
153+
}

server/controller/monitor/controller.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/deepflowio/deepflow/server/controller/model"
3131
mconfig "github.com/deepflowio/deepflow/server/controller/monitor/config"
3232
"github.com/deepflowio/deepflow/server/controller/trisolaris/refresh"
33+
"github.com/deepflowio/deepflow/server/libs/stats"
3334
)
3435

3536
type dbAndIP struct {
@@ -38,6 +39,8 @@ type dbAndIP struct {
3839
}
3940

4041
type ControllerCheck struct {
42+
stateCounter *SysStateCounter[metadbmodel.Controller]
43+
4144
cCtx context.Context
4245
cCancel context.CancelFunc
4346
cfg mconfig.MonitorConfig
@@ -50,9 +53,15 @@ type ControllerCheck struct {
5053

5154
func NewControllerCheck(cfg *config.ControllerConfig, ctx context.Context) *ControllerCheck {
5255
cCtx, cCancel := context.WithCancel(ctx)
56+
stateCounter := NewSysStateCounter[metadbmodel.Controller]()
57+
err := stats.RegisterCountableWithModulePrefix("controller_", "controller", stateCounter, stats.OptionStatTags{"type": "controller"})
58+
if err != nil {
59+
log.Error(err)
60+
}
5361
return &ControllerCheck{
5462
cCtx: cCtx,
5563
cCancel: cCancel,
64+
stateCounter: stateCounter,
5665
cfg: cfg.MonitorCfg,
5766
healthCheckPort: cfg.ListenPort,
5867
healthCheckNodePort: cfg.ListenNodePort,
@@ -240,6 +249,14 @@ func (c *ControllerCheck) healthCheck(orgDB *metadb.DB) {
240249
}
241250
}
242251
c.cleanExceptionControllerData(orgDB, controllerIPs)
252+
253+
controllers = []metadbmodel.Controller{}
254+
if err := metadb.DefaultDB.Find(&controllers).Error; err != nil {
255+
log.Errorf("get controller state from db error: %s", err.Error())
256+
} else {
257+
c.stateCounter.AddSysStateCounters(controllers)
258+
}
259+
243260
log.Info("controller health check end")
244261
}
245262

0 commit comments

Comments
 (0)