Browse Source

chore(go.d.plugin): fix duplicate boolToInt (#18987)

Ilya Mashchenko 4 months ago
parent
commit
1f178b1915

+ 2 - 8
src/go/plugin/go.d/agent/module/job.go

@@ -15,6 +15,7 @@ import (
 
 	"github.com/netdata/netdata/go/plugins/logger"
 	"github.com/netdata/netdata/go/plugins/pkg/netdataapi"
+	"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrix"
 )
 
 var obsoleteLock = &sync.Mutex{}
@@ -459,7 +460,7 @@ func (j *Job) processMetrics(metrics map[string]int64, startTime time.Time, sinc
 
 	j.updateChart(
 		j.collectStatusChart,
-		map[string]int64{"success": boolToInt(updated > 0), "failed": boolToInt(updated == 0)},
+		map[string]int64{"success": metrix.Bool(updated > 0), "failed": metrix.Bool(updated == 0)},
 		sinceLastRun,
 	)
 
@@ -668,13 +669,6 @@ func handleZero(v int) int {
 	return v
 }
 
-func boolToInt(b bool) int64 {
-	if b {
-		return 1
-	}
-	return 0
-}
-
 func cleanPluginName(name string) string {
 	r := strings.NewReplacer(" ", "_", ".", "_")
 	return r.Replace(name)

+ 6 - 11
src/go/plugin/go.d/modules/chrony/collect.go

@@ -10,6 +10,8 @@ import (
 	"strconv"
 	"strings"
 	"time"
+
+	"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrix"
 )
 
 const scaleFactor = 1000000000
@@ -58,10 +60,10 @@ func (c *Chrony) collectTracking(mx map[string]int64) error {
 	}
 
 	mx["stratum"] = int64(reply.Stratum)
-	mx["leap_status_normal"] = boolToInt(reply.LeapStatus == leapStatusNormal)
-	mx["leap_status_insert_second"] = boolToInt(reply.LeapStatus == leapStatusInsertSecond)
-	mx["leap_status_delete_second"] = boolToInt(reply.LeapStatus == leapStatusDeleteSecond)
-	mx["leap_status_unsynchronised"] = boolToInt(reply.LeapStatus == leapStatusUnsynchronised)
+	mx["leap_status_normal"] = metrix.Bool(reply.LeapStatus == leapStatusNormal)
+	mx["leap_status_insert_second"] = metrix.Bool(reply.LeapStatus == leapStatusInsertSecond)
+	mx["leap_status_delete_second"] = metrix.Bool(reply.LeapStatus == leapStatusDeleteSecond)
+	mx["leap_status_unsynchronised"] = metrix.Bool(reply.LeapStatus == leapStatusUnsynchronised)
 	mx["root_delay"] = int64(reply.RootDelay * scaleFactor)
 	mx["root_dispersion"] = int64(reply.RootDispersion * scaleFactor)
 	mx["skew"] = int64(reply.SkewPPM * scaleFactor)
@@ -132,13 +134,6 @@ func (c *Chrony) collectServerStats(mx map[string]int64) error {
 	return nil
 }
 
-func boolToInt(v bool) int64 {
-	if v {
-		return 1
-	}
-	return 0
-}
-
 func abs(v int64) int64 {
 	if v < 0 {
 		return -v

+ 0 - 7
src/go/plugin/go.d/modules/consul/collect.go

@@ -85,10 +85,3 @@ func (c *Consul) createRequest(urlPath string) (*http.Request, error) {
 
 	return req, nil
 }
-
-func boolToInt(v bool) int64 {
-	if v {
-		return 1
-	}
-	return 0
-}

+ 10 - 8
src/go/plugin/go.d/modules/consul/collect_autopilot.go

@@ -5,6 +5,8 @@ package consul
 import (
 	"net/http"
 	"time"
+
+	"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrix"
 )
 
 const (
@@ -42,15 +44,15 @@ func (c *Consul) collectAutopilotHealth(mx map[string]int64) error {
 		if srv.ID == c.cfg.Config.NodeID {
 			// SerfStatus: alive, left, failed or none:
 			// https://github.com/hashicorp/consul/blob/c7ef04c5979dbc311ff3c67b7bf3028a93e8b0f1/agent/consul/operator_autopilot_endpoint.go#L124-L133
-			mx["autopilot_server_sefStatus_alive"] = boolToInt(srv.SerfStatus == "alive")
-			mx["autopilot_server_sefStatus_left"] = boolToInt(srv.SerfStatus == "left")
-			mx["autopilot_server_sefStatus_failed"] = boolToInt(srv.SerfStatus == "failed")
-			mx["autopilot_server_sefStatus_none"] = boolToInt(srv.SerfStatus == "none")
+			mx["autopilot_server_sefStatus_alive"] = metrix.Bool(srv.SerfStatus == "alive")
+			mx["autopilot_server_sefStatus_left"] = metrix.Bool(srv.SerfStatus == "left")
+			mx["autopilot_server_sefStatus_failed"] = metrix.Bool(srv.SerfStatus == "failed")
+			mx["autopilot_server_sefStatus_none"] = metrix.Bool(srv.SerfStatus == "none")
 			// https://github.com/hashicorp/raft-autopilot/blob/d936f51c374c3b7902d5e4fdafe9f7d8d199ea53/types.go#L110
-			mx["autopilot_server_healthy_yes"] = boolToInt(srv.Healthy)
-			mx["autopilot_server_healthy_no"] = boolToInt(!srv.Healthy)
-			mx["autopilot_server_voter_yes"] = boolToInt(srv.Voter)
-			mx["autopilot_server_voter_no"] = boolToInt(!srv.Voter)
+			mx["autopilot_server_healthy_yes"] = metrix.Bool(srv.Healthy)
+			mx["autopilot_server_healthy_no"] = metrix.Bool(!srv.Healthy)
+			mx["autopilot_server_voter_yes"] = metrix.Bool(srv.Voter)
+			mx["autopilot_server_voter_no"] = metrix.Bool(!srv.Voter)
 			mx["autopilot_server_stable_time"] = int64(time.Since(srv.StableSince).Seconds())
 			mx["autopilot_server_stable_time"] = int64(time.Since(srv.StableSince).Seconds())
 			if !srv.Leader {

+ 6 - 4
src/go/plugin/go.d/modules/consul/collect_checks.go

@@ -2,6 +2,8 @@
 
 package consul
 
+import "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrix"
+
 const (
 	// https://www.consul.io/api-docs/agent/check#list-checks
 	urlPathAgentChecks = "/v1/agent/checks"
@@ -35,10 +37,10 @@ func (c *Consul) collectChecks(mx map[string]int64) error {
 			c.addHealthCheckCharts(check)
 		}
 
-		mx["health_check_"+id+"_passing_status"] = boolToInt(check.Status == "passing")
-		mx["health_check_"+id+"_warning_status"] = boolToInt(check.Status == "warning")
-		mx["health_check_"+id+"_critical_status"] = boolToInt(check.Status == "critical")
-		mx["health_check_"+id+"_maintenance_status"] = boolToInt(check.Status == "maintenance")
+		mx["health_check_"+id+"_passing_status"] = metrix.Bool(check.Status == "passing")
+		mx["health_check_"+id+"_warning_status"] = metrix.Bool(check.Status == "warning")
+		mx["health_check_"+id+"_critical_status"] = metrix.Bool(check.Status == "critical")
+		mx["health_check_"+id+"_maintenance_status"] = metrix.Bool(check.Status == "maintenance")
 	}
 
 	for id := range c.checks {

+ 3 - 2
src/go/plugin/go.d/modules/consul/collect_metrics.go

@@ -4,6 +4,7 @@ package consul
 
 import (
 	"fmt"
+	"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrix"
 	"math"
 	"strconv"
 	"strings"
@@ -142,8 +143,8 @@ func (c *Consul) collectGaugeBool(mx map[string]int64, mfs prometheus.MetricFami
 	v := mf.Metrics()[0].Gauge().Value()
 
 	if !math.IsNaN(v) {
-		mx[name+"_yes"] = boolToInt(v == 1)
-		mx[name+"_no"] = boolToInt(v == 0)
+		mx[name+"_yes"] = metrix.Bool(v == 1)
+		mx[name+"_no"] = metrix.Bool(v == 0)
 	}
 }
 

+ 2 - 2
src/go/plugin/go.d/modules/consul/collect_net_rtt.go

@@ -4,7 +4,7 @@ import (
 	"math"
 	"time"
 
-	"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrics"
+	"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrix"
 )
 
 const (
@@ -42,7 +42,7 @@ func (c *Consul) collectNetworkRTT(mx map[string]int64) error {
 		return nil
 	}
 
-	sum := metrics.NewSummary()
+	sum := metrix.NewSummary()
 	for _, v := range coords {
 		d := calcDistance(thisNode, v)
 		sum.Observe(d.Seconds())

+ 1 - 1
src/go/plugin/go.d/modules/coredns/metrics.go

@@ -3,7 +3,7 @@
 package coredns
 
 import (
-	mtx "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrics"
+	mtx "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrix"
 )
 
 func newMetrics() *metrics {

+ 7 - 13
src/go/plugin/go.d/modules/elasticsearch/collect.go

@@ -5,6 +5,7 @@ package elasticsearch
 import (
 	"errors"
 	"fmt"
+	"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrix"
 	"math"
 	"slices"
 	"strconv"
@@ -82,9 +83,9 @@ func (es *Elasticsearch) collectClusterHealth(mx map[string]int64, ms *esMetrics
 
 	merge(mx, stm.ToMap(ms.ClusterHealth), "cluster")
 
-	mx["cluster_status_green"] = boolToInt(ms.ClusterHealth.Status == "green")
-	mx["cluster_status_yellow"] = boolToInt(ms.ClusterHealth.Status == "yellow")
-	mx["cluster_status_red"] = boolToInt(ms.ClusterHealth.Status == "red")
+	mx["cluster_status_green"] = metrix.Bool(ms.ClusterHealth.Status == "green")
+	mx["cluster_status_yellow"] = metrix.Bool(ms.ClusterHealth.Status == "yellow")
+	mx["cluster_status_red"] = metrix.Bool(ms.ClusterHealth.Status == "red")
 }
 
 func (es *Elasticsearch) collectClusterStats(mx map[string]int64, ms *esMetrics) {
@@ -114,9 +115,9 @@ func (es *Elasticsearch) collectLocalIndicesStats(mx map[string]int64, ms *esMet
 
 		px := fmt.Sprintf("node_index_%s_stats_", v.Index)
 
-		mx[px+"health_green"] = boolToInt(v.Health == "green")
-		mx[px+"health_yellow"] = boolToInt(v.Health == "yellow")
-		mx[px+"health_red"] = boolToInt(v.Health == "red")
+		mx[px+"health_green"] = metrix.Bool(v.Health == "green")
+		mx[px+"health_yellow"] = metrix.Bool(v.Health == "yellow")
+		mx[px+"health_red"] = metrix.Bool(v.Health == "red")
 		mx[px+"shards_count"] = strToInt(v.Rep)
 		mx[px+"docs_count"] = strToInt(v.DocsCount)
 		mx[px+"store_size_in_bytes"] = convertIndexStoreSizeToBytes(v.StoreSize)
@@ -254,13 +255,6 @@ func strToInt(s string) int64 {
 	return int64(v)
 }
 
-func boolToInt(v bool) int64 {
-	if v {
-		return 1
-	}
-	return 0
-}
-
 func removeSystemIndices(indices []esIndexStats) []esIndexStats {
 	return slices.DeleteFunc(indices, func(stats esIndexStats) bool {
 		return strings.HasPrefix(stats.Index, ".")

+ 2 - 3
src/go/plugin/go.d/modules/k8s_kubelet/collect.go

@@ -5,11 +5,10 @@ package k8s_kubelet
 import (
 	"math"
 
-	mtx "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrics"
+	"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
+	mtx "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrix"
 	"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/prometheus"
 	"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/stm"
-
-	"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
 )
 
 func (k *Kubelet) collect() (map[string]int64, error) {

Some files were not shown because too many files changed in this diff