From 56b1c299371a80b7ce2574a82a2e18b6a5591a50 Mon Sep 17 00:00:00 2001 From: Takeshi NAMAO Date: Thu, 4 Jan 2024 15:17:43 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AF=E3=83=A9=E3=82=B9=E3=82=BF=E3=83=BC?= =?UTF-8?q?=E3=83=A1=E3=83=88=E3=83=AA=E3=82=AF=E3=82=B9=E3=81=AF=E8=AA=8D?= =?UTF-8?q?=E8=AD=98=E3=81=97=E3=81=A6=E3=81=84=E3=82=8B=E3=83=8E=E3=83=BC?= =?UTF-8?q?=E3=83=89=E3=82=92=E5=85=A8=E3=81=A6=E8=BF=94=E3=81=97=E3=80=81?= =?UTF-8?q?gauge=20=E3=81=AE=E5=80=A4=E3=81=A7=E6=8E=A5=E7=B6=9A=E7=8A=B6?= =?UTF-8?q?=E6=85=8B=E3=82=92=E8=A1=A8=E3=81=99=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- collector/cluster_node.go | 14 ++++++++++---- collector/collector.go | 17 ++++++++++++++++- collector/sora_api.go | 7 ++++--- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/collector/cluster_node.go b/collector/cluster_node.go index 116eb3b..b1f9a3f 100644 --- a/collector/cluster_node.go +++ b/collector/cluster_node.go @@ -1,6 +1,8 @@ package collector -import "github.com/prometheus/client_golang/prometheus" +import ( + "github.com/prometheus/client_golang/prometheus" +) var ( soraClusterMetrics = SoraClusterMetrics{ @@ -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) diff --git a/collector/collector.go b/collector/collector.go index 2b65e09..1a99b56 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -1,6 +1,7 @@ package collector import ( + "bytes" "context" "crypto/tls" "encoding/json" @@ -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, @@ -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) diff --git a/collector/sora_api.go b/collector/sora_api.go index 8011fe4..74121dd 100644 --- a/collector/sora_api.go +++ b/collector/sora_api.go @@ -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 {