| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package server |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"math/rand" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"strconv" |
| 14 |
"testing" |
| 15 |
"time" |
| 16 |
|
| 17 |
"golang.org/x/telemetry" |
| 18 |
"golang.org/x/telemetry/counter" |
| 19 |
"golang.org/x/tools/gopls/internal/protocol" |
| 20 |
"golang.org/x/tools/internal/event" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
const promptTimeout = 24 * time.Hour |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
const gracePeriod = 7 * 24 * time.Hour |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
e] |
| 38 |
|
| 39 |
const samplesPerMille = 10 |
| 40 |
|
| 41 |
|
| 42 |
const ( |
| 43 |
TelemetryPromptWorkTitle = "Checking telemetry prompt" |
| 44 |
GoplsConfigDirEnvvar = "GOPLS_CONFIG_DIR" |
| 45 |
FakeTelemetryModefileEnvvar = "GOPLS_FAKE_TELEMETRY_MODEFILE" |
| 46 |
FakeSamplesPerMille = "GOPLS_FAKE_SAMPLES_PER_MILLE" |
| 47 |
TelemetryYes = "Yes, I'd like to help." |
| 48 |
TelemetryNo = "No, thanks." |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
const ( |
| 54 |
GoTelemetryGoplsClientStartTimeEnvvar = "GOTELEMETRY_GOPLS_CLIENT_START_TIME" |
| 55 |
GoTelemetryGoplsClientTokenEnvvar = "GOTELEMETRY_GOPLS_CLIENT_TOKEN" |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
func (s *server) getenv(key string) string { |
| 62 |
if v, ok := s.Options().Env[key]; ok { |
| 63 |
return v |
| 64 |
} |
| 65 |
return os.Getenv(key) |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
func (s *server) telemetryMode() string { |
| 71 |
if fake := s.getenv(FakeTelemetryModefileEnvvar); fake != "" { |
| 72 |
if data, err := os.ReadFile(fake); err == nil { |
| 73 |
return string(data) |
| 74 |
} |
| 75 |
return "local" |
| 76 |
} |
| 77 |
return telemetry.Mode() |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
func (s *server) setTelemetryMode(mode string) error { |
| 84 |
if fake := s.getenv(FakeTelemetryModefileEnvvar); fake != "" { |
| 85 |
return os.WriteFile(fake, []byte(mode), 0666) |
| 86 |
} |
| 87 |
return telemetry.SetMode(mode) |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
func (s *server) maybePromptForTelemetry(ctx context.Context, enabled bool) { |
| 99 |
if s.Options().VerboseWorkDoneProgress { |
| 100 |
work := s.progress.Start(ctx, TelemetryPromptWorkTitle, "Checking if gopls should prompt about telemetry...", nil, nil) |
| 101 |
defer work.End(ctx, "Done.") |
| 102 |
} |
| 103 |
|
| 104 |
errorf := func(format string, args ...any) { |
| 105 |
err := fmt.Errorf(format, args...) |
| 106 |
event.Error(ctx, "telemetry prompt failed", err) |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
configDir := s.getenv(GoplsConfigDirEnvvar) |
| 111 |
if configDir == "" && testing.Testing() { |
| 112 |
|
| 113 |
|
| 114 |
return |
| 115 |
} |
| 116 |
if configDir == "" { |
| 117 |
userDir, err := os.UserConfigDir() |
| 118 |
if err != nil { |
| 119 |
errorf("unable to determine user config dir: %v", err) |
| 120 |
return |
| 121 |
} |
| 122 |
configDir = filepath.Join(userDir, "gopls") |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
var ( |
| 128 |
promptDir = filepath.Join(configDir, "prompt") |
| 129 |
promptFile = filepath.Join(promptDir, "telemetry") |
| 130 |
) |
| 131 |
|
| 132 |
|
| 133 |
const ( |
| 134 |
pUnknown = "" |
| 135 |
pNotReady = "-" |
| 136 |
pYes = "yes" |
| 137 |
pNo = "no" |
| 138 |
pPending = "pending" |
| 139 |
pFailed = "failed" |
| 140 |
) |
| 141 |
validStates := map[string]bool{ |
| 142 |
pNotReady: true, |
| 143 |
pYes: true, |
| 144 |
pNo: true, |
| 145 |
pPending: true, |
| 146 |
pFailed: true, |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
var ( |
| 151 |
state = pUnknown |
| 152 |
attempts = 0 |
| 153 |
|
| 154 |
|
| 155 |
token = 0 |
| 156 |
creationTime int64 |
| 157 |
) |
| 158 |
if content, err := os.ReadFile(promptFile); err == nil { |
| 159 |
if n, _ := fmt.Sscanf(string(content), "%s %d %d %d", &state, &attempts, &creationTime, &token); (n == 2 || n == 4) && validStates[state] { |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
} else { |
| 164 |
state, attempts, creationTime, token = pUnknown, 0, 0, 0 |
| 165 |
|
| 166 |
errorf("malformed prompt result %q", string(content)) |
| 167 |
} |
| 168 |
} else if !os.IsNotExist(err) { |
| 169 |
errorf("reading prompt file: %v", err) |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
counter.New("gopls/telemetryprompt/corrupted").Inc() |
| 176 |
return |
| 177 |
} |
| 178 |
|
| 179 |
counter.New(fmt.Sprintf("gopls/telemetryprompt/attempts:%d", attempts)).Inc() |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
if state == pYes { |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
counter.New("gopls/telemetryprompt/accepted").Inc() |
| 192 |
return |
| 193 |
} |
| 194 |
if state == pNo { |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
counter.New("gopls/telemetryprompt/declined").Inc() |
| 201 |
return |
| 202 |
} |
| 203 |
if attempts >= 5 { |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
counter.New("gopls/telemetryprompt/expired").Inc() |
| 208 |
return |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
if !enabled { |
| 215 |
return |
| 216 |
} |
| 217 |
|
| 218 |
if s.telemetryMode() == "on" || s.telemetryMode() == "off" { |
| 219 |
|
| 220 |
return |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
if state == pUnknown { |
| 225 |
|
| 226 |
if err := os.MkdirAll(promptDir, 0777); err != nil { |
| 227 |
errorf("creating prompt dir: %v", err) |
| 228 |
return |
| 229 |
} |
| 230 |
state = pNotReady |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
if creationTime == 0 { |
| 235 |
creationTime = time.Now().Unix() |
| 236 |
if v := s.getenv(GoTelemetryGoplsClientStartTimeEnvvar); v != "" { |
| 237 |
if sec, err := strconv.ParseInt(v, 10, 64); err == nil && sec > 0 { |
| 238 |
creationTime = sec |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
if token == 0 { |
| 243 |
token = rand.Intn(1000) + 1 |
| 244 |
if v := s.getenv(GoTelemetryGoplsClientTokenEnvvar); v != "" { |
| 245 |
if tok, err := strconv.Atoi(v); err == nil && 1 <= tok && tok <= 1000 { |
| 246 |
token = tok |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
if state == pNotReady { |
| 253 |
threshold := samplesPerMille |
| 254 |
if v := s.getenv(FakeSamplesPerMille); v != "" { |
| 255 |
if t, err := strconv.Atoi(v); err == nil { |
| 256 |
threshold = t |
| 257 |
} |
| 258 |
} |
| 259 |
if token <= threshold && time.Now().Unix()-creationTime > gracePeriod.Milliseconds()/1000 { |
| 260 |
state = pPending |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
release, ok, err := acquireLockFile(promptFile) |
| 272 |
if err != nil { |
| 273 |
errorf("acquiring prompt: %v", err) |
| 274 |
return |
| 275 |
} |
| 276 |
if !ok { |
| 277 |
|
| 278 |
return |
| 279 |
} |
| 280 |
defer release() |
| 281 |
|
| 282 |
if state != pNotReady { |
| 283 |
attempts++ |
| 284 |
} |
| 285 |
|
| 286 |
pendingContent := fmt.Appendf(nil, "%s %d %d %d", state, attempts, creationTime, token) |
| 287 |
if err := os.WriteFile(promptFile, pendingContent, 0666); err != nil { |
| 288 |
errorf("writing pending state: %v", err) |
| 289 |
return |
| 290 |
} |
| 291 |
|
| 292 |
if state == pNotReady { |
| 293 |
return |
| 294 |
} |
| 295 |
|
| 296 |
var prompt = `Go telemetry helps us improve Go by periodically sending anonymous metrics and crash reports to the Go team. Learn more at https://go.dev/doc/telemetry. |
| 297 |
|
| 298 |
Would you like to enable Go telemetry? |
| 299 |
` |
| 300 |
if s.Options().LinkifyShowMessage { |
| 301 |
prompt = `Go telemetry helps us improve Go by periodically sending anonymous metrics and crash reports to the Go team. Learn more at [go.dev/doc/telemetry](https://go.dev/doc/telemetry). |
| 302 |
|
| 303 |
Would you like to enable Go telemetry? |
| 304 |
` |
| 305 |
} |
| 306 |
|
| 307 |
action, err := showMessageRequest(ctx, s.client, protocol.Info, prompt, TelemetryYes, TelemetryNo) |
| 308 |
if err != nil { |
| 309 |
errorf("ShowMessageRequest failed: %v", err) |
| 310 |
} |
| 311 |
|
| 312 |
message := func(typ protocol.MessageType, msg string) { |
| 313 |
if !showMessage(ctx, s.client, typ, msg) { |
| 314 |
|
| 315 |
errorf("showMessage failed: %v", err) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
result := pFailed |
| 320 |
if action == "" { |
| 321 |
|
| 322 |
errorf("no response") |
| 323 |
} else { |
| 324 |
|
| 325 |
switch action { |
| 326 |
case TelemetryYes: |
| 327 |
result = pYes |
| 328 |
if err := s.setTelemetryMode("on"); err == nil { |
| 329 |
message(protocol.Info, telemetryOnMessage(s.Options().LinkifyShowMessage)) |
| 330 |
} else { |
| 331 |
errorf("enabling telemetry failed: %v", err) |
| 332 |
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) |
| 333 |
message(protocol.Error, msg) |
| 334 |
} |
| 335 |
|
| 336 |
case TelemetryNo: |
| 337 |
result = pNo |
| 338 |
default: |
| 339 |
errorf("unrecognized response %q", action) |
| 340 |
message(protocol.Error, fmt.Sprintf("Unrecognized response %q", action)) |
| 341 |
} |
| 342 |
} |
| 343 |
resultContent := fmt.Appendf(nil, "%s %d %d %d", result, attempts, creationTime, token) |
| 344 |
if err := os.WriteFile(promptFile, resultContent, 0666); err != nil { |
| 345 |
errorf("error writing result state to prompt file: %v", err) |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
func showMessageRequest(ctx context.Context, cli protocol.Client, typ protocol.MessageType, message string, actions ...string) (string, error) { |
| 352 |
var actionItems []protocol.MessageActionItem |
| 353 |
for _, action := range actions { |
| 354 |
actionItems = append(actionItems, protocol.MessageActionItem{Title: action}) |
| 355 |
} |
| 356 |
params := &protocol.ShowMessageRequestParams{ |
| 357 |
Type: typ, |
| 358 |
Message: message, |
| 359 |
Actions: actionItems, |
| 360 |
} |
| 361 |
|
| 362 |
const timeout = 15 * time.Second |
| 363 |
ctx, cancel := context.WithTimeout(ctx, timeout) |
| 364 |
defer cancel() |
| 365 |
|
| 366 |
result, err := cli.ShowMessageRequest(ctx, params) |
| 367 |
|
| 368 |
if err != nil { |
| 369 |
if cause := context.Cause(ctx); cause == context.DeadlineExceeded || cause == context.Canceled { |
| 370 |
return "", nil |
| 371 |
} |
| 372 |
return "", err |
| 373 |
} |
| 374 |
if result == nil { |
| 375 |
return "", nil |
| 376 |
} |
| 377 |
return result.Title, nil |
| 378 |
} |
| 379 |
|
| 380 |
func telemetryOnMessage(linkify bool) string { |
| 381 |
format := `Thank you. Telemetry uploading is now enabled. |
| 382 |
|
| 383 |
To disable telemetry uploading, run %s. |
| 384 |
` |
| 385 |
var runCmd = "`go run golang.org/x/telemetry/cmd/gotelemetry@latest local`" |
| 386 |
if linkify { |
| 387 |
runCmd = "[gotelemetry local](https://golang.org/x/telemetry/cmd/gotelemetry)" |
| 388 |
} |
| 389 |
return fmt.Sprintf(format, runCmd) |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
func acquireLockFile(path string) (func(), bool, error) { |
| 400 |
lockpath := path + ".lock" |
| 401 |
fi, err := os.Stat(lockpath) |
| 402 |
if err == nil { |
| 403 |
if time.Since(fi.ModTime()) > promptTimeout { |
| 404 |
_ = os.Remove(lockpath) |
| 405 |
} else { |
| 406 |
return nil, false, nil |
| 407 |
} |
| 408 |
} else if !os.IsNotExist(err) { |
| 409 |
return nil, false, fmt.Errorf("statting lockfile: %v", err) |
| 410 |
} |
| 411 |
|
| 412 |
f, err := os.OpenFile(lockpath, os.O_CREATE|os.O_EXCL, 0666) |
| 413 |
if err != nil { |
| 414 |
if os.IsExist(err) { |
| 415 |
return nil, false, nil |
| 416 |
} |
| 417 |
return nil, false, fmt.Errorf("creating lockfile: %v", err) |
| 418 |
} |
| 419 |
fi, err = f.Stat() |
| 420 |
if err != nil { |
| 421 |
return nil, false, err |
| 422 |
} |
| 423 |
release := func() { |
| 424 |
_ = f.Close() |
| 425 |
fi2, err := os.Stat(lockpath) |
| 426 |
if err == nil && os.SameFile(fi, fi2) { |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
_ = os.Remove(lockpath) |
| 434 |
} |
| 435 |
} |
| 436 |
return release, true, nil |
| 437 |
} |