diff --git a/data/txHandler.go b/data/txHandler.go index 51a4c48c0b..8a3555ad51 100644 --- a/data/txHandler.go +++ b/data/txHandler.go @@ -258,7 +258,7 @@ func (handler *TxHandler) backlogGaugeThread() { for { select { case <-ticker.C: - transactionMessagesBacklogSizeGauge.Set(float64(len(handler.backlogQueue))) + transactionMessagesBacklogSizeGauge.Set(uint64(len(handler.backlogQueue))) case <-handler.ctx.Done(): return } diff --git a/ledger/metrics.go b/ledger/metrics.go index 11c5f53ecd..4700dfcd55 100644 --- a/ledger/metrics.go +++ b/ledger/metrics.go @@ -56,10 +56,10 @@ func (mt *metricsTracker) close() { func (mt *metricsTracker) newBlock(blk bookkeeping.Block, delta ledgercore.StateDelta) { rnd := blk.Round() - mt.ledgerRound.Set(float64(rnd)) - mt.ledgerTransactionsTotal.Add(float64(len(blk.Payset)), map[string]string{}) + mt.ledgerRound.Set(uint64(rnd)) + mt.ledgerTransactionsTotal.AddUint64(uint64(len(blk.Payset)), nil) // TODO rewards: need to provide meaningful metric here. - mt.ledgerRewardClaimsTotal.Add(float64(1), map[string]string{}) + mt.ledgerRewardClaimsTotal.Inc(nil) } func (mt *metricsTracker) committedUpTo(committedRnd basics.Round) (retRound, lookback basics.Round) { diff --git a/logging/usage.go b/logging/usage.go index d5f73e155d..2a668ceb72 100644 --- a/logging/usage.go +++ b/logging/usage.go @@ -54,7 +54,7 @@ func UsageLogThread(ctx context.Context, log Logger, period time.Duration, wg *s Utime, Stime, _ = util.GetCurrentProcessTimes() runtime.ReadMemStats(&mst) - ramUsageGauge.Set(float64(mst.HeapInuse)) + ramUsageGauge.Set(uint64(mst.HeapInuse)) if hasPrev { userNanos := Utime - prevUtime diff --git a/network/wsNetwork.go b/network/wsNetwork.go index 49de936cd8..b617cf63b1 100644 --- a/network/wsNetwork.go +++ b/network/wsNetwork.go @@ -1182,8 +1182,8 @@ func (wn *WebsocketNetwork) ServeHTTP(response http.ResponseWriter, request *htt wn.maybeSendMessagesOfInterest(peer, nil) - peers.Set(float64(wn.NumPeers())) - incomingPeers.Set(float64(wn.numIncomingPeers())) + peers.Set(uint64(wn.NumPeers())) + incomingPeers.Set(uint64(wn.numIncomingPeers())) } func (wn *WebsocketNetwork) maybeSendMessagesOfInterest(peer *wsPeer, messagesOfInterestEnc []byte) { @@ -2214,8 +2214,8 @@ func (wn *WebsocketNetwork) tryConnect(addr, gossipAddr string) { wn.maybeSendMessagesOfInterest(peer, nil) - peers.Set(float64(wn.NumPeers())) - outgoingPeers.Set(float64(wn.numOutgoingPeers())) + peers.Set(uint64(wn.NumPeers())) + outgoingPeers.Set(uint64(wn.numOutgoingPeers())) if wn.prioScheme != nil { challenge := response.Header.Get(PriorityChallengeHeader) @@ -2332,9 +2332,9 @@ func (wn *WebsocketNetwork) removePeer(peer *wsPeer, reason disconnectReason) { PPCount: atomic.LoadUint64(&peer.ppMessageCount), }) - peers.Set(float64(wn.NumPeers())) - incomingPeers.Set(float64(wn.numIncomingPeers())) - outgoingPeers.Set(float64(wn.numOutgoingPeers())) + peers.Set(uint64(wn.NumPeers())) + incomingPeers.Set(uint64(wn.numIncomingPeers())) + outgoingPeers.Set(uint64(wn.numOutgoingPeers())) wn.peersLock.Lock() defer wn.peersLock.Unlock() @@ -2399,8 +2399,8 @@ func (wn *WebsocketNetwork) countPeersSetGauges() { numIn++ } } - networkIncomingConnections.Set(float64(numIn)) - networkOutgoingConnections.Set(float64(numOut)) + networkIncomingConnections.Set(uint64(numIn)) + networkOutgoingConnections.Set(uint64(numOut)) } func justHost(hostPort string) string { diff --git a/node/node.go b/node/node.go index 92ea334511..45e1e20ab1 100644 --- a/node/node.go +++ b/node/node.go @@ -1018,7 +1018,7 @@ func (node *AlgorandFullNode) txPoolGaugeThread(done <-chan struct{}) { for true { select { case <-ticker.C: - txPoolGauge.Set(float64(node.transactionPool.PendingCount())) + txPoolGauge.Set(uint64(node.transactionPool.PendingCount())) case <-done: return } diff --git a/util/metrics/counter.go b/util/metrics/counter.go index 9ff647da19..bb8355cacc 100644 --- a/util/metrics/counter.go +++ b/util/metrics/counter.go @@ -66,13 +66,12 @@ func (counter *Counter) Inc(labels map[string]string) { if len(labels) == 0 { counter.fastAddUint64(1) } else { - counter.Add(1.0, labels) + counter.addLabels(1.0, labels) } } -// Add increases counter by x -// For adding an integer, see AddUint64(x) -func (counter *Counter) Add(x float64, labels map[string]string) { +// addLabels increases counter by x +func (counter *Counter) addLabels(x uint64, labels map[string]string) { counter.Lock() defer counter.Unlock() @@ -95,13 +94,12 @@ func (counter *Counter) Add(x float64, labels map[string]string) { } // AddUint64 increases counter by x -// If labels is nil this is much faster than Add() -// Calls through to Add() if labels is not nil. +// If labels is nil this is much faster than if labels is not nil. func (counter *Counter) AddUint64(x uint64, labels map[string]string) { if len(labels) == 0 { counter.fastAddUint64(x) } else { - counter.Add(float64(x), labels) + counter.addLabels(x, labels) } } @@ -122,7 +120,7 @@ func (counter *Counter) fastAddUint64(x uint64) { // is the first Add. Create a dummy // counterValue for the no-labels value. // Dummy counterValue simplifies display in WriteMetric. - counter.Add(0, nil) + counter.addLabels(0, nil) } } @@ -191,9 +189,9 @@ func (counter *Counter) WriteMetric(buf *strings.Builder, parentLabels string) { buf.WriteString("} ") value := l.counter if len(l.labels) == 0 { - value += float64(atomic.LoadUint64(&counter.intValue)) + value += atomic.LoadUint64(&counter.intValue) } - buf.WriteString(strconv.FormatFloat(value, 'f', -1, 32)) + buf.WriteString(strconv.FormatUint(value, 10)) buf.WriteString("\n") } } @@ -210,12 +208,12 @@ func (counter *Counter) AddMetric(values map[string]float64) { for _, l := range counter.values { sum := l.counter if len(l.labels) == 0 { - sum += float64(atomic.LoadUint64(&counter.intValue)) + sum += atomic.LoadUint64(&counter.intValue) } var suffix string if len(l.formattedLabels) > 0 { suffix = ":" + l.formattedLabels } - values[sanitizeTelemetryName(counter.name+suffix)] = sum + values[sanitizeTelemetryName(counter.name+suffix)] = float64(sum) } } diff --git a/util/metrics/counterCommon.go b/util/metrics/counterCommon.go index b8122496f1..2a810ace69 100644 --- a/util/metrics/counterCommon.go +++ b/util/metrics/counterCommon.go @@ -35,7 +35,7 @@ type Counter struct { } type counterValues struct { - counter float64 + counter uint64 labels map[string]string formattedLabels string } diff --git a/util/metrics/counter_test.go b/util/metrics/counter_test.go index 119d5d9138..fe7d553e4c 100644 --- a/util/metrics/counter_test.go +++ b/util/metrics/counter_test.go @@ -144,8 +144,8 @@ func TestMetricCounterMixed(t *testing.T) { counter := MakeCounter(MetricName{Name: "metric_test_name1", Description: "this is the metric test for counter object"}) - counter.Add(5.25, nil) - counter.Add(8.25, map[string]string{}) + counter.AddUint64(5, nil) + counter.AddUint64(8, map[string]string{}) for i := 0; i < 20; i++ { counter.Inc(nil) // wait half-a cycle @@ -169,7 +169,7 @@ func TestMetricCounterMixed(t *testing.T) { for k, v := range test.metrics { // we have increased each one of the labels exactly 4 times. See that the counter was counting correctly. // ( counters starts at zero ) - require.Equal(t, "35.5", v, fmt.Sprintf("The metric '%s' reached value '%s'", k, v)) + require.Equal(t, "35", v, fmt.Sprintf("The metric '%s' reached value '%s'", k, v)) } } @@ -188,13 +188,13 @@ testname{host="myhost"} 0 ` require.Equal(t, expected, sbOut.String()) - c.Add(2.3, nil) + c.AddUint64(2, nil) // ensure non-zero counters are logged sbOut = strings.Builder{} c.WriteMetric(&sbOut, `host="myhost"`) expected = `# HELP testname testhelp # TYPE testname counter -testname{host="myhost"} 2.3 +testname{host="myhost"} 2 ` require.Equal(t, expected, sbOut.String()) } diff --git a/util/metrics/gauge.go b/util/metrics/gauge.go index a5c10e02a9..ce203d47c0 100644 --- a/util/metrics/gauge.go +++ b/util/metrics/gauge.go @@ -19,16 +19,14 @@ package metrics import ( "strconv" "strings" - - "github.com/algorand/go-deadlock" + "sync/atomic" ) // Gauge represent a single gauge variable. type Gauge struct { - deadlock.Mutex + value uint64 name string description string - value float64 } // MakeGauge create a new gauge with the provided name and description. @@ -60,24 +58,17 @@ func (gauge *Gauge) Deregister(reg *Registry) { } // Add increases gauge by x -func (gauge *Gauge) Add(x float64) { - gauge.Lock() - defer gauge.Unlock() - gauge.value += x +func (gauge *Gauge) Add(x uint64) { + atomic.AddUint64(&gauge.value, x) } // Set sets gauge to x -func (gauge *Gauge) Set(x float64) { - gauge.Lock() - defer gauge.Unlock() - gauge.value = x +func (gauge *Gauge) Set(x uint64) { + atomic.StoreUint64(&gauge.value, x) } // WriteMetric writes the metric into the output stream func (gauge *Gauge) WriteMetric(buf *strings.Builder, parentLabels string) { - gauge.Lock() - defer gauge.Unlock() - buf.WriteString("# HELP ") buf.WriteString(gauge.name) buf.WriteString(" ") @@ -91,14 +82,14 @@ func (gauge *Gauge) WriteMetric(buf *strings.Builder, parentLabels string) { buf.WriteString(parentLabels) } buf.WriteString("} ") - buf.WriteString(strconv.FormatFloat(gauge.value, 'f', -1, 32)) + value := atomic.LoadUint64(&gauge.value) + buf.WriteString(strconv.FormatUint(value, 10)) buf.WriteString("\n") } // AddMetric adds the metric into the map func (gauge *Gauge) AddMetric(values map[string]float64) { - gauge.Lock() - defer gauge.Unlock() + value := atomic.LoadUint64(&gauge.value) - values[sanitizeTelemetryName(gauge.name)] = gauge.value + values[sanitizeTelemetryName(gauge.name)] = float64(value) } diff --git a/util/metrics/gauge_test.go b/util/metrics/gauge_test.go index efacf961e1..afa0bbd593 100644 --- a/util/metrics/gauge_test.go +++ b/util/metrics/gauge_test.go @@ -52,8 +52,8 @@ func TestMetricGauge(t *testing.T) { gauges[i] = MakeGauge(MetricName{Name: fmt.Sprintf("gauge_%d", i), Description: "this is the metric test for gauge object"}) } for i := 0; i < 9; i++ { - gauges[i%3].Set(float64(i * 100)) - gauges[i%3].Add(float64(i)) + gauges[i%3].Set(uint64(i * 100)) + gauges[i%3].Add(uint64(i)) // wait half-a cycle time.Sleep(test.sampleRate / 2) } diff --git a/util/metrics/registry_test.go b/util/metrics/registry_test.go index 8452cad391..17328f1cba 100644 --- a/util/metrics/registry_test.go +++ b/util/metrics/registry_test.go @@ -29,17 +29,17 @@ func TestWriteAdd(t *testing.T) { // Test AddMetrics and WriteMetrics with a counter counter := MakeCounter(MetricName{Name: "gauge-name", Description: "gauge description"}) - counter.Add(12.34, nil) + counter.AddUint64(12, nil) labelCounter := MakeCounter(MetricName{Name: "label-counter", Description: "counter with labels"}) - labelCounter.Add(5, map[string]string{"label": "a label value"}) + labelCounter.AddUint64(5, map[string]string{"label": "a label value"}) results := make(map[string]float64) DefaultRegistry().AddMetrics(results) require.Equal(t, 2, len(results), "results", results) require.Contains(t, results, "gauge-name") - require.InDelta(t, 12.34, results["gauge-name"], 0.01) + require.InDelta(t, 12, results["gauge-name"], 0.01) require.Contains(t, results, "label-counter_label__a_label_value_") require.InDelta(t, 5, results["label-counter_label__a_label_value_"], 0.01) @@ -50,7 +50,7 @@ func TestWriteAdd(t *testing.T) { DefaultRegistry().AddMetrics(results) require.Contains(t, results, "gauge-name") - require.InDelta(t, 12.34, results["gauge-name"], 0.01) + require.InDelta(t, 12, results["gauge-name"], 0.01) // not included in string builder bufAfter := strings.Builder{}