| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/json" |
| 11 |
"flag" |
| 12 |
"fmt" |
| 13 |
"log" |
| 14 |
"os" |
| 15 |
"path/filepath" |
| 16 |
"strings" |
| 17 |
|
| 18 |
"golang.org/x/telemetry/cmd/gotelemetry/internal/csv" |
| 19 |
"golang.org/x/telemetry/cmd/gotelemetry/internal/view" |
| 20 |
"golang.org/x/telemetry/internal/counter" |
| 21 |
"golang.org/x/telemetry/internal/telemetry" |
| 22 |
"golang.org/x/telemetry/internal/upload" |
| 23 |
) |
| 24 |
|
| 25 |
type command struct { |
| 26 |
usage string |
| 27 |
short string |
| 28 |
long string |
| 29 |
flags *flag.FlagSet |
| 30 |
hasArgs bool |
| 31 |
run func([]string) |
| 32 |
} |
| 33 |
|
| 34 |
func (c command) name() string { |
| 35 |
name, _, _ := strings.Cut(c.usage, " ") |
| 36 |
return name |
| 37 |
} |
| 38 |
|
| 39 |
var ( |
| 40 |
viewFlags = flag.NewFlagSet("view", flag.ExitOnError) |
| 41 |
viewServer view.Server |
| 42 |
normalCommands = []*command{ |
| 43 |
{ |
| 44 |
usage: "on", |
| 45 |
short: "enable telemetry collection and uploading", |
| 46 |
long: `Gotelemetry on enables telemetry collection and uploading. |
| 47 |
|
| 48 |
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. |
| 49 |
|
| 50 |
For more details, see https://telemetry.go.dev/privacy. |
| 51 |
This data is collected in accordance with the Google Privacy Policy (https://policies.google.com/privacy). |
| 52 |
|
| 53 |
To disable telemetry uploading, but keep local data collection, run “gotelemetry local”. |
| 54 |
To disable both collection and uploading, run “gotelemetry off“. |
| 55 |
`, |
| 56 |
run: runOn, |
| 57 |
}, |
| 58 |
{ |
| 59 |
usage: "local", |
| 60 |
short: "enable telemetry collection but disable uploading", |
| 61 |
long: `Gotelemetry local enables telemetry collection but not uploading. |
| 62 |
|
| 63 |
When telemetry is in local mode, counter data is written to the local file system, but will not be uploaded to remote servers. |
| 64 |
|
| 65 |
To enable telemetry uploading, run “gotelemetry on”. |
| 66 |
To disable both collection and uploading, run “gotelemetry off”`, |
| 67 |
run: runLocal, |
| 68 |
}, |
| 69 |
{ |
| 70 |
usage: "off", |
| 71 |
short: "disable telemetry collection and uploading", |
| 72 |
long: `Gotelemetry off disables telemetry collection and uploading. |
| 73 |
|
| 74 |
When telemetry is disabled, local counter data is neither collected nor uploaded. |
| 75 |
|
| 76 |
To enable local collection (but not uploading) of telemetry data, run “gotelemetry local“. |
| 77 |
To enable both collection and uploading, run “gotelemetry on”.`, |
| 78 |
run: runOff, |
| 79 |
}, |
| 80 |
{ |
| 81 |
usage: "view [flags]", |
| 82 |
short: "run a web viewer for local telemetry data", |
| 83 |
long: `Gotelemetry view runs a web viewer for local telemetry data. |
| 84 |
|
| 85 |
This viewer displays charts for locally collected data, as well as information about the current upload configuration.`, |
| 86 |
flags: viewFlags, |
| 87 |
run: runView, |
| 88 |
}, |
| 89 |
{ |
| 90 |
usage: "env", |
| 91 |
short: "print the current telemetry environment", |
| 92 |
run: runEnv, |
| 93 |
}, |
| 94 |
{ |
| 95 |
usage: "clean", |
| 96 |
short: "remove all local telemetry data", |
| 97 |
long: `Gotelemetry clean removes locally collected counters and reports. |
| 98 |
|
| 99 |
Removing counter files that are currently in use may fail on some operating |
| 100 |
systems. |
| 101 |
|
| 102 |
Gotelemetry clean does not affect the current telemetry mode.`, |
| 103 |
run: runClean, |
| 104 |
}, |
| 105 |
} |
| 106 |
experimentalCommands = []*command{ |
| 107 |
{ |
| 108 |
usage: "csv", |
| 109 |
short: "print all known counters", |
| 110 |
run: runCSV, |
| 111 |
}, |
| 112 |
{ |
| 113 |
usage: "dump [files]", |
| 114 |
short: "view counter file data", |
| 115 |
run: runDump, |
| 116 |
hasArgs: true, |
| 117 |
}, |
| 118 |
{ |
| 119 |
usage: "upload", |
| 120 |
short: "run upload with logging enabled", |
| 121 |
run: runUpload, |
| 122 |
}, |
| 123 |
} |
| 124 |
) |
| 125 |
|
| 126 |
func init() { |
| 127 |
viewFlags.StringVar(&viewServer.Addr, "addr", "localhost:4040", "server listens on the given TCP network address") |
| 128 |
viewFlags.BoolVar(&viewServer.Dev, "dev", false, "rebuild static assets on save") |
| 129 |
viewFlags.StringVar(&viewServer.FsConfig, "config", "", "load a config from the filesystem") |
| 130 |
viewFlags.BoolVar(&viewServer.Open, "open", true, "open the browser to the server address") |
| 131 |
|
| 132 |
for _, cmd := range append(normalCommands, experimentalCommands...) { |
| 133 |
name := cmd.name() |
| 134 |
if cmd.flags == nil { |
| 135 |
cmd.flags = flag.NewFlagSet(name, flag.ExitOnError) |
| 136 |
} |
| 137 |
cmd.flags.Usage = func() { |
| 138 |
help(name) |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func output(msgs ...any) { |
| 144 |
fmt.Fprintln(flag.CommandLine.Output(), msgs...) |
| 145 |
} |
| 146 |
|
| 147 |
func usage() { |
| 148 |
printCommand := func(cmd *command) { |
| 149 |
output(fmt.Sprintf("\t%s\t%s", cmd.name(), cmd.short)) |
| 150 |
} |
| 151 |
output("Gotelemetry is a tool for managing Go telemetry data and settings.") |
| 152 |
output() |
| 153 |
output("Usage:") |
| 154 |
output() |
| 155 |
output("\tgotelemetry <command> [arguments]") |
| 156 |
output() |
| 157 |
output("The commands are:") |
| 158 |
output() |
| 159 |
for _, cmd := range normalCommands { |
| 160 |
printCommand(cmd) |
| 161 |
} |
| 162 |
output() |
| 163 |
output("Use \"gotelemetry help <command>\" for details about any command.") |
| 164 |
output() |
| 165 |
output("The following additional commands are available for diagnostic") |
| 166 |
output("purposes, and may change or be removed in the future:") |
| 167 |
output() |
| 168 |
for _, cmd := range experimentalCommands { |
| 169 |
printCommand(cmd) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func failf(format string, args ...any) { |
| 174 |
fmt.Fprintf(os.Stderr, format, args...) |
| 175 |
os.Exit(1) |
| 176 |
} |
| 177 |
|
| 178 |
func warnf(format string, args ...any) { |
| 179 |
fmt.Fprintf(os.Stderr, "Warning: "+format+"\n", args...) |
| 180 |
} |
| 181 |
|
| 182 |
func findCommand(name string) *command { |
| 183 |
for _, cmd := range append(normalCommands, experimentalCommands...) { |
| 184 |
if cmd.name() == name { |
| 185 |
return cmd |
| 186 |
} |
| 187 |
} |
| 188 |
return nil |
| 189 |
} |
| 190 |
|
| 191 |
func help(name string) { |
| 192 |
cmd := findCommand(name) |
| 193 |
if cmd == nil { |
| 194 |
failf("unknown command %q", name) |
| 195 |
} |
| 196 |
output(fmt.Sprintf("Usage: gotelemetry %s", cmd.usage)) |
| 197 |
output() |
| 198 |
if cmd.long != "" { |
| 199 |
output(cmd.long) |
| 200 |
} else { |
| 201 |
output(fmt.Sprintf("Gotelemetry %s is used to %s.", cmd.name(), cmd.short)) |
| 202 |
} |
| 203 |
anyflags := false |
| 204 |
cmd.flags.VisitAll(func(*flag.Flag) { |
| 205 |
anyflags = true |
| 206 |
}) |
| 207 |
if anyflags { |
| 208 |
output() |
| 209 |
output("Flags:") |
| 210 |
output() |
| 211 |
cmd.flags.PrintDefaults() |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func runOn(_ []string) { |
| 216 |
if old, _ := telemetry.Default.Mode(); old == "on" { |
| 217 |
return |
| 218 |
} |
| 219 |
if err := telemetry.Default.SetMode("on"); err != nil { |
| 220 |
failf("Failed to enable telemetry: %v", err) |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
fmt.Fprintln(os.Stderr, telemetryOnMessage()) |
| 226 |
} |
| 227 |
|
| 228 |
func telemetryOnMessage() string { |
| 229 |
return `Telemetry uploading is now enabled and data will be 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. |
| 230 |
|
| 231 |
For more details, see https://telemetry.go.dev/privacy. |
| 232 |
This data is collected in accordance with the Google Privacy Policy (https://policies.google.com/privacy). |
| 233 |
|
| 234 |
To disable telemetry uploading, but keep local data collection, run “gotelemetry local”. |
| 235 |
To disable both collection and uploading, run “gotelemetry off“.` |
| 236 |
} |
| 237 |
|
| 238 |
func runLocal(_ []string) { |
| 239 |
if old, _ := telemetry.Default.Mode(); old == "local" { |
| 240 |
return |
| 241 |
} |
| 242 |
if err := telemetry.Default.SetMode("local"); err != nil { |
| 243 |
failf("Failed to set the telemetry mode to local: %v", err) |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
func runOff(_ []string) { |
| 248 |
if old, _ := telemetry.Default.Mode(); old == "off" { |
| 249 |
return |
| 250 |
} |
| 251 |
if err := telemetry.Default.SetMode("off"); err != nil { |
| 252 |
failf("Failed to disable telemetry: %v", err) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
func runView(_ []string) { |
| 257 |
viewServer.Serve() |
| 258 |
} |
| 259 |
|
| 260 |
func runEnv(_ []string) { |
| 261 |
m, t := telemetry.Default.Mode() |
| 262 |
fmt.Printf("mode: %s %s\n", m, t) |
| 263 |
fmt.Println() |
| 264 |
fmt.Println("modefile:", telemetry.Default.ModeFile()) |
| 265 |
fmt.Println("localdir:", telemetry.Default.LocalDir()) |
| 266 |
fmt.Println("uploaddir:", telemetry.Default.UploadDir()) |
| 267 |
} |
| 268 |
|
| 269 |
func runClean(_ []string) { |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
for dir, suffixes := range map[string][]string{ |
| 274 |
telemetry.Default.LocalDir(): {"." + counter.FileVersion + ".count", ".json"}, |
| 275 |
telemetry.Default.UploadDir(): {".json"}, |
| 276 |
} { |
| 277 |
entries, err := os.ReadDir(dir) |
| 278 |
if err != nil { |
| 279 |
if !os.IsNotExist(err) { |
| 280 |
warnf("failed to read telemetry dir: %v", err) |
| 281 |
} |
| 282 |
continue |
| 283 |
} |
| 284 |
for _, entry := range entries { |
| 285 |
|
| 286 |
|
| 287 |
remove := false |
| 288 |
for _, suffix := range suffixes { |
| 289 |
if strings.HasSuffix(entry.Name(), suffix) { |
| 290 |
remove = true |
| 291 |
break |
| 292 |
} |
| 293 |
} |
| 294 |
if remove { |
| 295 |
path := filepath.Join(dir, entry.Name()) |
| 296 |
if err := os.Remove(path); err != nil { |
| 297 |
warnf("failed to remove %s: %v", path, err) |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
func runCSV(_ []string) { |
| 305 |
csv.Csv() |
| 306 |
} |
| 307 |
|
| 308 |
func runDump(args []string) { |
| 309 |
if len(args) == 0 { |
| 310 |
localdir := telemetry.Default.LocalDir() |
| 311 |
fi, err := os.ReadDir(localdir) |
| 312 |
if err != nil && len(args) == 0 { |
| 313 |
log.Fatal(err) |
| 314 |
} |
| 315 |
for _, f := range fi { |
| 316 |
args = append(args, filepath.Join(localdir, f.Name())) |
| 317 |
} |
| 318 |
} |
| 319 |
for _, file := range args { |
| 320 |
if !strings.HasSuffix(file, ".count") { |
| 321 |
log.Printf("%s: not a counter file, skipping", file) |
| 322 |
continue |
| 323 |
} |
| 324 |
data, err := os.ReadFile(file) |
| 325 |
if err != nil { |
| 326 |
log.Printf("%v, skipping", err) |
| 327 |
continue |
| 328 |
} |
| 329 |
f, err := counter.Parse(file, data) |
| 330 |
if err != nil { |
| 331 |
log.Printf("%v, skipping", err) |
| 332 |
continue |
| 333 |
} |
| 334 |
js, err := json.MarshalIndent(f, "", "\t") |
| 335 |
if err != nil { |
| 336 |
log.Printf("%s: failed to print - %v", file, err) |
| 337 |
} |
| 338 |
fmt.Printf("-- %v --\n%s\n", file, js) |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
func runUpload(_ []string) { |
| 343 |
if err := upload.Run(upload.RunConfig{ |
| 344 |
LogWriter: os.Stderr, |
| 345 |
}); err != nil { |
| 346 |
fmt.Printf("Upload failed: %v\n", err) |
| 347 |
} else { |
| 348 |
fmt.Println("Upload completed.") |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
func main() { |
| 353 |
log.SetFlags(0) |
| 354 |
flag.Usage = usage |
| 355 |
flag.Parse() |
| 356 |
|
| 357 |
args := flag.Args() |
| 358 |
if flag.NArg() == 0 { |
| 359 |
flag.Usage() |
| 360 |
os.Exit(2) |
| 361 |
} |
| 362 |
|
| 363 |
if args[0] == "help" { |
| 364 |
flag.CommandLine.SetOutput(os.Stdout) |
| 365 |
switch len(args) { |
| 366 |
case 1: |
| 367 |
flag.Usage() |
| 368 |
case 2: |
| 369 |
help(args[1]) |
| 370 |
default: |
| 371 |
flag.Usage() |
| 372 |
failf("too many arguments to \"help\"") |
| 373 |
} |
| 374 |
os.Exit(0) |
| 375 |
} |
| 376 |
|
| 377 |
cmd := findCommand(args[0]) |
| 378 |
if cmd == nil { |
| 379 |
flag.Usage() |
| 380 |
os.Exit(2) |
| 381 |
} |
| 382 |
|
| 383 |
cmd.flags.Parse(args[1:]) |
| 384 |
args = cmd.flags.Args() |
| 385 |
if !cmd.hasArgs && len(args) > 0 { |
| 386 |
help(cmd.name()) |
| 387 |
failf("command %s does not accept any arguments", cmd.name()) |
| 388 |
} |
| 389 |
cmd.run(args) |
| 390 |
} |