Deprecate v1.0.0 HTTP and log helpers in favour of the toolkit API
CI / test (push) Successful in 1m12s
CI / test (push) Successful in 1m12s
LogHandler and HTTPTransport now delegate to NewTraceHandler and NewHTTPTransport. HTTPMiddleware keeps its v1.0.0 metric names but shares responseWriter and normalizeMethod with NewHTTPMiddleware. The package example shows obsx.New. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
b53a6a6c30
commit
8837e8d316
8 files changed
+40
-149
No files matched your search
@@ -20,7 +20,10 @@ import (
|
||||
//
|
||||
// The route is the matched http.ServeMux pattern (Request.Pattern), so wrap
|
||||
// the mux itself; unmatched requests are reported as route "unmatched".
|
||||
// Call it once per Metrics: the metrics are registered on each call.
|
||||
//
|
||||
// Deprecated: use NewHTTPMiddleware (or Observer.HTTPMiddleware), which adds
|
||||
// access logs, in-flight and exemplar support and uses the fleet-wide
|
||||
// http_server_* metric names. HTTPMiddleware keeps its v1.0.0 metric names.
|
||||
func HTTPMiddleware(m *Metrics) func(http.Handler) http.Handler {
|
||||
var requests *prometheus.CounterVec
|
||||
var duration *prometheus.HistogramVec
|
||||
@@ -31,7 +34,7 @@ func HTTPMiddleware(m *Metrics) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
method := knownMethod(r.Method)
|
||||
method := normalizeMethod(r.Method)
|
||||
ctx := otel.GetTextMapPropagator().Extract(r.Context(), propagation.HeaderCarrier(r.Header))
|
||||
ctx, span := otel.Tracer(tracerName).Start(ctx, method,
|
||||
trace.WithSpanKind(trace.SpanKindServer),
|
||||
@@ -39,109 +42,46 @@ func HTTPMiddleware(m *Metrics) func(http.Handler) http.Handler {
|
||||
)
|
||||
defer span.End()
|
||||
|
||||
sw := &statusWriter{ResponseWriter: w, status: http.StatusOK}
|
||||
rw := &responseWriter{ResponseWriter: w}
|
||||
r = r.WithContext(ctx)
|
||||
defer func() {
|
||||
// A panic propagates to net/http, which answers 500 or drops the connection.
|
||||
p := recover()
|
||||
if p != nil {
|
||||
sw.status = http.StatusInternalServerError
|
||||
status := rw.status
|
||||
switch {
|
||||
case p != nil:
|
||||
status = http.StatusInternalServerError
|
||||
case status == 0:
|
||||
status = http.StatusOK
|
||||
}
|
||||
route := r.Pattern // set by ServeMux on this request during ServeHTTP
|
||||
if route == "" {
|
||||
route = "unmatched"
|
||||
route = routeUnmatched
|
||||
} else {
|
||||
span.SetName(route)
|
||||
span.SetAttributes(semconv.HTTPRoute(route))
|
||||
}
|
||||
span.SetAttributes(semconv.HTTPResponseStatusCode(sw.status))
|
||||
if sw.status >= 500 {
|
||||
span.SetStatus(codes.Error, http.StatusText(sw.status))
|
||||
span.SetAttributes(semconv.HTTPResponseStatusCode(status))
|
||||
if status >= 500 {
|
||||
span.SetStatus(codes.Error, http.StatusText(status))
|
||||
}
|
||||
if m != nil {
|
||||
requests.WithLabelValues(method, route, strconv.Itoa(sw.status)).Inc()
|
||||
requests.WithLabelValues(method, route, strconv.Itoa(status)).Inc()
|
||||
duration.WithLabelValues(method, route).Observe(time.Since(start).Seconds())
|
||||
}
|
||||
if p != nil {
|
||||
panic(p)
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(sw, r)
|
||||
next.ServeHTTP(rw, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// knownMethod bounds label cardinality: clients can send arbitrary methods.
|
||||
func knownMethod(m string) string {
|
||||
switch m {
|
||||
case http.MethodGet, http.MethodHead, http.MethodPost, http.MethodPut, http.MethodPatch,
|
||||
http.MethodDelete, http.MethodConnect, http.MethodOptions, http.MethodTrace:
|
||||
return m
|
||||
}
|
||||
return "OTHER"
|
||||
}
|
||||
|
||||
type statusWriter struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
wrote bool
|
||||
}
|
||||
|
||||
func (w *statusWriter) WriteHeader(code int) {
|
||||
if !w.wrote {
|
||||
w.status, w.wrote = code, true
|
||||
}
|
||||
w.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
func (w *statusWriter) Write(b []byte) (int, error) {
|
||||
w.wrote = true
|
||||
return w.ResponseWriter.Write(b)
|
||||
}
|
||||
|
||||
// Flush keeps streaming handlers (SSE) working through the wrapper.
|
||||
func (w *statusWriter) Flush() {
|
||||
if f, ok := w.ResponseWriter.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
// Unwrap gives http.ResponseController access to Hijack, deadlines, etc.
|
||||
func (w *statusWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter }
|
||||
|
||||
// HTTPTransport wraps base (nil means http.DefaultTransport) so that every
|
||||
// outgoing request gets a client span and carries the trace context headers.
|
||||
//
|
||||
// Deprecated: use NewHTTPTransport, which HTTPTransport now calls without metrics.
|
||||
func HTTPTransport(base http.RoundTripper) http.RoundTripper {
|
||||
if base == nil {
|
||||
base = http.DefaultTransport
|
||||
}
|
||||
return transport{base}
|
||||
}
|
||||
|
||||
type transport struct{ base http.RoundTripper }
|
||||
|
||||
// shortcut: the span ends when response headers arrive, not when the body is
|
||||
// fully read; wrap resp.Body if body transfer time must be in the span.
|
||||
func (t transport) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
method := knownMethod(r.Method)
|
||||
ctx, span := otel.Tracer(tracerName).Start(r.Context(), method,
|
||||
trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(semconv.HTTPRequestMethodKey.String(method), semconv.ServerAddress(r.URL.Host)),
|
||||
)
|
||||
defer span.End()
|
||||
|
||||
r = r.Clone(ctx) // RoundTrippers must not modify the caller's request
|
||||
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header))
|
||||
|
||||
resp, err := t.base.RoundTrip(r)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
return nil, err
|
||||
}
|
||||
span.SetAttributes(semconv.HTTPResponseStatusCode(resp.StatusCode))
|
||||
if resp.StatusCode >= 400 {
|
||||
span.SetStatus(codes.Error, http.StatusText(resp.StatusCode))
|
||||
}
|
||||
return resp, nil
|
||||
return NewHTTPTransport(base, HTTPClientConfig{})
|
||||
}
|
||||
Reference in new issue
Block a user