Skip to content

Commit acad4b1

Browse files
committed
telemetry/countertest: generalize Read, ReadStack
This CL removes the requirement that Open be called before a test reads the value of a counter. Fixes: golang/go#65600 Change-Id: Ib8b34fa6f01698e2df45158572f8fde16b9b3834 Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/562180 Run-TryBot: Peter Weinberger <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 213092a commit acad4b1

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

counter/countertest/countertest.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package countertest
1010

1111
import (
12-
"fmt"
1312
"path/filepath"
1413
"sync"
1514

@@ -49,16 +48,10 @@ func Open(telemetryDir string) {
4948

5049
// ReadCounter reads the given counter.
5150
func ReadCounter(c *counter.Counter) (count uint64, _ error) {
52-
if !isOpen() {
53-
return 0, fmt.Errorf("unmet requirement - Open must be called")
54-
}
5551
return ic.Read(c)
5652
}
5753

5854
// ReadStackCounter reads the given StackCounter.
5955
func ReadStackCounter(c *counter.StackCounter) (stackCounts map[string]uint64, _ error) {
60-
if !isOpen() {
61-
return nil, fmt.Errorf("unmet requirement - Open must be called")
62-
}
6356
return ic.ReadStack(c)
6457
}

internal/counter/counter.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ func (c *Counter) refresh() {
293293
// Read reads the given counter.
294294
// This is the implementation of x/telemetry/counter/countertest.ReadCounter.
295295
func Read(c *Counter) (uint64, error) {
296+
if c.file.current.Load() == nil {
297+
return c.state.load().extra(), nil
298+
}
296299
pf, err := readFile(c.file)
297300
if err != nil {
298301
return 0, err

internal/counter/counter_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,17 @@ func TestStack(t *testing.T) {
404404
t.Errorf("line %d differs:\n%s\n%s", i, n0[i], n1[i])
405405
}
406406
}
407+
// check that ReadStack gives the same results
408+
mp, err := ReadStack(c)
409+
if len(mp) != 2 {
410+
t.Errorf("ReadStack returned %d values, expected 2", len(mp))
411+
}
412+
for k, v := range mp {
413+
if v != 1 {
414+
t.Errorf("got %d for %q, expected 1", v, k)
415+
}
416+
}
417+
407418
oldnames := make(map[string]bool)
408419
for _, nm := range names {
409420
oldnames[nm] = true

internal/counter/rotate_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func TestRotateCounters(t *testing.T) {
4444
// the value of c is in its extra field
4545
t.Errorf("got %d, expected 2", state.extra())
4646
}
47+
// Read should give the same answer
48+
if v, err := Read(c); err != nil || v != 2 {
49+
t.Errorf("Read got %d, %v, expected 2, nil", v, err)
50+
}
4751
f.rotate()
4852
c.Inc() // this goes through counter.add() safely
4953
if c.file.current.Load() == nil {
@@ -60,7 +64,10 @@ func TestRotateCounters(t *testing.T) {
6064
// the value of c is in the mapped file
6165
t.Errorf("got %d, expected 3", c.ptr.count.Load())
6266
}
63-
67+
// and Read should give the same result
68+
if v, err := Read(c); err != nil || v != 3 {
69+
t.Errorf("Read gave %d, %v, expected 3, nil", v, err)
70+
}
6471
// move into the future and rotate the file, remapping it
6572
now := getnow()
6673
counterTime = func() time.Time { return now.Add(7 * 24 * time.Hour) }

internal/counter/stackcounter.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,13 @@ func eq(a, b []uintptr) bool {
185185
// This is the implementation of
186186
// golang.org/x/telemetry/counter/countertest.ReadStackCounter.
187187
func ReadStack(c *StackCounter) (map[string]uint64, error) {
188-
pf, err := readFile(c.file)
189-
if err != nil {
190-
return nil, err
191-
}
192188
ret := map[string]uint64{}
193-
prefix := c.name + "\n"
194-
195-
for k, v := range pf.Count {
196-
if strings.HasPrefix(k, prefix) {
197-
ret[k] = v
189+
for _, ctr := range c.Counters() {
190+
v, err := Read(ctr)
191+
if err != nil {
192+
return nil, err
198193
}
194+
ret[DecodeStack(ctr.Name())] = v
199195
}
200196
return ret, nil
201197
}

0 commit comments

Comments
 (0)