mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-14 10:13:10 +02:00
d351eb4de2
* WIP * Finish http middleware implementation This is currently done as httptrace.NewHandler(). This should maybe be moved to a different package or renamed, but atm this name seemed the least objectionable to me. There are a few TODOs sprinkled in the code for questions I didn't know the answers to. There is an example provided, which when copied to a main func locally work against the jaeger all in one. Real tests need to be added. * Address feedback * Tie in propagation, update attributes, more options, revise example, etc. Dropped the httpConfig type because it seemed redundant an unecessary abstraction. * WithRouteTag * address out of band feedback * move to othttp package * Add a very basic test. I ended up needing to export the Traceparentheader const so that it was accessible to this test. * Add file header
83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
// 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.
|
|
package othttp
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
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
|
|
record func(n int) // must not be nil
|
|
|
|
read int64
|
|
err error
|
|
}
|
|
|
|
func (w *bodyWrapper) Read(b []byte) (int, error) {
|
|
n, err := w.ReadCloser.Read(b)
|
|
w.read += int64(n)
|
|
w.err = err
|
|
w.record(n)
|
|
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
|
|
record func(n int) // must not be nil
|
|
|
|
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)
|
|
w.record(n)
|
|
w.written += int64(n)
|
|
w.err = err
|
|
return n, err
|
|
}
|
|
|
|
func (w *respWriterWrapper) WriteHeader(statusCode int) {
|
|
w.wroteHeader = true
|
|
w.statusCode = statusCode
|
|
w.ResponseWriter.WriteHeader(statusCode)
|
|
}
|