http.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "fmt"
  18. "io"
  19. "io/ioutil"
  20. "net/http"
  21. "time"
  22. "github.com/uber/jaeger-client-go/thrift"
  23. "github.com/uber/jaeger-client-go"
  24. j "github.com/uber/jaeger-client-go/thrift-gen/jaeger"
  25. )
  26. // Default timeout for http request in seconds
  27. const defaultHTTPTimeout = time.Second * 5
  28. // HTTPTransport implements Transport by forwarding spans to a http server.
  29. type HTTPTransport struct {
  30. url string
  31. client *http.Client
  32. batchSize int
  33. spans []*j.Span
  34. process *j.Process
  35. httpCredentials *HTTPBasicAuthCredentials
  36. }
  37. // HTTPBasicAuthCredentials stores credentials for HTTP basic auth.
  38. type HTTPBasicAuthCredentials struct {
  39. username string
  40. password string
  41. }
  42. // HTTPOption sets a parameter for the HttpCollector
  43. type HTTPOption func(c *HTTPTransport)
  44. // HTTPTimeout sets maximum timeout for http request.
  45. func HTTPTimeout(duration time.Duration) HTTPOption {
  46. return func(c *HTTPTransport) { c.client.Timeout = duration }
  47. }
  48. // HTTPBatchSize sets the maximum batch size, after which a collect will be
  49. // triggered. The default batch size is 100 spans.
  50. func HTTPBatchSize(n int) HTTPOption {
  51. return func(c *HTTPTransport) { c.batchSize = n }
  52. }
  53. // HTTPBasicAuth sets the credentials required to perform HTTP basic auth
  54. func HTTPBasicAuth(username string, password string) HTTPOption {
  55. return func(c *HTTPTransport) {
  56. c.httpCredentials = &HTTPBasicAuthCredentials{username: username, password: password}
  57. }
  58. }
  59. // HTTPRoundTripper configures the underlying Transport on the *http.Client
  60. // that is used
  61. func HTTPRoundTripper(transport http.RoundTripper) HTTPOption {
  62. return func(c *HTTPTransport) {
  63. c.client.Transport = transport
  64. }
  65. }
  66. // NewHTTPTransport returns a new HTTP-backend transport. url should be an http
  67. // url of the collector to handle POST request, typically something like:
  68. // http://hostname:14268/api/traces?format=jaeger.thrift
  69. func NewHTTPTransport(url string, options ...HTTPOption) *HTTPTransport {
  70. c := &HTTPTransport{
  71. url: url,
  72. client: &http.Client{Timeout: defaultHTTPTimeout},
  73. batchSize: 100,
  74. spans: []*j.Span{},
  75. }
  76. for _, option := range options {
  77. option(c)
  78. }
  79. return c
  80. }
  81. // Append implements Transport.
  82. func (c *HTTPTransport) Append(span *jaeger.Span) (int, error) {
  83. if c.process == nil {
  84. c.process = jaeger.BuildJaegerProcessThrift(span)
  85. }
  86. jSpan := jaeger.BuildJaegerThrift(span)
  87. c.spans = append(c.spans, jSpan)
  88. if len(c.spans) >= c.batchSize {
  89. return c.Flush()
  90. }
  91. return 0, nil
  92. }
  93. // Flush implements Transport.
  94. func (c *HTTPTransport) Flush() (int, error) {
  95. count := len(c.spans)
  96. if count == 0 {
  97. return 0, nil
  98. }
  99. err := c.send(c.spans)
  100. c.spans = c.spans[:0]
  101. return count, err
  102. }
  103. // Close implements Transport.
  104. func (c *HTTPTransport) Close() error {
  105. return nil
  106. }
  107. func (c *HTTPTransport) send(spans []*j.Span) error {
  108. batch := &j.Batch{
  109. Spans: spans,
  110. Process: c.process,
  111. }
  112. body, err := serializeThrift(batch)
  113. if err != nil {
  114. return err
  115. }
  116. req, err := http.NewRequest("POST", c.url, body)
  117. if err != nil {
  118. return err
  119. }
  120. req.Header.Set("Content-Type", "application/x-thrift")
  121. if c.httpCredentials != nil {
  122. req.SetBasicAuth(c.httpCredentials.username, c.httpCredentials.password)
  123. }
  124. resp, err := c.client.Do(req)
  125. if err != nil {
  126. return err
  127. }
  128. io.Copy(ioutil.Discard, resp.Body)
  129. resp.Body.Close()
  130. if resp.StatusCode >= http.StatusBadRequest {
  131. return fmt.Errorf("error from collector: %d", resp.StatusCode)
  132. }
  133. return nil
  134. }
  135. func serializeThrift(obj thrift.TStruct) (*bytes.Buffer, error) {
  136. t := thrift.NewTMemoryBuffer()
  137. p := thrift.NewTBinaryProtocolTransport(t)
  138. if err := obj.Write(p); err != nil {
  139. return nil, err
  140. }
  141. return t.Buffer, nil
  142. }