1 // Copyright 2024 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build !cmd_go_bootstrap && !compiler_bootstrap 6 7 // Package telemetry is a shim package around the golang.org/x/telemetry 8 // and golang.org/x/telemetry/counter packages that has code build tagged 9 // out for cmd_go_bootstrap so that the bootstrap Go command does not 10 // depend on net (which is a dependency of golang.org/x/telemetry/counter 11 // on Windows). 12 package telemetry 13 14 import ( 15 "os" 16 17 "cmd/internal/telemetry/counter" 18 19 "golang.org/x/telemetry" 20 ) 21 22 var openCountersCalled, maybeChildCalled bool 23 24 // MaybeParent does a once a day check to see if the weekly reports are 25 // ready to be processed or uploaded, and if so, starts the telemetry child to 26 // do so. It should only be called by cmd/go, and only after OpenCounters and MaybeChild 27 // have already been called. 28 func MaybeParent() { 29 if !counter.OpenCalled() || !maybeChildCalled { 30 panic("MaybeParent must be called after OpenCounters and MaybeChild") 31 } 32 telemetry.Start(telemetry.Config{ 33 Upload: true, 34 TelemetryDir: os.Getenv("TEST_TELEMETRY_DIR"), 35 }) 36 } 37 38 // MaybeChild executes the telemetry child logic if the calling program is 39 // the telemetry child process, and does nothing otherwise. It is meant to be 40 // called as the first thing in a program that uses telemetry.OpenCounters but cannot 41 // call telemetry.OpenCounters immediately when it starts. 42 func MaybeChild() { 43 maybeChildCalled = true 44 telemetry.MaybeChild(telemetry.Config{ 45 Upload: true, 46 TelemetryDir: os.Getenv("TEST_TELEMETRY_DIR"), 47 }) 48 } 49 50 // Mode returns the current telemetry mode. 51 // 52 // The telemetry mode is a global value that controls both the local collection 53 // and uploading of telemetry data. Possible mode values are: 54 // - "on": both collection and uploading is enabled 55 // - "local": collection is enabled, but uploading is disabled 56 // - "off": both collection and uploading are disabled 57 // 58 // When mode is "on", or "local", telemetry data is written to the local file 59 // system and may be inspected with the [gotelemetry] command. 60 // 61 // If an error occurs while reading the telemetry mode from the file system, 62 // Mode returns the default value "local". 63 // 64 // [gotelemetry]: https://pkg.go.dev/golang.org/x/telemetry/cmd/gotelemetry 65 func Mode() string { 66 return telemetry.Mode() 67 } 68 69 // SetMode sets the global telemetry mode to the given value. 70 // 71 // See the documentation of [Mode] for a description of the supported mode 72 // values. 73 // 74 // An error is returned if the provided mode value is invalid, or if an error 75 // occurs while persisting the mode value to the file system. 76 func SetMode(mode string) error { 77 return telemetry.SetMode(mode) 78 } 79 80 // Dir returns the telemetry directory. 81 func Dir() string { 82 return telemetry.Dir() 83 } 84