Skip to content

Commit

Permalink
Merge pull request #87 from instana/always_send_http_status_code
Browse files Browse the repository at this point in the history
Always send HTTP status code
  • Loading branch information
Andrew Slotin authored Mar 9, 2020
2 parents 5559293 + 53267f2 commit 5d055d0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
39 changes: 24 additions & 15 deletions adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"runtime"

"github.com/felixge/httpsnoop"
ot "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"
Expand Down Expand Up @@ -43,21 +42,12 @@ func (s *Sensor) TraceHandler(name, pattern string, handler http.HandlerFunc) (s
func (s *Sensor) TracingHandler(name string, handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
s.WithTracingContext(name, w, req, func(span ot.Span, ctx context.Context) {
// Capture response code for span
hooks := httpsnoop.Hooks{
WriteHeader: func(next httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc {
return func(code int) {
next(code)
span.SetTag(string(ext.HTTPStatusCode), code)
}
},
}

// Add hooks to response writer
wrappedWriter := httpsnoop.Wrap(w, hooks)
wrapped := &statusCodeRecorder{ResponseWriter: w}
handler.ServeHTTP(wrapped, req.WithContext(ctx))

// Serve original handler
handler.ServeHTTP(wrappedWriter, req.WithContext(ctx))
if wrapped.Status > 0 {
span.SetTag(string(ext.HTTPStatusCode), wrapped.Status)
}
})
}
}
Expand Down Expand Up @@ -152,3 +142,22 @@ func (s *Sensor) WithTracingContext(name string, w http.ResponseWriter, req *htt
f(span, ctx)
})
}

// wrapper over http.ResponseWriter to spy the returned status code
type statusCodeRecorder struct {
http.ResponseWriter
Status int
}

func (rec *statusCodeRecorder) WriteHeader(status int) {
rec.Status = status
rec.ResponseWriter.WriteHeader(status)
}

func (rec *statusCodeRecorder) Write(b []byte) (int, error) {
if rec.Status == 0 {
rec.Status = http.StatusOK
}

return rec.ResponseWriter.Write(b)
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/instana/go-sensor
go 1.8

require (
github.com/felixge/httpsnoop v1.0.0
github.com/gogo/protobuf v1.2.1 // indirect
github.com/looplab/fsm v0.1.0
github.com/opentracing/basictracer-go v1.0.0
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/felixge/httpsnoop v1.0.0 h1:gh8fMGz0rlOv/1WmRZm7OgncIOTsAj21iNJot48omJQ=
github.com/felixge/httpsnoop v1.0.0/go.mod h1:3+D9sFq0ahK/JeJPhCBUV1xlf4/eIYrUQaxulT0VzX8=
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
Expand All @@ -14,6 +12,7 @@ github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsq
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down

0 comments on commit 5d055d0

Please sign in to comment.