client.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. )
  21. type client struct {
  22. httpClient *http.Client
  23. }
  24. // Command is a command
  25. type Command struct {
  26. Command string `json:"command"`
  27. Context string `json:"context"`
  28. Source string `json:"source"`
  29. Status string `json:"status"`
  30. }
  31. const (
  32. // CLISource is sent for cli metrics
  33. CLISource = "cli"
  34. // APISource is sent for API metrics
  35. APISource = "api"
  36. // SuccessStatus is sent for API metrics
  37. SuccessStatus = "success"
  38. // FailureStatus is sent for API metrics
  39. FailureStatus = "failure"
  40. // CancelledStatus is sent for API metrics
  41. CancelledStatus = "cancelled"
  42. )
  43. // Client sends metrics to Docker Desktopn
  44. type Client interface {
  45. // Send sends the command to Docker Desktop. Note that the function doesn't
  46. // return anything, not even an error, this is because we don't really care
  47. // if the metrics were sent or not. We only fire and forget.
  48. Send(Command)
  49. }
  50. // NewClient returns a new metrics client
  51. func NewClient() Client {
  52. return &client{
  53. httpClient: &http.Client{
  54. Transport: &http.Transport{
  55. DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
  56. return conn()
  57. },
  58. },
  59. },
  60. }
  61. }
  62. func (c *client) Send(command Command) {
  63. wasIn := make(chan bool)
  64. // Fire and forget, we don't want to slow down the user waiting for DD
  65. // metrics endpoint to respond. We could lose some events but that's ok.
  66. go func() {
  67. defer func() {
  68. _ = recover()
  69. }()
  70. wasIn <- true
  71. req, err := json.Marshal(command)
  72. if err != nil {
  73. return
  74. }
  75. _, _ = c.httpClient.Post("http://localhost/usage", "application/json", bytes.NewBuffer(req))
  76. }()
  77. <-wasIn
  78. }