cmd/go: call telemetry.MaybeChild at start of go command

Call the new telemetry.MaybeChild function at the start of the go
command so that the child process logic can be run immediately without
running toolchain selection if this is the child process.

The Start function in the telemetry shim package has been renamed to
OpenCounters to make it clear that that's its only function.

The StartWithUpload function in the telemetry shim package has been
renamed to MaybeParent because that's its actual effective behavior in
cmd/go, the only place it's called: it won't run as the child because
MaybeChild has already been called and would have run as the child if
the program was the telemetry child, and it won't open counters because
telemetry.Start has been called. Checks are added that those functions
are always called before so that the function name and comment are
accurate.

It might make sense to add a true telemetry.MaybeParent function that
doesn't try to start the child or open counters to make things a little
simpler.

Change-Id: Ie81e2418af85cef18ec41f75db66365f6597b8b1
Reviewed-on: https://go-review.googlesource.com/c/go/+/592535
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Michael Matloob 2024-06-13 12:38:20 -04:00
parent 956f8a67dd
commit dbe03e4831
23 changed files with 51 additions and 31 deletions

View file

@ -46,7 +46,7 @@ func usage() {
func main() {
log.SetFlags(0)
log.SetPrefix("addr2line: ")
telemetry.Start()
telemetry.OpenCounters()
// pprof expects this behavior when checking for addr2line
if len(os.Args) > 1 && os.Args[1] == "--help" {

View file

@ -26,7 +26,7 @@ import (
func main() {
log.SetFlags(0)
log.SetPrefix("asm: ")
telemetry.Start()
telemetry.OpenCounters()
buildcfg.Check()
GOARCH := buildcfg.GOARCH

View file

@ -26,7 +26,7 @@ var wflag = flag.Bool("w", false, "write build ID")
func main() {
log.SetPrefix("buildid: ")
log.SetFlags(0)
telemetry.Start()
telemetry.OpenCounters()
flag.Usage = usage
flag.Parse()
telemetry.Inc("buildid/invocations")

View file

@ -258,7 +258,7 @@ var goarch, goos, gomips, gomips64 string
var gccBaseCmd []string
func main() {
telemetry.Start()
telemetry.OpenCounters()
objabi.AddVersionFlag() // -V
objabi.Flagparse(usage)
telemetry.Inc("cgo/invocations")

View file

@ -59,7 +59,7 @@ func handlePanic() {
// code, and finally writes the compiled package definition to disk.
func Main(archInit func(*ssagen.ArchInfo)) {
base.Timer.Start("fe", "init")
telemetry.Start()
telemetry.OpenCounters()
telemetry.Inc("compile/invocations")
defer handlePanic()

View file

@ -109,7 +109,7 @@ const (
)
func main() {
telemetry.Start()
telemetry.OpenCounters()
// First argument should be mode/subcommand.
if len(os.Args) < 2 {

View file

@ -87,7 +87,7 @@ const (
)
func main() {
telemetry.Start()
telemetry.OpenCounters()
objabi.AddVersionFlag()
flag.Usage = usage

View file

@ -69,7 +69,7 @@ var (
func main() {
log.SetPrefix("distpack: ")
log.SetFlags(0)
telemetry.Start()
telemetry.OpenCounters()
flag.Usage = usage
flag.Parse()
telemetry.Inc("distpack/invocations")

View file

@ -87,7 +87,7 @@ func usage() {
func main() {
log.SetFlags(0)
log.SetPrefix("doc: ")
telemetry.Start()
telemetry.OpenCounters()
dirsInit()
err := do(os.Stdout, flag.CommandLine, os.Args[1:])
if err != nil {

View file

@ -65,7 +65,7 @@ func usage() {
}
func main() {
telemetry.Start()
telemetry.OpenCounters()
flag.Usage = usage
flag.Parse()
telemetry.Inc("fix/invocations")

View file

@ -95,11 +95,12 @@ var counterErrorsGOPATHEntryRelative = telemetry.NewCounter("go/errors:gopath-en
func main() {
log.SetFlags(0)
telemetry.Start() // Open the telemetry counter file so counters can be written to it.
telemetry.MaybeChild() // Run in child mode if this is the telemetry sidecar child process.
telemetry.OpenCounters() // Open the telemetry counter file so counters can be written to it.
handleChdirFlag()
toolchain.Select()
telemetry.StartWithUpload() // Run the upload process. Opening the counter file is idempotent.
telemetry.MaybeParent() // Run the upload process. Opening the counter file is idempotent.
flag.Usage = base.Usage
flag.Parse()
telemetry.Inc("go/invocations")

View file

@ -374,7 +374,7 @@ func main() {
}
func gofmtMain(s *sequencer) {
telemetry.Start()
telemetry.OpenCounters()
flag.Usage = usage
flag.Parse()
telemetry.Inc("gofmt/invocations")

View file

@ -19,25 +19,43 @@ import (
"golang.org/x/telemetry/counter"
)
// Start opens the counter files for writing if telemetry is supported
var openCountersCalled, maybeChildCalled bool
// OpenCounters opens the counter files for writing if telemetry is supported
// on the current platform (and does nothing otherwise).
func Start() {
func OpenCounters() {
openCountersCalled = true
telemetry.Start(telemetry.Config{
TelemetryDir: os.Getenv("TEST_TELEMETRY_DIR"),
})
}
// StartWithUpload opens the counter files for writing if telemetry
// is supported on the current platform and also enables a once a day
// check to see if the weekly reports are ready to be uploaded.
// It should only be called by cmd/go
func StartWithUpload() {
// MaybeParent does a once a day check to see if the weekly reports are
// ready to be processed or uploaded, and if so, starts the telemetry child to
// do so. It should only be called by cmd/go, and only after OpenCounters and MaybeChild
// have already been called.
func MaybeParent() {
if !openCountersCalled || !maybeChildCalled {
panic("MaybeParent must be called after OpenCounters and MaybeChild")
}
telemetry.Start(telemetry.Config{
Upload: true,
TelemetryDir: os.Getenv("TEST_TELEMETRY_DIR"),
})
}
// MaybeChild executes the telemetry child logic if the calling program is
// the telemetry child process, and does nothing otherwise. It is meant to be
// called as the first thing in a program that uses telemetry.OpenCounters but cannot
// call telemetry.OpenCounters immediately when it starts.
func MaybeChild() {
maybeChildCalled = true
telemetry.MaybeChild(telemetry.Config{
Upload: true,
TelemetryDir: os.Getenv("TEST_TELEMETRY_DIR"),
})
}
// Inc increments the counter with the given name.
func Inc(name string) {
counter.Inc(name)

View file

@ -12,8 +12,9 @@ type dummyCounter struct{}
func (dc dummyCounter) Inc() {}
func Start() {}
func StartWithUpload() {}
func OpenCounters() {}
func MaybeParent() {}
func MaybeChild() {}
func Inc(name string) {}
func NewCounter(name string) dummyCounter { return dummyCounter{} }
func NewStackCounter(name string, depth int) dummyCounter { return dummyCounter{} }

View file

@ -157,7 +157,7 @@ func (t *ternaryFlag) IsBoolFlag() bool { return true } // parse like a boolean
func Main(arch *sys.Arch, theArch Arch) {
log.SetPrefix("link: ")
log.SetFlags(0)
telemetry.Start()
telemetry.OpenCounters()
telemetry.Inc("link/invocations")
thearch = theArch

View file

@ -68,7 +68,7 @@ func (nflag) String() string {
func main() {
log.SetFlags(0)
telemetry.Start()
telemetry.OpenCounters()
flag.Usage = usage
flag.Parse()
telemetry.Inc("nm/invocations")

View file

@ -58,7 +58,7 @@ func usage() {
func main() {
log.SetFlags(0)
log.SetPrefix("objdump: ")
telemetry.Start()
telemetry.OpenCounters()
flag.Usage = usage
flag.Parse()

View file

@ -31,7 +31,7 @@ func usage() {
func main() {
log.SetFlags(0)
log.SetPrefix("pack: ")
telemetry.Start()
telemetry.OpenCounters()
// need "pack op archive" at least.
if len(os.Args) < 3 {
log.Print("not enough arguments")

View file

@ -32,7 +32,7 @@ import (
)
func main() {
telemetry.Start()
telemetry.OpenCounters()
telemetry.Inc("pprof/invocations")
options := &driver.Options{
Fetch: new(fetcher),

View file

@ -73,7 +73,7 @@ func main() {
log.SetFlags(0)
log.SetPrefix("preprofile: ")
telemetry.Start()
telemetry.OpenCounters()
flag.Usage = usage
flag.Parse()

View file

@ -116,7 +116,7 @@ func ignoreSignals() {
}
func main() {
telemetry.Start()
telemetry.OpenCounters()
flag.Usage = usage
flag.Parse()

View file

@ -64,7 +64,7 @@ var (
)
func main() {
telemetry.Start()
telemetry.OpenCounters()
flag.Usage = func() {
fmt.Fprint(os.Stderr, usageMessage)
os.Exit(2)

View file

@ -47,7 +47,7 @@ import (
)
func main() {
telemetry.Start()
telemetry.OpenCounters()
objabi.AddVersionFlag()
telemetry.Inc("vet/invocations")