Debian Codegraph @

debian/go/src/golang.org/x/telemetry/counter/countertest/countertest.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // countertest provides testing utilities for counters.
6 // This package cannot be used except for testing.
7 package countertest
8
9 import (
10 "sync"
11
12 "golang.org/x/telemetry/counter"
13 ic "golang.org/x/telemetry/internal/counter"
14 "golang.org/x/telemetry/internal/telemetry"
15 )
16
17 var (
18 openedMu sync.Mutex
19 opened bool
20 )
21
22 // SupportedPlatform reports if this platform supports Open()
23 const SupportedPlatform = !telemetry.DisabledOnPlatform
24
25 func isOpen() bool {
26 openedMu.Lock()
27 defer openedMu.Unlock()
28 return opened
29 }
30
31 // Open enables telemetry data writing to disk.
32 // This is supposed to be called once during the program execution
33 // (i.e. typically in TestMain), and must not be used with
34 // golang.org/x/telemetry/counter.Open.
35 func Open(telemetryDir string) {
36 openedMu.Lock()
37 defer openedMu.Unlock()
38 if opened {
39 panic("Open was called more than once")
40 }
41 telemetry.Default = telemetry.NewDir(telemetryDir)
42
43 counter.Open()
44 opened = true
45 }
46
47 // ReadCounter reads the given counter.
48 func ReadCounter(c *counter.Counter) (count uint64, _ error) {
49 return ic.Read(c)
50 }
51
52 // ReadStackCounter reads the given StackCounter.
53 func ReadStackCounter(c *counter.StackCounter) (stackCounts map[string]uint64, _ error) {
54 return ic.ReadStack(c)
55 }
56
57 // ReadFile reads the counters and stack counters from the given file.
58 func ReadFile(name string) (counters, stackCounters map[string]uint64, _ error) {
59 return ic.ReadFile(name)
60 }