Skip to content

Commit eef288d

Browse files
committed
cmd: add telemetry for commands in cmd
This change modifies the commands in cmd to open counter files, increment invocations counters and to increment counters for the names of the flags that were passed in. cmd/pprof and cmd/vet are both wrappers around tools defined in other modules which do their own flag processing so we can't directly increment flag counters right after flags are parsed. For those two commands we wait to increment counters until after the programs have returned. cmd/dist is built with the bootstrap go so it can't depend on telemetry yet. We can add telemetry support to it once 1.23 is the minimum bootstrap version. For #58894 Change-Id: Ic7f6009992465e55c56ad4dc6451bcb1ca51374a Reviewed-on: https://go-review.googlesource.com/c/go/+/585235 Reviewed-by: Sam Thanawalla <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent efc3473 commit eef288d

File tree

18 files changed

+88
-3
lines changed

18 files changed

+88
-3
lines changed

src/cmd/addr2line/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"strings"
2929

3030
"cmd/internal/objfile"
31+
"cmd/internal/telemetry"
3132
)
3233

3334
func printUsage(w *os.File) {
@@ -45,6 +46,7 @@ func usage() {
4546
func main() {
4647
log.SetFlags(0)
4748
log.SetPrefix("addr2line: ")
49+
telemetry.Start()
4850

4951
// pprof expects this behavior when checking for addr2line
5052
if len(os.Args) > 1 && os.Args[1] == "--help" {
@@ -54,6 +56,8 @@ func main() {
5456

5557
flag.Usage = usage
5658
flag.Parse()
59+
telemetry.Inc("addr2line/invocations")
60+
telemetry.CountFlags("addr2line/flag:", *flag.CommandLine)
5761
if flag.NArg() != 1 {
5862
usage()
5963
}

src/cmd/asm/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@ import (
2020
"cmd/internal/bio"
2121
"cmd/internal/obj"
2222
"cmd/internal/objabi"
23+
"cmd/internal/telemetry"
2324
)
2425

2526
func main() {
2627
log.SetFlags(0)
2728
log.SetPrefix("asm: ")
29+
telemetry.Start()
2830

2931
buildcfg.Check()
3032
GOARCH := buildcfg.GOARCH
3133

3234
flags.Parse()
35+
telemetry.Inc("asm/invocations")
36+
telemetry.CountFlags("asm/flag:", *flag.CommandLine)
3337

3438
architecture := arch.Set(GOARCH, *flags.Shared || *flags.Dynlink)
3539
if architecture == nil {

src/cmd/buildid/buildid.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"cmd/internal/buildid"
15+
"cmd/internal/telemetry"
1516
)
1617

1718
func usage() {
@@ -25,8 +26,11 @@ var wflag = flag.Bool("w", false, "write build ID")
2526
func main() {
2627
log.SetPrefix("buildid: ")
2728
log.SetFlags(0)
29+
telemetry.Start()
2830
flag.Usage = usage
2931
flag.Parse()
32+
telemetry.Inc("buildid/invocations")
33+
telemetry.CountFlags("buildid/flag:", *flag.CommandLine)
3034
if flag.NArg() != 1 {
3135
usage()
3236
}

src/cmd/cgo/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"cmd/internal/edit"
2929
"cmd/internal/notsha256"
3030
"cmd/internal/objabi"
31+
"cmd/internal/telemetry"
3132
)
3233

3334
// A Package collects information about the package we're going to write.
@@ -257,8 +258,11 @@ var goarch, goos, gomips, gomips64 string
257258
var gccBaseCmd []string
258259

259260
func main() {
261+
telemetry.Start()
260262
objabi.AddVersionFlag() // -V
261263
objabi.Flagparse(usage)
264+
telemetry.Inc("cgo/invocations")
265+
telemetry.CountFlags("cgo/flag:", *flag.CommandLine)
262266

263267
if *gccgoDefineCgoIncomplete {
264268
if !*gccgo {

src/cmd/covdata/covdata.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package main
77
import (
88
"cmd/internal/cov"
99
"cmd/internal/pkgpattern"
10+
"cmd/internal/telemetry"
1011
"flag"
1112
"fmt"
1213
"os"
@@ -108,6 +109,8 @@ const (
108109
)
109110

110111
func main() {
112+
telemetry.Start()
113+
111114
// First argument should be mode/subcommand.
112115
if len(os.Args) < 2 {
113116
usage("missing command selector")
@@ -143,6 +146,8 @@ func main() {
143146
op.Usage("")
144147
}
145148
flag.Parse()
149+
telemetry.Inc("covdata/invocations")
150+
telemetry.CountFlags("covdata/flag:", *flag.CommandLine)
146151

147152
// Mode-independent flag setup
148153
dbgtrace(1, "starting mode-independent setup")

src/cmd/cover/cover.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"cmd/internal/edit"
2828
"cmd/internal/objabi"
29+
"cmd/internal/telemetry"
2930
)
3031

3132
const usageMessage = "" +
@@ -86,9 +87,13 @@ const (
8687
)
8788

8889
func main() {
90+
telemetry.Start()
91+
8992
objabi.AddVersionFlag()
9093
flag.Usage = usage
9194
objabi.Flagparse(usage)
95+
telemetry.Inc("cover/invocations")
96+
telemetry.CountFlags("cover/flag:", *flag.CommandLine)
9297

9398
// Usage information when no arguments.
9499
if flag.NFlag() == 0 && flag.NArg() == 0 {

src/cmd/distpack/pack.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import (
4444
"runtime"
4545
"strings"
4646
"time"
47+
48+
"cmd/internal/telemetry"
4749
)
4850

4951
func usage() {
@@ -67,8 +69,11 @@ var (
6769
func main() {
6870
log.SetPrefix("distpack: ")
6971
log.SetFlags(0)
72+
telemetry.Start()
7073
flag.Usage = usage
7174
flag.Parse()
75+
telemetry.Inc("distpack/invocations")
76+
telemetry.CountFlags("distpack/flag:", *flag.CommandLine)
7277
if flag.NArg() != 0 {
7378
usage()
7479
}

src/cmd/doc/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ import (
5454
"path"
5555
"path/filepath"
5656
"strings"
57+
58+
"cmd/internal/telemetry"
5759
)
5860

5961
var (
@@ -85,6 +87,7 @@ func usage() {
8587
func main() {
8688
log.SetFlags(0)
8789
log.SetPrefix("doc: ")
90+
telemetry.Start()
8891
dirsInit()
8992
err := do(os.Stdout, flag.CommandLine, os.Args[1:])
9093
if err != nil {
@@ -105,6 +108,8 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
105108
flagSet.BoolVar(&showSrc, "src", false, "show source code for symbol")
106109
flagSet.BoolVar(&short, "short", false, "one-line representation for each symbol")
107110
flagSet.Parse(args)
111+
telemetry.Inc("doc/invocations")
112+
telemetry.CountFlags("doc/flag:", *flag.CommandLine)
108113
if chdir != "" {
109114
if err := os.Chdir(chdir); err != nil {
110115
return err

src/cmd/fix/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"path/filepath"
2222
"sort"
2323
"strings"
24+
25+
"cmd/internal/telemetry"
2426
)
2527

2628
var (
@@ -63,8 +65,11 @@ func usage() {
6365
}
6466

6567
func main() {
68+
telemetry.Start()
6669
flag.Usage = usage
6770
flag.Parse()
71+
telemetry.Inc("fix/invocations")
72+
telemetry.CountFlags("fix/flag:", *flag.CommandLine)
6873

6974
if !version.IsValid(*goVersion) {
7075
report(fmt.Errorf("invalid -go=%s", *goVersion))

src/cmd/gofmt/gofmt.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"strconv"
2626
"strings"
2727

28+
"cmd/internal/telemetry"
29+
2830
"golang.org/x/sync/semaphore"
2931
)
3032

@@ -372,8 +374,11 @@ func main() {
372374
}
373375

374376
func gofmtMain(s *sequencer) {
377+
telemetry.Start()
375378
flag.Usage = usage
376379
flag.Parse()
380+
telemetry.Inc("gofmt/invocations")
381+
telemetry.CountFlags("gofmt/flag:", *flag.CommandLine)
377382

378383
if *cpuprofile != "" {
379384
fdSem <- true

src/cmd/nm/nm.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"sort"
1414

1515
"cmd/internal/objfile"
16+
"cmd/internal/telemetry"
1617
)
1718

1819
const helpText = `usage: go tool nm [options] file...
@@ -67,8 +68,11 @@ func (nflag) String() string {
6768

6869
func main() {
6970
log.SetFlags(0)
71+
telemetry.Start()
7072
flag.Usage = usage
7173
flag.Parse()
74+
telemetry.Inc("nm/invocations")
75+
telemetry.CountFlags("nm/flag:", *flag.CommandLine)
7276

7377
switch *sortOrder {
7478
case "address", "name", "none", "size":

src/cmd/objdump/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"strings"
4242

4343
"cmd/internal/objfile"
44+
"cmd/internal/telemetry"
4445
)
4546

4647
var printCode = flag.Bool("S", false, "print Go code alongside assembly")
@@ -57,9 +58,12 @@ func usage() {
5758
func main() {
5859
log.SetFlags(0)
5960
log.SetPrefix("objdump: ")
61+
telemetry.Start()
6062

6163
flag.Usage = usage
6264
flag.Parse()
65+
telemetry.Inc("objdump/invocations")
66+
telemetry.CountFlags("objdump/flag:", *flag.CommandLine)
6367
if flag.NArg() != 1 && flag.NArg() != 3 {
6468
usage()
6569
}

src/cmd/pack/pack.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main
66

77
import (
88
"cmd/internal/archive"
9+
"cmd/internal/telemetry"
910
"fmt"
1011
"io"
1112
"io/fs"
@@ -30,13 +31,16 @@ func usage() {
3031
func main() {
3132
log.SetFlags(0)
3233
log.SetPrefix("pack: ")
34+
telemetry.Start()
3335
// need "pack op archive" at least.
3436
if len(os.Args) < 3 {
3537
log.Print("not enough arguments")
3638
fmt.Fprintln(os.Stderr)
3739
usage()
3840
}
3941
setOp(os.Args[1])
42+
telemetry.Inc("pack/invocations")
43+
telemetry.Inc("pack/op:" + string(op))
4044
var ar *Archive
4145
switch op {
4246
case 'p':

src/cmd/pprof/pprof.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package main
1212
import (
1313
"crypto/tls"
1414
"debug/dwarf"
15+
"flag"
1516
"fmt"
1617
"io"
1718
"net/http"
@@ -24,18 +25,23 @@ import (
2425
"time"
2526

2627
"cmd/internal/objfile"
28+
"cmd/internal/telemetry"
2729

2830
"github.com/google/pprof/driver"
2931
"github.com/google/pprof/profile"
3032
)
3133

3234
func main() {
35+
telemetry.Start()
36+
telemetry.Inc("pprof/invocations")
3337
options := &driver.Options{
3438
Fetch: new(fetcher),
3539
Obj: new(objTool),
3640
UI: newUI(),
3741
}
38-
if err := driver.PProf(options); err != nil {
42+
err := driver.PProf(options)
43+
telemetry.CountFlags("pprof/flag:", *flag.CommandLine) // pprof will use the flag package as its default
44+
if err != nil {
3945
fmt.Fprintf(os.Stderr, "%v\n", err)
4046
os.Exit(2)
4147
}

src/cmd/preprofile/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"bufio"
1919
"cmd/internal/objabi"
2020
"cmd/internal/pgo"
21+
"cmd/internal/telemetry"
2122
"flag"
2223
"fmt"
2324
"log"
@@ -31,8 +32,8 @@ func usage() {
3132
}
3233

3334
var (
34-
output = flag.String("o", "", "output file path")
35-
input = flag.String("i", "", "input pprof file path")
35+
output = flag.String("o", "", "output file path")
36+
input = flag.String("i", "", "input pprof file path")
3637
)
3738

3839
func preprocess(profileFile string, outputFile string) error {
@@ -72,9 +73,12 @@ func main() {
7273

7374
log.SetFlags(0)
7475
log.SetPrefix("preprofile: ")
76+
telemetry.Start()
7577

7678
flag.Usage = usage
7779
flag.Parse()
80+
telemetry.Inc("preprofile/invocations")
81+
telemetry.CountFlags("preprofile/flag:", *flag.CommandLine)
7882
if *input == "" {
7983
log.Print("Input pprof path required (-i)")
8084
usage()

src/cmd/test2json/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import (
9696
"os/exec"
9797
"os/signal"
9898

99+
"cmd/internal/telemetry"
99100
"cmd/internal/test2json"
100101
)
101102

@@ -115,8 +116,12 @@ func ignoreSignals() {
115116
}
116117

117118
func main() {
119+
telemetry.Start()
120+
118121
flag.Usage = usage
119122
flag.Parse()
123+
telemetry.Inc("test2json/invocations")
124+
telemetry.CountFlags("test2json/flag:", *flag.CommandLine)
120125

121126
var mode test2json.Mode
122127
if *flagT {

src/cmd/trace/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package main
77
import (
88
"bufio"
99
"cmd/internal/browser"
10+
"cmd/internal/telemetry"
1011
cmdv2 "cmd/trace/v2"
1112
"flag"
1213
"fmt"
@@ -65,11 +66,14 @@ var (
6566
)
6667

6768
func main() {
69+
telemetry.Start()
6870
flag.Usage = func() {
6971
fmt.Fprint(os.Stderr, usageMessage)
7072
os.Exit(2)
7173
}
7274
flag.Parse()
75+
telemetry.Inc("trace/invocations")
76+
telemetry.CountFlags("trace/flag:", *flag.CommandLine)
7377

7478
// Go 1.7 traces embed symbol info and does not require the binary.
7579
// But we optionally accept binary as first arg for Go 1.5 traces.

0 commit comments

Comments
 (0)