Skip to content

Commit

Permalink
gopls/telemetry: test that telemetry counters are written
Browse files Browse the repository at this point in the history
Change-Id: Iceb8406cf3290180690f29bcba9f2fe6019285ad
Reviewed-on: https://go-review.googlesource.com/c/tools/+/517135
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Hyang-Ah Hana Kim <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
  • Loading branch information
hyangah committed Aug 9, 2023
1 parent 5027187 commit 053d3c4
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gopls/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
golang.org/x/mod v0.12.0
golang.org/x/sync v0.3.0
golang.org/x/sys v0.11.0
golang.org/x/telemetry v0.0.0-20230728182230-e84a26264b60
golang.org/x/telemetry v0.0.0-20230808152233-a65b40c0fdb0
golang.org/x/text v0.12.0
golang.org/x/tools v0.6.0
golang.org/x/vuln v0.0.0-20230110180137-6ad3e3d07815
Expand Down
4 changes: 4 additions & 0 deletions gopls/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/telemetry v0.0.0-20230728182230-e84a26264b60 h1:OCiXqf7/gdoaS7dKppAtPxi783Ke/JIb+r20ZYGiEFg=
golang.org/x/telemetry v0.0.0-20230728182230-e84a26264b60/go.mod h1:kO7uNSGGmqCHII6C0TYfaLwSBIfcyhj53//nu0+Fy4A=
golang.org/x/telemetry v0.0.0-20230803164656-36ff770d3d6b h1:FZUooIb6Dx+mzx9n5mi6wmY/xpUZ4U1ffUVX1DCsuSs=
golang.org/x/telemetry v0.0.0-20230803164656-36ff770d3d6b/go.mod h1:kO7uNSGGmqCHII6C0TYfaLwSBIfcyhj53//nu0+Fy4A=
golang.org/x/telemetry v0.0.0-20230808152233-a65b40c0fdb0 h1:ZB9hzIbPBkRCCOVWOmfZEI5f6YiTbRAq6LK2x/StgiU=
golang.org/x/telemetry v0.0.0-20230808152233-a65b40c0fdb0/go.mod h1:kO7uNSGGmqCHII6C0TYfaLwSBIfcyhj53//nu0+Fy4A=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
Expand Down
5 changes: 3 additions & 2 deletions gopls/internal/bug/bug.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func Report(description string) {
report(description)
}

var bugReport = counter.NewStack("gopls/bug", 16)
// BugReportCount is a telemetry counter that tracks # of bug reports.
var BugReportCount = counter.NewStack("gopls/bug", 16)

func report(description string) {
_, file, line, ok := runtime.Caller(2) // all exported reporting functions call report directly
Expand Down Expand Up @@ -102,7 +103,7 @@ func report(description string) {
mu.Unlock()

if newBug {
bugReport.Inc()
BugReportCount.Inc()
}
// Call the handlers outside the critical section since a
// handler may itself fail and call bug.Report. Since handlers
Expand Down
82 changes: 82 additions & 0 deletions gopls/internal/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2023 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 go1.21 && !openbsd && !js && !wasip1 && !solaris && !android && !386
// +build go1.21,!openbsd,!js,!wasip1,!solaris,!android,!386

package telemetry_test

import (
"os"
"strconv"
"strings"
"testing"

"golang.org/x/telemetry/counter"
"golang.org/x/telemetry/counter/countertest" // requires go1.21+
"golang.org/x/tools/gopls/internal/bug"
"golang.org/x/tools/gopls/internal/hooks"
. "golang.org/x/tools/gopls/internal/lsp/regtest"
)

func TestMain(m *testing.M) {
tmp, err := os.MkdirTemp("", "gopls-telemetry-test")
if err != nil {
panic(err)
}
countertest.Open(tmp)
defer os.RemoveAll(tmp)
Main(m, hooks.Options)
}

func TestTelemetry(t *testing.T) {
var (
goversion = ""
editor = "vscode" // We set ClientName("Visual Studio Code") below.
)

// Verify that a properly configured session gets notified of a bug on the
// server.
WithOptions(
Modes(Default), // must be in-process to receive the bug report below
Settings{"showBugReports": true},
ClientName("Visual Studio Code"),
).Run(t, "", func(t *testing.T, env *Env) {
goversion = strconv.Itoa(env.GoVersion())
const desc = "got a bug"
bug.Report(desc) // want a stack counter with the trace starting from here.
env.Await(ShownMessage(desc))
})

// gopls/editor:client
// gopls/goversion:1.x
for _, c := range []*counter.Counter{
counter.New("gopls/client:" + editor),
counter.New("gopls/goversion:1." + goversion),
} {
count, err := countertest.ReadCounter(c)
if err != nil || count != 1 {
t.Errorf("ReadCounter(%q) = (%v, %v), want (1, nil)", c.Name(), count, err)
}
}

// gopls/bug
bugcount := bug.BugReportCount
counts, err := countertest.ReadStackCounter(bugcount)
if err != nil {
t.Fatalf("ReadStackCounter(bugreportcount) failed - %v", err)
}
if len(counts) != 1 || !hasEntry(counts, t.Name(), 1) {
t.Errorf("read stackcounter(%q) = (%#v, %v), want one entry", "gopls/bug", counts, err)
}
}

func hasEntry(counts map[string]uint64, pattern string, want uint64) bool {
for k, v := range counts {
if strings.Contains(k, pattern) && v == want {
return true
}
}
return false
}

0 comments on commit 053d3c4

Please sign in to comment.