Skip to content

Commit cc1696b

Browse files
committed
feat: controller and analyzer add state counter
1 parent 191d95e commit cc1696b

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-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
@@ -54,11 +57,18 @@ func NewAnalyzerCheck(cfg *config.ControllerConfig, ctx context.Context) *Analyz
5457
ch: make(chan dbAndIP, cfg.MonitorCfg.HealthCheckHandleChannelLen),
5558
normalAnalyzerDict: make(map[string]*dfHostCheck),
5659
exceptionAnalyzerDict: make(map[string]*dfHostCheck),
60+
stateCounter: NewSysStateCounter[metadbmodel.Analyzer](),
5761
}
5862
}
5963

6064
func (c *AnalyzerCheck) Start(sCtx context.Context) {
6165
log.Info("analyzer check start")
66+
67+
err := stats.RegisterCountableWithModulePrefix("server_", "analyzer", c.stateCounter, stats.OptionStatTags{"type": "analyzer"})
68+
if err != nil {
69+
log.Error(err)
70+
}
71+
6272
go func() {
6373
ticker := time.NewTicker(time.Duration(c.cfg.SyncDefaultORGDataInterval) * time.Second)
6474
defer ticker.Stop()
@@ -123,6 +133,7 @@ func (c *AnalyzerCheck) Start(sCtx context.Context) {
123133

124134
func (c *AnalyzerCheck) Stop() {
125135
if c.cCancel != nil {
136+
c.stateCounter.Closed()
126137
c.cCancel()
127138
}
128139
log.Info("analyzer check stopped")
@@ -231,6 +242,12 @@ func (c *AnalyzerCheck) healthCheck(orgDB *metadb.DB) {
231242
}
232243
}
233244
}
245+
246+
analyzers = []metadbmodel.Analyzer{}
247+
if err := metadb.DefaultDB.Find(&analyzers).Error; err == nil {
248+
c.stateCounter.AddSysStateCounters(analyzers)
249+
}
250+
234251
log.Info("analyzer health check end")
235252
}
236253

server/controller/monitor/common.go

Lines changed: 54 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,56 @@ 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+
counters := make(map[string]*SysState, len(s.Counters))
123+
s.mu.Lock()
124+
counters, s.Counters = s.Counters, counters
125+
s.mu.Unlock()
126+
127+
result := make([]*SysState, 0, len(counters))
128+
for _, counter := range counters {
129+
result = append(result, counter)
130+
}
131+
return result
132+
}
133+
134+
func (s *SysStateCounter[T]) AddSysStateCounters(sysComponents []T) {
135+
s.mu.Lock()
136+
defer s.mu.Unlock()
137+
for _, sysComponent := range sysComponents {
138+
s.Counters[sysComponent.GetLcuuid()] = &SysState{
139+
Name: sysComponent.GetName(),
140+
IP: sysComponent.GetIP(),
141+
State: uint64(sysComponent.GetState()),
142+
}
143+
}
144+
}
145+
146+
func (s *SysStateCounter[T]) Closed() bool {
147+
return false
148+
}

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
@@ -59,11 +62,18 @@ func NewControllerCheck(cfg *config.ControllerConfig, ctx context.Context) *Cont
5962
ch: make(chan dbAndIP, cfg.MonitorCfg.HealthCheckHandleChannelLen),
6063
normalControllerDict: make(map[string]*dfHostCheck),
6164
exceptionControllerDict: make(map[string]*dfHostCheck),
65+
stateCounter: NewSysStateCounter[metadbmodel.Controller](),
6266
}
6367
}
6468

6569
func (c *ControllerCheck) Start(sCtx context.Context) {
6670
log.Info("controller check start")
71+
72+
err := stats.RegisterCountableWithModulePrefix("server_", "controller", c.stateCounter, stats.OptionStatTags{"type": "controller"})
73+
if err != nil {
74+
log.Error(err)
75+
}
76+
6777
go func() {
6878
ticker := time.NewTicker(time.Duration(c.cfg.SyncDefaultORGDataInterval) * time.Second)
6979
defer ticker.Stop()
@@ -119,6 +129,7 @@ func (c *ControllerCheck) Start(sCtx context.Context) {
119129

120130
func (c *ControllerCheck) Stop() {
121131
if c.cCancel != nil {
132+
c.stateCounter.Closed()
122133
c.cCancel()
123134
}
124135
log.Info("controller check stopped")
@@ -240,6 +251,12 @@ func (c *ControllerCheck) healthCheck(orgDB *metadb.DB) {
240251
}
241252
}
242253
c.cleanExceptionControllerData(orgDB, controllerIPs)
254+
255+
controllers = []metadbmodel.Controller{}
256+
if err := metadb.DefaultDB.Find(&controllers).Error; err == nil {
257+
c.stateCounter.AddSysStateCounters(controllers)
258+
}
259+
243260
log.Info("controller health check end")
244261
}
245262

0 commit comments

Comments
 (0)