Skip to content

Commit 8b6cf12

Browse files
authored
metrics: refactor metrics (#28035)
This change includes a lot of things, listed below. ### Split up interfaces, write vs read The interfaces have been split up into one write-interface and one read-interface, with `Snapshot` being the gateway from write to read. This simplifies the semantics _a lot_. Example of splitting up an interface into one readonly 'snapshot' part, and one updatable writeonly part: ```golang type MeterSnapshot interface { Count() int64 Rate1() float64 Rate5() float64 Rate15() float64 RateMean() float64 } // Meters count events to produce exponentially-weighted moving average rates // at one-, five-, and fifteen-minutes and a mean rate. type Meter interface { Mark(int64) Snapshot() MeterSnapshot Stop() } ``` ### A note about concurrency This PR makes the concurrency model clearer. We have actual meters and snapshot of meters. The `meter` is the thing which can be accessed from the registry, and updates can be made to it. - For all `meters`, (`Gauge`, `Timer` etc), it is assumed that they are accessed by different threads, making updates. Therefore, all `meters` update-methods (`Inc`, `Add`, `Update`, `Clear` etc) need to be concurrency-safe. - All `meters` have a `Snapshot()` method. This method is _usually_ called from one thread, a backend-exporter. But it's fully possible to have several exporters simultaneously: therefore this method should also be concurrency-safe. TLDR: `meter`s are accessible via registry, all their methods must be concurrency-safe. For all `Snapshot`s, it is assumed that an individual exporter-thread has obtained a `meter` from the registry, and called the `Snapshot` method to obtain a readonly snapshot. This snapshot is _not_ guaranteed to be concurrency-safe. There's no need for a snapshot to be concurrency-safe, since exporters should not share snapshots. Note, though: that by happenstance a lot of the snapshots _are_ concurrency-safe, being unmutable minimal representations of a value. Only the more complex ones are _not_ threadsafe, those that lazily calculate things like `Variance()`, `Mean()`. Example of how a background exporter typically works, obtaining the snapshot and sequentially accessing the non-threadsafe methods in it: ```golang ms := metric.Snapshot() ... fields := map[string]interface{}{ "count": ms.Count(), "max": ms.Max(), "mean": ms.Mean(), "min": ms.Min(), "stddev": ms.StdDev(), "variance": ms.Variance(), ``` TLDR: `snapshots` are not guaranteed to be concurrency-safe (but often are). ### Sample changes I also changed the `Sample` type: previously, it iterated the samples fully every time `Mean()`,`Sum()`, `Min()` or `Max()` was invoked. Since we now have readonly base data, we can just iterate it once, in the constructor, and set all four values at once. The same thing has been done for runtimehistogram. ### ResettingTimer API Back when ResettingTImer was implemented, as part of #15910, Anton implemented a `Percentiles` on the new type. However, the method did not conform to the other existing types which also had a `Percentiles`. 1. The existing ones, on input, took `0.5` to mean `50%`. Anton used `50` to mean `50%`. 2. The existing ones returned `float64` outputs, thus interpolating between values. A value-set of `0, 10`, at `50%` would return `5`, whereas Anton's would return either `0` or `10`. This PR removes the 'new' version, and uses only the 'legacy' percentiles, also for the ResettingTimer type. The resetting timer snapshot was also defined so that it would expose the internal values. This has been removed, and getters for `Max, Min, Mean` have been added instead. ### Unexport types A lot of types were exported, but do not need to be. This PR unexports quite a lot of them.
1 parent 8d38b1f commit 8b6cf12

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1035
-2032
lines changed

core/state/statedb.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,12 +1061,10 @@ func (s *StateDB) deleteStorage(addr common.Address, addrHash common.Hash, root
10611061
slotDeletionSkip.Inc(1)
10621062
}
10631063
n := int64(len(slots))
1064-
if n > slotDeletionMaxCount.Value() {
1065-
slotDeletionMaxCount.Update(n)
1066-
}
1067-
if int64(size) > slotDeletionMaxSize.Value() {
1068-
slotDeletionMaxSize.Update(int64(size))
1069-
}
1064+
1065+
slotDeletionMaxCount.UpdateIfGt(int64(len(slots)))
1066+
slotDeletionMaxSize.UpdateIfGt(int64(size))
1067+
10701068
slotDeletionTimer.UpdateSince(start)
10711069
slotDeletionCount.Mark(n)
10721070
slotDeletionSize.Mark(int64(size))

metrics/counter.go

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import (
44
"sync/atomic"
55
)
66

7+
type CounterSnapshot interface {
8+
Count() int64
9+
}
10+
711
// Counters hold an int64 value that can be incremented and decremented.
812
type Counter interface {
913
Clear()
10-
Count() int64
1114
Dec(int64)
1215
Inc(int64)
13-
Snapshot() Counter
16+
Snapshot() CounterSnapshot
1417
}
1518

1619
// GetOrRegisterCounter returns an existing Counter or constructs and registers
@@ -38,13 +41,13 @@ func NewCounter() Counter {
3841
if !Enabled {
3942
return NilCounter{}
4043
}
41-
return &StandardCounter{}
44+
return new(StandardCounter)
4245
}
4346

4447
// NewCounterForced constructs a new StandardCounter and returns it no matter if
4548
// the global switch is enabled or not.
4649
func NewCounterForced() Counter {
47-
return &StandardCounter{}
50+
return new(StandardCounter)
4851
}
4952

5053
// NewRegisteredCounter constructs and registers a new StandardCounter.
@@ -70,75 +73,40 @@ func NewRegisteredCounterForced(name string, r Registry) Counter {
7073
return c
7174
}
7275

73-
// CounterSnapshot is a read-only copy of another Counter.
74-
type CounterSnapshot int64
75-
76-
// Clear panics.
77-
func (CounterSnapshot) Clear() {
78-
panic("Clear called on a CounterSnapshot")
79-
}
76+
// counterSnapshot is a read-only copy of another Counter.
77+
type counterSnapshot int64
8078

8179
// Count returns the count at the time the snapshot was taken.
82-
func (c CounterSnapshot) Count() int64 { return int64(c) }
83-
84-
// Dec panics.
85-
func (CounterSnapshot) Dec(int64) {
86-
panic("Dec called on a CounterSnapshot")
87-
}
88-
89-
// Inc panics.
90-
func (CounterSnapshot) Inc(int64) {
91-
panic("Inc called on a CounterSnapshot")
92-
}
93-
94-
// Snapshot returns the snapshot.
95-
func (c CounterSnapshot) Snapshot() Counter { return c }
80+
func (c counterSnapshot) Count() int64 { return int64(c) }
9681

9782
// NilCounter is a no-op Counter.
9883
type NilCounter struct{}
9984

100-
// Clear is a no-op.
101-
func (NilCounter) Clear() {}
102-
103-
// Count is a no-op.
104-
func (NilCounter) Count() int64 { return 0 }
105-
106-
// Dec is a no-op.
107-
func (NilCounter) Dec(i int64) {}
108-
109-
// Inc is a no-op.
110-
func (NilCounter) Inc(i int64) {}
111-
112-
// Snapshot is a no-op.
113-
func (NilCounter) Snapshot() Counter { return NilCounter{} }
85+
func (NilCounter) Clear() {}
86+
func (NilCounter) Dec(i int64) {}
87+
func (NilCounter) Inc(i int64) {}
88+
func (NilCounter) Snapshot() CounterSnapshot { return (*emptySnapshot)(nil) }
11489

11590
// StandardCounter is the standard implementation of a Counter and uses the
11691
// sync/atomic package to manage a single int64 value.
117-
type StandardCounter struct {
118-
count atomic.Int64
119-
}
92+
type StandardCounter atomic.Int64
12093

12194
// Clear sets the counter to zero.
12295
func (c *StandardCounter) Clear() {
123-
c.count.Store(0)
124-
}
125-
126-
// Count returns the current count.
127-
func (c *StandardCounter) Count() int64 {
128-
return c.count.Load()
96+
(*atomic.Int64)(c).Store(0)
12997
}
13098

13199
// Dec decrements the counter by the given amount.
132100
func (c *StandardCounter) Dec(i int64) {
133-
c.count.Add(-i)
101+
(*atomic.Int64)(c).Add(-i)
134102
}
135103

136104
// Inc increments the counter by the given amount.
137105
func (c *StandardCounter) Inc(i int64) {
138-
c.count.Add(i)
106+
(*atomic.Int64)(c).Add(i)
139107
}
140108

141109
// Snapshot returns a read-only copy of the counter.
142-
func (c *StandardCounter) Snapshot() Counter {
143-
return CounterSnapshot(c.Count())
110+
func (c *StandardCounter) Snapshot() CounterSnapshot {
111+
return counterSnapshot((*atomic.Int64)(c).Load())
144112
}

metrics/counter_float64.go

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import (
55
"sync/atomic"
66
)
77

8+
type CounterFloat64Snapshot interface {
9+
Count() float64
10+
}
11+
812
// CounterFloat64 holds a float64 value that can be incremented and decremented.
913
type CounterFloat64 interface {
1014
Clear()
11-
Count() float64
1215
Dec(float64)
1316
Inc(float64)
14-
Snapshot() CounterFloat64
17+
Snapshot() CounterFloat64Snapshot
1518
}
1619

1720
// GetOrRegisterCounterFloat64 returns an existing CounterFloat64 or constructs and registers
@@ -71,47 +74,19 @@ func NewRegisteredCounterFloat64Forced(name string, r Registry) CounterFloat64 {
7174
return c
7275
}
7376

74-
// CounterFloat64Snapshot is a read-only copy of another CounterFloat64.
75-
type CounterFloat64Snapshot float64
76-
77-
// Clear panics.
78-
func (CounterFloat64Snapshot) Clear() {
79-
panic("Clear called on a CounterFloat64Snapshot")
80-
}
77+
// counterFloat64Snapshot is a read-only copy of another CounterFloat64.
78+
type counterFloat64Snapshot float64
8179

8280
// Count returns the value at the time the snapshot was taken.
83-
func (c CounterFloat64Snapshot) Count() float64 { return float64(c) }
84-
85-
// Dec panics.
86-
func (CounterFloat64Snapshot) Dec(float64) {
87-
panic("Dec called on a CounterFloat64Snapshot")
88-
}
81+
func (c counterFloat64Snapshot) Count() float64 { return float64(c) }
8982

90-
// Inc panics.
91-
func (CounterFloat64Snapshot) Inc(float64) {
92-
panic("Inc called on a CounterFloat64Snapshot")
93-
}
94-
95-
// Snapshot returns the snapshot.
96-
func (c CounterFloat64Snapshot) Snapshot() CounterFloat64 { return c }
97-
98-
// NilCounterFloat64 is a no-op CounterFloat64.
9983
type NilCounterFloat64 struct{}
10084

101-
// Clear is a no-op.
102-
func (NilCounterFloat64) Clear() {}
103-
104-
// Count is a no-op.
105-
func (NilCounterFloat64) Count() float64 { return 0.0 }
106-
107-
// Dec is a no-op.
108-
func (NilCounterFloat64) Dec(i float64) {}
109-
110-
// Inc is a no-op.
111-
func (NilCounterFloat64) Inc(i float64) {}
112-
113-
// Snapshot is a no-op.
114-
func (NilCounterFloat64) Snapshot() CounterFloat64 { return NilCounterFloat64{} }
85+
func (NilCounterFloat64) Clear() {}
86+
func (NilCounterFloat64) Count() float64 { return 0.0 }
87+
func (NilCounterFloat64) Dec(i float64) {}
88+
func (NilCounterFloat64) Inc(i float64) {}
89+
func (NilCounterFloat64) Snapshot() CounterFloat64Snapshot { return NilCounterFloat64{} }
11590

11691
// StandardCounterFloat64 is the standard implementation of a CounterFloat64 and uses the
11792
// atomic to manage a single float64 value.
@@ -124,11 +99,6 @@ func (c *StandardCounterFloat64) Clear() {
12499
c.floatBits.Store(0)
125100
}
126101

127-
// Count returns the current value.
128-
func (c *StandardCounterFloat64) Count() float64 {
129-
return math.Float64frombits(c.floatBits.Load())
130-
}
131-
132102
// Dec decrements the counter by the given amount.
133103
func (c *StandardCounterFloat64) Dec(v float64) {
134104
atomicAddFloat(&c.floatBits, -v)
@@ -140,8 +110,9 @@ func (c *StandardCounterFloat64) Inc(v float64) {
140110
}
141111

142112
// Snapshot returns a read-only copy of the counter.
143-
func (c *StandardCounterFloat64) Snapshot() CounterFloat64 {
144-
return CounterFloat64Snapshot(c.Count())
113+
func (c *StandardCounterFloat64) Snapshot() CounterFloat64Snapshot {
114+
v := math.Float64frombits(c.floatBits.Load())
115+
return counterFloat64Snapshot(v)
145116
}
146117

147118
func atomicAddFloat(fbits *atomic.Uint64, v float64) {

metrics/counter_float_64_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func BenchmarkCounterFloat64Parallel(b *testing.B) {
2727
}()
2828
}
2929
wg.Wait()
30-
if have, want := c.Count(), 10.0*float64(b.N); have != want {
30+
if have, want := c.Snapshot().Count(), 10.0*float64(b.N); have != want {
3131
b.Fatalf("have %f want %f", have, want)
3232
}
3333
}
@@ -36,39 +36,39 @@ func TestCounterFloat64Clear(t *testing.T) {
3636
c := NewCounterFloat64()
3737
c.Inc(1.0)
3838
c.Clear()
39-
if count := c.Count(); count != 0 {
39+
if count := c.Snapshot().Count(); count != 0 {
4040
t.Errorf("c.Count(): 0 != %v\n", count)
4141
}
4242
}
4343

4444
func TestCounterFloat64Dec1(t *testing.T) {
4545
c := NewCounterFloat64()
4646
c.Dec(1.0)
47-
if count := c.Count(); count != -1.0 {
47+
if count := c.Snapshot().Count(); count != -1.0 {
4848
t.Errorf("c.Count(): -1.0 != %v\n", count)
4949
}
5050
}
5151

5252
func TestCounterFloat64Dec2(t *testing.T) {
5353
c := NewCounterFloat64()
5454
c.Dec(2.0)
55-
if count := c.Count(); count != -2.0 {
55+
if count := c.Snapshot().Count(); count != -2.0 {
5656
t.Errorf("c.Count(): -2.0 != %v\n", count)
5757
}
5858
}
5959

6060
func TestCounterFloat64Inc1(t *testing.T) {
6161
c := NewCounterFloat64()
6262
c.Inc(1.0)
63-
if count := c.Count(); count != 1.0 {
63+
if count := c.Snapshot().Count(); count != 1.0 {
6464
t.Errorf("c.Count(): 1.0 != %v\n", count)
6565
}
6666
}
6767

6868
func TestCounterFloat64Inc2(t *testing.T) {
6969
c := NewCounterFloat64()
7070
c.Inc(2.0)
71-
if count := c.Count(); count != 2.0 {
71+
if count := c.Snapshot().Count(); count != 2.0 {
7272
t.Errorf("c.Count(): 2.0 != %v\n", count)
7373
}
7474
}
@@ -85,15 +85,15 @@ func TestCounterFloat64Snapshot(t *testing.T) {
8585

8686
func TestCounterFloat64Zero(t *testing.T) {
8787
c := NewCounterFloat64()
88-
if count := c.Count(); count != 0 {
88+
if count := c.Snapshot().Count(); count != 0 {
8989
t.Errorf("c.Count(): 0 != %v\n", count)
9090
}
9191
}
9292

9393
func TestGetOrRegisterCounterFloat64(t *testing.T) {
9494
r := NewRegistry()
9595
NewRegisteredCounterFloat64("foo", r).Inc(47.0)
96-
if c := GetOrRegisterCounterFloat64("foo", r); c.Count() != 47.0 {
96+
if c := GetOrRegisterCounterFloat64("foo", r).Snapshot(); c.Count() != 47.0 {
9797
t.Fatal(c)
9898
}
9999
}

metrics/counter_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,39 @@ func TestCounterClear(t *testing.T) {
1414
c := NewCounter()
1515
c.Inc(1)
1616
c.Clear()
17-
if count := c.Count(); count != 0 {
17+
if count := c.Snapshot().Count(); count != 0 {
1818
t.Errorf("c.Count(): 0 != %v\n", count)
1919
}
2020
}
2121

2222
func TestCounterDec1(t *testing.T) {
2323
c := NewCounter()
2424
c.Dec(1)
25-
if count := c.Count(); count != -1 {
25+
if count := c.Snapshot().Count(); count != -1 {
2626
t.Errorf("c.Count(): -1 != %v\n", count)
2727
}
2828
}
2929

3030
func TestCounterDec2(t *testing.T) {
3131
c := NewCounter()
3232
c.Dec(2)
33-
if count := c.Count(); count != -2 {
33+
if count := c.Snapshot().Count(); count != -2 {
3434
t.Errorf("c.Count(): -2 != %v\n", count)
3535
}
3636
}
3737

3838
func TestCounterInc1(t *testing.T) {
3939
c := NewCounter()
4040
c.Inc(1)
41-
if count := c.Count(); count != 1 {
41+
if count := c.Snapshot().Count(); count != 1 {
4242
t.Errorf("c.Count(): 1 != %v\n", count)
4343
}
4444
}
4545

4646
func TestCounterInc2(t *testing.T) {
4747
c := NewCounter()
4848
c.Inc(2)
49-
if count := c.Count(); count != 2 {
49+
if count := c.Snapshot().Count(); count != 2 {
5050
t.Errorf("c.Count(): 2 != %v\n", count)
5151
}
5252
}
@@ -63,15 +63,15 @@ func TestCounterSnapshot(t *testing.T) {
6363

6464
func TestCounterZero(t *testing.T) {
6565
c := NewCounter()
66-
if count := c.Count(); count != 0 {
66+
if count := c.Snapshot().Count(); count != 0 {
6767
t.Errorf("c.Count(): 0 != %v\n", count)
6868
}
6969
}
7070

7171
func TestGetOrRegisterCounter(t *testing.T) {
7272
r := NewRegistry()
7373
NewRegisteredCounter("foo", r).Inc(47)
74-
if c := GetOrRegisterCounter("foo", r); c.Count() != 47 {
74+
if c := GetOrRegisterCounter("foo", r).Snapshot(); c.Count() != 47 {
7575
t.Fatal(c)
7676
}
7777
}

metrics/doc.go

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)