Skip to content

Commit

Permalink
add metrics for UP/DOWN nodes (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyang0 committed Oct 17, 2023
1 parent 9265573 commit ce8d1bc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
13 changes: 13 additions & 0 deletions metrics/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,27 @@ func (m *Metrics) ResourceMiddleware(cluster cluster.Cluster) func(http.Handler)
if err != nil {
logger.Error(ctx, err, "Get all nodes err")
}
podUpDownNodes := map[string][]int{}
for node := range nodes {
if podUpDownNodes[node.Podname] == nil {
podUpDownNodes[node.Podname] = []int{0, 0}
}
if node.IsDown() {
podUpDownNodes[node.Podname][1]++
} else {
podUpDownNodes[node.Podname][0]++
}
metrics, err := m.rmgr.GetNodeMetrics(ctx, node)
if err != nil {
logger.Error(ctx, err, "Get metrics failed")
continue
}
m.SendMetrics(ctx, metrics...)
}
for podname, ud := range podUpDownNodes {
m.SendPodUpDownNodes(ctx, podname, ud[0], ud[1])
}

h.ServeHTTP(w, r)
})
}
Expand Down
37 changes: 35 additions & 2 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ import (
const (
deployCountKey = "core.%s.deploy.count"
deployCountName = "core_deploy"
gaugeType = "gauge"
counterType = "counter"
upNodesKey = "core.up.nodes"
upNodesName = "core_up_nodes"
downNodesKey = "core.down.nodes"
downNodesName = "core_down_nodes"

gaugeType = "gauge"
counterType = "counter"
)

// Metrics define metrics
Expand Down Expand Up @@ -53,6 +58,24 @@ func (m *Metrics) SendDeployCount(ctx context.Context, n int) {
m.SendMetrics(ctx, metrics)
}

func (m *Metrics) SendPodUpDownNodes(ctx context.Context, podname string, up, down int) {
log.WithFunc("metrics.SendUpDownNodes").Info(ctx, "Update deploy counter")
m1 := &plugintypes.Metrics{
Name: upNodesName,
Labels: []string{m.Hostname, podname},
Key: upNodesKey,
Value: strconv.Itoa(up),
}

m2 := &plugintypes.Metrics{
Name: downNodesName,
Labels: []string{m.Hostname, podname},
Key: downNodesKey,
Value: strconv.Itoa(down),
}
m.SendMetrics(ctx, m1, m2)
}

// SendMetrics update metrics
func (m *Metrics) SendMetrics(ctx context.Context, metrics ...*plugintypes.Metrics) {
logger := log.WithFunc("metrics.SendMetrics")
Expand Down Expand Up @@ -208,6 +231,16 @@ func InitMetrics(ctx context.Context, config types.Config, metricsDescriptions [
Help: "core deploy counter",
}, []string{"hostname"})

Client.Collectors[upNodesName] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: upNodesName,
Help: "number of up nodes",
}, []string{"hostname", "podname"})

Client.Collectors[downNodesName] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: downNodesName,
Help: "number of down nodes",
}, []string{"hostname", "podname"})

once.Do(func() {
prometheus.MustRegister(maps.Values(Client.Collectors)...)
})
Expand Down

0 comments on commit ce8d1bc

Please sign in to comment.