"cmd/internal/sys"
cmdgo "cmd/go"
-
- "golang.org/x/telemetry/counter/countertest"
)
func init() {
web.EnableTestHooks(interceptors)
}
- cmdgo.TelemetryStart = func() {
- // TODO(matloob): we'll ideally want to call telemetry.Start here
- // but it calls counter.Open, which we don't want to do because
- // we want to call countertest.Open.
- if telemetryDir := os.Getenv("TESTGO_TELEMETRY_DIR"); telemetryDir != "" {
- countertest.Open(telemetryDir)
- }
- }
-
cmdgo.Main()
os.Exit(0)
}
tg.parallel()
tg.tempDir("home/go")
tg.setenv(homeEnvName(), tg.path("home"))
+ // Set TEST_TELEMETRY_DIR to a path that doesn't exist
+ // so that the counter uploading code doesn't write
+ // the counter token file to the temp dir after the test finishes.
+ tg.setenv("TEST_TELEMETRY_DIR", "/no-telemetry-dir")
tg.run("env", "GOPATH")
tg.grepStdout(regexp.QuoteMeta(tg.path("home/go")), "want GOPATH=$HOME/go")
tg.setenv("GOPATH", "")
tg.tempDir("home")
tg.setenv(homeEnvName(), tg.path("home"))
+ // Set TEST_TELEMETRY_DIR to a path that doesn't exist
+ // so that the counter uploading code doesn't write
+ // the counter token file to the temp dir after the test finishes.
+ tg.setenv("TEST_TELEMETRY_DIR", "/no-telemetry-dir")
tg.runFail("install", "github.com/golang/example/hello")
tg.grepStderr(regexp.QuoteMeta(tg.path("home/go/src/github.com/golang/example/hello"))+`.*from \$GOPATH`, "expected default GOPATH")
package base
import (
+ "cmd/internal/telemetry"
"context"
"flag"
"fmt"
"cmd/go/internal/cfg"
"cmd/go/internal/str"
-
- "golang.org/x/telemetry/counter"
)
// A Command is an implementation of a go command
var counterNames = map[string]bool{}
+type Counter interface {
+ Inc()
+}
+
// NewCounter registers a new counter. It must be called from an init function
// or global variable initializer.
-func NewCounter(name string) *counter.Counter {
+func NewCounter(name string) Counter {
if counterNames[name] {
panic(fmt.Errorf("counter %q initialized twice", name))
}
counterNames[name] = true
- return counter.New(name)
+ return telemetry.NewCounter(name)
}
func RegisteredCounterNames() []string {
package main
import (
+ "cmd/internal/telemetry"
"context"
"flag"
"fmt"
"cmd/go/internal/vet"
"cmd/go/internal/work"
"cmd/go/internal/workcmd"
-
- "golang.org/x/telemetry/counter"
)
func init() {
func main() {
log.SetFlags(0)
- TelemetryStart() // Open the telemetry counter file so counters can be written to it.
+ telemetry.StartWithUpload() // Open the telemetry counter file so counters can be written to it.
handleChdirFlag()
toolchain.Select()
flag.Usage = base.Usage
flag.Parse()
- counter.CountFlags("go/flag:", *flag.CommandLine)
+ telemetry.CountFlags("go/flag:", *flag.CommandLine)
args := flag.Args()
if len(args) < 1 {
cfg.CmdName = args[0] // for error messages
if args[0] == "help" {
- counter.Inc("go/subcommand:" + strings.Join(append([]string{"help"}, args[1:]...), "-"))
+ telemetry.Inc("go/subcommand:" + strings.Join(append([]string{"help"}, args[1:]...), "-"))
help.Help(os.Stdout, args[1:])
return
}
}
if args[used] == "help" {
// Accept 'go mod help' and 'go mod help foo' for 'go help mod' and 'go help mod foo'.
- counter.Inc("go/subcommand:" + strings.ReplaceAll(cfg.CmdName, " ", "-") + "-" + strings.Join(args[used:], "-"))
+ telemetry.Inc("go/subcommand:" + strings.ReplaceAll(cfg.CmdName, " ", "-") + "-" + strings.Join(args[used:], "-"))
help.Help(os.Stdout, append(slices.Clip(args[:used]), args[used+1:]...))
base.Exit()
}
if cmdName == "" {
cmdName = args[0]
}
- counter.Inc("go/subcommand:unknown")
+ telemetry.Inc("go/subcommand:unknown")
fmt.Fprintf(os.Stderr, "go %s: unknown command\nRun 'go help%s' for usage.\n", cmdName, helpArg)
base.SetExitStatus(2)
base.Exit()
}
- counter.Inc("go/subcommand:" + strings.ReplaceAll(cfg.CmdName, " ", "-"))
+ telemetry.Inc("go/subcommand:" + strings.ReplaceAll(cfg.CmdName, " ", "-"))
invoke(cmd, args[used-1:])
base.Exit()
}
} else {
base.SetFromGOFLAGS(&cmd.Flag)
cmd.Flag.Parse(args[1:])
- counter.CountFlags("go/flag:"+strings.ReplaceAll(cfg.CmdName, " ", "-")+"-", cmd.Flag)
+ telemetry.CountFlags("go/flag:"+strings.ReplaceAll(cfg.CmdName, " ", "-")+"-", cmd.Flag)
args = cmd.Flag.Args()
}
_, dir, _ = strings.Cut(a, "=")
os.Args = slices.Delete(os.Args, used, used+1)
}
- counter.Inc("go/flag:C")
+ telemetry.Inc("go/flag:C")
if err := os.Chdir(dir); err != nil {
base.Fatalf("go: %v", err)
telemetryDir = filepath.Join(work, "telemetry")
must(os.MkdirAll(telemetryDir, 0777))
- must(s.Setenv("TESTGO_TELEMETRY_DIR", filepath.Join(work, "telemetry")))
+ must(s.Setenv("TEST_TELEMETRY_DIR", filepath.Join(work, "telemetry")))
must(os.MkdirAll(filepath.Join(work, "tmp"), 0777))
must(s.Setenv(tempEnvName(), filepath.Join(work, "tmp")))
+++ /dev/null
-// Copyright 2024 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-//go:build !cmd_go_bootstrap
-
-package main
-
-import "golang.org/x/telemetry"
-
-var TelemetryStart = func() {
- telemetry.Start(telemetry.Config{Upload: true})
-}
+++ /dev/null
-// Copyright 2024 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-//go:build cmd_go_bootstrap
-
-package main
-
-var TelemetryStart = func() {}
--- /dev/null
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build !cmd_go_bootstrap
+
+// Package telemetry is a shim package around the golang.org/x/telemetry
+// and golang.org/x/telemetry/counter packages that has code build tagged
+// out for cmd_go_bootstrap so that the bootstrap Go command does not
+// depend on net (which is a dependency of golang.org/x/telemetry/counter
+// on Windows).
+package telemetry
+
+import (
+ "flag"
+ "os"
+
+ "golang.org/x/telemetry"
+ "golang.org/x/telemetry/counter"
+)
+
+// Start opens the counter files for writing if telemetry is supported
+// on the current platform (and does nothing otherwise).
+func Start() {
+ 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() {
+ telemetry.Start(telemetry.Config{
+ Upload: true,
+ TelemetryDir: os.Getenv("TEST_TELEMETRY_DIR"),
+ })
+}
+
+func Inc(name string) {
+ counter.Inc(name)
+}
+
+func NewCounter(name string) *counter.Counter {
+ return counter.New(name)
+}
+
+func CountFlags(prefix string, flagSet flag.FlagSet) {
+ counter.CountFlags(prefix, flagSet)
+}
--- /dev/null
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build cmd_go_bootstrap
+
+package telemetry
+
+import "flag"
+
+type dummyCounter struct{}
+
+func (dc dummyCounter) Inc() {}
+
+func Start() {}
+func StartWithUpload() {}
+func Inc(name string) {}
+func NewCounter(name string) dummyCounter { return dummyCounter{} }
+func CountFlags(name string, flagSet flag.FlagSet) {}