metrics.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package http
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "net/http"
  6. "net/http/httptrace"
  7. "sync/atomic"
  8. "time"
  9. "github.com/aws/smithy-go/metrics"
  10. )
  11. var now = time.Now
  12. // withMetrics instruments an HTTP client and context to collect HTTP metrics.
  13. func withMetrics(parent context.Context, client ClientDo, meter metrics.Meter) (
  14. context.Context, ClientDo, error,
  15. ) {
  16. // WithClientTrace is an expensive operation - avoid calling it if we're
  17. // not actually using a metrics sink.
  18. if _, ok := meter.(metrics.NopMeter); ok {
  19. return parent, client, nil
  20. }
  21. hm, err := newHTTPMetrics(meter)
  22. if err != nil {
  23. return nil, nil, err
  24. }
  25. ctx := httptrace.WithClientTrace(parent, &httptrace.ClientTrace{
  26. DNSStart: hm.DNSStart,
  27. ConnectStart: hm.ConnectStart,
  28. TLSHandshakeStart: hm.TLSHandshakeStart,
  29. GotConn: hm.GotConn(parent),
  30. PutIdleConn: hm.PutIdleConn(parent),
  31. ConnectDone: hm.ConnectDone(parent),
  32. DNSDone: hm.DNSDone(parent),
  33. TLSHandshakeDone: hm.TLSHandshakeDone(parent),
  34. GotFirstResponseByte: hm.GotFirstResponseByte(parent),
  35. })
  36. return ctx, &timedClientDo{client, hm}, nil
  37. }
  38. type timedClientDo struct {
  39. ClientDo
  40. hm *httpMetrics
  41. }
  42. func (c *timedClientDo) Do(r *http.Request) (*http.Response, error) {
  43. c.hm.doStart.Store(now())
  44. resp, err := c.ClientDo.Do(r)
  45. c.hm.DoRequestDuration.Record(r.Context(), c.hm.doStart.Elapsed())
  46. return resp, err
  47. }
  48. type httpMetrics struct {
  49. DNSLookupDuration metrics.Float64Histogram // client.http.connections.dns_lookup_duration
  50. ConnectDuration metrics.Float64Histogram // client.http.connections.acquire_duration
  51. TLSHandshakeDuration metrics.Float64Histogram // client.http.connections.tls_handshake_duration
  52. ConnectionUsage metrics.Int64UpDownCounter // client.http.connections.usage
  53. DoRequestDuration metrics.Float64Histogram // client.http.do_request_duration
  54. TimeToFirstByte metrics.Float64Histogram // client.http.time_to_first_byte
  55. doStart safeTime
  56. dnsStart safeTime
  57. connectStart safeTime
  58. tlsStart safeTime
  59. }
  60. func newHTTPMetrics(meter metrics.Meter) (*httpMetrics, error) {
  61. hm := &httpMetrics{}
  62. var err error
  63. hm.DNSLookupDuration, err = meter.Float64Histogram("client.http.connections.dns_lookup_duration", func(o *metrics.InstrumentOptions) {
  64. o.UnitLabel = "s"
  65. o.Description = "The time it takes a request to perform DNS lookup."
  66. })
  67. if err != nil {
  68. return nil, err
  69. }
  70. hm.ConnectDuration, err = meter.Float64Histogram("client.http.connections.acquire_duration", func(o *metrics.InstrumentOptions) {
  71. o.UnitLabel = "s"
  72. o.Description = "The time it takes a request to acquire a connection."
  73. })
  74. if err != nil {
  75. return nil, err
  76. }
  77. hm.TLSHandshakeDuration, err = meter.Float64Histogram("client.http.connections.tls_handshake_duration", func(o *metrics.InstrumentOptions) {
  78. o.UnitLabel = "s"
  79. o.Description = "The time it takes an HTTP request to perform the TLS handshake."
  80. })
  81. if err != nil {
  82. return nil, err
  83. }
  84. hm.ConnectionUsage, err = meter.Int64UpDownCounter("client.http.connections.usage", func(o *metrics.InstrumentOptions) {
  85. o.UnitLabel = "{connection}"
  86. o.Description = "Current state of connections pool."
  87. })
  88. if err != nil {
  89. return nil, err
  90. }
  91. hm.DoRequestDuration, err = meter.Float64Histogram("client.http.do_request_duration", func(o *metrics.InstrumentOptions) {
  92. o.UnitLabel = "s"
  93. o.Description = "Time spent performing an entire HTTP transaction."
  94. })
  95. if err != nil {
  96. return nil, err
  97. }
  98. hm.TimeToFirstByte, err = meter.Float64Histogram("client.http.time_to_first_byte", func(o *metrics.InstrumentOptions) {
  99. o.UnitLabel = "s"
  100. o.Description = "Time from start of transaction to when the first response byte is available."
  101. })
  102. if err != nil {
  103. return nil, err
  104. }
  105. return hm, nil
  106. }
  107. func (m *httpMetrics) DNSStart(httptrace.DNSStartInfo) {
  108. m.dnsStart.Store(now())
  109. }
  110. func (m *httpMetrics) ConnectStart(string, string) {
  111. m.connectStart.Store(now())
  112. }
  113. func (m *httpMetrics) TLSHandshakeStart() {
  114. m.tlsStart.Store(now())
  115. }
  116. func (m *httpMetrics) GotConn(ctx context.Context) func(httptrace.GotConnInfo) {
  117. return func(httptrace.GotConnInfo) {
  118. m.addConnAcquired(ctx, 1)
  119. }
  120. }
  121. func (m *httpMetrics) PutIdleConn(ctx context.Context) func(error) {
  122. return func(error) {
  123. m.addConnAcquired(ctx, -1)
  124. }
  125. }
  126. func (m *httpMetrics) DNSDone(ctx context.Context) func(httptrace.DNSDoneInfo) {
  127. return func(httptrace.DNSDoneInfo) {
  128. m.DNSLookupDuration.Record(ctx, m.dnsStart.Elapsed())
  129. }
  130. }
  131. func (m *httpMetrics) ConnectDone(ctx context.Context) func(string, string, error) {
  132. return func(string, string, error) {
  133. m.ConnectDuration.Record(ctx, m.connectStart.Elapsed())
  134. }
  135. }
  136. func (m *httpMetrics) TLSHandshakeDone(ctx context.Context) func(tls.ConnectionState, error) {
  137. return func(tls.ConnectionState, error) {
  138. m.TLSHandshakeDuration.Record(ctx, m.tlsStart.Elapsed())
  139. }
  140. }
  141. func (m *httpMetrics) GotFirstResponseByte(ctx context.Context) func() {
  142. return func() {
  143. m.TimeToFirstByte.Record(ctx, m.doStart.Elapsed())
  144. }
  145. }
  146. func (m *httpMetrics) addConnAcquired(ctx context.Context, incr int64) {
  147. m.ConnectionUsage.Add(ctx, incr, func(o *metrics.RecordMetricOptions) {
  148. o.Properties.Set("state", "acquired")
  149. })
  150. }
  151. // Not used: it is recommended to track acquired vs idle conn, but we can't
  152. // determine when something is truly idle with the current HTTP client hooks
  153. // available to us.
  154. func (m *httpMetrics) addConnIdle(ctx context.Context, incr int64) {
  155. m.ConnectionUsage.Add(ctx, incr, func(o *metrics.RecordMetricOptions) {
  156. o.Properties.Set("state", "idle")
  157. })
  158. }
  159. type safeTime struct {
  160. atomic.Value // time.Time
  161. }
  162. func (st *safeTime) Store(v time.Time) {
  163. st.Value.Store(v)
  164. }
  165. func (st *safeTime) Load() time.Time {
  166. t, _ := st.Value.Load().(time.Time)
  167. return t
  168. }
  169. func (st *safeTime) Elapsed() float64 {
  170. end := now()
  171. elapsed := end.Sub(st.Load())
  172. return float64(elapsed) / 1e9
  173. }