Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: use uint64 for Counter and Gauge types #4911

Merged
merged 4 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data/txHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions ledger/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion logging/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions network/wsNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 10 additions & 12 deletions util/metrics/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that this was changed and nothing was lost because everything was already using AddUint64

counter.Lock()
defer counter.Unlock()

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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")
}
}
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion util/metrics/counterCommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Counter struct {
}

type counterValues struct {
counter float64
counter uint64
labels map[string]string
formattedLabels string
}
10 changes: 5 additions & 5 deletions util/metrics/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}

Expand All @@ -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())
}
Expand Down
29 changes: 10 additions & 19 deletions util/metrics/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(" ")
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions util/metrics/gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions util/metrics/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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{}
Expand Down