Skip to content

Commit

Permalink
Merge pull request #60 from Comcast/feature/move-empty-request-body-t…
Browse files Browse the repository at this point in the history
…racking

Moved the empty request tracking into the ServerHandler
  • Loading branch information
schmidtw authored Dec 12, 2017
2 parents a0ec4da + b6d08fd commit 8c310fc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 32 deletions.
5 changes: 3 additions & 2 deletions src/caduceus/caduceus.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ func caduceus(arguments []string) int {
senderWrapper: caduceusSenderWrapper,
Logger: logger,
},
doJob: workerPool.Send,
emptyRequests: metricsRegistry.NewCounter(EmptyRequestBodyCounter),
doJob: workerPool.Send,
}

profileWrapper := &ProfileHandler{
Expand All @@ -202,7 +203,7 @@ func caduceus(arguments []string) int {
Logger: logger,
}

caduceusHandler := alice.New(authHandler.Decorate, TrackEmptyRequestBody(metricsRegistry))
caduceusHandler := alice.New(authHandler.Decorate)

router := mux.NewRouter()

Expand Down
11 changes: 9 additions & 2 deletions src/caduceus/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package main

import (
"encoding/json"
"github.com/Comcast/webpa-common/logging"
"github.com/go-kit/kit/log"
"io/ioutil"
"net/http"
"time"

"github.com/Comcast/webpa-common/logging"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/metrics"
)

type Send func(inFunc func(workerID int)) error
Expand All @@ -32,6 +34,7 @@ type ServerHandler struct {
log.Logger
caduceusHandler RequestHandler
caduceusHealth HealthTracker
emptyRequests metrics.Counter
doJob Send
}

Expand All @@ -54,6 +57,10 @@ func (sh *ServerHandler) ServeHTTP(response http.ResponseWriter, request *http.R
return
}

if len(payload) == 0 {
sh.emptyRequests.Add(1.0)
}

targetURL := request.URL.String()

caduceusRequest := CaduceusRequest{
Expand Down
28 changes: 0 additions & 28 deletions src/caduceus/metrics.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package main

import (
"bytes"
"io/ioutil"
"net/http"

"github.com/Comcast/webpa-common/xmetrics"
"github.com/go-kit/kit/metrics/provider"
)

const (
Expand All @@ -21,26 +16,3 @@ func Metrics() []xmetrics.Metric {
},
}
}

// TrackEmptyRequestBody increments the EmptyRequestBodyCounter anytime an empty request is received
func TrackEmptyRequestBody(provider provider.Provider) func(http.Handler) http.Handler {
counter := provider.NewCounter(EmptyRequestBodyCounter)

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
// don't trust the Content-Length header ...
body, err := ioutil.ReadAll(request.Body)
if err != nil {
response.WriteHeader(http.StatusBadRequest)
return
}

if len(body) == 0 {
counter.Add(1.0)
}

request.Body = ioutil.NopCloser(bytes.NewReader(body))
next.ServeHTTP(response, request)
})
}
}

0 comments on commit 8c310fc

Please sign in to comment.