wrap.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright The OpenTelemetry Authors
  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 otelhttp // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
  15. import (
  16. "context"
  17. "io"
  18. "net/http"
  19. "go.opentelemetry.io/otel/propagation"
  20. )
  21. var _ io.ReadCloser = &bodyWrapper{}
  22. // bodyWrapper wraps a http.Request.Body (an io.ReadCloser) to track the number
  23. // of bytes read and the last error.
  24. type bodyWrapper struct {
  25. io.ReadCloser
  26. record func(n int64) // must not be nil
  27. read int64
  28. err error
  29. }
  30. func (w *bodyWrapper) Read(b []byte) (int, error) {
  31. n, err := w.ReadCloser.Read(b)
  32. n1 := int64(n)
  33. w.read += n1
  34. w.err = err
  35. w.record(n1)
  36. return n, err
  37. }
  38. func (w *bodyWrapper) Close() error {
  39. return w.ReadCloser.Close()
  40. }
  41. var _ http.ResponseWriter = &respWriterWrapper{}
  42. // respWriterWrapper wraps a http.ResponseWriter in order to track the number of
  43. // bytes written, the last error, and to catch the first written statusCode.
  44. // TODO: The wrapped http.ResponseWriter doesn't implement any of the optional
  45. // types (http.Hijacker, http.Pusher, http.CloseNotifier, http.Flusher, etc)
  46. // that may be useful when using it in real life situations.
  47. type respWriterWrapper struct {
  48. http.ResponseWriter
  49. record func(n int64) // must not be nil
  50. // used to inject the header
  51. ctx context.Context
  52. props propagation.TextMapPropagator
  53. written int64
  54. statusCode int
  55. err error
  56. wroteHeader bool
  57. }
  58. func (w *respWriterWrapper) Header() http.Header {
  59. return w.ResponseWriter.Header()
  60. }
  61. func (w *respWriterWrapper) Write(p []byte) (int, error) {
  62. if !w.wroteHeader {
  63. w.WriteHeader(http.StatusOK)
  64. }
  65. n, err := w.ResponseWriter.Write(p)
  66. n1 := int64(n)
  67. w.record(n1)
  68. w.written += n1
  69. w.err = err
  70. return n, err
  71. }
  72. // WriteHeader persists initial statusCode for span attribution.
  73. // All calls to WriteHeader will be propagated to the underlying ResponseWriter
  74. // and will persist the statusCode from the first call.
  75. // Blocking consecutive calls to WriteHeader alters expected behavior and will
  76. // remove warning logs from net/http where developers will notice incorrect handler implementations.
  77. func (w *respWriterWrapper) WriteHeader(statusCode int) {
  78. if !w.wroteHeader {
  79. w.wroteHeader = true
  80. w.statusCode = statusCode
  81. }
  82. w.ResponseWriter.WriteHeader(statusCode)
  83. }