client.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. "time"
  21. )
  22. type client struct {
  23. httpClient *http.Client
  24. }
  25. // Command is a command
  26. type Command struct {
  27. Command string `json:"command"`
  28. Context string `json:"context"`
  29. Source string `json:"source"`
  30. Status string `json:"status"`
  31. }
  32. const (
  33. // CLISource is sent for cli metrics
  34. CLISource = "cli"
  35. // APISource is sent for API metrics
  36. APISource = "api"
  37. // SuccessStatus is sent for API metrics
  38. SuccessStatus = "success"
  39. // FailureStatus is sent for API metrics
  40. FailureStatus = "failure"
  41. // CanceledStatus is sent for API metrics
  42. CanceledStatus = "canceled"
  43. )
  44. // Client sends metrics to Docker Desktopn
  45. type Client interface {
  46. // Send sends the command to Docker Desktop. Note that the function doesn't
  47. // return anything, not even an error, this is because we don't really care
  48. // if the metrics were sent or not. We only fire and forget.
  49. Send(Command)
  50. }
  51. // NewClient returns a new metrics client
  52. func NewClient() Client {
  53. return &client{
  54. httpClient: &http.Client{
  55. Transport: &http.Transport{
  56. DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
  57. return conn()
  58. },
  59. },
  60. },
  61. }
  62. }
  63. func (c *client) Send(command Command) {
  64. result := make(chan bool, 1)
  65. go func() {
  66. postMetrics(command, c)
  67. result <- true
  68. }()
  69. // wait for the post finished, or timeout in case anything freezes.
  70. // Posting metrics without Desktop listening returns in less than a ms, and a handful of ms (often <2ms) when Desktop is listening
  71. select {
  72. case <-result:
  73. case <-time.After(50 * time.Millisecond):
  74. }
  75. }
  76. func postMetrics(command Command, c *client) {
  77. req, err := json.Marshal(command)
  78. if err == nil {
  79. _, _ = c.httpClient.Post("http://localhost/usage", "application/json", bytes.NewBuffer(req))
  80. }
  81. }