client.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package metrics
  14. import (
  15. "bytes"
  16. "context"
  17. "encoding/json"
  18. "net"
  19. "net/http"
  20. "os"
  21. "time"
  22. )
  23. type client struct {
  24. httpClient *http.Client
  25. }
  26. // Command is a command
  27. type Command struct {
  28. Command string `json:"command"`
  29. Context string `json:"context"`
  30. Source string `json:"source"`
  31. Status string `json:"status"`
  32. }
  33. // CLISource is sent for cli metrics
  34. var CLISource = "cli"
  35. func init() {
  36. if v, ok := os.LookupEnv("DOCKER_METRICS_SOURCE"); ok {
  37. CLISource = v
  38. }
  39. }
  40. // Client sends metrics to Docker Desktopn
  41. type Client interface {
  42. // Send sends the command to Docker Desktop. Note that the function doesn't
  43. // return anything, not even an error, this is because we don't really care
  44. // if the metrics were sent or not. We only fire and forget.
  45. Send(Command)
  46. }
  47. // NewClient returns a new metrics client
  48. func NewClient() Client {
  49. return &client{
  50. httpClient: &http.Client{
  51. Transport: &http.Transport{
  52. DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
  53. return conn()
  54. },
  55. },
  56. },
  57. }
  58. }
  59. func (c *client) Send(command Command) {
  60. result := make(chan bool, 1)
  61. go func() {
  62. postMetrics(command, c)
  63. result <- true
  64. }()
  65. // wait for the post finished, or timeout in case anything freezes.
  66. // Posting metrics without Desktop listening returns in less than a ms, and a handful of ms (often <2ms) when Desktop is listening
  67. select {
  68. case <-result:
  69. case <-time.After(50 * time.Millisecond):
  70. }
  71. }
  72. func postMetrics(command Command, c *client) {
  73. req, err := json.Marshal(command)
  74. if err == nil {
  75. _, _ = c.httpClient.Post("http://localhost/usage", "application/json", bytes.NewBuffer(req))
  76. }
  77. }