Skip to content

Commit 44a37a9

Browse files
committed
Use snoopwrap
1 parent e0d9b9d commit 44a37a9

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

nethttp/server.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,11 @@ package nethttp
55
import (
66
"net/http"
77

8+
"github.com/felixge/httpsnoop"
89
opentracing "github.com/opentracing/opentracing-go"
910
"github.com/opentracing/opentracing-go/ext"
1011
)
1112

12-
type statusCodeTracker struct {
13-
http.ResponseWriter
14-
status int
15-
}
16-
17-
func (w *statusCodeTracker) WriteHeader(status int) {
18-
w.status = status
19-
w.ResponseWriter.WriteHeader(status)
20-
}
21-
2213
type mwOptions struct {
2314
opNameFunc func(r *http.Request) string
2415
spanObserver func(span opentracing.Span, r *http.Request)
@@ -107,12 +98,19 @@ func MiddlewareFunc(tr opentracing.Tracer, h http.HandlerFunc, options ...MWOpti
10798
}
10899
ext.Component.Set(sp, componentName)
109100

110-
w = &statusCodeTracker{w, 200}
101+
status := 200
102+
w = httpsnoop.Wrap(w, httpsnoop.Hooks{
103+
WriteHeader: func(next httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc {
104+
return func(code int) {
105+
status = code
106+
next(code)
107+
}
108+
}})
111109
r = r.WithContext(opentracing.ContextWithSpan(r.Context(), sp))
112110

113111
h(w, r)
114112

115-
ext.HTTPStatusCode.Set(sp, uint16(w.(*statusCodeTracker).status))
113+
ext.HTTPStatusCode.Set(sp, uint16(status))
116114
sp.Finish()
117115
}
118116
return http.HandlerFunc(fn)

0 commit comments

Comments
 (0)