Skip to content

Commit

Permalink
gopls: add gopls/client telemetry counters
Browse files Browse the repository at this point in the history
These counters are incremented once per session
when the LSP initialize message is received.

This cl adds a new dependency on x/telemetry.

Note that writing to the disk will be enabled when
GOPLS_TELEMETRY_EXP is set. We plan to remove this
condition once we are ready.

Updates golang/go#61038

Change-Id: Ibb8ebbd039ab5ffbaa869dee01bee0ba5450f350
Reviewed-on: https://go-review.googlesource.com/c/tools/+/506635
Reviewed-by: Jamal Carvalho <jamal@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
  • Loading branch information
hyangah committed Aug 1, 2023
1 parent d0b18e2 commit 4b271f9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions gopls/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
golang.org/x/mod v0.12.0
golang.org/x/sync v0.3.0
golang.org/x/sys v0.10.0
golang.org/x/telemetry v0.0.0-20230728182230-e84a26264b60
golang.org/x/text v0.11.0
golang.org/x/tools v0.6.0
golang.org/x/vuln v0.0.0-20230110180137-6ad3e3d07815
Expand Down
3 changes: 3 additions & 0 deletions gopls/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
Expand All @@ -74,6 +75,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.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/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.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o=
Expand Down
3 changes: 3 additions & 0 deletions gopls/internal/lsp/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/source"
"golang.org/x/tools/gopls/internal/span"
"golang.org/x/tools/gopls/internal/telemetry"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/jsonrpc2"
)
Expand All @@ -30,6 +31,8 @@ func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitializ
ctx, done := event.Start(ctx, "lsp.Server.initialize")
defer done()

telemetry.RecordClientInfo(params)

s.stateMu.Lock()
if s.state >= serverInitializing {
defer s.stateMu.Unlock()
Expand Down
52 changes: 52 additions & 0 deletions gopls/internal/telemetry/telemetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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.

package telemetry

import (
"os"

"golang.org/x/telemetry/counter"
"golang.org/x/tools/gopls/internal/lsp/protocol"
)

// Start starts telemetry instrumentation.
func Start() {
if os.Getenv("GOPLS_TELEMETRY_EXP") != "" {
counter.Open()
// TODO: add upload logic.
}
}

// RecordClientInfo records gopls client info.
func RecordClientInfo(params *protocol.ParamInitialize) {
client := "gopls/client:other"
if params != nil && params.ClientInfo != nil {
switch params.ClientInfo.Name {
case "Visual Studio Code":
client = "gopls/client:vscode"
case "VSCodium":
client = "gopls/client:vscodium"
case "code-server":
// https://github.com/coder/code-server/blob/3cb92edc76ecc2cfa5809205897d93d4379b16a6/ci/build/build-vscode.sh#L19
client = "gopls/client:code-server"
case "Eglot":
// https://lists.gnu.org/archive/html/bug-gnu-emacs/2023-03/msg00954.html
client = "gopls/client:eglot"
case "govim":
// https://github.com/govim/govim/pull/1189
client = "gopls/client:govim"
case "Neovim":
// https://github.com/neovim/neovim/blob/42333ea98dfcd2994ee128a3467dfe68205154cd/runtime/lua/vim/lsp.lua#L1361
client = "gopls/client:neovim"
case "coc.nvim":
// https://github.com/neoclide/coc.nvim/blob/3dc6153a85ed0f185abec1deb972a66af3fbbfb4/src/language-client/client.ts#L994
client = "gopls/client:coc.nvim"
case "Sublime Text LSP":
// https://github.com/sublimelsp/LSP/blob/e608f878e7e9dd34aabe4ff0462540fadcd88fcc/plugin/core/sessions.py#L493
client = "gopls/client:sublimetext"
}
}
counter.Inc(client)
}
2 changes: 2 additions & 0 deletions gopls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import (

"golang.org/x/tools/gopls/internal/hooks"
"golang.org/x/tools/gopls/internal/lsp/cmd"
"golang.org/x/tools/gopls/internal/telemetry"
"golang.org/x/tools/internal/tool"
)

func main() {
telemetry.Start()
ctx := context.Background()
tool.Main(ctx, cmd.New("gopls", "", nil, hooks.Options), os.Args[1:])
}

0 comments on commit 4b271f9

Please sign in to comment.