| 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 | package counter |
| 6 | |
| 7 | // The implementation of this package and tests are located in |
| 8 | // internal/counter, which can be shared with the upload package. |
| 9 | // TODO(hyangah): use of type aliases prevents nice documentation |
| 10 | // rendering in go doc or pkgsite. Fix this either by avoiding |
| 11 | // type aliasing or restructuring the internal/counter package. |
| 12 | import ( |
| 13 | "flag" |
| 14 | "path" |
| 15 | "runtime/debug" |
| 16 | |
| 17 | "golang.org/x/telemetry/internal/counter" |
| 18 | ) |
| 19 | |
| 20 | // Inc increments the counter with the given name. |
| 21 | func Inc(name string) { |
| 22 | New(name).Inc() |
| 23 | } |
| 24 | |
| 25 | // Add adds n to the counter with the given name. |
| 26 | func Add(name string, n int64) { |
| 27 | New(name).Add(n) |
| 28 | } |
| 29 | |
| 30 | // New returns a counter with the given name. |
| 31 | // New can be called in global initializers and will be compiled down to |
| 32 | // linker-initialized data. That is, calling New to initialize a global |
| 33 | // has no cost at program startup. |
| 34 | // |
| 35 | // See "Counter Naming" in the package doc for a description of counter naming |
| 36 | // conventions. |
| 37 | func New(name string) *Counter { |
| 38 | // Note: not calling DefaultFile.New in order to keep this |
| 39 | // function something the compiler can inline and convert |
| 40 | // into static data initializations, with no init-time footprint. |
| 41 | // TODO(hyangah): is it trivial enough for the compiler to inline? |
| 42 | return counter.New(name) |
| 43 | } |
| 44 | |
| 45 | // A Counter is a single named event counter. |
| 46 | // A Counter is safe for use by multiple goroutines simultaneously. |
| 47 | // |
| 48 | // Counters should typically be created using New |
| 49 | // and stored as global variables, like: |
| 50 | // |
| 51 | // package mypackage |
| 52 | // var errorCount = counter.New("mypackage/errors") |
| 53 | // |
| 54 | // (The initialization of errorCount in this example is handled |
| 55 | // entirely by the compiler and linker; this line executes no code |
| 56 | // at program startup.) |
| 57 | // |
| 58 | // Then code can call Add to increment the counter |
| 59 | // each time the corresponding event is observed. |
| 60 | // |
| 61 | // Although it is possible to use New to create |
| 62 | // a Counter each time a particular event needs to be recorded, |
| 63 | // that usage fails to amortize the construction cost over |
| 64 | // multiple calls to Add, so it is more expensive and not recommended. |
| 65 | type Counter = counter.Counter |
| 66 | |
| 67 | // A StackCounter is the in-memory knowledge about a stack counter. |
| 68 | // StackCounters are more expensive to use than regular Counters, |
| 69 | // requiring, at a minimum, a call to runtime.Callers. |
| 70 | type StackCounter = counter.StackCounter |
| 71 | |
| 72 | // NewStack returns a new stack counter with the given name and depth. |
| 73 | // |
| 74 | // See "Counter Naming" in the package doc for a description of counter naming |
| 75 | // conventions. |
| 76 | func NewStack(name string, depth int) *StackCounter { |
| 77 | return counter.NewStack(name, depth) |
| 78 | } |
| 79 | |
| 80 | // Open prepares telemetry counters for recording to the file system. |
| 81 | // |
| 82 | // If the telemetry mode is "off", Open is a no-op. Otherwise, it opens the |
| 83 | // counter file on disk and starts to mmap telemetry counters to the file. |
| 84 | // Open also persists any counters already created in the current process. |
| 85 | // |
| 86 | // Programs using telemetry should call Open exactly once. |
| 87 | func Open() { |
| 88 | counter.Open() |
| 89 | } |
| 90 | |
| 91 | // CountFlags creates a counter for every flag that is set |
| 92 | // and increments the counter. The name of the counter is |
| 93 | // the concatenation of prefix and the flag name. |
| 94 | // |
| 95 | // For instance, CountFlags("gopls/flag:", *flag.CommandLine) |
| 96 | func CountFlags(prefix string, fs flag.FlagSet) { |
| 97 | fs.Visit(func(f *flag.Flag) { |
| 98 | New(prefix + f.Name).Inc() |
| 99 | }) |
| 100 | } |
| 101 | |
| 102 | // CountCommandLineFlags creates a counter for every flag |
| 103 | // that is set in the default flag.CommandLine FlagSet using |
| 104 | // the counter name binaryName+"/flag:"+flagName where |
| 105 | // binaryName is the base name of the Path embedded in the |
| 106 | // binary's build info. If the binary does not have embedded build |
| 107 | // info, the "flag:"+flagName counter will be incremented. |
| 108 | // |
| 109 | // CountCommandLineFlags must be called after flags are parsed |
| 110 | // with flag.Parse. |
| 111 | // |
| 112 | // For instance, if the -S flag is passed to cmd/compile and |
| 113 | // CountCommandLineFlags is called after flags are parsed, |
| 114 | // the "compile/flag:S" counter will be incremented. |
| 115 | func CountCommandLineFlags() { |
| 116 | prefix := "flag:" |
| 117 | if buildInfo, ok := debug.ReadBuildInfo(); ok && buildInfo.Path != "" { |
| 118 | prefix = path.Base(buildInfo.Path) + "/" + prefix |
| 119 | } |
| 120 | CountFlags(prefix, *flag.CommandLine) |
| 121 | } |