golang.org/x/tools/gopls@v0.15.3/internal/telemetry/telemetry.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build go1.19
     6  // +build go1.19
     7  
     8  package telemetry
     9  
    10  import (
    11  	"fmt"
    12  
    13  	"golang.org/x/telemetry"
    14  	"golang.org/x/telemetry/counter"
    15  	"golang.org/x/telemetry/crashmonitor"
    16  	"golang.org/x/telemetry/upload"
    17  )
    18  
    19  // CounterOpen calls [counter.Open].
    20  func CounterOpen() {
    21  	counter.Open()
    22  }
    23  
    24  // StartCrashMonitor calls [crashmonitor.Start].
    25  func StartCrashMonitor() {
    26  	crashmonitor.Start()
    27  }
    28  
    29  // CrashMonitorSupported calls [crashmonitor.Supported].
    30  func CrashMonitorSupported() bool {
    31  	return crashmonitor.Supported()
    32  }
    33  
    34  // NewStackCounter calls [counter.NewStack].
    35  func NewStackCounter(name string, depth int) *counter.StackCounter {
    36  	return counter.NewStack(name, depth)
    37  }
    38  
    39  // Mode calls x/telemetry.Mode.
    40  func Mode() string {
    41  	return telemetry.Mode()
    42  }
    43  
    44  // SetMode calls x/telemetry.SetMode.
    45  func SetMode(mode string) error {
    46  	return telemetry.SetMode(mode)
    47  }
    48  
    49  // Upload starts a goroutine for telemetry upload.
    50  func Upload() {
    51  	go upload.Run(nil)
    52  }
    53  
    54  // RecordClientInfo records gopls client info.
    55  func RecordClientInfo(clientName string) {
    56  	key := "gopls/client:other"
    57  	switch clientName {
    58  	case "Visual Studio Code":
    59  		key = "gopls/client:vscode"
    60  	case "Visual Studio Code - Insiders":
    61  		key = "gopls/client:vscode-insiders"
    62  	case "VSCodium":
    63  		key = "gopls/client:vscodium"
    64  	case "code-server":
    65  		// https://github.com/coder/code-server/blob/3cb92edc76ecc2cfa5809205897d93d4379b16a6/ci/build/build-vscode.sh#L19
    66  		key = "gopls/client:code-server"
    67  	case "Eglot":
    68  		// https://lists.gnu.org/archive/html/bug-gnu-emacs/2023-03/msg00954.html
    69  		key = "gopls/client:eglot"
    70  	case "govim":
    71  		// https://github.com/govim/govim/pull/1189
    72  		key = "gopls/client:govim"
    73  	case "Neovim":
    74  		// https://github.com/neovim/neovim/blob/42333ea98dfcd2994ee128a3467dfe68205154cd/runtime/lua/vim/lsp.lua#L1361
    75  		key = "gopls/client:neovim"
    76  	case "coc.nvim":
    77  		// https://github.com/neoclide/coc.nvim/blob/3dc6153a85ed0f185abec1deb972a66af3fbbfb4/src/language-client/client.ts#L994
    78  		key = "gopls/client:coc.nvim"
    79  	case "Sublime Text LSP":
    80  		// https://github.com/sublimelsp/LSP/blob/e608f878e7e9dd34aabe4ff0462540fadcd88fcc/plugin/core/sessions.py#L493
    81  		key = "gopls/client:sublimetext"
    82  	default:
    83  		// Accumulate at least a local counter for an unknown
    84  		// client name, but also fall through to count it as
    85  		// ":other" for collection.
    86  		if clientName != "" {
    87  			counter.New(fmt.Sprintf("gopls/client-other:%s", clientName)).Inc()
    88  		}
    89  	}
    90  	counter.Inc(key)
    91  }
    92  
    93  // RecordViewGoVersion records the Go minor version number (1.x) used for a view.
    94  func RecordViewGoVersion(x int) {
    95  	if x < 0 {
    96  		return
    97  	}
    98  	name := fmt.Sprintf("gopls/goversion:1.%d", x)
    99  	counter.Inc(name)
   100  }
   101  
   102  // AddForwardedCounters adds the given counters on behalf of clients.
   103  // Names and values must have the same length.
   104  func AddForwardedCounters(names []string, values []int64) {
   105  	for i, n := range names {
   106  		v := values[i]
   107  		if n == "" || v < 0 {
   108  			continue // Should we report an error? Who is the audience?
   109  		}
   110  		counter.Add("fwd/"+n, v)
   111  	}
   112  }