2023-09-07 19:55:58 -04:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-11-01 09:10:09 -04:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2023-09-07 19:55:58 -04:00
|
|
|
|
2023-10-13 15:38:21 -04:00
|
|
|
package otelhttp // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
2023-09-07 19:55:58 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2024-11-01 09:10:09 -04:00
|
|
|
"sync/atomic"
|
2023-09-07 19:55:58 -04:00
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ io.ReadCloser = &bodyWrapper{}
|
|
|
|
|
|
|
|
// bodyWrapper wraps a http.Request.Body (an io.ReadCloser) to track the number
|
2023-10-13 15:38:21 -04:00
|
|
|
// of bytes read and the last error.
|
2023-09-07 19:55:58 -04:00
|
|
|
type bodyWrapper struct {
|
|
|
|
io.ReadCloser
|
|
|
|
record func(n int64) // must not be nil
|
|
|
|
|
2024-11-01 09:10:09 -04:00
|
|
|
read atomic.Int64
|
2023-09-07 19:55:58 -04:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bodyWrapper) Read(b []byte) (int, error) {
|
|
|
|
n, err := w.ReadCloser.Read(b)
|
|
|
|
n1 := int64(n)
|
2024-11-01 09:10:09 -04:00
|
|
|
w.read.Add(n1)
|
2023-09-07 19:55:58 -04:00
|
|
|
w.err = err
|
|
|
|
w.record(n1)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bodyWrapper) Close() error {
|
|
|
|
return w.ReadCloser.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ http.ResponseWriter = &respWriterWrapper{}
|
|
|
|
|
|
|
|
// respWriterWrapper wraps a http.ResponseWriter in order to track the number of
|
2023-12-12 10:26:50 -05:00
|
|
|
// bytes written, the last error, and to catch the first written statusCode.
|
2023-09-07 19:55:58 -04:00
|
|
|
// TODO: The wrapped http.ResponseWriter doesn't implement any of the optional
|
|
|
|
// types (http.Hijacker, http.Pusher, http.CloseNotifier, http.Flusher, etc)
|
|
|
|
// that may be useful when using it in real life situations.
|
|
|
|
type respWriterWrapper struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
record func(n int64) // must not be nil
|
|
|
|
|
|
|
|
// used to inject the header
|
|
|
|
ctx context.Context
|
|
|
|
|
|
|
|
props propagation.TextMapPropagator
|
|
|
|
|
|
|
|
written int64
|
|
|
|
statusCode int
|
|
|
|
err error
|
|
|
|
wroteHeader bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *respWriterWrapper) Header() http.Header {
|
|
|
|
return w.ResponseWriter.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *respWriterWrapper) Write(p []byte) (int, error) {
|
|
|
|
if !w.wroteHeader {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
n, err := w.ResponseWriter.Write(p)
|
|
|
|
n1 := int64(n)
|
|
|
|
w.record(n1)
|
|
|
|
w.written += n1
|
|
|
|
w.err = err
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2023-12-12 10:26:50 -05:00
|
|
|
// WriteHeader persists initial statusCode for span attribution.
|
|
|
|
// All calls to WriteHeader will be propagated to the underlying ResponseWriter
|
|
|
|
// and will persist the statusCode from the first call.
|
|
|
|
// Blocking consecutive calls to WriteHeader alters expected behavior and will
|
|
|
|
// remove warning logs from net/http where developers will notice incorrect handler implementations.
|
2023-09-07 19:55:58 -04:00
|
|
|
func (w *respWriterWrapper) WriteHeader(statusCode int) {
|
2023-12-12 10:26:50 -05:00
|
|
|
if !w.wroteHeader {
|
|
|
|
w.wroteHeader = true
|
|
|
|
w.statusCode = statusCode
|
2023-09-07 19:55:58 -04:00
|
|
|
}
|
|
|
|
w.ResponseWriter.WriteHeader(statusCode)
|
|
|
|
}
|
2024-11-01 09:10:09 -04:00
|
|
|
|
|
|
|
func (w *respWriterWrapper) Flush() {
|
|
|
|
if !w.wroteHeader {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, ok := w.ResponseWriter.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|