1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
// 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 (
"context"
"errors"
"os"
"strconv"
"strings"
"testing"
"time"
"golang.org/x/telemetry/counter"
"golang.org/x/telemetry/counter/countertest" // requires go1.21+
"golang.org/x/tools/gopls/internal/protocol"
"golang.org/x/tools/gopls/internal/protocol/command"
"golang.org/x/tools/gopls/internal/telemetry"
. "golang.org/x/tools/gopls/internal/test/integration"
"golang.org/x/tools/gopls/internal/util/bug"
)
func TestMain(m *testing.M) {
tmp, err := os.MkdirTemp("", "gopls-telemetry-test-counters")
if err != nil {
panic(err)
}
countertest.Open(tmp)
code := Main(m)
os.RemoveAll(tmp) // golang/go#68243: ignore error; cleanup fails on Windows
os.Exit(code)
}
func TestTelemetry(t *testing.T) {
var (
goversion = ""
editor = "vscode" // We set ClientName("Visual Studio Code") below.
)
// Run gopls once to determine the Go version.
WithOptions(
Modes(Default),
).Run(t, "", func(_ *testing.T, env *Env) {
goversion = strconv.Itoa(env.GoVersion())
})
// counters that should be incremented once per session
sessionCounters := []*counter.Counter{
counter.New("gopls/client:" + editor),
counter.New("gopls/goversion:1." + goversion),
counter.New("fwd/vscode/linter:a"),
counter.New("gopls/gotoolchain:local"),
}
initialCounts := make([]uint64, len(sessionCounters))
for i, c := range sessionCounters {
count, err := countertest.ReadCounter(c)
if err != nil {
continue // counter db not open, or counter not found
}
initialCounts[i] = count
}
// 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"),
EnvVars{
"GOTOOLCHAIN": "local", // so that the local counter is incremented
},
).Run(t, "", func(_ *testing.T, env *Env) {
goversion = strconv.Itoa(env.GoVersion())
addForwardedCounters(env, []string{"vscode/linter:a"}, []int64{1})
const desc = "got a bug"
// This will increment a counter named something like:
//
// `gopls/bug
// golang.org/x/tools/gopls/internal/util/bug.report:+35
// golang.org/x/tools/gopls/internal/util/bug.Report:=68
// golang.org/x/tools/gopls/internal/telemetry_test.TestTelemetry.func2:+4
// golang.org/x/tools/gopls/internal/test/integration.(*Runner).Run.func1:+87
// testing.tRunner:+150
// runtime.goexit:+0`
//
bug.Report(desc) // want a stack counter with the trace starting from here.
env.Await(ShownMessage(desc))
})
// gopls/editor:client
// gopls/goversion:1.x
// fwd/vscode/linter:a
// gopls/gotoolchain:local
for i, c := range sessionCounters {
want := initialCounts[i] + 1
got, err := countertest.ReadCounter(c)
if err != nil || got != want {
t.Errorf("ReadCounter(%q) = (%v, %v), want (%v, nil)", c.Name(), got, err, want)
t.Logf("Current timestamp = %v", time.Now().UTC())
}
}
// 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)
t.Logf("Current timestamp = %v", time.Now().UTC())
}
}
func addForwardedCounters(env *Env, names []string, values []int64) {
args, err := command.MarshalArgs(command.AddTelemetryCountersArgs{
Names: names, Values: values,
})
if err != nil {
env.T.Fatal(err)
}
var res error
env.ExecuteCommand(&protocol.ExecuteCommandParams{
Command: command.AddTelemetryCounters.String(),
Arguments: args,
}, &res)
if res != nil {
env.T.Errorf("%v failed - %v", command.AddTelemetryCounters, res)
}
}
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
}
func TestLatencyCounter(t *testing.T) {
const operation = "TestLatencyCounter" // a unique operation name
stop := telemetry.StartLatencyTimer(operation)
stop(context.Background(), nil)
for isError, want := range map[bool]uint64{false: 1, true: 0} {
if got := totalLatencySamples(t, operation, isError); got != want {
t.Errorf("totalLatencySamples(operation=%v, isError=%v) = %d, want %d", operation, isError, got, want)
}
}
}
func TestLatencyCounter_Error(t *testing.T) {
const operation = "TestLatencyCounter_Error" // a unique operation name
stop := telemetry.StartLatencyTimer(operation)
stop(context.Background(), errors.New("bad"))
for isError, want := range map[bool]uint64{false: 0, true: 1} {
if got := totalLatencySamples(t, operation, isError); got != want {
t.Errorf("totalLatencySamples(operation=%v, isError=%v) = %d, want %d", operation, isError, got, want)
}
}
}
func TestLatencyCounter_Cancellation(t *testing.T) {
const operation = "TestLatencyCounter_Cancellation"
stop := telemetry.StartLatencyTimer(operation)
ctx, cancel := context.WithCancel(context.Background())
cancel()
stop(ctx, nil)
for isError, want := range map[bool]uint64{false: 0, true: 0} {
if got := totalLatencySamples(t, operation, isError); got != want {
t.Errorf("totalLatencySamples(operation=%v, isError=%v) = %d, want %d", operation, isError, got, want)
}
}
}
func totalLatencySamples(t *testing.T, operation string, isError bool) uint64 {
var total uint64
telemetry.ForEachLatencyCounter(operation, isError, func(c *counter.Counter) {
count, err := countertest.ReadCounter(c)
if err != nil {
t.Errorf("ReadCounter(%s) failed: %v", c.Name(), err)
} else {
total += count
}
})
return total
}
func TestLatencyInstrumentation(t *testing.T) {
const files = `
-- go.mod --
module mod.test/a
go 1.18
-- a.go --
package a
func _() {
x := 0
_ = x
}
`
// 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
).Run(t, files, func(_ *testing.T, env *Env) {
env.OpenFile("a.go")
before := totalLatencySamples(t, "completion", false)
loc := env.RegexpSearch("a.go", "x")
for i := 0; i < 10; i++ {
env.Completion(loc)
}
after := totalLatencySamples(t, "completion", false)
if after-before < 10 {
t.Errorf("after 10 completions, completion counter went from %d to %d", before, after)
}
})
}
|