2019-10-07 19:25:11 +02:00
|
|
|
// Copyright 2019, OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2019-10-17 20:18:45 +02:00
|
|
|
|
2019-10-07 19:25:11 +02:00
|
|
|
package othttp
|
|
|
|
|
|
|
|
import (
|
2019-10-17 20:18:45 +02:00
|
|
|
"context"
|
2019-10-07 19:25:11 +02:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2019-10-17 20:18:45 +02:00
|
|
|
|
2020-01-28 20:13:46 +02:00
|
|
|
"go.opentelemetry.io/otel/api/propagation"
|
2019-10-07 19:25:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ io.ReadCloser = &bodyWrapper{}
|
|
|
|
|
|
|
|
// bodyWrapper wraps a http.Request.Body (an io.ReadCloser) to track the number
|
|
|
|
// of bytes read and the last error
|
|
|
|
type bodyWrapper struct {
|
|
|
|
io.ReadCloser
|
2019-10-17 20:18:45 +02:00
|
|
|
record func(n int64) // must not be nil
|
2019-10-07 19:25:11 +02:00
|
|
|
|
|
|
|
read int64
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bodyWrapper) Read(b []byte) (int, error) {
|
|
|
|
n, err := w.ReadCloser.Read(b)
|
2019-10-17 20:18:45 +02:00
|
|
|
n1 := int64(n)
|
|
|
|
w.read += n1
|
2019-10-07 19:25:11 +02:00
|
|
|
w.err = err
|
2019-10-17 20:18:45 +02:00
|
|
|
w.record(n1)
|
2019-10-07 19:25:11 +02:00
|
|
|
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
|
|
|
|
// bytes written, the last error, and to catch the returned statusCode
|
|
|
|
// 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
|
2019-10-17 20:18:45 +02:00
|
|
|
record func(n int64) // must not be nil
|
|
|
|
|
|
|
|
// used to inject the header
|
|
|
|
ctx context.Context
|
2020-02-14 09:16:05 +02:00
|
|
|
|
|
|
|
props propagation.Propagators
|
2019-10-07 19:25:11 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
w.wroteHeader = true
|
|
|
|
}
|
|
|
|
n, err := w.ResponseWriter.Write(p)
|
2019-10-17 20:18:45 +02:00
|
|
|
n1 := int64(n)
|
|
|
|
w.record(n1)
|
|
|
|
w.written += n1
|
2019-10-07 19:25:11 +02:00
|
|
|
w.err = err
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *respWriterWrapper) WriteHeader(statusCode int) {
|
2019-10-17 20:18:45 +02:00
|
|
|
if w.wroteHeader {
|
|
|
|
return
|
|
|
|
}
|
2019-10-07 19:25:11 +02:00
|
|
|
w.wroteHeader = true
|
|
|
|
w.statusCode = statusCode
|
2020-02-14 09:16:05 +02:00
|
|
|
propagation.InjectHTTP(w.ctx, w.props, w.Header())
|
2019-10-07 19:25:11 +02:00
|
|
|
w.ResponseWriter.WriteHeader(statusCode)
|
|
|
|
}
|