Skip to content

Commit fcf5463

Browse files
committed
gopls/internal/server: add counters to inform v0.17.0
Add two counters to help inform decisions for [email protected]: - Add a gopls/gotoolchain:{auto,local,other} counter to help us understand toolchain upgradability. - Add a gopls/telemetryprompt/accepted counter to track telemetry prompt acceptance. Fixes golang/go#68240 Change-Id: I8fc06b3a266761dbf7c2781267dfb1235eef1a63 Reviewed-on: https://go-review.googlesource.com/c/tools/+/595560 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent 70a59b2 commit fcf5463

File tree

6 files changed

+63
-7
lines changed

6 files changed

+63
-7
lines changed

Diff for: gopls/internal/cache/view.go

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type GoEnv struct {
6767
GOPRIVATE string
6868
GOFLAGS string
6969
GO111MODULE string
70+
GOTOOLCHAIN string
7071

7172
// Go version output.
7273
GoVersion int // The X in Go 1.X
@@ -992,6 +993,7 @@ func FetchGoEnv(ctx context.Context, folder protocol.DocumentURI, opts *settings
992993
"GOMODCACHE": &env.GOMODCACHE,
993994
"GOFLAGS": &env.GOFLAGS,
994995
"GO111MODULE": &env.GO111MODULE,
996+
"GOTOOLCHAIN": &env.GOTOOLCHAIN,
995997
}
996998
if err := loadGoEnv(ctx, dir, opts.EnvSlice(), runner, envvars); err != nil {
997999
return nil, err

Diff for: gopls/internal/server/general.go

+13
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,19 @@ func (s *server) newFolder(ctx context.Context, folder protocol.DocumentURI, nam
468468
if err != nil {
469469
return nil, err
470470
}
471+
472+
// Increment folder counters.
473+
switch {
474+
case env.GOTOOLCHAIN == "auto" || strings.Contains(env.GOTOOLCHAIN, "+auto"):
475+
counter.New("gopls/gotoolchain:auto").Inc()
476+
case env.GOTOOLCHAIN == "path" || strings.Contains(env.GOTOOLCHAIN, "+path"):
477+
counter.New("gopls/gotoolchain:path").Inc()
478+
case env.GOTOOLCHAIN == "local": // local+auto and local+path handled above
479+
counter.New("gopls/gotoolchain:local").Inc()
480+
default:
481+
counter.New("gopls/gotoolchain:other").Inc()
482+
}
483+
471484
return &cache.Folder{
472485
Dir: folder,
473486
Name: name,

Diff for: gopls/internal/server/prompt.go

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"golang.org/x/telemetry"
17+
"golang.org/x/telemetry/counter"
1718
"golang.org/x/tools/gopls/internal/protocol"
1819
"golang.org/x/tools/internal/event"
1920
)
@@ -308,6 +309,7 @@ Would you like to enable Go telemetry?
308309
result = pYes
309310
if err := s.setTelemetryMode("on"); err == nil {
310311
message(protocol.Info, telemetryOnMessage(s.Options().LinkifyShowMessage))
312+
counter.New("gopls/telemetryprompt/accepted").Inc()
311313
} else {
312314
errorf("enabling telemetry failed: %v", err)
313315
msg := fmt.Sprintf("Failed to enable Go telemetry: %v\nTo enable telemetry manually, please run `go run golang.org/x/telemetry/cmd/gotelemetry@latest on`", err)

Diff for: gopls/internal/telemetry/telemetry_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ import (
2626
)
2727

2828
func TestMain(m *testing.M) {
29-
tmp, err := os.MkdirTemp("", "gopls-telemetry-test")
29+
tmp, err := os.MkdirTemp("", "gopls-telemetry-test-counters")
3030
if err != nil {
3131
panic(err)
3232
}
3333
countertest.Open(tmp)
3434
code := Main(m)
35-
os.RemoveAll(tmp)
35+
os.RemoveAll(tmp) // golang/go#68243: ignore error; cleanup fails on Windows
3636
os.Exit(code)
3737
}
3838

@@ -54,6 +54,7 @@ func TestTelemetry(t *testing.T) {
5454
counter.New("gopls/client:" + editor),
5555
counter.New("gopls/goversion:1." + goversion),
5656
counter.New("fwd/vscode/linter:a"),
57+
counter.New("gopls/gotoolchain:local"),
5758
}
5859
initialCounts := make([]uint64, len(sessionCounters))
5960
for i, c := range sessionCounters {
@@ -70,6 +71,9 @@ func TestTelemetry(t *testing.T) {
7071
Modes(Default), // must be in-process to receive the bug report below
7172
Settings{"showBugReports": true},
7273
ClientName("Visual Studio Code"),
74+
EnvVars{
75+
"GOTOOLCHAIN": "local", // so that the local counter is incremented
76+
},
7377
).Run(t, "", func(_ *testing.T, env *Env) {
7478
goversion = strconv.Itoa(env.GoVersion())
7579
addForwardedCounters(env, []string{"vscode/linter:a"}, []int64{1})
@@ -93,6 +97,7 @@ func TestTelemetry(t *testing.T) {
9397
// gopls/editor:client
9498
// gopls/goversion:1.x
9599
// fwd/vscode/linter:a
100+
// gopls/gotoolchain:local
96101
for i, c := range sessionCounters {
97102
want := initialCounts[i] + 1
98103
got, err := countertest.ReadCounter(c)

Diff for: gopls/internal/test/integration/misc/misc_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,22 @@ import (
99
"strings"
1010
"testing"
1111

12+
"golang.org/x/telemetry/counter/countertest"
1213
"golang.org/x/tools/gopls/internal/protocol"
13-
"golang.org/x/tools/gopls/internal/test/integration"
1414
. "golang.org/x/tools/gopls/internal/test/integration"
1515
"golang.org/x/tools/gopls/internal/util/bug"
1616
)
1717

1818
func TestMain(m *testing.M) {
1919
bug.PanicOnBugs = true
20-
os.Exit(integration.Main(m))
20+
tmp, err := os.MkdirTemp("", "gopls-misc-test-counters")
21+
if err != nil {
22+
panic(err)
23+
}
24+
countertest.Open(tmp)
25+
code := Main(m)
26+
os.RemoveAll(tmp) // golang/go#68243: ignore error; cleanup fails on Windows
27+
os.Exit(code)
2128
}
2229

2330
// TestDocumentURIFix ensures that a DocumentURI supplied by the

Diff for: gopls/internal/test/integration/misc/prompt_test.go

+30-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"testing"
1414
"time"
1515

16+
"golang.org/x/telemetry/counter"
17+
"golang.org/x/telemetry/counter/countertest"
1618
"golang.org/x/tools/gopls/internal/protocol"
1719
"golang.org/x/tools/gopls/internal/protocol/command"
1820
"golang.org/x/tools/gopls/internal/server"
@@ -250,6 +252,10 @@ func main() {
250252

251253
// Test that responding to the telemetry prompt results in the expected state.
252254
func TestTelemetryPrompt_Response(t *testing.T) {
255+
if !countertest.SupportedPlatform {
256+
t.Skip("requires counter support")
257+
}
258+
253259
const src = `
254260
-- go.mod --
255261
module mod.com
@@ -262,18 +268,32 @@ func main() {
262268
}
263269
`
264270

271+
acceptanceCounterName := "gopls/telemetryprompt/accepted"
272+
acceptanceCounter := counter.New(acceptanceCounterName)
273+
// We must increment the acceptance counter in order for the initial read
274+
// below to succeed.
275+
//
276+
// TODO(rfindley): ReadCounter should simply return 0 for uninitialized
277+
// counters.
278+
acceptanceCounter.Inc()
279+
265280
tests := []struct {
266281
name string // subtest name
267282
response string // response to choose for the telemetry dialog
268283
wantMode string // resulting telemetry mode
269284
wantMsg string // substring contained in the follow-up popup (if empty, no popup is expected)
285+
wantInc uint64 // expected 'prompt accepted' counter increment
270286
}{
271-
{"yes", server.TelemetryYes, "on", "uploading is now enabled"},
272-
{"no", server.TelemetryNo, "", ""},
273-
{"empty", "", "", ""},
287+
{"yes", server.TelemetryYes, "on", "uploading is now enabled", 1},
288+
{"no", server.TelemetryNo, "", "", 0},
289+
{"empty", "", "", "", 0},
274290
}
275291
for _, test := range tests {
276292
t.Run(test.name, func(t *testing.T) {
293+
initialCount, err := countertest.ReadCounter(acceptanceCounter)
294+
if err != nil {
295+
t.Fatalf("ReadCounter(%q) failed: %v", acceptanceCounterName, err)
296+
}
277297
modeFile := filepath.Join(t.TempDir(), "mode")
278298
telemetryStartTime := time.Now().Add(-8 * 24 * time.Hour)
279299
msgRE := regexp.MustCompile(".*Would you like to enable Go telemetry?")
@@ -320,6 +340,13 @@ func main() {
320340
if gotMode != test.wantMode {
321341
t.Errorf("after prompt, mode=%s, want %s", gotMode, test.wantMode)
322342
}
343+
finalCount, err := countertest.ReadCounter(acceptanceCounter)
344+
if err != nil {
345+
t.Fatalf("ReadCounter(%q) failed: %v", acceptanceCounterName, err)
346+
}
347+
if gotInc := finalCount - initialCount; gotInc != test.wantInc {
348+
t.Errorf("%q mismatch: got %d, want %d", acceptanceCounterName, gotInc, test.wantInc)
349+
}
323350
})
324351
})
325352
}

0 commit comments

Comments
 (0)