const (
FileVersion = "v1"
)
CounterTime returns the current UTC time. Mutable for testing.
var CounterTime = func() time.Time { return time.Now().UTC() }
var ( CrashOnBugs = false // for testing; if set, exit on fatal log messages )
ErrDisabled is the error returned when telemetry is disabled.
var ErrDisabled = errors.New("counter: disabled as Go telemetry is off")
func DecodeStack(ename string) string
DecodeStack expands the (compressed) stack encoded in the counter name.
func EncodeStack(pcs []uintptr, prefix string) string
EncodeStack returns the name of the counter to use for the given stack of program counters. The name encodes the stack.
func IsStackCounter(name string) bool
IsStackCounter reports whether the counter name is for a stack counter.
func Open(rotate bool) func()
Open associates counting with the defaultFile. The returned function is for testing only, and should be called after all Inc()s are finished, but before any reports are generated. (Otherwise expired count files will not be deleted on Windows.)
func Read(c *Counter) (uint64, error)
Read reads the given counter. This is the implementation of x/telemetry/counter/countertest.ReadCounter.
func ReadFile(name string) (counters, stackCounters map[string]uint64, _ error)
ReadFile reads the counters and stack counters from the given file. This is the implementation of x/telemetry/counter/countertest.ReadFile.
func ReadMapped(name string) ([]byte, error)
ReadMapped reads the contents of the given file by memory mapping.
This avoids file synchronization issues.
func ReadStack(c *StackCounter) (map[string]uint64, error)
ReadStack reads the given stack counter. This is the implementation of golang.org/x/telemetry/counter/countertest.ReadStackCounter.
A Counter is a single named event counter. A Counter is safe for use by multiple goroutines simultaneously.
Counters should typically be created using New and stored as global variables, like:
package mypackage var errorCount = counter.New("mypackage/errors")
(The initialization of errorCount in this example is handled entirely by the compiler and linker; this line executes no code at program startup.)
Then code can call Add to increment the counter each time the corresponding event is observed.
Although it is possible to use New to create a Counter each time a particular event needs to be recorded, that usage fails to amortize the construction cost over multiple calls to Add, so it is more expensive and not recommended.
type Counter struct {
// contains filtered or unexported fields
}
func New(name string) *Counter
New returns a counter with the given name. New can be called in global initializers and will be compiled down to linker-initialized data. That is, calling New to initialize a global has no cost at program startup.
func (c *Counter) Add(n int64)
Add adds n to the counter. n cannot be negative, as counts cannot decrease.
func (c *Counter) Inc()
Inc adds 1 to the counter.
func (c *Counter) Name() string
type File struct { Meta map[string]string Count map[string]uint64 }
func Parse(filename string, data []byte) (*File, error)
a StackCounter is the in-memory knowledge about a stack counter. StackCounters are more expensive to use than regular Counters, requiring, at a minimum, a call to runtime.Callers.
type StackCounter struct {
// contains filtered or unexported fields
}
func NewStack(name string, depth int) *StackCounter
func (c *StackCounter) Counters() []*Counter
Counters returns the known Counters for a StackCounter. There may be more in the count file.
func (c *StackCounter) Inc()
Inc increments a stack counter. It computes the caller's stack and looks up the corresponding counter. It then increments that counter, creating it if necessary.
func (c *StackCounter) Names() []string
Names reports all the counter names associated with a StackCounter.