Skip to content

Commit

Permalink
クラスターメトリクスは認識しているノードを全て返し、gauge の値で接続状態を表すように変更する
Browse files Browse the repository at this point in the history
  • Loading branch information
tnamao committed Jan 4, 2024
1 parent ba8c30e commit 56b1c29
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
14 changes: 10 additions & 4 deletions collector/cluster_node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package collector

import "github.com/prometheus/client_golang/prometheus"
import (
"github.com/prometheus/client_golang/prometheus"
)

var (
soraClusterMetrics = SoraClusterMetrics{
Expand All @@ -27,10 +29,14 @@ func (m *SoraClusterMetrics) Describe(ch chan<- *prometheus.Desc) {

func (m *SoraClusterMetrics) Collect(ch chan<- prometheus.Metric, nodeList []soraClusterNode, report soraClusterReport) {
for _, node := range nodeList {
if node.ClusterNodeName != nil {
ch <- newGauge(m.clusterNode, 1, *node.ClusterNodeName, *node.Mode)
value := 0.0
if node.Connected {
value = 1.0
}
if node.ClusterNodeName != "" {
ch <- newGauge(m.clusterNode, value, node.ClusterNodeName, node.Mode)
} else {
ch <- newGauge(m.clusterNode, 1, *node.NodeName, *node.Mode)
ch <- newGauge(m.clusterNode, value, node.NodeName, node.Mode)
}
}
ch <- newGauge(m.raftState, 1.0, report.RaftState)
Expand Down
17 changes: 16 additions & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package collector

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
Expand Down Expand Up @@ -50,6 +51,10 @@ type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}

type SoraListClusterNodesRequest struct {
IncludeAllKnownNodes bool `json:"include_all_known_nodes"`
}

func NewCollector(options *CollectorOptions) *Collector {
return &Collector{
URI: options.URI,
Expand Down Expand Up @@ -112,7 +117,17 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {

var nodeList []soraClusterNode
if c.EnableSoraClusterMetrics {
req, err = http.NewRequestWithContext(ctx, http.MethodPost, c.URI, nil)
requestParams := SoraListClusterNodesRequest{
IncludeAllKnownNodes: true,
}
encodedParams, err := json.Marshal(requestParams)
if err != nil {
level.Error(c.logger).Log("msg", "failed to encode Sora ListClusterNodes API request parameters", "err", err)
ch <- newGauge(c.soraUp, 0)
return
}

req, err = http.NewRequestWithContext(ctx, http.MethodPost, c.URI, bytes.NewBuffer(encodedParams))
if err != nil {
level.Error(c.logger).Log("msg", "failed to create request to sora", "err", err)
ch <- newGauge(c.soraUp, 0)
Expand Down
7 changes: 4 additions & 3 deletions collector/sora_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ type soraClusterReport struct {
}

type soraClusterNode struct {
ClusterNodeName *string `json:"cluster_node_name"`
NodeName *string `json:"node_name"`
Mode *string `json:"mode"`
ClusterNodeName string `json:"cluster_node_name"`
NodeName string `json:"node_name"`
Mode string `json:"mode"`
Connected bool `json:"connected"`
}

type soraLicenseInfo struct {
Expand Down

0 comments on commit 56b1c29

Please sign in to comment.