debug.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright (c) 2021 Tailscale Inc & 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. package controlclient
  5. import (
  6. "bytes"
  7. "compress/gzip"
  8. "context"
  9. "log"
  10. "net/http"
  11. "time"
  12. "tailscale.com/util/goroutines"
  13. )
  14. func dumpGoroutinesToURL(c *http.Client, targetURL string) {
  15. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  16. defer cancel()
  17. zbuf := new(bytes.Buffer)
  18. zw := gzip.NewWriter(zbuf)
  19. zw.Write(goroutines.ScrubbedGoroutineDump())
  20. zw.Close()
  21. req, err := http.NewRequestWithContext(ctx, "PUT", targetURL, zbuf)
  22. if err != nil {
  23. log.Printf("dumpGoroutinesToURL: %v", err)
  24. return
  25. }
  26. req.Header.Set("Content-Encoding", "gzip")
  27. t0 := time.Now()
  28. _, err = c.Do(req)
  29. d := time.Since(t0).Round(time.Millisecond)
  30. if err != nil {
  31. log.Printf("dumpGoroutinesToURL error: %v to %v (after %v)", err, targetURL, d)
  32. } else {
  33. log.Printf("dumpGoroutinesToURL complete to %v (after %v)", targetURL, d)
  34. }
  35. }