http.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (c) 2017 Uber Technologies, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package transport
  15. import (
  16. "bytes"
  17. "context"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "net/http"
  22. "time"
  23. "github.com/uber/jaeger-client-go/thrift"
  24. "github.com/uber/jaeger-client-go"
  25. j "github.com/uber/jaeger-client-go/thrift-gen/jaeger"
  26. )
  27. // Default timeout for http request in seconds
  28. const defaultHTTPTimeout = time.Second * 5
  29. // HTTPTransport implements Transport by forwarding spans to a http server.
  30. type HTTPTransport struct {
  31. url string
  32. client *http.Client
  33. batchSize int
  34. spans []*j.Span
  35. process *j.Process
  36. httpCredentials *HTTPBasicAuthCredentials
  37. headers map[string]string
  38. }
  39. // HTTPBasicAuthCredentials stores credentials for HTTP basic auth.
  40. type HTTPBasicAuthCredentials struct {
  41. username string
  42. password string
  43. }
  44. // HTTPOption sets a parameter for the HttpCollector
  45. type HTTPOption func(c *HTTPTransport)
  46. // HTTPTimeout sets maximum timeout for http request.
  47. func HTTPTimeout(duration time.Duration) HTTPOption {
  48. return func(c *HTTPTransport) { c.client.Timeout = duration }
  49. }
  50. // HTTPBatchSize sets the maximum batch size, after which a collect will be
  51. // triggered. The default batch size is 100 spans.
  52. func HTTPBatchSize(n int) HTTPOption {
  53. return func(c *HTTPTransport) { c.batchSize = n }
  54. }
  55. // HTTPBasicAuth sets the credentials required to perform HTTP basic auth
  56. func HTTPBasicAuth(username string, password string) HTTPOption {
  57. return func(c *HTTPTransport) {
  58. c.httpCredentials = &HTTPBasicAuthCredentials{username: username, password: password}
  59. }
  60. }
  61. // HTTPRoundTripper configures the underlying Transport on the *http.Client
  62. // that is used
  63. func HTTPRoundTripper(transport http.RoundTripper) HTTPOption {
  64. return func(c *HTTPTransport) {
  65. c.client.Transport = transport
  66. }
  67. }
  68. // HTTPHeaders defines the HTTP headers that will be attached to the jaeger client's HTTP request
  69. func HTTPHeaders(headers map[string]string) HTTPOption {
  70. return func(c *HTTPTransport) {
  71. c.headers = headers
  72. }
  73. }
  74. // NewHTTPTransport returns a new HTTP-backend transport. url should be an http
  75. // url of the collector to handle POST request, typically something like:
  76. // http://hostname:14268/api/traces?format=jaeger.thrift
  77. func NewHTTPTransport(url string, options ...HTTPOption) *HTTPTransport {
  78. c := &HTTPTransport{
  79. url: url,
  80. client: &http.Client{Timeout: defaultHTTPTimeout},
  81. batchSize: 100,
  82. spans: []*j.Span{},
  83. }
  84. for _, option := range options {
  85. option(c)
  86. }
  87. return c
  88. }
  89. // Append implements Transport.
  90. func (c *HTTPTransport) Append(span *jaeger.Span) (int, error) {
  91. if c.process == nil {
  92. c.process = jaeger.BuildJaegerProcessThrift(span)
  93. }
  94. jSpan := jaeger.BuildJaegerThrift(span)
  95. c.spans = append(c.spans, jSpan)
  96. if len(c.spans) >= c.batchSize {
  97. return c.Flush()
  98. }
  99. return 0, nil
  100. }
  101. // Flush implements Transport.
  102. func (c *HTTPTransport) Flush() (int, error) {
  103. count := len(c.spans)
  104. if count == 0 {
  105. return 0, nil
  106. }
  107. err := c.send(c.spans)
  108. c.spans = c.spans[:0]
  109. return count, err
  110. }
  111. // Close implements Transport.
  112. func (c *HTTPTransport) Close() error {
  113. return nil
  114. }
  115. func (c *HTTPTransport) send(spans []*j.Span) error {
  116. batch := &j.Batch{
  117. Spans: spans,
  118. Process: c.process,
  119. }
  120. body, err := serializeThrift(batch)
  121. if err != nil {
  122. return err
  123. }
  124. req, err := http.NewRequest("POST", c.url, body)
  125. if err != nil {
  126. return err
  127. }
  128. req.Header.Set("Content-Type", "application/x-thrift")
  129. for k, v := range c.headers {
  130. req.Header.Set(k, v)
  131. }
  132. if c.httpCredentials != nil {
  133. req.SetBasicAuth(c.httpCredentials.username, c.httpCredentials.password)
  134. }
  135. resp, err := c.client.Do(req)
  136. if err != nil {
  137. return err
  138. }
  139. io.Copy(ioutil.Discard, resp.Body)
  140. resp.Body.Close()
  141. if resp.StatusCode >= http.StatusBadRequest {
  142. return fmt.Errorf("error from collector: %d", resp.StatusCode)
  143. }
  144. return nil
  145. }
  146. func serializeThrift(obj thrift.TStruct) (*bytes.Buffer, error) {
  147. t := thrift.NewTMemoryBuffer()
  148. p := thrift.NewTBinaryProtocolTransport(t)
  149. if err := obj.Write(context.Background(), p); err != nil {
  150. return nil, err
  151. }
  152. return t.Buffer, nil
  153. }