Skip to content

Commit

Permalink
all: revert to telemetry mode values (on|off|local)
Browse files Browse the repository at this point in the history
This CL is a partial revert of CL 530436, adding back the 'local'
telemetry mode, which allows local collection but not uploading. The new
meaning of "off" is to disallow both collection and uploading.

For golang/go#63832

Change-Id: Iae7e537e31beef185c27f3877ff23dbae1df6dfd
Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/541376
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Peter Weinberger <pjw@google.com>
  • Loading branch information
findleyr committed Nov 13, 2023
1 parent d4a6526 commit 7324770
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 22 deletions.
5 changes: 3 additions & 2 deletions cmd/gotelemetry/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 31 additions & 8 deletions cmd/gotelemetry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,39 @@ var (
normalCommands = []*command{
{
usage: "on",
short: "enable telemetry uploading",
long: `Gotelemetry on enables telemetry uploading.
short: "enable telemetry collection and uploading",
long: `Gotelemetry on enables telemetry collection and uploading.
When telemetry is enabled, telemetry data is periodically sent to https://telemetry.go.dev/. Uploaded data is used to help improve the Go toolchain and related tools, and it will be published as part of a public dataset.
When telemetry is enabled, telemetry data is written to the local file system and periodically sent to https://telemetry.go.dev/. Uploaded data is used to help improve the Go toolchain and related tools, and it will be published as part of a public dataset.
For more details, see https://telemetry.go.dev/privacy.
This data is collected in accordance with the Google Privacy Policy (https://policies.google.com/privacy).
To disable telemetry uploading, run “gotelemetry off”`,
To disable telemetry uploading, but keep local data collection, run “gotelemetry local”.
To disable both collection and uploading, run “gotelemetry off“.
`,
run: runOn,
},
{
usage: "local",
short: "enable telemetry collection but disable uploading",
long: `Gotelemetry local enables telemetry collection but not uploading.
When telemetry is in local mode, counter data is written to the local file system, but will not be uploaded to remote servers.
To enable telemetry uploading, run “gotelemetry on”.
To disable both collection and uploading, run “gotelemetry off”`,
run: runLocal,
},
{
usage: "off",
short: "disable telemetry uploading",
long: `Gotelemetry off disables telemetry uploading.
short: "disable telemetry collection and uploading",
long: `Gotelemetry off disables telemetry collection and uploading.
When telemetry uploading is off, local counters data will still be written to the local file system, but will not be uploaded to remove servers.
When telemetry is disabled, local counter data is neither collected nor uploaded.
To enable telemetry uploading, run “gotelemetry on”`,
To enable local collection (but not uploading) of telemetry data, run “gotelemetry local“.
To enable both collection and uploading, run “gotelemetry on”.`,
run: runOff,
},
{
Expand Down Expand Up @@ -199,6 +213,15 @@ This data is collected in accordance with the Google Privacy Policy (https://pol
To disable telemetry uploading, run “gotelemetry off”.`
}

func runLocal(_ []string) {
if old, _ := it.Mode(); old == "local" {
return
}
if err := it.SetMode("local"); err != nil {
failf("Failed to set the telemetry mode to local: %v", err)
}
}

func runOff(_ []string) {
if old, _ := it.Mode(); old == "off" {
return
Expand Down
7 changes: 7 additions & 0 deletions internal/counter/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func (f *file) lookup(name string) counterPtr {
return counterPtr{current, ptr}
}

// ErrDisabled is the error returned when telemetry is disabled.
var ErrDisabled = errors.New("counter: disabled by GOTELEMETRY=off")

var (
errNoBuildInfo = errors.New("counter: missing build info")
errCorrupt = errors.New("counter: corrupt counter file")
Expand All @@ -122,6 +125,10 @@ func (f *file) init(begin, end time.Time) {
f.err = errNoBuildInfo
return
}
if mode, _ := telemetry.Mode(); mode == "off" {
f.err = ErrDisabled
return
}
dir := telemetry.LocalDir

if err := os.MkdirAll(dir, 0777); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/telemetry/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func init() {
}

// SetMode updates the telemetry mode with the given mode.
// Acceptable values for mode are "on" or "off".
// Acceptable values for mode are "on", "off", or "local".
//
// SetMode always writes the mode file, and explicitly records the date at
// which the modefile was updated. This means that calling SetMode with "on"
Expand All @@ -60,7 +60,7 @@ func (m ModeFilePath) SetMode(mode string) error {
func (m ModeFilePath) SetModeAsOf(mode string, asofTime time.Time) error {
mode = strings.TrimSpace(mode)
switch mode {
case "on", "off":
case "on", "off", "local":
default:
return fmt.Errorf("invalid telemetry mode: %q", mode)
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func (m ModeFilePath) Mode() (string, time.Time) {
}
data, err := os.ReadFile(fname)
if err != nil {
return "off", time.Time{} // default
return "local", time.Time{} // default
}
mode := string(data)
mode = strings.TrimSpace(mode)
Expand Down
4 changes: 2 additions & 2 deletions internal/telemetry/mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestTelemetryModeWithNoModeConfig(t *testing.T) {
modefile ModeFilePath
want string
}{
{ModeFilePath(filepath.Join(tmp, "mode")), "off"},
{ModeFilePath(filepath.Join(tmp, "mode")), "local"},
{"", "off"},
}
for _, tt := range tests {
Expand All @@ -53,7 +53,7 @@ func TestSetMode(t *testing.T) {
}{
{"on", false},
{"off", false},
{"local", true}, // golang/go#63143: local mode is no longer supported
{"local", false},
{"https://mytelemetry.com", true},
{"http://insecure.com", true},
{"bogus", true},
Expand Down
3 changes: 3 additions & 0 deletions internal/upload/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var (

// reports generates reports from inactive count files
func reports(todo *work) ([]string, error) {
if mode, _ := it.Mode(); mode == "off" {
return nil, nil // no reports
}
today := thisInstant.Format("2006-01-02")
lastWeek := latestReport(todo.uploaded)
if lastWeek >= today { //should never happen
Expand Down
15 changes: 8 additions & 7 deletions mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import (

// Mode returns the current telemetry mode.
//
// The telemetry mode is a global value that controls the
// uploading of telemetry data. Possible mode values are:
// - "on": uploading is enabled
// - "off": uploading is disabled
// The telemetry mode is a global value that controls both the local collection
// and uploading of telemetry data. Possible mode values are:
// - "on": both collection and uploading is enabled
// - "local": collection is enabled, but uploading is disabled
// - "off": both collection and uploading are disabled
//
// When mode is "off", local data is still written to the local file system and
// may be inspected with the [gotelemetry] command.
// When mode is "on", or "local", telemetry data is written to the local file
// system and may be inspected with the [gotelemetry] command.
//
// If an error occurs while reading the telemetry mode from the file system,
// Mode returns the default value "off".
// Mode returns the default value "local".
//
// [gotelemetry]: https://pkg.go.dev/golang.org/x/telemetry/cmd/gotelemetry
func Mode() string {
Expand Down

0 comments on commit 7324770

Please sign in to comment.