Skip to content

Commit ad27916

Browse files
committed
cmd/go: rename flag counters and add buildmode values separately
Rename the subcommand flag counter names from go/flag/<subcommand>/<flagname> to go/<subcommand>/flag/<flagname>. Also remove the special case that adds counters for buildmode flag values and instead add an additional counter for the flag values. The new counter has the form go/<subcommand>/flag/buildmode:<flagvalue>. We use a new CountFlagValue function that's been added to the internal/telemetry package to help with this. Finally add the go/invocations counter Change-Id: I995b6b0009ba0f58faeb3e2a75f3b137e4136209 Reviewed-on: https://go-review.googlesource.com/c/go/+/583917 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 328aa9e commit ad27916

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

src/cmd/go/main.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func main() {
9898

9999
flag.Usage = base.Usage
100100
flag.Parse()
101+
telemetry.Inc("go/invocations")
101102
telemetry.CountFlags("go/flag:", *flag.CommandLine)
102103

103104
args := flag.Args()
@@ -252,14 +253,9 @@ func invoke(cmd *base.Command, args []string) {
252253
} else {
253254
base.SetFromGOFLAGS(&cmd.Flag)
254255
cmd.Flag.Parse(args[1:])
255-
prefix := "go/flag:" + strings.ReplaceAll(cfg.CmdName, " ", "-") + "-"
256-
cmd.Flag.Visit(func(f *flag.Flag) {
257-
counterName := prefix + f.Name
258-
if f.Name == "buildmode" { // Special case: there is a limited set of buildmode values
259-
counterName += "-" + f.Value.String()
260-
}
261-
telemetry.Inc(counterName)
262-
})
256+
flagCounterPrefix := "go/" + strings.ReplaceAll(cfg.CmdName, " ", "-") + "/flag"
257+
telemetry.CountFlags(flagCounterPrefix+":", cmd.Flag)
258+
telemetry.CountFlagValue(flagCounterPrefix+"/", cmd.Flag, "buildmode")
263259
args = cmd.Flag.Args()
264260
}
265261

src/cmd/internal/telemetry/telemetry.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,18 @@ func NewStackCounter(name string, depth int) *counter.StackCounter {
5959
func CountFlags(prefix string, flagSet flag.FlagSet) {
6060
counter.CountFlags(prefix, flagSet)
6161
}
62+
63+
// CountFlagValue creates a counter for the flag value
64+
// if it is set and increments the counter. The name of the
65+
// counter is the concatenation of prefix, the flagName, ":",
66+
// and value.String() for the flag's value.
67+
func CountFlagValue(prefix string, flagSet flag.FlagSet, flagName string) {
68+
// TODO(matloob): Maybe pass in a list of flagNames if we end up counting
69+
// values for more than one?
70+
// TODO(matloob): Add this to x/telemetry?
71+
flagSet.Visit(func(f *flag.Flag) {
72+
if f.Name == flagName {
73+
counter.New(prefix + f.Name + ":" + f.Value.String()).Inc()
74+
}
75+
})
76+
}

src/cmd/internal/telemetry/telemetry_bootstrap.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ type dummyCounter struct{}
1212

1313
func (dc dummyCounter) Inc() {}
1414

15-
func Start() {}
16-
func StartWithUpload() {}
17-
func Inc(name string) {}
18-
func NewCounter(name string) dummyCounter { return dummyCounter{} }
19-
func NewStackCounter(name string, depth int) dummyCounter { return dummyCounter{} }
20-
func CountFlags(name string, flagSet flag.FlagSet) {}
15+
func Start() {}
16+
func StartWithUpload() {}
17+
func Inc(name string) {}
18+
func NewCounter(name string) dummyCounter { return dummyCounter{} }
19+
func NewStackCounter(name string, depth int) dummyCounter { return dummyCounter{} }
20+
func CountFlags(name string, flagSet flag.FlagSet) {}
21+
func CountFlagValue(prefix string, flagSet flag.FlagSet, flagName string) {}

0 commit comments

Comments
 (0)